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 getbool(const char *str
);
114 static char *SplitRemote(char **pathp
);
115 static int ChgrpAllowed(gid_t g
);
116 static int OwnerMatch(struct stat
*st1
, struct stat
*st2
);
117 #ifdef _ST_FLAGS_PRESENT_
118 static int FlagsMatch(struct stat
*st1
, struct stat
*st2
);
120 #define FlagsMatch(st1, st2) 1
122 static struct hlink
*hltlookup(struct stat
*);
123 static struct hlink
*hltadd(struct stat
*, const char *);
124 static char *checkHLPath(struct stat
*st
, const char *spath
, const char *dpath
);
125 static int validate_check(const char *spath
, const char *dpath
);
126 static int shash(const char *s
);
127 static void hltdelete(struct hlink
*);
128 static void hltsetdino(struct hlink
*, ino_t
);
129 static int YesNo(const char *path
);
130 static int xrename(const char *src
, const char *dst
, u_long flags
);
131 static int xlink(const char *src
, const char *dst
, u_long flags
);
132 static int xremove(struct HostConf
*host
, const char *path
);
133 static int DoCopy(copy_info_t info
, struct stat
*stat1
, int depth
);
134 static int ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
135 int64_t *CountReadBytes
, int n
);
137 int AskConfirmation
= 1;
154 const char *ssh_argv
[16];
157 const char *UseCpFile
;
158 const char *MD5CacheFile
;
159 const char *FSMIDCacheFile
;
160 const char *UseHLPath
;
162 static int DstBaseLen
;
163 static int HardLinkCount
;
164 static int GroupCount
;
165 static gid_t
*GroupList
;
167 int64_t CountSourceBytes
;
168 int64_t CountSourceItems
;
169 int64_t CountCopiedItems
;
170 int64_t CountSourceReadBytes
;
171 int64_t CountTargetReadBytes
;
172 int64_t CountWriteBytes
;
173 int64_t CountRemovedItems
;
174 int64_t CountLinkedItems
;
176 static struct HostConf SrcHost
;
177 static struct HostConf DstHost
;
180 main(int ac
, char **av
)
187 struct timeval start
;
188 struct copy_info info
;
190 signal(SIGPIPE
, SIG_IGN
);
192 gettimeofday(&start
, NULL
);
194 while ((opt
= getopt(ac
, av
, ":CdnF:fH:Ii:j:K:klM:mopqRSs:uVvX:x")) != -1) {
196 /* TODO: sort the branches */
223 UseCpFile
= ".cpignore";
233 fatal("too many -F options");
234 ssh_argv
[ssh_argc
++] = optarg
;
246 AskConfirmation
= getbool(optarg
);
249 DeviceOpt
= getbool(optarg
);
252 SafetyOpt
= getbool(optarg
);
259 FSMIDCacheFile
= ".FSMID.CHECK";
263 FSMIDCacheFile
= optarg
;
267 MD5CacheFile
= optarg
;
271 MD5CacheFile
= ".MD5.CHECKSUMS";
274 setvbuf(stdout
, NULL
, _IOLBF
, 0);
277 fatal("missing argument for option: -%c\n", optopt
);
281 fatal("illegal option: -%c\n", optopt
);
297 fatal("too many arguments");
300 * If we are told to go into slave mode, run the HC protocol
303 DstRootPrivs
= (geteuid() == 0);
309 * Extract the source and/or/neither target [user@]host and
310 * make any required connections.
312 if (src
&& (ptr
= SplitRemote(&src
)) != NULL
) {
316 fatal("The MD5 options are not currently supported for remote sources");
317 if (hc_connect(&SrcHost
, ReadOnlyOpt
) < 0)
319 } else if (ReadOnlyOpt
)
320 fatal("The -R option is only supported for remote sources");
322 if (dst
&& (ptr
= SplitRemote(&dst
)) != NULL
) {
326 fatal("The FSMID options are not currently supported for remote targets");
327 if (hc_connect(&DstHost
, 0) < 0)
332 * dst may be NULL only if -m option is specified,
333 * which forces an update of the MD5 checksums
335 if (dst
== NULL
&& UseMD5Opt
== 0) {
341 DstRootPrivs
= (hc_geteuid(&DstHost
) == 0);
343 GroupCount
= hc_getgroups(&DstHost
, &GroupList
);
347 fprintf(stderr
, "DstRootPrivs == %s\n", DstRootPrivs
? "true" : "false");
348 fprintf(stderr
, "GroupCount == %d\n", GroupCount
);
349 for (i
= 0; i
< GroupCount
; i
++)
350 fprintf(stderr
, "Group[%d] == %d\n", i
, GroupList
[i
]);
353 bzero(&info
, sizeof(info
));
355 DstBaseLen
= strlen(dst
);
358 info
.sdevNo
= (dev_t
)-1;
359 info
.ddevNo
= (dev_t
)-1;
360 i
= DoCopy(&info
, NULL
, -1);
364 info
.sdevNo
= (dev_t
)-1;
365 info
.ddevNo
= (dev_t
)-1;
366 i
= DoCopy(&info
, NULL
, -1);
373 if (SummaryOpt
&& i
== 0) {
377 gettimeofday(&end
, NULL
);
379 /* don't count stat's in our byte statistics */
380 CountSourceBytes
+= sizeof(struct stat
) * CountSourceItems
;
381 CountSourceReadBytes
+= sizeof(struct stat
) * CountSourceItems
;
382 CountWriteBytes
+= sizeof(struct stat
) * CountCopiedItems
;
383 CountWriteBytes
+= sizeof(struct stat
) * CountRemovedItems
;
386 duration
= (end
.tv_sec
- start
.tv_sec
);
387 duration
+= (double)(end
.tv_usec
- start
.tv_usec
) / 1000000.0;
390 logstd("cpdup completed successfully\n");
391 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
392 "%lld bytes written (%.1fX speedup)\n",
393 (long long)CountSourceBytes
,
394 (long long)CountSourceReadBytes
,
395 (long long)CountTargetReadBytes
,
396 (long long)CountWriteBytes
,
397 ((double)CountSourceBytes
* 2.0) / ((double)(CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
)));
398 logstd("%lld source items, %lld items copied, %lld items linked, "
399 "%lld things deleted\n",
400 (long long)CountSourceItems
,
401 (long long)CountCopiedItems
,
402 (long long)CountLinkedItems
,
403 (long long)CountRemovedItems
);
404 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
406 (int)((CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
) / duration
/ 1024.0),
407 (int)(CountSourceBytes
/ duration
/ 1024.0));
409 exit((i
== 0) ? 0 : 1);
413 getbool(const char *str
)
415 if (strcmp(str
, "0") == 0)
417 if (strcmp(str
, "1") == 0)
419 fatal("option requires boolean argument (0 or 1): -%c\n", optopt
);
425 * Check if path specifies a remote path, using the same syntax as scp(1),
426 * i.e. a path is considered remote if the first colon is not preceded by
427 * a slash, so e.g. "./foo:bar" is considered local.
428 * If a remote path is detected, the colon is replaced with a null byte,
429 * and the return value is a pointer to the next character.
430 * Otherwise NULL is returned.
432 * A path prefix of localhost is the same as a locally specified file or
433 * directory path, but prevents any further interpretation of the path
434 * as being a remote hostname (for paths that have colons in them).
437 SplitRemote(char **pathp
)
442 if (path
[(cindex
= strcspn(path
, ":/"))] == ':') {
444 if (strcmp(path
, "localhost") != 0)
445 return (path
+ cindex
);
446 *pathp
= path
+ cindex
;
452 * Check if group g is in our GroupList.
454 * Typically the number of groups a user belongs to isn't large
455 * enough to warrant more effort than a simple linear search.
456 * However, we perform an optimization by moving a group to the
457 * top of the list when we have a hit. This assumes that there
458 * isn't much variance in the gids of files that a non-root user
459 * copies. So most of the time the search will terminate on the
460 * first element of the list.
463 ChgrpAllowed(gid_t g
)
467 for (i
= 0; i
< GroupCount
; i
++)
468 if (GroupList
[i
] == g
) {
470 /* Optimize: Move g to the front of the list. */
472 GroupList
[i
] = GroupList
[i
- 1];
481 * The following two functions return true if the ownership (UID + GID)
482 * or the flags of two files match, respectively.
484 * Only perform weak checking if we don't have sufficient privileges on
485 * the target machine, so we don't waste transfers with things that are
486 * bound to fail anyway.
489 OwnerMatch(struct stat
*st1
, struct stat
*st2
)
492 /* Both UID and GID must match. */
493 return (st1
->st_uid
== st2
->st_uid
&& st1
->st_gid
== st2
->st_gid
);
495 /* Ignore UID, and also ignore GID if we can't chgrp to that group. */
496 return (st1
->st_gid
== st2
->st_gid
|| !ChgrpAllowed(st1
->st_gid
));
499 #ifdef _ST_FLAGS_PRESENT_
501 FlagsMatch(struct stat
*st1
, struct stat
*st2
)
504 return (st1
->st_flags
== st2
->st_flags
);
506 /* Only consider the user-settable flags. */
507 return (((st1
->st_flags
^ st2
->st_flags
) & UF_SETTABLE
) == 0);
512 static struct hlink
*
513 hltlookup(struct stat
*stp
)
518 n
= stp
->st_ino
& HLMASK
;
520 for (hl
= hltable
[n
]; hl
; hl
= hl
->next
) {
521 if (hl
->ino
== stp
->st_ino
) {
530 static struct hlink
*
531 hltadd(struct stat
*stp
, const char *path
)
534 int plen
= strlen(path
);
537 new = malloc(offsetof(struct hlink
, name
[plen
+ 1]));
539 fatal("out of memory");
542 /* initialize and link the new element into the table */
543 new->ino
= stp
->st_ino
;
544 new->dino
= (ino_t
)-1;
546 bcopy(path
, new->name
, plen
+ 1);
549 n
= stp
->st_ino
& HLMASK
;
550 new->next
= hltable
[n
];
552 hltable
[n
]->prev
= new;
559 hltsetdino(struct hlink
*hl
, ino_t inum
)
565 hltdelete(struct hlink
*hl
)
567 assert(hl
->refs
== 1);
571 hl
->next
->prev
= hl
->prev
;
572 hl
->prev
->next
= hl
->next
;
575 hl
->next
->prev
= NULL
;
577 hltable
[hl
->ino
& HLMASK
] = hl
->next
;
584 hltrels(struct hlink
*hl
)
586 assert(hl
->refs
== 1);
591 * If UseHLPath is defined check to see if the file in question is
592 * the same as the source file, and if it is return a pointer to the
593 * -H path based file for hardlinking. Else return NULL.
596 checkHLPath(struct stat
*st1
, const char *spath
, const char *dpath
)
602 asprintf(&hpath
, "%s%s", UseHLPath
, dpath
+ DstBaseLen
);
605 * stat info matches ?
607 if (hc_stat(&DstHost
, hpath
, &sthl
) < 0 ||
608 st1
->st_size
!= sthl
.st_size
||
609 st1
->st_mtime
!= sthl
.st_mtime
||
610 !OwnerMatch(st1
, &sthl
) ||
611 !FlagsMatch(st1
, &sthl
)
618 * If ForceOpt or ValidateOpt is set we have to compare the files
620 if (ForceOpt
|| ValidateOpt
) {
621 error
= validate_check(spath
, hpath
);
631 * Return 0 if the contents of the file <spath> matches the contents of
635 validate_check(const char *spath
, const char *dpath
)
641 fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0);
642 fd2
= hc_open(&DstHost
, dpath
, O_RDONLY
, 0);
645 if (fd1
>= 0 && fd2
>= 0) {
648 char *iobuf1
= malloc(GETIOSIZE
);
649 char *iobuf2
= malloc(GETIOSIZE
);
651 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
652 CountSourceReadBytes
+= n
;
653 x
= hc_read(&DstHost
, fd2
, iobuf2
, GETIOSIZE
);
655 CountTargetReadBytes
+= x
;
658 if (bcmp(iobuf1
, iobuf2
, n
) != 0)
667 hc_close(&SrcHost
, fd1
);
669 hc_close(&DstHost
, fd2
);
674 DoCopy(copy_info_t info
, struct stat
*stat1
, int depth
)
676 const char *spath
= info
->spath
;
677 const char *dpath
= info
->dpath
;
678 dev_t sdevNo
= info
->sdevNo
;
679 dev_t ddevNo
= info
->ddevNo
;
682 unsigned long st2_flags
;
683 int r
, mres
, fres
, st2Valid
;
687 r
= mres
= fres
= st2Valid
= 0;
693 if (hc_lstat(&SrcHost
, spath
, &st1
) != 0) {
700 /* skip snapshot files because they're sparse and _huge_ */
701 if (stat1
->st_flags
& SF_SNAPSHOT
)
704 st2
.st_mode
= 0; /* in case lstat fails */
705 st2
.st_flags
= 0; /* in case lstat fails */
706 if (dpath
&& hc_lstat(&DstHost
, dpath
, &st2
) == 0) {
708 #ifdef _ST_FLAGS_PRESENT_
709 st2_flags
= st2
.st_flags
;
713 if (S_ISREG(stat1
->st_mode
))
714 size
= stat1
->st_size
;
720 if (S_ISREG(stat1
->st_mode
) && stat1
->st_nlink
> 1 && dpath
) {
721 if ((hln
= hltlookup(stat1
)) != NULL
) {
725 if (st2
.st_ino
== hln
->dino
) {
727 * hard link is already correct, nothing to do
730 logstd("%-32s nochange\n", (dpath
) ? dpath
: spath
);
731 if (hln
->nlinked
== stat1
->st_nlink
) {
740 * hard link is not correct, attempt to unlink it
742 if (xremove(&DstHost
, dpath
) < 0) {
743 logerr("%-32s hardlink: unable to unlink: %s\n",
744 ((dpath
) ? dpath
: spath
), strerror(errno
));
753 if (xlink(hln
->name
, dpath
, stat1
->st_flags
) < 0) {
754 int tryrelink
= (errno
== EMLINK
);
755 logerr("%-32s hardlink: unable to link to %s: %s\n",
756 (dpath
? dpath
: spath
), hln
->name
, strerror(errno
)
761 logerr("%-20s hardlink: will attempt to copy normally\n",
762 (dpath
? dpath
: spath
));
767 if (hln
->nlinked
== stat1
->st_nlink
) {
773 logstd("%-32s hardlink: %s\n",
774 (dpath
? dpath
: spath
),
775 (st2Valid
? "relinked" : "linked")
786 * first instance of hardlink must be copied normally
789 hln
= hltadd(stat1
, dpath
);
794 * Do we need to copy the file/dir/link/whatever? Early termination
795 * if we do not. Always redo links. Directories are always traversed
796 * except when the FSMID options are used.
798 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
803 && stat1
->st_mode
== st2
.st_mode
804 && FlagsMatch(stat1
, &st2
)
806 if (S_ISLNK(stat1
->st_mode
) || S_ISDIR(stat1
->st_mode
)) {
808 * If FSMID tracking is turned on we can avoid recursing through
809 * an entire directory subtree if the FSMID matches.
811 #ifdef _ST_FSMID_PRESENT_
813 (UseFSMIDOpt
&& (fres
= fsmid_check(stat1
->st_fsmid
, dpath
)) == 0)
815 if (VerboseOpt
>= 3) {
816 if (UseFSMIDOpt
) /* always true!?! */
817 logstd("%-32s fsmid-nochange\n", (dpath
? dpath
: spath
));
819 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
827 stat1
->st_size
== st2
.st_size
&&
828 (ValidateOpt
== 2 || stat1
->st_mtime
== st2
.st_mtime
) &&
829 OwnerMatch(stat1
, &st2
)
831 && (UseMD5Opt
== 0 || !S_ISREG(stat1
->st_mode
) ||
832 (mres
= md5_check(spath
, dpath
)) == 0)
834 #ifdef _ST_FSMID_PRESENT_
835 && (UseFSMIDOpt
== 0 ||
836 (fres
= fsmid_check(stat1
->st_fsmid
, dpath
)) == 0)
838 && (ValidateOpt
== 0 || !S_ISREG(stat1
->st_mode
) ||
839 validate_check(spath
, dpath
) == 0)
842 * The files are identical, but if we are running as
843 * root we might need to adjust ownership/group/flags.
846 int changedflags
= 0;
849 hltsetdino(hln
, st2
.st_ino
);
851 if (!OwnerMatch(stat1
, &st2
)) {
852 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
855 #ifdef _ST_FLAGS_PRESENT_
856 if (!FlagsMatch(stat1
, &st2
)) {
857 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
861 if (VerboseOpt
>= 3) {
864 logstd("%-32s md5-nochange",
865 (dpath
? dpath
: spath
));
869 logstd("%-32s fsmid-nochange",
870 (dpath
? dpath
: spath
));
871 } else if (ValidateOpt
) {
872 logstd("%-32s nochange (contents validated)",
873 (dpath
? dpath
: spath
));
875 logstd("%-32s nochange", (dpath
? dpath
: spath
));
878 logstd(" (uid/gid differ)");
880 logstd(" (flags differ)");
883 CountSourceBytes
+= size
;
890 if (st2Valid
&& !S_ISDIR(stat1
->st_mode
) && S_ISDIR(st2
.st_mode
)) {
892 logerr("%-32s SAFETY - refusing to copy file over directory\n",
893 (dpath
? dpath
: spath
)
897 goto done
; /* continue with the cpdup anyway */
899 if (QuietOpt
== 0 || AskConfirmation
) {
900 logstd("%-32s WARNING: non-directory source will blow away\n"
901 "%-32s preexisting dest directory, continuing anyway!\n",
902 ((dpath
) ? dpath
: spath
), "");
905 RemoveRecur(dpath
, ddevNo
, &st2
);
910 * The various comparisons failed, copy it.
912 if (S_ISDIR(stat1
->st_mode
)) {
916 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
919 if (!st2Valid
|| S_ISDIR(st2
.st_mode
) == 0) {
921 xremove(&DstHost
, dpath
);
922 if (hc_mkdir(&DstHost
, dpath
, stat1
->st_mode
| 0700) != 0) {
923 logerr("%s: mkdir failed: %s\n",
924 (dpath
? dpath
: spath
), strerror(errno
));
928 if (hc_lstat(&DstHost
, dpath
, &st2
) != 0) {
929 if (NotForRealOpt
== 0)
930 logerr("%s: lstat of newly made dir failed: %s\n",
931 (dpath
? dpath
: spath
), strerror(errno
));
938 if (!OwnerMatch(stat1
, &st2
) &&
939 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
) != 0
941 logerr("%s: chown of newly made dir failed: %s\n",
942 (dpath
? dpath
: spath
), strerror(errno
));
944 /* Note that we should not set skipdir = 1 here. */
950 * Directory must be scanable by root for cpdup to
951 * work. We'll fix it later if the directory isn't
952 * supposed to be readable ( which is why we fixup
953 * st2.st_mode to match what we did ).
955 if ((st2
.st_mode
& 0700) != 0700) {
956 hc_chmod(&DstHost
, dpath
, st2
.st_mode
| 0700);
960 logstd("%s\n", dpath
? dpath
: spath
);
965 * When copying a directory, stop if the source crosses a mount
968 if (sdevNo
!= (dev_t
)-1 && stat1
->st_dev
!= sdevNo
)
971 sdevNo
= stat1
->st_dev
;
974 * When copying a directory, stop if the destination crosses
977 * The target directory will have been created and stat'd
978 * for st2 if it did not previously exist. st2Valid is left
979 * as a flag. If the stat failed st2 will still only have its
980 * default initialization.
982 * So we simply assume here that the directory is within the
983 * current target mount if we had to create it (aka st2Valid is 0)
984 * and we leave ddevNo alone.
987 if (ddevNo
!= (dev_t
)-1 && st2
.st_dev
!= ddevNo
)
994 List
*list
= malloc(sizeof(List
));
998 logstd("Scanning %s ...\n", spath
);
1000 if (ScanDir(list
, &SrcHost
, spath
, &CountSourceReadBytes
, 0) == 0) {
1002 while ((node
= IterateList(list
, node
, 0)) != NULL
) {
1004 char *ndpath
= NULL
;
1006 nspath
= mprintf("%s/%s", spath
, node
->no_Name
);
1008 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1010 info
->spath
= nspath
;
1011 info
->dpath
= ndpath
;
1012 info
->sdevNo
= sdevNo
;
1013 info
->ddevNo
= ddevNo
;
1015 r
+= DoCopy(info
, node
->no_Stat
, depth
);
1017 r
+= DoCopy(info
, node
->no_Stat
, depth
+ 1);
1026 * Remove files/directories from destination that do not appear
1029 if (dpath
&& ScanDir(list
, &DstHost
, dpath
,
1030 &CountTargetReadBytes
, 3) == 0) {
1032 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1034 * If object does not exist in source or .cpignore
1035 * then recursively remove it.
1039 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1040 RemoveRecur(ndpath
, ddevNo
, node
->no_Stat
);
1049 if (dpath
&& st2Valid
) {
1050 struct timeval tv
[2];
1052 if (ForceOpt
|| !OwnerMatch(stat1
, &st2
))
1053 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1054 if (stat1
->st_mode
!= st2
.st_mode
)
1055 hc_chmod(&DstHost
, dpath
, stat1
->st_mode
);
1056 #ifdef _ST_FLAGS_PRESENT_
1057 if (!FlagsMatch(stat1
, &st2
))
1058 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1060 if (ForceOpt
|| stat1
->st_mtime
!= st2
.st_mtime
) {
1061 bzero(tv
, sizeof(tv
));
1062 tv
[0].tv_sec
= stat1
->st_mtime
;
1063 tv
[1].tv_sec
= stat1
->st_mtime
;
1064 hc_utimes(&DstHost
, dpath
, tv
);
1067 } else if (dpath
== NULL
) {
1069 * If dpath is NULL, we are just updating the MD5
1072 if (UseMD5Opt
&& S_ISREG(stat1
->st_mode
)) {
1073 mres
= md5_check(spath
, NULL
);
1075 if (VerboseOpt
> 1) {
1077 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1079 logstd("%-32s md5-ok\n", (dpath
) ? dpath
: spath
);
1080 } else if (!QuietOpt
&& mres
< 0) {
1081 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1085 } else if (S_ISREG(stat1
->st_mode
)) {
1092 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1094 path
= mprintf("%s", dpath
);
1097 * Handle check failure message.
1101 logerr("%-32s md5-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1105 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1108 * Not quite ready to do the copy yet. If UseHLPath is defined,
1109 * see if we can hardlink instead.
1111 * If we can hardlink, and the target exists, we have to remove it
1112 * first or the hardlink will fail. This can occur in a number of
1113 * situations but most typically when the '-f -H' combination is
1116 if (UseHLPath
&& (hpath
= checkHLPath(stat1
, spath
, dpath
)) != NULL
) {
1118 xremove(&DstHost
, dpath
);
1119 if (hc_link(&DstHost
, hpath
, dpath
) == 0) {
1122 logstd("%-32s hardlinked(-H)\n",
1123 (dpath
? dpath
: spath
));
1129 * Shucks, we may have hit a filesystem hard linking limit,
1130 * we have to copy instead.
1135 if ((fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0)) >= 0) {
1136 if ((fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600)) < 0) {
1138 * There could be a .tmp file from a previously interrupted
1139 * run, delete and retry. Fail if we still can't get at it.
1141 #ifdef _ST_FLAGS_PRESENT_
1142 hc_chflags(&DstHost
, path
, 0);
1144 hc_remove(&DstHost
, path
);
1145 fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_TRUNC
, 0600);
1149 char *iobuf1
= malloc(GETIOSIZE
);
1153 * Matt: What about holes?
1156 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
1158 if (hc_write(&DstHost
, fd2
, iobuf1
, n
) != n
)
1162 hc_close(&DstHost
, fd2
);
1164 struct timeval tv
[2];
1166 bzero(tv
, sizeof(tv
));
1167 tv
[0].tv_sec
= stat1
->st_mtime
;
1168 tv
[1].tv_sec
= stat1
->st_mtime
;
1170 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1171 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1172 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1173 #ifdef _ST_FLAGS_PRESENT_
1174 if (stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
1175 hc_utimes(&DstHost
, path
, tv
);
1177 hc_utimes(&DstHost
, path
, tv
);
1179 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1180 logerr("%-32s rename-after-copy failed: %s\n",
1181 (dpath
? dpath
: spath
), strerror(errno
)
1186 logstd("%-32s copy-ok\n", (dpath
? dpath
: spath
));
1187 #ifdef _ST_FLAGS_PRESENT_
1188 if (DstRootPrivs
? stat1
->st_flags
: stat1
->st_flags
& UF_SETTABLE
)
1189 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1192 #ifdef _ST_FLAGS_PRESENT_
1193 if ((stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
)) == 0)
1194 hc_utimes(&DstHost
, dpath
, tv
);
1196 CountSourceReadBytes
+= size
;
1197 CountWriteBytes
+= size
;
1198 CountSourceBytes
+= size
;
1202 logerr("%-32s %s failed: %s\n",
1203 (dpath
? dpath
: spath
), op
, strerror(errno
)
1205 hc_remove(&DstHost
, path
);
1210 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1211 (dpath
? dpath
: spath
), getuid(), geteuid(),
1216 hc_close(&SrcHost
, fd1
);
1218 logerr("%-32s copy: open failed: %s\n",
1219 (dpath
? dpath
: spath
),
1228 if (!r
&& hc_stat(&DstHost
, dpath
, &st2
) == 0) {
1229 hltsetdino(hln
, st2
.st_ino
);
1235 } else if (S_ISLNK(stat1
->st_mode
)) {
1236 char *link1
= malloc(GETLINKSIZE
);
1237 char *link2
= malloc(GETLINKSIZE
);
1242 n1
= hc_readlink(&SrcHost
, spath
, link1
, GETLINKSIZE
- 1);
1244 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1245 n2
= hc_readlink(&DstHost
, dpath
, link2
, GETLINKSIZE
- 1);
1247 path
= mprintf("%s", dpath
);
1251 if (ForceOpt
|| n1
!= n2
|| bcmp(link1
, link2
, n1
) != 0) {
1252 hc_umask(&DstHost
, ~stat1
->st_mode
);
1253 xremove(&DstHost
, path
);
1255 if (hc_symlink(&DstHost
, link1
, path
) < 0) {
1256 logerr("%-32s symlink (%s->%s) failed: %s\n",
1257 (dpath
? dpath
: spath
), link1
, path
,
1262 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1263 hc_lchown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1265 * there is no lchmod() or lchflags(), we
1266 * cannot chmod or chflags a softlink.
1268 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1269 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1270 (dpath
? dpath
: spath
),
1271 path
, dpath
, strerror(errno
));
1272 } else if (VerboseOpt
) {
1273 logstd("%-32s softlink-ok\n", (dpath
? dpath
: spath
));
1275 hc_umask(&DstHost
, 000);
1276 CountWriteBytes
+= n1
;
1280 if (VerboseOpt
>= 3)
1281 logstd("%-32s nochange", (dpath
? dpath
: spath
));
1282 if (!OwnerMatch(stat1
, &st2
)) {
1283 hc_lchown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1284 if (VerboseOpt
>= 3)
1285 logstd(" (uid/gid differ)");
1287 if (VerboseOpt
>= 3)
1290 CountSourceBytes
+= n1
;
1291 CountSourceReadBytes
+= n1
;
1293 CountTargetReadBytes
+= n2
;
1297 logerr("%-32s softlink-failed\n", (dpath
? dpath
: spath
));
1302 } else if ((S_ISCHR(stat1
->st_mode
) || S_ISBLK(stat1
->st_mode
)) && DeviceOpt
) {
1307 stat1
->st_mode
!= st2
.st_mode
||
1308 stat1
->st_rdev
!= st2
.st_rdev
||
1309 !OwnerMatch(stat1
, &st2
)
1312 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1313 xremove(&DstHost
, path
);
1315 path
= mprintf("%s", dpath
);
1318 if (hc_mknod(&DstHost
, path
, stat1
->st_mode
, stat1
->st_rdev
) == 0) {
1319 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1320 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1322 xremove(&DstHost
, dpath
);
1323 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1324 logerr("%-32s dev-rename-after-create failed: %s\n",
1325 (dpath
? dpath
: spath
),
1328 } else if (VerboseOpt
) {
1329 logstd("%-32s dev-ok\n", (dpath
? dpath
: spath
));
1334 logerr("%-32s dev failed: %s\n",
1335 (dpath
? dpath
: spath
), strerror(errno
)
1339 if (VerboseOpt
>= 3)
1340 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1348 if (hln
->dino
== (ino_t
)-1) {
1350 /*hln = NULL; unneeded */
1359 ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
1360 int64_t *CountReadBytes
, int n
)
1363 struct HCDirEntry
*den
;
1364 struct stat
*statptr
;
1368 * scan .cpignore file for files/directories to ignore
1369 * (only in the source directory, i.e. if n == 0).
1375 char *buf
= malloc(GETBUFSIZE
);
1379 if (UseCpFile
[0] == '/') {
1380 fpath
= mprintf("%s", UseCpFile
);
1382 fpath
= mprintf("%s/%s", path
, UseCpFile
);
1384 AddList(list
, strrchr(fpath
, '/') + 1, 1, NULL
);
1385 if ((fd
= hc_open(host
, fpath
, O_RDONLY
, 0)) >= 0) {
1387 while ((nread
= hc_read(host
, fd
, buf
+ bufused
,
1388 GETBUFSIZE
- bufused
- 1)) > 0) {
1389 *CountReadBytes
+= nread
;
1392 for (next
= buf
; (nl
= strchr(next
, '\n')); next
= nl
+1) {
1394 AddList(list
, next
, 1, NULL
);
1396 bufused
= strlen(next
);
1398 bcopy(next
, buf
, bufused
);
1401 /* last line has no trailing newline */
1403 AddList(list
, buf
, 1, NULL
);
1412 * Automatically exclude MD5CacheFile that we create on the
1413 * source from the copy to the destination.
1415 * Automatically exclude a FSMIDCacheFile on the source that
1416 * would otherwise overwrite the one we maintain on the target.
1419 AddList(list
, MD5CacheFile
, 1, NULL
);
1421 AddList(list
, FSMIDCacheFile
, 1, NULL
);
1424 if ((dir
= hc_opendir(host
, path
)) == NULL
)
1426 while ((den
= hc_readdir(host
, dir
, &statptr
)) != NULL
) {
1430 if (strcmp(den
->d_name
, ".") != 0 && strcmp(den
->d_name
, "..") != 0)
1431 AddList(list
, den
->d_name
, n
, statptr
);
1433 hc_closedir(host
, dir
);
1443 RemoveRecur(const char *dpath
, dev_t devNo
, struct stat
*dstat
)
1447 if (dstat
== NULL
) {
1448 if (hc_lstat(&DstHost
, dpath
, &st
) == 0)
1451 if (dstat
!= NULL
) {
1452 if (devNo
== (dev_t
)-1)
1453 devNo
= dstat
->st_dev
;
1454 if (dstat
->st_dev
== devNo
) {
1455 if (S_ISDIR(dstat
->st_mode
)) {
1458 if ((dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1459 List
*list
= malloc(sizeof(List
));
1461 struct HCDirEntry
*den
;
1464 while ((den
= hc_readdir(&DstHost
, dir
, &dstat
)) != NULL
) {
1465 if (strcmp(den
->d_name
, ".") == 0)
1467 if (strcmp(den
->d_name
, "..") == 0)
1469 AddList(list
, den
->d_name
, 3, dstat
);
1471 hc_closedir(&DstHost
, dir
);
1472 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1475 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1476 RemoveRecur(ndpath
, devNo
, node
->no_Stat
);
1482 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1484 if (hc_rmdir(&DstHost
, dpath
) < 0) {
1485 logerr("%-32s rmdir failed: %s\n",
1486 dpath
, strerror(errno
)
1489 CountRemovedItems
++;
1494 logstd("%-32s not-removed\n", dpath
);
1495 } else if (hc_rmdir(&DstHost
, dpath
) == 0) {
1497 logstd("%-32s rmdir-ok\n", dpath
);
1498 CountRemovedItems
++;
1500 logerr("%-32s rmdir failed: %s\n",
1501 dpath
, strerror(errno
)
1506 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1508 if (xremove(&DstHost
, dpath
) < 0) {
1509 logerr("%-32s remove failed: %s\n",
1510 dpath
, strerror(errno
)
1513 CountRemovedItems
++;
1518 logstd("%-32s not-removed\n", dpath
);
1519 } else if (xremove(&DstHost
, dpath
) == 0) {
1521 logstd("%-32s remove-ok\n", dpath
);
1522 CountRemovedItems
++;
1524 logerr("%-32s remove failed: %s\n",
1525 dpath
, strerror(errno
)
1535 InitList(List
*list
)
1537 bzero(list
, sizeof(List
));
1538 list
->li_Node
.no_Next
= &list
->li_Node
;
1542 ResetList(List
*list
)
1546 while ((node
= list
->li_Node
.no_Next
) != &list
->li_Node
) {
1547 list
->li_Node
.no_Next
= node
->no_Next
;
1548 if (node
->no_Stat
!= NULL
)
1549 free(node
->no_Stat
);
1556 IterateList(List
*list
, Node
*node
, int n
)
1559 node
= list
->li_Node
.no_Next
;
1561 node
= node
->no_Next
;
1562 while (node
->no_Value
!= n
&& node
!= &list
->li_Node
)
1563 node
= node
->no_Next
;
1564 return (node
== &list
->li_Node
? NULL
: node
);
1568 AddList(List
*list
, const char *name
, int n
, struct stat
*st
)
1574 * Scan against wildcards. Only a node value of 1 can be a wildcard
1575 * ( usually scanned from .cpignore )
1578 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1579 if (strcmp(name
, node
->no_Name
) == 0 ||
1580 (n
!= 1 && node
->no_Value
== 1 &&
1581 fnmatch(node
->no_Name
, name
, 0) == 0)
1583 return(node
->no_Value
);
1588 * Look for exact match
1592 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1593 if (strcmp(name
, node
->no_Name
) == 0) {
1594 return(node
->no_Value
);
1597 node
= malloc(sizeof(Node
) + strlen(name
) + 1);
1599 fatal("out of memory");
1601 node
->no_Next
= list
->li_Node
.no_Next
;
1602 list
->li_Node
.no_Next
= node
;
1604 node
->no_HNext
= list
->li_Hash
[hv
];
1605 list
->li_Hash
[hv
] = node
;
1607 strcpy(node
->no_Name
, name
);
1615 shash(const char *s
)
1622 if (*s
== '*' || *s
== '?' ||
1623 *s
== '{' || *s
== '}' ||
1624 *s
== '[' || *s
== ']' ||
1629 hv
= (hv
<< 5) ^ *s
^ (hv
>> 23);
1632 return(((hv
>> 16) ^ hv
) & HMASK
);
1636 YesNo(const char *path
)
1640 fprintf(stderr
, "remove %s (Yes/No) [No]? ", path
);
1643 first
= ch
= getchar();
1644 while (ch
!= '\n' && ch
!= EOF
)
1646 return ((first
== 'y' || first
== 'Y'));
1650 * xrename() - rename with override
1652 * If the rename fails, attempt to override st_flags on the
1653 * destination and rename again. If that fails too, try to
1654 * set the flags back the way they were and give up.
1658 xrename(const char *src
, const char *dst
, u_long flags
)
1662 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1663 #ifdef _ST_FLAGS_PRESENT_
1664 hc_chflags(&DstHost
, dst
, 0);
1665 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0)
1666 hc_chflags(&DstHost
, dst
, flags
);
1673 xlink(const char *src
, const char *dst
, u_long flags
)
1676 #ifdef _ST_FLAGS_PRESENT_
1680 if ((r
= hc_link(&DstHost
, src
, dst
)) < 0) {
1681 #ifdef _ST_FLAGS_PRESENT_
1682 hc_chflags(&DstHost
, src
, 0);
1683 r
= hc_link(&DstHost
, src
, dst
);
1685 hc_chflags(&DstHost
, src
, flags
);
1695 xremove(struct HostConf
*host
, const char *path
)
1699 res
= hc_remove(host
, path
);
1700 #ifdef _ST_FLAGS_PRESENT_
1701 if (res
== -EPERM
) {
1702 hc_chflags(host
, path
, 0);
1703 res
= hc_remove(host
, path
);