4 * CPDUP <options> source destination
6 * (c) Copyright 1997-1999 by Matthew Dillon and Dima Ruban. Permission to
7 * use and distribute based on the FreeBSD copyright. Supplied as-is,
8 * USE WITH EXTREME CAUTION.
10 * This program attempts to duplicate the source onto the destination as
11 * exactly as possible, retaining modify times, flags, perms, uid, and gid.
12 * It can duplicate devices, files (including hardlinks), softlinks,
13 * directories, and so forth. It is recursive by default! The duplication
14 * is inclusive of removal of files/directories on the destination that do
15 * not exist on the source. This program supports a per-directory exception
16 * file called .cpignore, or a user-specified exception file.
20 * - does not cross partition boundries on source
21 * - asks for confirmation on deletions unless -i0 is specified
22 * - refuses to replace a destination directory with a source file
23 * unless -s0 is specified.
24 * - terminates on error
28 * - does not copy file if mtime, flags, perms, and size match unless
31 * - copies to temporary and renames-over the original, allowing
32 * you to update live systems
34 * - copies uid, gid, mtime, perms, flags, softlinks, devices, hardlinks,
35 * and recurses through directories.
37 * - accesses a per-directory exclusion file, .cpignore, containing
38 * standard wildcarded ( ? / * style, NOT regex) exclusions.
40 * - tries to play permissions and flags smart in regards to overwriting
41 * schg files and doing related stuff.
43 * - Can do MD5 consistancy checks
45 * - Is able to do incremental mirroring/backups via hardlinks from
46 * the 'previous' version (supplied with -H path).
48 * $DragonFly: src/bin/cpdup/cpdup.c,v 1.32 2008/11/11 04:36:00 dillon Exp $
52 * Example: cc -O cpdup.c -o cpdup -lmd
54 * ".MD5.CHECKSUMS" contains md5 checksumms for the current directory.
55 * This file is stored on the source.
63 #define HMASK (HSIZE-1)
65 #define HLMASK (HLSIZE - 1)
67 #define GETBUFSIZE 8192
68 #define GETPATHSIZE 2048
69 #define GETLINKSIZE 1024
70 #define GETIOSIZE 65536
72 #ifndef _ST_FLAGS_PRESENT_
73 #define st_flags st_mode
78 struct Node
*no_HNext
;
99 typedef struct copy_info
{
106 static struct hlink
*hltable
[HLSIZE
];
108 static void RemoveRecur(const char *dpath
, dev_t devNo
, struct stat
*dstat
);
109 static void InitList(List
*list
);
110 static void ResetList(List
*list
);
111 static Node
*IterateList(List
*list
, Node
*node
, int n
);
112 static int AddList(List
*list
, const char *name
, int n
, struct stat
*st
);
113 static int CheckList(List
*list
, const char *path
, const char *name
);
114 static int getbool(const char *str
);
115 static char *SplitRemote(char **pathp
);
116 static int ChgrpAllowed(gid_t g
);
117 static int OwnerMatch(struct stat
*st1
, struct stat
*st2
);
118 #ifdef _ST_FLAGS_PRESENT_
119 static int FlagsMatch(struct stat
*st1
, struct stat
*st2
);
121 #define FlagsMatch(st1, st2) 1
123 static struct hlink
*hltlookup(struct stat
*);
124 static struct hlink
*hltadd(struct stat
*, const char *);
125 static char *checkHLPath(struct stat
*st
, const char *spath
, const char *dpath
);
126 static int validate_check(const char *spath
, const char *dpath
);
127 static int shash(const char *s
);
128 static void hltdelete(struct hlink
*);
129 static void hltsetdino(struct hlink
*, ino_t
);
130 static int YesNo(const char *path
);
131 static int xrename(const char *src
, const char *dst
, u_long flags
);
132 static int xlink(const char *src
, const char *dst
, u_long flags
);
133 static int xremove(struct HostConf
*host
, const char *path
);
134 static int xrmdir(struct HostConf
*host
, const char *path
);
135 static int DoCopy(copy_info_t info
, struct stat
*stat1
, int depth
);
136 static int ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
137 int64_t *CountReadBytes
, int n
);
139 int AskConfirmation
= 1;
156 const char *ssh_argv
[16];
159 const char *UseCpFile
;
160 const char *MD5CacheFile
;
161 const char *FSMIDCacheFile
;
162 const char *UseHLPath
;
164 static int DstBaseLen
;
165 static int HardLinkCount
;
166 static int GroupCount
;
167 static gid_t
*GroupList
;
169 int64_t CountSourceBytes
;
170 int64_t CountSourceItems
;
171 int64_t CountCopiedItems
;
172 int64_t CountSourceReadBytes
;
173 int64_t CountTargetReadBytes
;
174 int64_t CountWriteBytes
;
175 int64_t CountRemovedItems
;
176 int64_t CountLinkedItems
;
178 static struct HostConf SrcHost
;
179 static struct HostConf DstHost
;
182 main(int ac
, char **av
)
189 struct timeval start
;
190 struct copy_info info
;
192 signal(SIGPIPE
, SIG_IGN
);
194 gettimeofday(&start
, NULL
);
196 while ((opt
= getopt(ac
, av
, ":CdnF:fH:Ii:j:K:klM:mopqRSs:uVvX:x")) != -1) {
198 /* TODO: sort the branches */
225 UseCpFile
= ".cpignore";
235 fatal("too many -F options");
236 ssh_argv
[ssh_argc
++] = optarg
;
248 AskConfirmation
= getbool(optarg
);
251 DeviceOpt
= getbool(optarg
);
254 SafetyOpt
= getbool(optarg
);
261 FSMIDCacheFile
= ".FSMID.CHECK";
265 FSMIDCacheFile
= optarg
;
269 MD5CacheFile
= optarg
;
273 MD5CacheFile
= ".MD5.CHECKSUMS";
276 setvbuf(stdout
, NULL
, _IOLBF
, 0);
279 fatal("missing argument for option: -%c\n", optopt
);
283 fatal("illegal option: -%c\n", optopt
);
299 fatal("too many arguments");
302 * If we are told to go into slave mode, run the HC protocol
305 DstRootPrivs
= (geteuid() == 0);
311 * Extract the source and/or/neither target [user@]host and
312 * make any required connections.
314 if (src
&& (ptr
= SplitRemote(&src
)) != NULL
) {
318 fatal("The MD5 options are not currently supported for remote sources");
319 if (hc_connect(&SrcHost
, ReadOnlyOpt
) < 0)
321 } else if (ReadOnlyOpt
)
322 fatal("The -R option is only supported for remote sources");
324 if (dst
&& (ptr
= SplitRemote(&dst
)) != NULL
) {
328 fatal("The FSMID options are not currently supported for remote targets");
329 if (hc_connect(&DstHost
, 0) < 0)
334 * dst may be NULL only if -m option is specified,
335 * which forces an update of the MD5 checksums
337 if (dst
== NULL
&& UseMD5Opt
== 0) {
343 DstRootPrivs
= (hc_geteuid(&DstHost
) == 0);
345 GroupCount
= hc_getgroups(&DstHost
, &GroupList
);
349 fprintf(stderr
, "DstRootPrivs == %s\n", DstRootPrivs
? "true" : "false");
350 fprintf(stderr
, "GroupCount == %d\n", GroupCount
);
351 for (i
= 0; i
< GroupCount
; i
++)
352 fprintf(stderr
, "Group[%d] == %d\n", i
, GroupList
[i
]);
355 bzero(&info
, sizeof(info
));
357 DstBaseLen
= strlen(dst
);
360 info
.sdevNo
= (dev_t
)-1;
361 info
.ddevNo
= (dev_t
)-1;
362 i
= DoCopy(&info
, NULL
, -1);
366 info
.sdevNo
= (dev_t
)-1;
367 info
.ddevNo
= (dev_t
)-1;
368 i
= DoCopy(&info
, NULL
, -1);
375 if (SummaryOpt
&& i
== 0) {
379 gettimeofday(&end
, NULL
);
381 /* don't count stat's in our byte statistics */
382 CountSourceBytes
+= sizeof(struct stat
) * CountSourceItems
;
383 CountSourceReadBytes
+= sizeof(struct stat
) * CountSourceItems
;
384 CountWriteBytes
+= sizeof(struct stat
) * CountCopiedItems
;
385 CountWriteBytes
+= sizeof(struct stat
) * CountRemovedItems
;
388 duration
= (end
.tv_sec
- start
.tv_sec
);
389 duration
+= (double)(end
.tv_usec
- start
.tv_usec
) / 1000000.0;
392 logstd("cpdup completed successfully\n");
393 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
394 "%lld bytes written (%.1fX speedup)\n",
395 (long long)CountSourceBytes
,
396 (long long)CountSourceReadBytes
,
397 (long long)CountTargetReadBytes
,
398 (long long)CountWriteBytes
,
399 ((double)CountSourceBytes
* 2.0) / ((double)(CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
)));
400 logstd("%lld source items, %lld items copied, %lld items linked, "
401 "%lld things deleted\n",
402 (long long)CountSourceItems
,
403 (long long)CountCopiedItems
,
404 (long long)CountLinkedItems
,
405 (long long)CountRemovedItems
);
406 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
408 (int)((CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
) / duration
/ 1024.0),
409 (int)(CountSourceBytes
/ duration
/ 1024.0));
411 exit((i
== 0) ? 0 : 1);
415 getbool(const char *str
)
417 if (strcmp(str
, "0") == 0)
419 if (strcmp(str
, "1") == 0)
421 fatal("option requires boolean argument (0 or 1): -%c\n", optopt
);
427 * Check if path specifies a remote path, using the same syntax as scp(1),
428 * i.e. a path is considered remote if the first colon is not preceded by
429 * a slash, so e.g. "./foo:bar" is considered local.
430 * If a remote path is detected, the colon is replaced with a null byte,
431 * and the return value is a pointer to the next character.
432 * Otherwise NULL is returned.
434 * A path prefix of localhost is the same as a locally specified file or
435 * directory path, but prevents any further interpretation of the path
436 * as being a remote hostname (for paths that have colons in them).
439 SplitRemote(char **pathp
)
444 if (path
[(cindex
= strcspn(path
, ":/"))] == ':') {
446 if (strcmp(path
, "localhost") != 0)
447 return (path
+ cindex
);
448 *pathp
= path
+ cindex
;
454 * Check if group g is in our GroupList.
456 * Typically the number of groups a user belongs to isn't large
457 * enough to warrant more effort than a simple linear search.
458 * However, we perform an optimization by moving a group to the
459 * top of the list when we have a hit. This assumes that there
460 * isn't much variance in the gids of files that a non-root user
461 * copies. So most of the time the search will terminate on the
462 * first element of the list.
465 ChgrpAllowed(gid_t g
)
469 for (i
= 0; i
< GroupCount
; i
++)
470 if (GroupList
[i
] == g
) {
472 /* Optimize: Move g to the front of the list. */
474 GroupList
[i
] = GroupList
[i
- 1];
483 * The following two functions return true if the ownership (UID + GID)
484 * or the flags of two files match, respectively.
486 * Only perform weak checking if we don't have sufficient privileges on
487 * the target machine, so we don't waste transfers with things that are
488 * bound to fail anyway.
491 OwnerMatch(struct stat
*st1
, struct stat
*st2
)
494 /* Both UID and GID must match. */
495 return (st1
->st_uid
== st2
->st_uid
&& st1
->st_gid
== st2
->st_gid
);
497 /* Ignore UID, and also ignore GID if we can't chgrp to that group. */
498 return (st1
->st_gid
== st2
->st_gid
|| !ChgrpAllowed(st1
->st_gid
));
501 #ifdef _ST_FLAGS_PRESENT_
503 FlagsMatch(struct stat
*st1
, struct stat
*st2
)
506 * Ignore UF_ARCHIVE. It gets set automatically by the filesystem, for
507 * filesystems that support it. If the destination filesystem supports it, but
508 * it's cleared on the source file, then multiple invocations of cpdup would
509 * all try to copy the file because the flags wouldn't match.
511 * When unpriveleged, ignore flags we can't set
513 u_long ignored
= DstRootPrivs
? 0 : SF_SETTABLE
;
516 ignored
|= UF_ARCHIVE
;
518 return (((st1
->st_flags
^ st2
->st_flags
) & ~ignored
) == 0);
523 static struct hlink
*
524 hltlookup(struct stat
*stp
)
529 n
= stp
->st_ino
& HLMASK
;
531 for (hl
= hltable
[n
]; hl
; hl
= hl
->next
) {
532 if (hl
->ino
== stp
->st_ino
) {
541 static struct hlink
*
542 hltadd(struct stat
*stp
, const char *path
)
545 int plen
= strlen(path
);
548 new = malloc(offsetof(struct hlink
, name
[plen
+ 1]));
550 fatal("out of memory");
553 /* initialize and link the new element into the table */
554 new->ino
= stp
->st_ino
;
555 new->dino
= (ino_t
)-1;
557 bcopy(path
, new->name
, plen
+ 1);
560 n
= stp
->st_ino
& HLMASK
;
561 new->next
= hltable
[n
];
563 hltable
[n
]->prev
= new;
570 hltsetdino(struct hlink
*hl
, ino_t inum
)
576 hltdelete(struct hlink
*hl
)
578 assert(hl
->refs
== 1);
582 hl
->next
->prev
= hl
->prev
;
583 hl
->prev
->next
= hl
->next
;
586 hl
->next
->prev
= NULL
;
588 hltable
[hl
->ino
& HLMASK
] = hl
->next
;
595 hltrels(struct hlink
*hl
)
597 assert(hl
->refs
== 1);
602 * If UseHLPath is defined check to see if the file in question is
603 * the same as the source file, and if it is return a pointer to the
604 * -H path based file for hardlinking. Else return NULL.
607 checkHLPath(struct stat
*st1
, const char *spath
, const char *dpath
)
613 asprintf(&hpath
, "%s%s", UseHLPath
, dpath
+ DstBaseLen
);
616 * stat info matches ?
618 if (hc_stat(&DstHost
, hpath
, &sthl
) < 0 ||
619 st1
->st_size
!= sthl
.st_size
||
620 st1
->st_mtime
!= sthl
.st_mtime
||
621 !OwnerMatch(st1
, &sthl
) ||
622 !FlagsMatch(st1
, &sthl
)
629 * If ForceOpt or ValidateOpt is set we have to compare the files
631 if (ForceOpt
|| ValidateOpt
) {
632 error
= validate_check(spath
, hpath
);
642 * Return 0 if the contents of the file <spath> matches the contents of
646 validate_check(const char *spath
, const char *dpath
)
652 fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0);
653 fd2
= hc_open(&DstHost
, dpath
, O_RDONLY
, 0);
656 if (fd1
>= 0 && fd2
>= 0) {
659 char *iobuf1
= malloc(GETIOSIZE
);
660 char *iobuf2
= malloc(GETIOSIZE
);
662 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
663 CountSourceReadBytes
+= n
;
664 x
= hc_read(&DstHost
, fd2
, iobuf2
, GETIOSIZE
);
666 CountTargetReadBytes
+= x
;
669 if (bcmp(iobuf1
, iobuf2
, n
) != 0)
678 hc_close(&SrcHost
, fd1
);
680 hc_close(&DstHost
, fd2
);
685 DoCopy(copy_info_t info
, struct stat
*stat1
, int depth
)
687 const char *spath
= info
->spath
;
688 const char *dpath
= info
->dpath
;
689 dev_t sdevNo
= info
->sdevNo
;
690 dev_t ddevNo
= info
->ddevNo
;
693 unsigned long st2_flags
;
694 int r
, mres
, fres
, st2Valid
;
698 r
= mres
= fres
= st2Valid
= 0;
704 if (hc_lstat(&SrcHost
, spath
, &st1
) != 0) {
711 /* skip snapshot files because they're sparse and _huge_ */
712 if (stat1
->st_flags
& SF_SNAPSHOT
)
715 st2
.st_mode
= 0; /* in case lstat fails */
716 st2
.st_flags
= 0; /* in case lstat fails */
717 if (dpath
&& hc_lstat(&DstHost
, dpath
, &st2
) == 0) {
719 #ifdef _ST_FLAGS_PRESENT_
720 st2_flags
= st2
.st_flags
;
724 if (S_ISREG(stat1
->st_mode
))
725 size
= stat1
->st_size
;
731 if (S_ISREG(stat1
->st_mode
) && stat1
->st_nlink
> 1 && dpath
) {
732 if ((hln
= hltlookup(stat1
)) != NULL
) {
736 if (st2
.st_ino
== hln
->dino
) {
738 * hard link is already correct, nothing to do
741 logstd("%-32s nochange\n", (dpath
) ? dpath
: spath
);
742 if (hln
->nlinked
== stat1
->st_nlink
) {
751 * hard link is not correct, attempt to unlink it
753 if (xremove(&DstHost
, dpath
) < 0) {
754 logerr("%-32s hardlink: unable to unlink: %s\n",
755 ((dpath
) ? dpath
: spath
), strerror(errno
));
764 if (xlink(hln
->name
, dpath
, stat1
->st_flags
) < 0) {
765 int tryrelink
= (errno
== EMLINK
);
766 logerr("%-32s hardlink: unable to link to %s: %s\n",
767 (dpath
? dpath
: spath
), hln
->name
, strerror(errno
)
772 logerr("%-20s hardlink: will attempt to copy normally\n",
773 (dpath
? dpath
: spath
));
778 if (hln
->nlinked
== stat1
->st_nlink
) {
784 logstd("%-32s hardlink: %s\n",
785 (dpath
? dpath
: spath
),
786 (st2Valid
? "relinked" : "linked")
797 * first instance of hardlink must be copied normally
800 hln
= hltadd(stat1
, dpath
);
805 * Do we need to copy the file/dir/link/whatever? Early termination
806 * if we do not. Always redo links. Directories are always traversed
807 * except when the FSMID options are used.
809 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
814 && stat1
->st_mode
== st2
.st_mode
815 && FlagsMatch(stat1
, &st2
)
817 if (S_ISLNK(stat1
->st_mode
) || S_ISDIR(stat1
->st_mode
)) {
819 * If FSMID tracking is turned on we can avoid recursing through
820 * an entire directory subtree if the FSMID matches.
822 #ifdef _ST_FSMID_PRESENT_
824 (UseFSMIDOpt
&& (fres
= fsmid_check(stat1
->st_fsmid
, dpath
)) == 0)
826 if (VerboseOpt
>= 3) {
827 if (UseFSMIDOpt
) /* always true!?! */
828 logstd("%-32s fsmid-nochange\n", (dpath
? dpath
: spath
));
830 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
838 stat1
->st_size
== st2
.st_size
&&
839 (ValidateOpt
== 2 || stat1
->st_mtime
== st2
.st_mtime
) &&
840 OwnerMatch(stat1
, &st2
)
842 && (UseMD5Opt
== 0 || !S_ISREG(stat1
->st_mode
) ||
843 (mres
= md5_check(spath
, dpath
)) == 0)
845 #ifdef _ST_FSMID_PRESENT_
846 && (UseFSMIDOpt
== 0 ||
847 (fres
= fsmid_check(stat1
->st_fsmid
, dpath
)) == 0)
849 && (ValidateOpt
== 0 || !S_ISREG(stat1
->st_mode
) ||
850 validate_check(spath
, dpath
) == 0)
853 * The files are identical, but if we are running as
854 * root we might need to adjust ownership/group/flags.
857 int changedflags
= 0;
860 hltsetdino(hln
, st2
.st_ino
);
862 if (!OwnerMatch(stat1
, &st2
)) {
863 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
866 #ifdef _ST_FLAGS_PRESENT_
867 if (!FlagsMatch(stat1
, &st2
)) {
868 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
872 if (VerboseOpt
>= 3) {
875 logstd("%-32s md5-nochange",
876 (dpath
? dpath
: spath
));
880 logstd("%-32s fsmid-nochange",
881 (dpath
? dpath
: spath
));
882 } else if (ValidateOpt
) {
883 logstd("%-32s nochange (contents validated)",
884 (dpath
? dpath
: spath
));
886 logstd("%-32s nochange", (dpath
? dpath
: spath
));
889 logstd(" (uid/gid differ)");
891 logstd(" (flags differ)");
894 CountSourceBytes
+= size
;
901 if (st2Valid
&& !S_ISDIR(stat1
->st_mode
) && S_ISDIR(st2
.st_mode
)) {
903 logerr("%-32s SAFETY - refusing to copy file over directory\n",
904 (dpath
? dpath
: spath
)
908 goto done
; /* continue with the cpdup anyway */
910 if (QuietOpt
== 0 || AskConfirmation
) {
911 logstd("%-32s WARNING: non-directory source will blow away\n"
912 "%-32s preexisting dest directory, continuing anyway!\n",
913 ((dpath
) ? dpath
: spath
), "");
916 RemoveRecur(dpath
, ddevNo
, &st2
);
921 * The various comparisons failed, copy it.
923 if (S_ISDIR(stat1
->st_mode
)) {
927 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
930 if (!st2Valid
|| S_ISDIR(st2
.st_mode
) == 0) {
932 xremove(&DstHost
, dpath
);
933 if (hc_mkdir(&DstHost
, dpath
, stat1
->st_mode
| 0700) != 0) {
934 logerr("%s: mkdir failed: %s\n",
935 (dpath
? dpath
: spath
), strerror(errno
));
939 if (hc_lstat(&DstHost
, dpath
, &st2
) != 0) {
940 if (NotForRealOpt
== 0)
941 logerr("%s: lstat of newly made dir failed: %s\n",
942 (dpath
? dpath
: spath
), strerror(errno
));
949 if (!OwnerMatch(stat1
, &st2
) &&
950 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
) != 0
952 logerr("%s: chown of newly made dir failed: %s\n",
953 (dpath
? dpath
: spath
), strerror(errno
));
955 /* Note that we should not set skipdir = 1 here. */
959 logstd("%-32s mkdir-ok\n", (dpath
? dpath
: spath
));
963 * Directory must be scanable by root for cpdup to
964 * work. We'll fix it later if the directory isn't
965 * supposed to be readable ( which is why we fixup
966 * st2.st_mode to match what we did ).
968 if ((st2
.st_mode
& 0700) != 0700) {
969 hc_chmod(&DstHost
, dpath
, st2
.st_mode
| 0700);
973 logstd("%s\n", dpath
? dpath
: spath
);
978 * When copying a directory, stop if the source crosses a mount
981 if (sdevNo
!= (dev_t
)-1 && stat1
->st_dev
!= sdevNo
)
984 sdevNo
= stat1
->st_dev
;
987 * When copying a directory, stop if the destination crosses
990 * The target directory will have been created and stat'd
991 * for st2 if it did not previously exist. st2Valid is left
992 * as a flag. If the stat failed st2 will still only have its
993 * default initialization.
995 * So we simply assume here that the directory is within the
996 * current target mount if we had to create it (aka st2Valid is 0)
997 * and we leave ddevNo alone.
1000 if (ddevNo
!= (dev_t
)-1 && st2
.st_dev
!= ddevNo
)
1003 ddevNo
= st2
.st_dev
;
1007 List
*list
= malloc(sizeof(List
));
1011 logstd("Scanning %s ...\n", spath
);
1013 if (ScanDir(list
, &SrcHost
, spath
, &CountSourceReadBytes
, 0) == 0) {
1015 while ((node
= IterateList(list
, node
, 0)) != NULL
) {
1017 char *ndpath
= NULL
;
1019 nspath
= mprintf("%s/%s", spath
, node
->no_Name
);
1021 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1023 info
->spath
= nspath
;
1024 info
->dpath
= ndpath
;
1025 info
->sdevNo
= sdevNo
;
1026 info
->ddevNo
= ddevNo
;
1028 r
+= DoCopy(info
, node
->no_Stat
, depth
);
1030 r
+= DoCopy(info
, node
->no_Stat
, depth
+ 1);
1039 * Remove files/directories from destination that do not appear
1042 if (dpath
&& ScanDir(list
, &DstHost
, dpath
,
1043 &CountTargetReadBytes
, 3) == 0) {
1045 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1047 * If object does not exist in source or .cpignore
1048 * then recursively remove it.
1052 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1053 RemoveRecur(ndpath
, ddevNo
, node
->no_Stat
);
1062 if (dpath
&& st2Valid
) {
1063 struct timeval tv
[2];
1065 if (ForceOpt
|| !OwnerMatch(stat1
, &st2
))
1066 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1067 if (stat1
->st_mode
!= st2
.st_mode
)
1068 hc_chmod(&DstHost
, dpath
, stat1
->st_mode
);
1069 #ifdef _ST_FLAGS_PRESENT_
1070 if (!FlagsMatch(stat1
, &st2
))
1071 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1073 if (ForceOpt
|| stat1
->st_mtime
!= st2
.st_mtime
) {
1074 bzero(tv
, sizeof(tv
));
1075 tv
[0].tv_sec
= stat1
->st_mtime
;
1076 tv
[1].tv_sec
= stat1
->st_mtime
;
1077 hc_utimes(&DstHost
, dpath
, tv
);
1080 } else if (dpath
== NULL
) {
1082 * If dpath is NULL, we are just updating the MD5
1085 if (UseMD5Opt
&& S_ISREG(stat1
->st_mode
)) {
1086 mres
= md5_check(spath
, NULL
);
1088 if (VerboseOpt
> 1) {
1090 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1092 logstd("%-32s md5-ok\n", (dpath
) ? dpath
: spath
);
1093 } else if (!QuietOpt
&& mres
< 0) {
1094 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1098 } else if (S_ISREG(stat1
->st_mode
)) {
1105 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1107 path
= mprintf("%s", dpath
);
1110 * Handle check failure message.
1114 logerr("%-32s md5-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1118 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1121 * Not quite ready to do the copy yet. If UseHLPath is defined,
1122 * see if we can hardlink instead.
1124 * If we can hardlink, and the target exists, we have to remove it
1125 * first or the hardlink will fail. This can occur in a number of
1126 * situations but most typically when the '-f -H' combination is
1129 if (UseHLPath
&& (hpath
= checkHLPath(stat1
, spath
, dpath
)) != NULL
) {
1131 xremove(&DstHost
, dpath
);
1132 if (hc_link(&DstHost
, hpath
, dpath
) == 0) {
1135 logstd("%-32s hardlinked(-H)\n",
1136 (dpath
? dpath
: spath
));
1142 * Shucks, we may have hit a filesystem hard linking limit,
1143 * we have to copy instead.
1148 if ((fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0)) >= 0) {
1149 if ((fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600)) < 0) {
1151 * There could be a .tmp file from a previously interrupted
1152 * run, delete and retry. Fail if we still can't get at it.
1154 #ifdef _ST_FLAGS_PRESENT_
1155 hc_chflags(&DstHost
, path
, 0);
1157 hc_remove(&DstHost
, path
);
1158 fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_TRUNC
, 0600);
1162 char *iobuf1
= malloc(GETIOSIZE
);
1166 * Matt: What about holes?
1169 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
1171 if (hc_write(&DstHost
, fd2
, iobuf1
, n
) != n
)
1175 hc_close(&DstHost
, fd2
);
1177 struct timeval tv
[2];
1179 bzero(tv
, sizeof(tv
));
1180 tv
[0].tv_sec
= stat1
->st_mtime
;
1181 tv
[1].tv_sec
= stat1
->st_mtime
;
1183 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1184 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1185 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1186 #ifdef _ST_FLAGS_PRESENT_
1187 if (stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
1188 hc_utimes(&DstHost
, path
, tv
);
1190 hc_utimes(&DstHost
, path
, tv
);
1192 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1193 logerr("%-32s rename-after-copy failed: %s\n",
1194 (dpath
? dpath
: spath
), strerror(errno
)
1199 logstd("%-32s copy-ok\n", (dpath
? dpath
: spath
));
1200 #ifdef _ST_FLAGS_PRESENT_
1201 if (DstRootPrivs
? stat1
->st_flags
: stat1
->st_flags
& UF_SETTABLE
)
1202 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1205 #ifdef _ST_FLAGS_PRESENT_
1206 if ((stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
)) == 0)
1207 hc_utimes(&DstHost
, dpath
, tv
);
1209 CountSourceReadBytes
+= size
;
1210 CountWriteBytes
+= size
;
1211 CountSourceBytes
+= size
;
1215 logerr("%-32s %s failed: %s\n",
1216 (dpath
? dpath
: spath
), op
, strerror(errno
)
1218 hc_remove(&DstHost
, path
);
1223 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1224 (dpath
? dpath
: spath
), getuid(), geteuid(),
1229 hc_close(&SrcHost
, fd1
);
1231 logerr("%-32s copy: open failed: %s\n",
1232 (dpath
? dpath
: spath
),
1241 if (!r
&& hc_stat(&DstHost
, dpath
, &st2
) == 0) {
1242 hltsetdino(hln
, st2
.st_ino
);
1248 } else if (S_ISLNK(stat1
->st_mode
)) {
1249 char *link1
= malloc(GETLINKSIZE
);
1250 char *link2
= malloc(GETLINKSIZE
);
1255 n1
= hc_readlink(&SrcHost
, spath
, link1
, GETLINKSIZE
- 1);
1257 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1258 n2
= hc_readlink(&DstHost
, dpath
, link2
, GETLINKSIZE
- 1);
1260 path
= mprintf("%s", dpath
);
1264 if (ForceOpt
|| n1
!= n2
|| bcmp(link1
, link2
, n1
) != 0) {
1265 hc_umask(&DstHost
, ~stat1
->st_mode
);
1266 xremove(&DstHost
, path
);
1268 if (hc_symlink(&DstHost
, link1
, path
) < 0) {
1269 logerr("%-32s symlink (%s->%s) failed: %s\n",
1270 (dpath
? dpath
: spath
), link1
, path
,
1275 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1276 hc_lchown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1278 * there is no lchmod() or lchflags(), we
1279 * cannot chmod or chflags a softlink.
1281 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1282 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1283 (dpath
? dpath
: spath
),
1284 path
, dpath
, strerror(errno
));
1285 } else if (VerboseOpt
) {
1286 logstd("%-32s softlink-ok\n", (dpath
? dpath
: spath
));
1288 hc_umask(&DstHost
, 000);
1289 CountWriteBytes
+= n1
;
1293 if (VerboseOpt
>= 3)
1294 logstd("%-32s nochange", (dpath
? dpath
: spath
));
1295 if (!OwnerMatch(stat1
, &st2
)) {
1296 hc_lchown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1297 if (VerboseOpt
>= 3)
1298 logstd(" (uid/gid differ)");
1300 if (VerboseOpt
>= 3)
1303 CountSourceBytes
+= n1
;
1304 CountSourceReadBytes
+= n1
;
1306 CountTargetReadBytes
+= n2
;
1310 logerr("%-32s softlink-failed\n", (dpath
? dpath
: spath
));
1315 } else if ((S_ISCHR(stat1
->st_mode
) || S_ISBLK(stat1
->st_mode
)) && DeviceOpt
) {
1320 stat1
->st_mode
!= st2
.st_mode
||
1321 stat1
->st_rdev
!= st2
.st_rdev
||
1322 !OwnerMatch(stat1
, &st2
)
1325 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1326 xremove(&DstHost
, path
);
1328 path
= mprintf("%s", dpath
);
1331 if (hc_mknod(&DstHost
, path
, stat1
->st_mode
, stat1
->st_rdev
) == 0) {
1332 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1333 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1335 xremove(&DstHost
, dpath
);
1336 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1337 logerr("%-32s dev-rename-after-create failed: %s\n",
1338 (dpath
? dpath
: spath
),
1341 } else if (VerboseOpt
) {
1342 logstd("%-32s dev-ok\n", (dpath
? dpath
: spath
));
1347 logerr("%-32s dev failed: %s\n",
1348 (dpath
? dpath
: spath
), strerror(errno
)
1352 if (VerboseOpt
>= 3)
1353 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1361 if (hln
->dino
== (ino_t
)-1) {
1363 /*hln = NULL; unneeded */
1372 ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
1373 int64_t *CountReadBytes
, int n
)
1376 struct HostConf
*cphost
;
1377 struct HCDirEntry
*den
;
1378 struct stat
*statptr
;
1382 * scan .cpignore file for files/directories to ignore
1383 * (only in the source directory, i.e. if n == 0).
1389 char *buf
= malloc(GETBUFSIZE
);
1393 if (UseCpFile
[0] == '/') {
1394 fpath
= mprintf("%s", UseCpFile
);
1397 fpath
= mprintf("%s/%s", path
, UseCpFile
);
1398 AddList(list
, strrchr(fpath
, '/') + 1, 1, NULL
);
1401 fd
= hc_open(cphost
, fpath
, O_RDONLY
, 0);
1404 while ((nread
= hc_read(cphost
, fd
, buf
+ bufused
,
1405 GETBUFSIZE
- bufused
- 1)) > 0) {
1406 *CountReadBytes
+= nread
;
1409 for (next
= buf
; (nl
= strchr(next
, '\n')); next
= nl
+1) {
1411 AddList(list
, next
, 1, NULL
);
1413 bufused
= strlen(next
);
1415 bcopy(next
, buf
, bufused
);
1418 /* last line has no trailing newline */
1420 AddList(list
, buf
, 1, NULL
);
1422 hc_close(cphost
, fd
);
1429 * Automatically exclude MD5CacheFile that we create on the
1430 * source from the copy to the destination.
1432 * Automatically exclude a FSMIDCacheFile on the source that
1433 * would otherwise overwrite the one we maintain on the target.
1436 AddList(list
, MD5CacheFile
, 1, NULL
);
1438 AddList(list
, FSMIDCacheFile
, 1, NULL
);
1441 if ((dir
= hc_opendir(host
, path
)) == NULL
)
1443 while ((den
= hc_readdir(host
, dir
, &statptr
)) != NULL
) {
1447 if (strcmp(den
->d_name
, ".") != 0 && strcmp(den
->d_name
, "..") != 0) {
1448 if (UseCpFile
&& UseCpFile
[0] == '/') {
1449 if (CheckList(list
, path
, den
->d_name
) == 0)
1452 AddList(list
, den
->d_name
, n
, statptr
);
1455 hc_closedir(host
, dir
);
1465 RemoveRecur(const char *dpath
, dev_t devNo
, struct stat
*dstat
)
1469 if (dstat
== NULL
) {
1470 if (hc_lstat(&DstHost
, dpath
, &st
) == 0)
1473 if (dstat
!= NULL
) {
1474 if (devNo
== (dev_t
)-1)
1475 devNo
= dstat
->st_dev
;
1476 if (dstat
->st_dev
== devNo
) {
1477 if (S_ISDIR(dstat
->st_mode
)) {
1480 if ((dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1481 List
*list
= malloc(sizeof(List
));
1483 struct HCDirEntry
*den
;
1486 while ((den
= hc_readdir(&DstHost
, dir
, &dstat
)) != NULL
) {
1487 if (strcmp(den
->d_name
, ".") == 0)
1489 if (strcmp(den
->d_name
, "..") == 0)
1491 AddList(list
, den
->d_name
, 3, dstat
);
1493 hc_closedir(&DstHost
, dir
);
1494 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1497 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1498 RemoveRecur(ndpath
, devNo
, node
->no_Stat
);
1504 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1506 if (xrmdir(&DstHost
, dpath
) < 0) {
1507 logerr("%-32s rmdir failed: %s\n",
1508 dpath
, strerror(errno
)
1511 CountRemovedItems
++;
1516 logstd("%-32s not-removed\n", dpath
);
1517 } else if (xrmdir(&DstHost
, dpath
) == 0) {
1519 logstd("%-32s rmdir-ok\n", dpath
);
1520 CountRemovedItems
++;
1522 logerr("%-32s rmdir failed: %s\n",
1523 dpath
, strerror(errno
)
1528 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1530 if (xremove(&DstHost
, dpath
) < 0) {
1531 logerr("%-32s remove failed: %s\n",
1532 dpath
, strerror(errno
)
1535 CountRemovedItems
++;
1540 logstd("%-32s not-removed\n", dpath
);
1541 } else if (xremove(&DstHost
, dpath
) == 0) {
1543 logstd("%-32s remove-ok\n", dpath
);
1544 CountRemovedItems
++;
1546 logerr("%-32s remove failed: %s\n",
1547 dpath
, strerror(errno
)
1557 InitList(List
*list
)
1559 bzero(list
, sizeof(List
));
1560 list
->li_Node
.no_Next
= &list
->li_Node
;
1564 ResetList(List
*list
)
1568 while ((node
= list
->li_Node
.no_Next
) != &list
->li_Node
) {
1569 list
->li_Node
.no_Next
= node
->no_Next
;
1570 if (node
->no_Stat
!= NULL
)
1571 free(node
->no_Stat
);
1578 IterateList(List
*list
, Node
*node
, int n
)
1581 node
= list
->li_Node
.no_Next
;
1583 node
= node
->no_Next
;
1584 while (node
->no_Value
!= n
&& node
!= &list
->li_Node
)
1585 node
= node
->no_Next
;
1586 return (node
== &list
->li_Node
? NULL
: node
);
1590 AddList(List
*list
, const char *name
, int n
, struct stat
*st
)
1596 * Scan against wildcards. Only a node value of 1 can be a wildcard
1597 * ( usually scanned from .cpignore )
1599 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1600 if (strcmp(name
, node
->no_Name
) == 0 ||
1601 (n
!= 1 && node
->no_Value
== 1 &&
1602 fnmatch(node
->no_Name
, name
, 0) == 0)
1604 return(node
->no_Value
);
1609 * Look for exact match
1613 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1614 if (strcmp(name
, node
->no_Name
) == 0) {
1615 return(node
->no_Value
);
1618 node
= malloc(sizeof(Node
) + strlen(name
) + 1);
1620 fatal("out of memory");
1622 node
->no_Next
= list
->li_Node
.no_Next
;
1623 list
->li_Node
.no_Next
= node
;
1625 node
->no_HNext
= list
->li_Hash
[hv
];
1626 list
->li_Hash
[hv
] = node
;
1628 strcpy(node
->no_Name
, name
);
1636 * Match against n=1 (cpignore) entries
1638 * Returns 0 on match, non-zero if no match
1641 CheckList(List
*list
, const char *path
, const char *name
)
1647 asprintf(&fpath
, "%s/%s", path
, name
);
1650 * Scan against wildcards. Only a node value of 1 can be a wildcard
1651 * ( usually scanned from .cpignore )
1653 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1654 if (node
->no_Value
!= 1)
1656 if (fnmatch(node
->no_Name
, fpath
, 0) == 0) {
1663 * Look for exact match
1666 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1667 if (node
->no_Value
!= 1)
1669 if (strcmp(fpath
, node
->no_Name
) == 0) {
1680 shash(const char *s
)
1687 if (*s
== '*' || *s
== '?' ||
1688 *s
== '{' || *s
== '}' ||
1689 *s
== '[' || *s
== ']' ||
1694 hv
= (hv
<< 5) ^ *s
^ (hv
>> 23);
1697 return(((hv
>> 16) ^ hv
) & HMASK
);
1701 YesNo(const char *path
)
1705 fprintf(stderr
, "remove %s (Yes/No) [No]? ", path
);
1708 first
= ch
= getchar();
1709 while (ch
!= '\n' && ch
!= EOF
)
1711 return ((first
== 'y' || first
== 'Y'));
1715 * xrename() - rename with override
1717 * If the rename fails, attempt to override st_flags on the
1718 * destination and rename again. If that fails too, try to
1719 * set the flags back the way they were and give up.
1723 xrename(const char *src
, const char *dst
, u_long flags
)
1727 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1728 #ifdef _ST_FLAGS_PRESENT_
1729 hc_chflags(&DstHost
, dst
, 0);
1730 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0)
1731 hc_chflags(&DstHost
, dst
, flags
);
1738 xlink(const char *src
, const char *dst
, u_long flags
)
1741 #ifdef _ST_FLAGS_PRESENT_
1745 if ((r
= hc_link(&DstHost
, src
, dst
)) < 0) {
1746 #ifdef _ST_FLAGS_PRESENT_
1747 hc_chflags(&DstHost
, src
, 0);
1748 r
= hc_link(&DstHost
, src
, dst
);
1750 hc_chflags(&DstHost
, src
, flags
);
1760 xremove(struct HostConf
*host
, const char *path
)
1764 res
= hc_remove(host
, path
);
1765 #ifdef _ST_FLAGS_PRESENT_
1766 if (res
== -EPERM
) {
1767 hc_chflags(host
, path
, 0);
1768 res
= hc_remove(host
, path
);
1775 xrmdir(struct HostConf
*host
, const char *path
)
1779 res
= hc_rmdir(host
, path
);
1780 #ifdef _ST_FLAGS_PRESENT_
1781 if (res
== -EPERM
) {
1782 hc_chflags(host
, path
, 0);
1783 res
= hc_rmdir(host
, path
);