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 -lcrypto
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
);
138 static int mtimecmp(struct stat
*st1
, struct stat
*st2
);
140 int AskConfirmation
= 1;
157 const char *ssh_argv
[16];
160 const char *UseCpFile
;
161 const char *MD5CacheFile
;
162 const char *FSMIDCacheFile
;
163 const char *UseHLPath
;
165 static int DstBaseLen
;
166 static int HardLinkCount
;
167 static int GroupCount
;
168 static gid_t
*GroupList
;
170 int64_t CountSourceBytes
;
171 int64_t CountSourceItems
;
172 int64_t CountCopiedItems
;
173 int64_t CountSourceReadBytes
;
174 int64_t CountTargetReadBytes
;
175 int64_t CountWriteBytes
;
176 int64_t CountRemovedItems
;
177 int64_t CountLinkedItems
;
179 static struct HostConf SrcHost
;
180 static struct HostConf DstHost
;
183 main(int ac
, char **av
)
190 struct timeval start
;
191 struct copy_info info
;
193 signal(SIGPIPE
, SIG_IGN
);
195 gettimeofday(&start
, NULL
);
197 while ((opt
= getopt(ac
, av
, ":CdF:fH:hIi:j:K:klM:mnoqRSs:uVvX:x")) != -1) {
207 fatal("too many -F options");
208 ssh_argv
[ssh_argc
++] = optarg
;
224 AskConfirmation
= getbool(optarg
);
227 DeviceOpt
= getbool(optarg
);
231 FSMIDCacheFile
= optarg
;
235 FSMIDCacheFile
= ".FSMID.CHECK";
243 MD5CacheFile
= optarg
;
247 MD5CacheFile
= ".MD5.CHECKSUMS";
265 SafetyOpt
= getbool(optarg
);
268 setvbuf(stdout
, NULL
, _IOLBF
, 0);
280 UseCpFile
= ".cpignore";
283 fatal("missing argument for option: -%c\n", optopt
);
287 fatal("illegal option: -%c\n", optopt
);
303 fatal("too many arguments");
306 * If we are told to go into slave mode, run the HC protocol
309 DstRootPrivs
= (geteuid() == 0);
315 * Extract the source and/or/neither target [user@]host and
316 * make any required connections.
318 if (src
&& (ptr
= SplitRemote(&src
)) != NULL
) {
322 fatal("The MD5 options are not currently supported for remote sources");
323 if (hc_connect(&SrcHost
, ReadOnlyOpt
) < 0)
325 } else if (ReadOnlyOpt
)
326 fatal("The -R option is only supported for remote sources");
328 if (dst
&& (ptr
= SplitRemote(&dst
)) != NULL
) {
332 fatal("The FSMID options are not currently supported for remote targets");
333 if (hc_connect(&DstHost
, 0) < 0)
338 * dst may be NULL only if -m option is specified,
339 * which forces an update of the MD5 checksums
341 if (dst
== NULL
&& UseMD5Opt
== 0) {
347 DstRootPrivs
= (hc_geteuid(&DstHost
) == 0);
349 GroupCount
= hc_getgroups(&DstHost
, &GroupList
);
353 fprintf(stderr
, "DstRootPrivs == %s\n", DstRootPrivs
? "true" : "false");
354 fprintf(stderr
, "GroupCount == %d\n", GroupCount
);
355 for (i
= 0; i
< GroupCount
; i
++)
356 fprintf(stderr
, "Group[%d] == %d\n", i
, GroupList
[i
]);
359 bzero(&info
, sizeof(info
));
361 DstBaseLen
= strlen(dst
);
364 info
.sdevNo
= (dev_t
)-1;
365 info
.ddevNo
= (dev_t
)-1;
366 i
= DoCopy(&info
, NULL
, -1);
370 info
.sdevNo
= (dev_t
)-1;
371 info
.ddevNo
= (dev_t
)-1;
372 i
= DoCopy(&info
, NULL
, -1);
379 if (SummaryOpt
&& i
== 0) {
383 gettimeofday(&end
, NULL
);
385 /* don't count stat's in our byte statistics */
386 CountSourceBytes
+= sizeof(struct stat
) * CountSourceItems
;
387 CountSourceReadBytes
+= sizeof(struct stat
) * CountSourceItems
;
388 CountWriteBytes
+= sizeof(struct stat
) * CountCopiedItems
;
389 CountWriteBytes
+= sizeof(struct stat
) * CountRemovedItems
;
392 duration
= (end
.tv_sec
- start
.tv_sec
);
393 duration
+= (double)(end
.tv_usec
- start
.tv_usec
) / 1000000.0;
396 logstd("cpdup completed successfully\n");
397 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
398 "%lld bytes written (%.1fX speedup)\n",
399 (long long)CountSourceBytes
,
400 (long long)CountSourceReadBytes
,
401 (long long)CountTargetReadBytes
,
402 (long long)CountWriteBytes
,
403 ((double)CountSourceBytes
* 2.0) / ((double)(CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
)));
404 logstd("%lld source items, %lld items copied, %lld items linked, "
405 "%lld things deleted\n",
406 (long long)CountSourceItems
,
407 (long long)CountCopiedItems
,
408 (long long)CountLinkedItems
,
409 (long long)CountRemovedItems
);
410 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
412 (int)((CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
) / duration
/ 1024.0),
413 (int)(CountSourceBytes
/ duration
/ 1024.0));
415 exit((i
== 0) ? 0 : 1);
419 getbool(const char *str
)
421 if (strcmp(str
, "0") == 0)
423 if (strcmp(str
, "1") == 0)
425 fatal("option requires boolean argument (0 or 1): -%c\n", optopt
);
431 * Check if path specifies a remote path, using the same syntax as scp(1),
432 * i.e. a path is considered remote if the first colon is not preceded by
433 * a slash, so e.g. "./foo:bar" is considered local.
434 * If a remote path is detected, the colon is replaced with a null byte,
435 * and the return value is a pointer to the next character.
436 * Otherwise NULL is returned.
438 * A path prefix of localhost is the same as a locally specified file or
439 * directory path, but prevents any further interpretation of the path
440 * as being a remote hostname (for paths that have colons in them).
443 SplitRemote(char **pathp
)
448 if (path
[(cindex
= strcspn(path
, ":/"))] == ':') {
450 if (strcmp(path
, "localhost") != 0)
451 return (path
+ cindex
);
452 *pathp
= path
+ cindex
;
458 * Check if group g is in our GroupList.
460 * Typically the number of groups a user belongs to isn't large
461 * enough to warrant more effort than a simple linear search.
462 * However, we perform an optimization by moving a group to the
463 * top of the list when we have a hit. This assumes that there
464 * isn't much variance in the gids of files that a non-root user
465 * copies. So most of the time the search will terminate on the
466 * first element of the list.
469 ChgrpAllowed(gid_t g
)
473 for (i
= 0; i
< GroupCount
; i
++)
474 if (GroupList
[i
] == g
) {
476 /* Optimize: Move g to the front of the list. */
478 GroupList
[i
] = GroupList
[i
- 1];
487 * The following two functions return true if the ownership (UID + GID)
488 * or the flags of two files match, respectively.
490 * Only perform weak checking if we don't have sufficient privileges on
491 * the target machine, so we don't waste transfers with things that are
492 * bound to fail anyway.
495 OwnerMatch(struct stat
*st1
, struct stat
*st2
)
498 /* Both UID and GID must match. */
499 return (st1
->st_uid
== st2
->st_uid
&& st1
->st_gid
== st2
->st_gid
);
501 /* Ignore UID, and also ignore GID if we can't chgrp to that group. */
502 return (st1
->st_gid
== st2
->st_gid
|| !ChgrpAllowed(st1
->st_gid
));
505 #ifdef _ST_FLAGS_PRESENT_
507 FlagsMatch(struct stat
*st1
, struct stat
*st2
)
510 * Ignore UF_ARCHIVE. It gets set automatically by the filesystem, for
511 * filesystems that support it. If the destination filesystem supports it, but
512 * it's cleared on the source file, then multiple invocations of cpdup would
513 * all try to copy the file because the flags wouldn't match.
515 * When unpriveleged, ignore flags we can't set
517 u_long ignored
= DstRootPrivs
? 0 : SF_SETTABLE
;
520 ignored
|= UF_ARCHIVE
;
522 return (((st1
->st_flags
^ st2
->st_flags
) & ~ignored
) == 0);
527 static struct hlink
*
528 hltlookup(struct stat
*stp
)
533 n
= stp
->st_ino
& HLMASK
;
535 for (hl
= hltable
[n
]; hl
; hl
= hl
->next
) {
536 if (hl
->ino
== stp
->st_ino
) {
545 static struct hlink
*
546 hltadd(struct stat
*stp
, const char *path
)
549 int plen
= strlen(path
);
552 new = malloc(offsetof(struct hlink
, name
[plen
+ 1]));
554 fatal("out of memory");
557 /* initialize and link the new element into the table */
558 new->ino
= stp
->st_ino
;
559 new->dino
= (ino_t
)-1;
561 bcopy(path
, new->name
, plen
+ 1);
564 n
= stp
->st_ino
& HLMASK
;
565 new->next
= hltable
[n
];
567 hltable
[n
]->prev
= new;
574 hltsetdino(struct hlink
*hl
, ino_t inum
)
580 hltdelete(struct hlink
*hl
)
582 assert(hl
->refs
== 1);
586 hl
->next
->prev
= hl
->prev
;
587 hl
->prev
->next
= hl
->next
;
590 hl
->next
->prev
= NULL
;
592 hltable
[hl
->ino
& HLMASK
] = hl
->next
;
599 hltrels(struct hlink
*hl
)
601 assert(hl
->refs
== 1);
606 * If UseHLPath is defined check to see if the file in question is
607 * the same as the source file, and if it is return a pointer to the
608 * -H path based file for hardlinking. Else return NULL.
611 checkHLPath(struct stat
*st1
, const char *spath
, const char *dpath
)
617 if (asprintf(&hpath
, "%s%s", UseHLPath
, dpath
+ DstBaseLen
) < 0)
618 fatal("out of memory");
621 * stat info matches ?
623 if (hc_stat(&DstHost
, hpath
, &sthl
) < 0 ||
624 st1
->st_size
!= sthl
.st_size
||
625 mtimecmp(st1
, &sthl
) != 0 ||
626 !OwnerMatch(st1
, &sthl
) ||
627 !FlagsMatch(st1
, &sthl
)
634 * If ForceOpt or ValidateOpt is set we have to compare the files
636 if (ForceOpt
|| ValidateOpt
) {
637 error
= validate_check(spath
, hpath
);
647 * Return 0 if the contents of the file <spath> matches the contents of
651 validate_check(const char *spath
, const char *dpath
)
657 fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0);
658 fd2
= hc_open(&DstHost
, dpath
, O_RDONLY
, 0);
661 if (fd1
>= 0 && fd2
>= 0) {
664 char *iobuf1
= malloc(GETIOSIZE
);
665 char *iobuf2
= malloc(GETIOSIZE
);
667 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
668 CountSourceReadBytes
+= n
;
669 x
= hc_read(&DstHost
, fd2
, iobuf2
, GETIOSIZE
);
671 CountTargetReadBytes
+= x
;
674 if (bcmp(iobuf1
, iobuf2
, n
) != 0)
683 hc_close(&SrcHost
, fd1
);
685 hc_close(&DstHost
, fd2
);
690 DoCopy(copy_info_t info
, struct stat
*stat1
, int depth
)
692 const char *spath
= info
->spath
;
693 const char *dpath
= info
->dpath
;
694 dev_t sdevNo
= info
->sdevNo
;
695 dev_t ddevNo
= info
->ddevNo
;
698 unsigned long st2_flags
;
699 int r
, mres
, fres
, st2Valid
;
703 r
= mres
= fres
= st2Valid
= 0;
709 if (hc_lstat(&SrcHost
, spath
, &st1
) != 0) {
716 /* skip snapshot files because they're sparse and _huge_ */
717 if (stat1
->st_flags
& SF_SNAPSHOT
)
720 st2
.st_mode
= 0; /* in case lstat fails */
721 st2
.st_flags
= 0; /* in case lstat fails */
722 if (dpath
&& hc_lstat(&DstHost
, dpath
, &st2
) == 0) {
724 #ifdef _ST_FLAGS_PRESENT_
725 st2_flags
= st2
.st_flags
;
729 if (S_ISREG(stat1
->st_mode
))
730 size
= stat1
->st_size
;
736 if (S_ISREG(stat1
->st_mode
) && stat1
->st_nlink
> 1 && dpath
) {
737 if ((hln
= hltlookup(stat1
)) != NULL
) {
741 if (st2
.st_ino
== hln
->dino
) {
743 * hard link is already correct, nothing to do
746 logstd("%-32s nochange\n", (dpath
) ? dpath
: spath
);
747 if (hln
->nlinked
== stat1
->st_nlink
) {
756 * hard link is not correct, attempt to unlink it
758 if (xremove(&DstHost
, dpath
) < 0) {
759 logerr("%-32s hardlink: unable to unlink: %s\n",
760 ((dpath
) ? dpath
: spath
), strerror(errno
));
769 if (xlink(hln
->name
, dpath
, stat1
->st_flags
) < 0) {
770 int tryrelink
= (errno
== EMLINK
);
771 logerr("%-32s hardlink: unable to link to %s: %s\n",
772 (dpath
? dpath
: spath
), hln
->name
, strerror(errno
)
777 logerr("%-20s hardlink: will attempt to copy normally\n",
778 (dpath
? dpath
: spath
));
783 if (hln
->nlinked
== stat1
->st_nlink
) {
789 logstd("%-32s hardlink: %s\n",
790 (dpath
? dpath
: spath
),
791 (st2Valid
? "relinked" : "linked")
802 * first instance of hardlink must be copied normally
805 hln
= hltadd(stat1
, dpath
);
810 * Do we need to copy the file/dir/link/whatever? Early termination
811 * if we do not. Always redo links. Directories are always traversed
812 * except when the FSMID options are used.
814 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
819 && stat1
->st_mode
== st2
.st_mode
820 && FlagsMatch(stat1
, &st2
)
822 if (S_ISLNK(stat1
->st_mode
) || S_ISDIR(stat1
->st_mode
)) {
824 * If FSMID tracking is turned on we can avoid recursing through
825 * an entire directory subtree if the FSMID matches.
827 #ifdef _ST_FSMID_PRESENT_
829 (UseFSMIDOpt
&& (fres
= fsmid_check(stat1
->st_fsmid
, dpath
)) == 0)
831 if (VerboseOpt
>= 3) {
832 if (UseFSMIDOpt
) /* always true!?! */
833 logstd("%-32s fsmid-nochange\n", (dpath
? dpath
: spath
));
835 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
843 stat1
->st_size
== st2
.st_size
&&
844 (ValidateOpt
== 2 || mtimecmp(stat1
, &st2
) == 0) &&
845 OwnerMatch(stat1
, &st2
)
847 && (UseMD5Opt
== 0 || !S_ISREG(stat1
->st_mode
) ||
848 (mres
= md5_check(spath
, dpath
)) == 0)
850 #ifdef _ST_FSMID_PRESENT_
851 && (UseFSMIDOpt
== 0 ||
852 (fres
= fsmid_check(stat1
->st_fsmid
, dpath
)) == 0)
854 && (ValidateOpt
== 0 || !S_ISREG(stat1
->st_mode
) ||
855 validate_check(spath
, dpath
) == 0)
858 * The files are identical, but if we are running as
859 * root we might need to adjust ownership/group/flags.
862 int changedflags
= 0;
865 hltsetdino(hln
, st2
.st_ino
);
867 if (!OwnerMatch(stat1
, &st2
)) {
868 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
871 #ifdef _ST_FLAGS_PRESENT_
872 if (!FlagsMatch(stat1
, &st2
)) {
873 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
877 if (VerboseOpt
>= 3) {
880 logstd("%-32s md5-nochange",
881 (dpath
? dpath
: spath
));
885 logstd("%-32s fsmid-nochange",
886 (dpath
? dpath
: spath
));
887 } else if (ValidateOpt
) {
888 logstd("%-32s nochange (contents validated)",
889 (dpath
? dpath
: spath
));
891 logstd("%-32s nochange", (dpath
? dpath
: spath
));
894 logstd(" (uid/gid differ)");
896 logstd(" (flags differ)");
899 CountSourceBytes
+= size
;
906 if (st2Valid
&& !S_ISDIR(stat1
->st_mode
) && S_ISDIR(st2
.st_mode
)) {
908 logerr("%-32s SAFETY - refusing to copy file over directory\n",
909 (dpath
? dpath
: spath
)
913 goto done
; /* continue with the cpdup anyway */
915 if (QuietOpt
== 0 || AskConfirmation
) {
916 logstd("%-32s WARNING: non-directory source will blow away\n"
917 "%-32s preexisting dest directory, continuing anyway!\n",
918 ((dpath
) ? dpath
: spath
), "");
921 RemoveRecur(dpath
, ddevNo
, &st2
);
926 * The various comparisons failed, copy it.
928 if (S_ISDIR(stat1
->st_mode
)) {
932 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
935 if (!st2Valid
|| S_ISDIR(st2
.st_mode
) == 0) {
937 xremove(&DstHost
, dpath
);
938 if (hc_mkdir(&DstHost
, dpath
, stat1
->st_mode
| 0700) != 0) {
939 logerr("%s: mkdir failed: %s\n",
940 (dpath
? dpath
: spath
), strerror(errno
));
944 if (hc_lstat(&DstHost
, dpath
, &st2
) != 0) {
945 if (NotForRealOpt
== 0)
946 logerr("%s: lstat of newly made dir failed: %s\n",
947 (dpath
? dpath
: spath
), strerror(errno
));
954 if (!OwnerMatch(stat1
, &st2
) &&
955 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
) != 0
957 logerr("%s: chown of newly made dir failed: %s\n",
958 (dpath
? dpath
: spath
), strerror(errno
));
960 /* Note that we should not set skipdir = 1 here. */
964 logstd("%-32s mkdir-ok\n", (dpath
? dpath
: spath
));
968 * Directory must be scanable by root for cpdup to
969 * work. We'll fix it later if the directory isn't
970 * supposed to be readable ( which is why we fixup
971 * st2.st_mode to match what we did ).
973 if ((st2
.st_mode
& 0700) != 0700) {
974 hc_chmod(&DstHost
, dpath
, st2
.st_mode
| 0700);
978 logstd("%s\n", dpath
? dpath
: spath
);
983 * When copying a directory, stop if the source crosses a mount
986 if (sdevNo
!= (dev_t
)-1 && stat1
->st_dev
!= sdevNo
)
989 sdevNo
= stat1
->st_dev
;
992 * When copying a directory, stop if the destination crosses
995 * The target directory will have been created and stat'd
996 * for st2 if it did not previously exist. st2Valid is left
997 * as a flag. If the stat failed st2 will still only have its
998 * default initialization.
1000 * So we simply assume here that the directory is within the
1001 * current target mount if we had to create it (aka st2Valid is 0)
1002 * and we leave ddevNo alone.
1005 if (ddevNo
!= (dev_t
)-1 && st2
.st_dev
!= ddevNo
)
1008 ddevNo
= st2
.st_dev
;
1012 List
*list
= malloc(sizeof(List
));
1016 logstd("Scanning %s ...\n", spath
);
1018 if (ScanDir(list
, &SrcHost
, spath
, &CountSourceReadBytes
, 0) == 0) {
1020 while ((node
= IterateList(list
, node
, 0)) != NULL
) {
1022 char *ndpath
= NULL
;
1024 nspath
= mprintf("%s/%s", spath
, node
->no_Name
);
1026 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1028 info
->spath
= nspath
;
1029 info
->dpath
= ndpath
;
1030 info
->sdevNo
= sdevNo
;
1031 info
->ddevNo
= ddevNo
;
1033 r
+= DoCopy(info
, node
->no_Stat
, depth
);
1035 r
+= DoCopy(info
, node
->no_Stat
, depth
+ 1);
1044 * Remove files/directories from destination that do not appear
1047 if (dpath
&& ScanDir(list
, &DstHost
, dpath
,
1048 &CountTargetReadBytes
, 3) == 0) {
1050 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1052 * If object does not exist in source or .cpignore
1053 * then recursively remove it.
1057 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1058 RemoveRecur(ndpath
, ddevNo
, node
->no_Stat
);
1067 if (dpath
&& st2Valid
) {
1068 struct timeval tv
[2];
1070 if (ForceOpt
|| !OwnerMatch(stat1
, &st2
))
1071 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1072 if (stat1
->st_mode
!= st2
.st_mode
)
1073 hc_chmod(&DstHost
, dpath
, stat1
->st_mode
);
1074 #ifdef _ST_FLAGS_PRESENT_
1075 if (!FlagsMatch(stat1
, &st2
))
1076 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1078 if (ForceOpt
|| mtimecmp(stat1
, &st2
) != 0) {
1079 bzero(tv
, sizeof(tv
));
1080 tv
[0].tv_sec
= stat1
->st_mtime
;
1081 tv
[1].tv_sec
= stat1
->st_mtime
;
1082 #if defined(st_mtime) /* A macro, so very likely on modern POSIX */
1083 tv
[0].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1084 tv
[1].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1086 hc_utimes(&DstHost
, dpath
, tv
);
1089 } else if (dpath
== NULL
) {
1091 * If dpath is NULL, we are just updating the MD5
1094 if (UseMD5Opt
&& S_ISREG(stat1
->st_mode
)) {
1095 mres
= md5_check(spath
, NULL
);
1097 if (VerboseOpt
> 1) {
1099 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1101 logstd("%-32s md5-ok\n", (dpath
) ? dpath
: spath
);
1102 } else if (!QuietOpt
&& mres
< 0) {
1103 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1107 } else if (S_ISREG(stat1
->st_mode
)) {
1114 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1116 path
= mprintf("%s", dpath
);
1119 * Handle check failure message.
1123 logerr("%-32s md5-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1127 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1130 * Not quite ready to do the copy yet. If UseHLPath is defined,
1131 * see if we can hardlink instead.
1133 * If we can hardlink, and the target exists, we have to remove it
1134 * first or the hardlink will fail. This can occur in a number of
1135 * situations but most typically when the '-f -H' combination is
1138 if (UseHLPath
&& (hpath
= checkHLPath(stat1
, spath
, dpath
)) != NULL
) {
1140 xremove(&DstHost
, dpath
);
1141 if (hc_link(&DstHost
, hpath
, dpath
) == 0) {
1144 logstd("%-32s hardlinked(-H)\n",
1145 (dpath
? dpath
: spath
));
1151 * Shucks, we may have hit a filesystem hard linking limit,
1152 * we have to copy instead.
1157 if ((fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0)) >= 0) {
1158 if ((fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600)) < 0) {
1160 * There could be a .tmp file from a previously interrupted
1161 * run, delete and retry. Fail if we still can't get at it.
1163 #ifdef _ST_FLAGS_PRESENT_
1164 hc_chflags(&DstHost
, path
, 0);
1166 hc_remove(&DstHost
, path
);
1167 fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_TRUNC
, 0600);
1171 char *iobuf1
= malloc(GETIOSIZE
);
1175 * Matt: What about holes?
1178 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
1180 if (hc_write(&DstHost
, fd2
, iobuf1
, n
) != n
)
1184 hc_close(&DstHost
, fd2
);
1186 struct timeval tv
[2];
1188 bzero(tv
, sizeof(tv
));
1189 tv
[0].tv_sec
= stat1
->st_mtime
;
1190 tv
[1].tv_sec
= stat1
->st_mtime
;
1191 #if defined(st_mtime)
1192 tv
[0].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1193 tv
[1].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1196 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1197 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1198 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1199 #ifdef _ST_FLAGS_PRESENT_
1200 if (stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
1201 hc_utimes(&DstHost
, path
, tv
);
1203 hc_utimes(&DstHost
, path
, tv
);
1205 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1206 logerr("%-32s rename-after-copy failed: %s\n",
1207 (dpath
? dpath
: spath
), strerror(errno
)
1212 logstd("%-32s copy-ok\n", (dpath
? dpath
: spath
));
1213 #ifdef _ST_FLAGS_PRESENT_
1214 if (DstRootPrivs
? stat1
->st_flags
: stat1
->st_flags
& UF_SETTABLE
)
1215 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1218 #ifdef _ST_FLAGS_PRESENT_
1219 if ((stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
)) == 0)
1220 hc_utimes(&DstHost
, dpath
, tv
);
1222 CountSourceReadBytes
+= size
;
1223 CountWriteBytes
+= size
;
1224 CountSourceBytes
+= size
;
1228 logerr("%-32s %s failed: %s\n",
1229 (dpath
? dpath
: spath
), op
, strerror(errno
)
1231 hc_remove(&DstHost
, path
);
1236 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1237 (dpath
? dpath
: spath
), getuid(), geteuid(),
1242 hc_close(&SrcHost
, fd1
);
1244 logerr("%-32s copy: open failed: %s\n",
1245 (dpath
? dpath
: spath
),
1254 if (!r
&& hc_stat(&DstHost
, dpath
, &st2
) == 0) {
1255 hltsetdino(hln
, st2
.st_ino
);
1261 } else if (S_ISLNK(stat1
->st_mode
)) {
1262 char *link1
= malloc(GETLINKSIZE
);
1263 char *link2
= malloc(GETLINKSIZE
);
1268 n1
= hc_readlink(&SrcHost
, spath
, link1
, GETLINKSIZE
- 1);
1270 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1271 n2
= hc_readlink(&DstHost
, dpath
, link2
, GETLINKSIZE
- 1);
1273 path
= mprintf("%s", dpath
);
1277 if (ForceOpt
|| n1
!= n2
|| bcmp(link1
, link2
, n1
) != 0) {
1278 hc_umask(&DstHost
, ~stat1
->st_mode
);
1279 xremove(&DstHost
, path
);
1281 if (hc_symlink(&DstHost
, link1
, path
) < 0) {
1282 logerr("%-32s symlink (%s->%s) failed: %s\n",
1283 (dpath
? dpath
: spath
), link1
, path
,
1288 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1289 hc_lchown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1291 * there is no lchmod() or lchflags(), we
1292 * cannot chmod or chflags a softlink.
1294 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1295 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1296 (dpath
? dpath
: spath
),
1297 path
, dpath
, strerror(errno
));
1298 } else if (VerboseOpt
) {
1299 logstd("%-32s softlink-ok\n", (dpath
? dpath
: spath
));
1301 hc_umask(&DstHost
, 000);
1302 CountWriteBytes
+= n1
;
1306 if (VerboseOpt
>= 3)
1307 logstd("%-32s nochange", (dpath
? dpath
: spath
));
1308 if (!OwnerMatch(stat1
, &st2
)) {
1309 hc_lchown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1310 if (VerboseOpt
>= 3)
1311 logstd(" (uid/gid differ)");
1313 if (VerboseOpt
>= 3)
1316 CountSourceBytes
+= n1
;
1317 CountSourceReadBytes
+= n1
;
1319 CountTargetReadBytes
+= n2
;
1323 logerr("%-32s softlink-failed\n", (dpath
? dpath
: spath
));
1328 } else if ((S_ISCHR(stat1
->st_mode
) || S_ISBLK(stat1
->st_mode
)) && DeviceOpt
) {
1333 stat1
->st_mode
!= st2
.st_mode
||
1334 stat1
->st_rdev
!= st2
.st_rdev
||
1335 !OwnerMatch(stat1
, &st2
)
1338 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1339 xremove(&DstHost
, path
);
1341 path
= mprintf("%s", dpath
);
1344 if (hc_mknod(&DstHost
, path
, stat1
->st_mode
, stat1
->st_rdev
) == 0) {
1345 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1346 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1348 xremove(&DstHost
, dpath
);
1349 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1350 logerr("%-32s dev-rename-after-create failed: %s\n",
1351 (dpath
? dpath
: spath
),
1354 } else if (VerboseOpt
) {
1355 logstd("%-32s dev-ok\n", (dpath
? dpath
: spath
));
1360 logerr("%-32s dev failed: %s\n",
1361 (dpath
? dpath
: spath
), strerror(errno
)
1365 if (VerboseOpt
>= 3)
1366 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1374 if (hln
->dino
== (ino_t
)-1) {
1376 /*hln = NULL; unneeded */
1385 ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
1386 int64_t *CountReadBytes
, int n
)
1389 struct HostConf
*cphost
;
1390 struct HCDirEntry
*den
;
1391 struct stat
*statptr
;
1395 * scan .cpignore file for files/directories to ignore
1396 * (only in the source directory, i.e. if n == 0).
1402 char *buf
= malloc(GETBUFSIZE
);
1406 if (UseCpFile
[0] == '/') {
1407 fpath
= mprintf("%s", UseCpFile
);
1410 fpath
= mprintf("%s/%s", path
, UseCpFile
);
1411 AddList(list
, strrchr(fpath
, '/') + 1, 1, NULL
);
1414 fd
= hc_open(cphost
, fpath
, O_RDONLY
, 0);
1417 while ((nread
= hc_read(cphost
, fd
, buf
+ bufused
,
1418 GETBUFSIZE
- bufused
- 1)) > 0) {
1419 *CountReadBytes
+= nread
;
1422 for (next
= buf
; (nl
= strchr(next
, '\n')); next
= nl
+1) {
1424 AddList(list
, next
, 1, NULL
);
1426 bufused
= strlen(next
);
1428 bcopy(next
, buf
, bufused
);
1431 /* last line has no trailing newline */
1433 AddList(list
, buf
, 1, NULL
);
1435 hc_close(cphost
, fd
);
1442 * Automatically exclude MD5CacheFile that we create on the
1443 * source from the copy to the destination.
1445 * Automatically exclude a FSMIDCacheFile on the source that
1446 * would otherwise overwrite the one we maintain on the target.
1449 AddList(list
, MD5CacheFile
, 1, NULL
);
1451 AddList(list
, FSMIDCacheFile
, 1, NULL
);
1454 if ((dir
= hc_opendir(host
, path
)) == NULL
)
1456 while ((den
= hc_readdir(host
, dir
, &statptr
)) != NULL
) {
1460 if (strcmp(den
->d_name
, ".") != 0 && strcmp(den
->d_name
, "..") != 0) {
1461 if (UseCpFile
&& UseCpFile
[0] == '/') {
1462 if (CheckList(list
, path
, den
->d_name
) == 0)
1465 AddList(list
, den
->d_name
, n
, statptr
);
1468 hc_closedir(host
, dir
);
1478 RemoveRecur(const char *dpath
, dev_t devNo
, struct stat
*dstat
)
1482 if (dstat
== NULL
) {
1483 if (hc_lstat(&DstHost
, dpath
, &st
) == 0)
1486 if (dstat
!= NULL
) {
1487 if (devNo
== (dev_t
)-1)
1488 devNo
= dstat
->st_dev
;
1489 if (dstat
->st_dev
== devNo
) {
1490 if (S_ISDIR(dstat
->st_mode
)) {
1493 if ((dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1494 List
*list
= malloc(sizeof(List
));
1496 struct HCDirEntry
*den
;
1499 while ((den
= hc_readdir(&DstHost
, dir
, &dstat
)) != NULL
) {
1500 if (strcmp(den
->d_name
, ".") == 0)
1502 if (strcmp(den
->d_name
, "..") == 0)
1504 AddList(list
, den
->d_name
, 3, dstat
);
1506 hc_closedir(&DstHost
, dir
);
1507 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1510 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1511 RemoveRecur(ndpath
, devNo
, node
->no_Stat
);
1517 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1519 if (xrmdir(&DstHost
, dpath
) < 0) {
1520 logerr("%-32s rmdir failed: %s\n",
1521 dpath
, strerror(errno
)
1524 CountRemovedItems
++;
1529 logstd("%-32s not-removed\n", dpath
);
1530 } else if (xrmdir(&DstHost
, dpath
) == 0) {
1532 logstd("%-32s rmdir-ok\n", dpath
);
1533 CountRemovedItems
++;
1535 logerr("%-32s rmdir failed: %s\n",
1536 dpath
, strerror(errno
)
1541 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1543 if (xremove(&DstHost
, dpath
) < 0) {
1544 logerr("%-32s remove failed: %s\n",
1545 dpath
, strerror(errno
)
1548 CountRemovedItems
++;
1553 logstd("%-32s not-removed\n", dpath
);
1554 } else if (xremove(&DstHost
, dpath
) == 0) {
1556 logstd("%-32s remove-ok\n", dpath
);
1557 CountRemovedItems
++;
1559 logerr("%-32s remove failed: %s\n",
1560 dpath
, strerror(errno
)
1570 InitList(List
*list
)
1572 bzero(list
, sizeof(List
));
1573 list
->li_Node
.no_Next
= &list
->li_Node
;
1577 ResetList(List
*list
)
1581 while ((node
= list
->li_Node
.no_Next
) != &list
->li_Node
) {
1582 list
->li_Node
.no_Next
= node
->no_Next
;
1583 if (node
->no_Stat
!= NULL
)
1584 free(node
->no_Stat
);
1591 IterateList(List
*list
, Node
*node
, int n
)
1594 node
= list
->li_Node
.no_Next
;
1596 node
= node
->no_Next
;
1597 while (node
->no_Value
!= n
&& node
!= &list
->li_Node
)
1598 node
= node
->no_Next
;
1599 return (node
== &list
->li_Node
? NULL
: node
);
1603 AddList(List
*list
, const char *name
, int n
, struct stat
*st
)
1609 * Scan against wildcards. Only a node value of 1 can be a wildcard
1610 * ( usually scanned from .cpignore )
1612 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1613 if (strcmp(name
, node
->no_Name
) == 0 ||
1614 (n
!= 1 && node
->no_Value
== 1 &&
1615 fnmatch(node
->no_Name
, name
, 0) == 0)
1617 return(node
->no_Value
);
1622 * Look for exact match
1626 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1627 if (strcmp(name
, node
->no_Name
) == 0) {
1628 return(node
->no_Value
);
1631 node
= malloc(sizeof(Node
) + strlen(name
) + 1);
1633 fatal("out of memory");
1635 node
->no_Next
= list
->li_Node
.no_Next
;
1636 list
->li_Node
.no_Next
= node
;
1638 node
->no_HNext
= list
->li_Hash
[hv
];
1639 list
->li_Hash
[hv
] = node
;
1641 strcpy(node
->no_Name
, name
);
1649 * Match against n=1 (cpignore) entries
1651 * Returns 0 on match, non-zero if no match
1654 CheckList(List
*list
, const char *path
, const char *name
)
1660 if (asprintf(&fpath
, "%s/%s", path
, name
) < 0)
1661 fatal("out of memory");
1664 * Scan against wildcards. Only a node value of 1 can be a wildcard
1665 * ( usually scanned from .cpignore )
1667 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1668 if (node
->no_Value
!= 1)
1670 if (fnmatch(node
->no_Name
, fpath
, 0) == 0) {
1677 * Look for exact match
1680 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1681 if (node
->no_Value
!= 1)
1683 if (strcmp(fpath
, node
->no_Name
) == 0) {
1694 shash(const char *s
)
1701 if (*s
== '*' || *s
== '?' ||
1702 *s
== '{' || *s
== '}' ||
1703 *s
== '[' || *s
== ']' ||
1708 hv
= (hv
<< 5) ^ *s
^ (hv
>> 23);
1711 return(((hv
>> 16) ^ hv
) & HMASK
);
1715 YesNo(const char *path
)
1719 fprintf(stderr
, "remove %s (Yes/No) [No]? ", path
);
1722 first
= ch
= getchar();
1723 while (ch
!= '\n' && ch
!= EOF
)
1725 return ((first
== 'y' || first
== 'Y'));
1729 * xrename() - rename with override
1731 * If the rename fails, attempt to override st_flags on the
1732 * destination and rename again. If that fails too, try to
1733 * set the flags back the way they were and give up.
1737 xrename(const char *src
, const char *dst
, u_long flags
)
1741 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1742 #ifdef _ST_FLAGS_PRESENT_
1743 hc_chflags(&DstHost
, dst
, 0);
1744 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0)
1745 hc_chflags(&DstHost
, dst
, flags
);
1752 xlink(const char *src
, const char *dst
, u_long flags
)
1755 #ifdef _ST_FLAGS_PRESENT_
1759 if ((r
= hc_link(&DstHost
, src
, dst
)) < 0) {
1760 #ifdef _ST_FLAGS_PRESENT_
1761 hc_chflags(&DstHost
, src
, 0);
1762 r
= hc_link(&DstHost
, src
, dst
);
1764 hc_chflags(&DstHost
, src
, flags
);
1774 xremove(struct HostConf
*host
, const char *path
)
1778 res
= hc_remove(host
, path
);
1779 #ifdef _ST_FLAGS_PRESENT_
1780 if (res
== -EPERM
) {
1781 hc_chflags(host
, path
, 0);
1782 res
= hc_remove(host
, path
);
1789 xrmdir(struct HostConf
*host
, const char *path
)
1793 res
= hc_rmdir(host
, path
);
1794 #ifdef _ST_FLAGS_PRESENT_
1795 if (res
== -EPERM
) {
1796 hc_chflags(host
, path
, 0);
1797 res
= hc_rmdir(host
, path
);
1804 * Compare mtimes. By default cpdup only compares the seconds field
1805 * because different operating systems and filesystems will store time
1806 * fields with varying amounts of precision.
1808 * This subroutine can be adjusted to also compare to microseconds or
1809 * nanoseconds precision. However, since cpdup() uses utimes() to
1810 * set a file's timestamp and utimes() only takes timeval's (usec precision),
1811 * I strongly recommend only comparing down to usec precision at best.
1814 mtimecmp(struct stat
*st1
, struct stat
*st2
)
1816 if (st1
->st_mtime
< st2
->st_mtime
)
1818 if (st1
->st_mtime
== st2
->st_mtime
)