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
;
98 typedef struct copy_info
{
105 struct hlink
*hltable
[HLSIZE
];
107 void RemoveRecur(const char *dpath
, dev_t devNo
);
108 void InitList(List
*list
);
109 void ResetList(List
*list
);
110 char *IterateList(List
*list
, Node
**nodeptr
, int n
);
111 int AddList(List
*list
, const char *name
, int n
);
112 static int getbool(const char *str
);
113 static char *SplitRemote(char *path
);
114 static int ChgrpAllowed(gid_t g
);
115 static int OwnerMatch(struct stat
*st1
, struct stat
*st2
);
116 #ifdef _ST_FLAGS_PRESENT_
117 static int FlagsMatch(struct stat
*st1
, struct stat
*st2
);
119 #define FlagsMatch(st1, st2) 1
121 static struct hlink
*hltlookup(struct stat
*);
122 static struct hlink
*hltadd(struct stat
*, const char *);
123 static char *checkHLPath(struct stat
*st
, const char *spath
, const char *dpath
);
124 static int validate_check(const char *spath
, const char *dpath
);
125 static int shash(const char *s
);
126 static void hltdelete(struct hlink
*);
127 static void hltsetdino(struct hlink
*, ino_t
);
128 int YesNo(const char *path
);
129 static int xrename(const char *src
, const char *dst
, u_long flags
);
130 static int xlink(const char *src
, const char *dst
, u_long flags
);
131 static int xremove(struct HostConf
*host
, const char *path
);
132 static int DoCopy(copy_info_t info
, int depth
);
133 static int ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
134 int64_t *CountReadBytes
, int n
);
136 int AskConfirmation
= 1;
149 int EnableDirectoryRetries
;
154 const char *ssh_argv
[16];
158 const char *UseCpFile
;
159 const char *UseHLPath
;
160 const char *MD5CacheFile
;
161 const char *FSMIDCacheFile
;
163 int64_t CountSourceBytes
;
164 int64_t CountSourceItems
;
165 int64_t CountCopiedItems
;
166 int64_t CountSourceReadBytes
;
167 int64_t CountTargetReadBytes
;
168 int64_t CountWriteBytes
;
169 int64_t CountRemovedItems
;
170 int64_t CountLinkedItems
;
172 struct HostConf SrcHost
;
173 struct HostConf DstHost
;
176 main(int ac
, char **av
)
183 struct timeval start
;
184 struct copy_info info
;
186 signal(SIGPIPE
, SIG_IGN
);
188 gettimeofday(&start
, NULL
);
190 while ((opt
= getopt(ac
, av
, ":CdF:fH:Ii:j:K:klM:mopqSs:uVvX:x")) != -1) {
192 /* TODO: sort the branches */
216 UseCpFile
= ".cpignore";
226 fatal("too many -F options");
227 ssh_argv
[ssh_argc
++] = optarg
;
236 AskConfirmation
= getbool(optarg
);
239 DeviceOpt
= getbool(optarg
);
242 SafetyOpt
= getbool(optarg
);
249 FSMIDCacheFile
= ".FSMID.CHECK";
253 FSMIDCacheFile
= optarg
;
257 MD5CacheFile
= optarg
;
261 MD5CacheFile
= ".MD5.CHECKSUMS";
264 setvbuf(stdout
, NULL
, _IOLBF
, 0);
267 fatal("missing argument for option: -%c\n", optopt
);
271 fatal("illegal option: -%c\n", optopt
);
287 fatal("too many arguments");
290 * If we are told to go into slave mode, run the HC protocol
293 DstRootPrivs
= (geteuid() == 0);
299 * Extract the source and/or/neither target [user@]host and
300 * make any required connections.
302 if (src
&& (ptr
= SplitRemote(src
)) != NULL
) {
306 fprintf(stderr
, "The MD5 options are not currently supported for remote sources\n");
309 if (hc_connect(&SrcHost
) < 0)
312 if (dst
&& (ptr
= SplitRemote(dst
)) != NULL
) {
316 fprintf(stderr
, "The FSMID options are not currently supported for remote targets\n");
319 if (hc_connect(&DstHost
) < 0)
324 * dst may be NULL only if -m option is specified,
325 * which forces an update of the MD5 checksums
327 if (dst
== NULL
&& UseMD5Opt
== 0) {
333 DstRootPrivs
= (hc_geteuid(&DstHost
) == 0);
335 GroupCount
= hc_getgroups(&DstHost
, &GroupList
);
339 fprintf(stderr
, "DstRootPrivs == %s\n", DstRootPrivs
? "true" : "false");
340 fprintf(stderr
, "GroupCount == %d\n", GroupCount
);
341 for (i
= 0; i
< GroupCount
; i
++)
342 fprintf(stderr
, "Group[%d] == %d\n", i
, GroupList
[i
]);
345 bzero(&info
, sizeof(info
));
347 DstBaseLen
= strlen(dst
);
350 info
.sdevNo
= (dev_t
)-1;
351 info
.ddevNo
= (dev_t
)-1;
352 i
= DoCopy(&info
, -1);
356 info
.sdevNo
= (dev_t
)-1;
357 info
.ddevNo
= (dev_t
)-1;
358 i
= DoCopy(&info
, -1);
365 if (SummaryOpt
&& i
== 0) {
369 gettimeofday(&end
, NULL
);
371 /* don't count stat's in our byte statistics */
372 CountSourceBytes
+= sizeof(struct stat
) * CountSourceItems
;
373 CountSourceReadBytes
+= sizeof(struct stat
) * CountSourceItems
;
374 CountWriteBytes
+= sizeof(struct stat
) * CountCopiedItems
;
375 CountWriteBytes
+= sizeof(struct stat
) * CountRemovedItems
;
378 duration
= (end
.tv_sec
- start
.tv_sec
);
379 duration
+= (double)(end
.tv_usec
- start
.tv_usec
) / 1000000.0;
382 logstd("cpdup completed successfully\n");
383 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
384 "%lld bytes written (%.1fX speedup)\n",
385 (long long)CountSourceBytes
,
386 (long long)CountSourceReadBytes
,
387 (long long)CountTargetReadBytes
,
388 (long long)CountWriteBytes
,
389 ((double)CountSourceBytes
* 2.0) / ((double)(CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
)));
390 logstd("%lld source items, %lld items copied, %lld items linked, "
391 "%lld things deleted\n",
392 (long long)CountSourceItems
,
393 (long long)CountCopiedItems
,
394 (long long)CountLinkedItems
,
395 (long long)CountRemovedItems
);
396 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
398 (int)((CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
) / duration
/ 1024.0),
399 (int)(CountSourceBytes
/ duration
/ 1024.0));
401 exit((i
== 0) ? 0 : 1);
405 getbool(const char *str
)
407 if (strcmp(str
, "0") == 0)
409 if (strcmp(str
, "1") == 0)
411 fatal("option requires boolean argument (0 or 1): -%c\n", optopt
);
417 * Check if path specifies a remote path, using the same syntax as scp(1),
418 * i.e. a path is considered remote if the first colon is not preceded by
419 * a slash, so e.g. "./foo:bar" is considered local.
420 * If a remote path is detected, the colon is replaced with a null byte,
421 * and the return value is a pointer to the next character.
422 * Otherwise NULL is returned.
425 SplitRemote(char *path
)
429 if (path
[(cindex
= strcspn(path
, ":/"))] == ':') {
431 return (path
+ cindex
);
437 * Check if group g is in our GroupList.
439 * Typically the number of groups a user belongs to isn't large
440 * enough to warrant more effort than a simple linear search.
441 * However, we perform an optimization by moving a group to the
442 * top of the list when we have a hit. This assumes that there
443 * isn't much variance in the gids of files that a non-root user
444 * copies. So most of the time the search will terminate on the
445 * first element of the list.
448 ChgrpAllowed(gid_t g
)
452 for (i
= 0; i
< GroupCount
; i
++)
453 if (GroupList
[i
] == g
) {
455 /* Optimize: Move g to the front of the list. */
457 GroupList
[i
] = GroupList
[i
- 1];
466 * The following two functions return true if the ownership (UID + GID)
467 * or the flags of two files match, respectively.
469 * Only perform weak checking if we don't have sufficient privileges on
470 * the target machine, so we don't waste transfers with things that are
471 * bound to fail anyway.
474 OwnerMatch(struct stat
*st1
, struct stat
*st2
)
477 /* Both UID and GID must match. */
478 return (st1
->st_uid
== st2
->st_uid
&& st1
->st_gid
== st2
->st_gid
);
480 /* Ignore UID, and also ignore GID if we can't chgrp to that group. */
481 return (st1
->st_gid
== st2
->st_gid
|| !ChgrpAllowed(st1
->st_gid
));
484 #ifdef _ST_FLAGS_PRESENT_
486 FlagsMatch(struct stat
*st1
, struct stat
*st2
)
489 return (st1
->st_flags
== st2
->st_flags
);
491 /* Only consider the user-settable flags. */
492 return (((st1
->st_flags
^ st2
->st_flags
) & UF_SETTABLE
) == 0);
497 static struct hlink
*
498 hltlookup(struct stat
*stp
)
503 n
= stp
->st_ino
& HLMASK
;
505 for (hl
= hltable
[n
]; hl
; hl
= hl
->next
) {
506 if (hl
->ino
== stp
->st_ino
) {
515 static struct hlink
*
516 hltadd(struct stat
*stp
, const char *path
)
519 int plen
= strlen(path
);
522 new = malloc(offsetof(struct hlink
, name
[plen
+ 1]));
524 fprintf(stderr
, "out of memory\n");
529 /* initialize and link the new element into the table */
530 new->ino
= stp
->st_ino
;
531 new->dino
= (ino_t
)-1;
533 bcopy(path
, new->name
, plen
+ 1);
536 n
= stp
->st_ino
& HLMASK
;
537 new->next
= hltable
[n
];
539 hltable
[n
]->prev
= new;
546 hltsetdino(struct hlink
*hl
, ino_t inum
)
552 hltdelete(struct hlink
*hl
)
554 assert(hl
->refs
== 1);
558 hl
->next
->prev
= hl
->prev
;
559 hl
->prev
->next
= hl
->next
;
562 hl
->next
->prev
= NULL
;
564 hltable
[hl
->ino
& HLMASK
] = hl
->next
;
571 hltrels(struct hlink
*hl
)
573 assert(hl
->refs
== 1);
578 * If UseHLPath is defined check to see if the file in question is
579 * the same as the source file, and if it is return a pointer to the
580 * -H path based file for hardlinking. Else return NULL.
583 checkHLPath(struct stat
*st1
, const char *spath
, const char *dpath
)
589 asprintf(&hpath
, "%s%s", UseHLPath
, dpath
+ DstBaseLen
);
592 * stat info matches ?
594 if (hc_stat(&DstHost
, hpath
, &sthl
) < 0 ||
595 st1
->st_size
!= sthl
.st_size
||
596 st1
->st_mtime
!= sthl
.st_mtime
||
597 !OwnerMatch(st1
, &sthl
) ||
598 !FlagsMatch(st1
, &sthl
)
605 * If ForceOpt or ValidateOpt is set we have to compare the files
607 if (ForceOpt
|| ValidateOpt
) {
608 error
= validate_check(spath
, hpath
);
618 * Return 0 if the contents of the file <spath> matches the contents of
622 validate_check(const char *spath
, const char *dpath
)
628 fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0);
629 fd2
= hc_open(&DstHost
, dpath
, O_RDONLY
, 0);
632 if (fd1
>= 0 && fd2
>= 0) {
635 char *iobuf1
= malloc(GETIOSIZE
);
636 char *iobuf2
= malloc(GETIOSIZE
);
638 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
639 CountSourceReadBytes
+= n
;
640 x
= hc_read(&DstHost
, fd2
, iobuf2
, GETIOSIZE
);
642 CountTargetReadBytes
+= x
;
645 if (bcmp(iobuf1
, iobuf2
, n
) != 0)
654 hc_close(&SrcHost
, fd1
);
656 hc_close(&DstHost
, fd2
);
661 DoCopy(copy_info_t info
, int depth
)
663 const char *spath
= info
->spath
;
664 const char *dpath
= info
->dpath
;
665 dev_t sdevNo
= info
->sdevNo
;
666 dev_t ddevNo
= info
->ddevNo
;
669 unsigned long st2_flags
;
670 int r
, mres
, fres
, st2Valid
;
674 r
= mres
= fres
= st2Valid
= 0;
679 if (hc_lstat(&SrcHost
, spath
, &st1
) != 0) {
684 /* skip snapshot files because they're sparse and _huge_ */
685 if (st1
.st_flags
& SF_SNAPSHOT
)
688 st2
.st_mode
= 0; /* in case lstat fails */
689 st2
.st_flags
= 0; /* in case lstat fails */
690 if (dpath
&& hc_lstat(&DstHost
, dpath
, &st2
) == 0) {
692 #ifdef _ST_FLAGS_PRESENT_
693 st2_flags
= st2
.st_flags
;
697 if (S_ISREG(st1
.st_mode
)) {
705 if (S_ISREG(st1
.st_mode
) && st1
.st_nlink
> 1 && dpath
) {
706 if ((hln
= hltlookup(&st1
)) != NULL
) {
710 if (st2
.st_ino
== hln
->dino
) {
712 * hard link is already correct, nothing to do
715 logstd("%-32s nochange\n", (dpath
) ? dpath
: spath
);
716 if (hln
->nlinked
== st1
.st_nlink
) {
725 * hard link is not correct, attempt to unlink it
727 if (xremove(&DstHost
, dpath
) < 0) {
728 logerr("%-32s hardlink: unable to unlink: %s\n",
729 ((dpath
) ? dpath
: spath
), strerror(errno
));
738 if (xlink(hln
->name
, dpath
, st1
.st_flags
) < 0) {
739 int tryrelink
= (errno
== EMLINK
);
740 logerr("%-32s hardlink: unable to link to %s: %s\n",
741 (dpath
? dpath
: spath
), hln
->name
, strerror(errno
)
746 logerr("%-20s hardlink: will attempt to copy normally\n");
751 if (hln
->nlinked
== st1
.st_nlink
) {
757 logstd("%-32s hardlink: %s\n",
758 (dpath
? dpath
: spath
),
759 (st2Valid
? "relinked" : "linked")
770 * first instance of hardlink must be copied normally
773 hln
= hltadd(&st1
, dpath
);
778 * Do we need to copy the file/dir/link/whatever? Early termination
779 * if we do not. Always redo links. Directories are always traversed
780 * except when the FSMID options are used.
782 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
787 && st1
.st_mode
== st2
.st_mode
788 && FlagsMatch(&st1
, &st2
)
790 if (S_ISLNK(st1
.st_mode
) || S_ISDIR(st1
.st_mode
)) {
792 * If FSMID tracking is turned on we can avoid recursing through
793 * an entire directory subtree if the FSMID matches.
795 #ifdef _ST_FSMID_PRESENT_
797 (UseFSMIDOpt
&& (fres
= fsmid_check(st1
.st_fsmid
, dpath
)) == 0)
799 if (VerboseOpt
>= 3) {
800 if (UseFSMIDOpt
) /* always true!?! */
801 logstd("%-32s fsmid-nochange\n", (dpath
? dpath
: spath
));
803 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
811 st1
.st_size
== st2
.st_size
&&
812 (ValidateOpt
== 2 || st1
.st_mtime
== st2
.st_mtime
) &&
813 OwnerMatch(&st1
, &st2
)
815 && (UseMD5Opt
== 0 || !S_ISREG(st1
.st_mode
) ||
816 (mres
= md5_check(spath
, dpath
)) == 0)
818 #ifdef _ST_FSMID_PRESENT_
819 && (UseFSMIDOpt
== 0 ||
820 (fres
= fsmid_check(st1
.st_fsmid
, dpath
)) == 0)
822 && (ValidateOpt
== 0 || !S_ISREG(st1
.st_mode
) ||
823 validate_check(spath
, dpath
) == 0)
826 * The files are identical, but if we are running as
827 * root we might need to adjust ownership/group/flags.
830 int changedflags
= 0;
833 hltsetdino(hln
, st2
.st_ino
);
835 if (!OwnerMatch(&st1
, &st2
)) {
836 hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
);
839 #ifdef _ST_FLAGS_PRESENT_
840 if (!FlagsMatch(&st1
, &st2
)) {
841 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
845 if (VerboseOpt
>= 3) {
848 logstd("%-32s md5-nochange",
849 (dpath
? dpath
: spath
));
853 logstd("%-32s fsmid-nochange",
854 (dpath
? dpath
: spath
));
855 } else if (ValidateOpt
) {
856 logstd("%-32s nochange (contents validated)",
857 (dpath
? dpath
: spath
));
859 logstd("%-32s nochange", (dpath
? dpath
: spath
));
862 logstd(" (uid/gid differ)");
864 logstd(" (flags differ)");
867 CountSourceBytes
+= size
;
874 if (st2Valid
&& !S_ISDIR(st1
.st_mode
) && S_ISDIR(st2
.st_mode
)) {
876 logerr("%-32s SAFETY - refusing to copy file over directory\n",
877 (dpath
? dpath
: spath
)
881 goto done
; /* continue with the cpdup anyway */
883 if (QuietOpt
== 0 || AskConfirmation
) {
884 logstd("%-32s WARNING: non-directory source will blow away\n"
885 "%-32s preexisting dest directory, continuing anyway!\n",
886 ((dpath
) ? dpath
: spath
), "");
889 RemoveRecur(dpath
, ddevNo
);
894 * The various comparisons failed, copy it.
896 if (S_ISDIR(st1
.st_mode
)) {
900 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
903 if (!st2Valid
|| S_ISDIR(st2
.st_mode
) == 0) {
905 xremove(&DstHost
, dpath
);
906 if (hc_mkdir(&DstHost
, dpath
, st1
.st_mode
| 0700) != 0) {
907 logerr("%s: mkdir failed: %s\n",
908 (dpath
? dpath
: spath
), strerror(errno
));
912 if (hc_lstat(&DstHost
, dpath
, &st2
) != 0) {
913 logerr("%s: lstat of newly made dir failed: %s\n",
914 (dpath
? dpath
: spath
), strerror(errno
));
921 if (!OwnerMatch(&st1
, &st2
) &&
922 hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
) != 0
924 logerr("%s: chown of newly made dir failed: %s\n",
925 (dpath
? dpath
: spath
), strerror(errno
));
927 /* Note that we should not set skipdir = 1 here. */
933 * Directory must be scanable by root for cpdup to
934 * work. We'll fix it later if the directory isn't
935 * supposed to be readable ( which is why we fixup
936 * st2.st_mode to match what we did ).
938 if ((st2
.st_mode
& 0700) != 0700) {
939 hc_chmod(&DstHost
, dpath
, st2
.st_mode
| 0700);
943 logstd("%s\n", dpath
? dpath
: spath
);
948 * When copying a directory, stop if the source crosses a mount
951 if (sdevNo
!= (dev_t
)-1 && st1
.st_dev
!= sdevNo
)
957 * When copying a directory, stop if the destination crosses
960 * The target directory will have been created and stat'd
961 * for st2 if it did not previously exist. st2Valid is left
962 * as a flag. If the stat failed st2 will still only have its
963 * default initialization.
965 * So we simply assume here that the directory is within the
966 * current target mount if we had to create it (aka st2Valid is 0)
967 * and we leave ddevNo alone.
970 if (ddevNo
!= (dev_t
)-1 && st2
.st_dev
!= ddevNo
)
977 List
*list
= malloc(sizeof(List
));
982 logstd("Scanning %s ...\n", spath
);
984 if (ScanDir(list
, &SrcHost
, spath
, &CountSourceReadBytes
, 0) == 0) {
985 while ((name
= IterateList(list
, &node
, 0)) != NULL
) {
989 nspath
= mprintf("%s/%s", spath
, name
);
991 ndpath
= mprintf("%s/%s", dpath
, name
);
993 info
->spath
= nspath
;
994 info
->dpath
= ndpath
;
995 info
->sdevNo
= sdevNo
;
996 info
->ddevNo
= ddevNo
;
998 r
+= DoCopy(info
, depth
);
1000 r
+= DoCopy(info
, depth
+ 1);
1009 * Remove files/directories from destination that do not appear
1012 if (dpath
&& ScanDir(list
, &DstHost
, dpath
,
1013 &CountTargetReadBytes
, 3) == 0) {
1014 while ((name
= IterateList(list
, &node
, 3)) != NULL
) {
1016 * If object does not exist in source or .cpignore
1017 * then recursively remove it.
1021 ndpath
= mprintf("%s/%s", dpath
, name
);
1022 RemoveRecur(ndpath
, ddevNo
);
1031 if (dpath
&& st2Valid
) {
1032 struct timeval tv
[2];
1034 if (ForceOpt
|| !OwnerMatch(&st1
, &st2
))
1035 hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
);
1036 if (st1
.st_mode
!= st2
.st_mode
)
1037 hc_chmod(&DstHost
, dpath
, st1
.st_mode
);
1038 #ifdef _ST_FLAGS_PRESENT_
1039 if (!FlagsMatch(&st1
, &st2
))
1040 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
1042 if (ForceOpt
|| st1
.st_mtime
!= st2
.st_mtime
) {
1043 bzero(tv
, sizeof(tv
));
1044 tv
[0].tv_sec
= st1
.st_mtime
;
1045 tv
[1].tv_sec
= st1
.st_mtime
;
1046 hc_utimes(&DstHost
, dpath
, tv
);
1049 } else if (dpath
== NULL
) {
1051 * If dpath is NULL, we are just updating the MD5
1054 if (UseMD5Opt
&& S_ISREG(st1
.st_mode
)) {
1055 mres
= md5_check(spath
, NULL
);
1057 if (VerboseOpt
> 1) {
1059 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1061 logstd("%-32s md5-ok\n", (dpath
) ? dpath
: spath
);
1062 } else if (!QuietOpt
&& mres
< 0) {
1063 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1067 } else if (S_ISREG(st1
.st_mode
)) {
1074 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1076 path
= mprintf("%s", dpath
);
1079 * Handle check failure message.
1083 logerr("%-32s md5-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1087 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1090 * Not quite ready to do the copy yet. If UseHLPath is defined,
1091 * see if we can hardlink instead.
1093 * If we can hardlink, and the target exists, we have to remove it
1094 * first or the hardlink will fail. This can occur in a number of
1095 * situations but most typically when the '-f -H' combination is
1098 if (UseHLPath
&& (hpath
= checkHLPath(&st1
, spath
, dpath
)) != NULL
) {
1100 xremove(&DstHost
, dpath
);
1101 if (hc_link(&DstHost
, hpath
, dpath
) == 0) {
1104 logstd("%-32s hardlinked(-H)\n",
1105 (dpath
? dpath
: spath
));
1111 * Shucks, we may have hit a filesystem hard linking limit,
1112 * we have to copy instead.
1117 if ((fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0)) >= 0) {
1118 if ((fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600)) < 0) {
1120 * There could be a .tmp file from a previously interrupted
1121 * run, delete and retry. Fail if we still can't get at it.
1123 #ifdef _ST_FLAGS_PRESENT_
1124 hc_chflags(&DstHost
, path
, 0);
1126 hc_remove(&DstHost
, path
);
1127 fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_TRUNC
, 0600);
1131 char *iobuf1
= malloc(GETIOSIZE
);
1135 * Matt: What about holes?
1138 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
1140 if (hc_write(&DstHost
, fd2
, iobuf1
, n
) != n
)
1144 hc_close(&DstHost
, fd2
);
1146 struct timeval tv
[2];
1148 bzero(tv
, sizeof(tv
));
1149 tv
[0].tv_sec
= st1
.st_mtime
;
1150 tv
[1].tv_sec
= st1
.st_mtime
;
1152 if (DstRootPrivs
|| ChgrpAllowed(st1
.st_gid
))
1153 hc_chown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1154 hc_chmod(&DstHost
, path
, st1
.st_mode
);
1155 #ifdef _ST_FLAGS_PRESENT_
1156 if (st1
.st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
1157 hc_utimes(&DstHost
, path
, tv
);
1159 hc_utimes(&DstHost
, path
, tv
);
1161 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1162 logerr("%-32s rename-after-copy failed: %s\n",
1163 (dpath
? dpath
: spath
), strerror(errno
)
1168 logstd("%-32s copy-ok\n", (dpath
? dpath
: spath
));
1169 #ifdef _ST_FLAGS_PRESENT_
1170 if (DstRootPrivs
? st1
.st_flags
: st1
.st_flags
& UF_SETTABLE
)
1171 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
1174 #ifdef _ST_FLAGS_PRESENT_
1175 if ((st1
.st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
)) == 0)
1176 hc_utimes(&DstHost
, dpath
, tv
);
1178 CountSourceReadBytes
+= size
;
1179 CountWriteBytes
+= size
;
1180 CountSourceBytes
+= size
;
1184 logerr("%-32s %s failed: %s\n",
1185 (dpath
? dpath
: spath
), op
, strerror(errno
)
1187 hc_remove(&DstHost
, path
);
1192 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1193 (dpath
? dpath
: spath
), getuid(), geteuid(),
1198 hc_close(&SrcHost
, fd1
);
1200 logerr("%-32s copy: open failed: %s\n",
1201 (dpath
? dpath
: spath
),
1210 if (!r
&& hc_stat(&DstHost
, dpath
, &st2
) == 0) {
1211 hltsetdino(hln
, st2
.st_ino
);
1217 } else if (S_ISLNK(st1
.st_mode
)) {
1218 char *link1
= malloc(GETLINKSIZE
);
1219 char *link2
= malloc(GETLINKSIZE
);
1224 n1
= hc_readlink(&SrcHost
, spath
, link1
, GETLINKSIZE
- 1);
1226 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1227 n2
= hc_readlink(&DstHost
, dpath
, link2
, GETLINKSIZE
- 1);
1229 path
= mprintf("%s", dpath
);
1233 if (ForceOpt
|| n1
!= n2
|| bcmp(link1
, link2
, n1
) != 0) {
1234 hc_umask(&DstHost
, ~st1
.st_mode
);
1235 xremove(&DstHost
, path
);
1237 if (hc_symlink(&DstHost
, link1
, path
) < 0) {
1238 logerr("%-32s symlink (%s->%s) failed: %s\n",
1239 (dpath
? dpath
: spath
), link1
, path
,
1244 if (DstRootPrivs
|| ChgrpAllowed(st1
.st_gid
))
1245 hc_lchown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1247 * there is no lchmod() or lchflags(), we
1248 * cannot chmod or chflags a softlink.
1250 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1251 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1252 (dpath
? dpath
: spath
),
1253 path
, dpath
, strerror(errno
));
1254 } else if (VerboseOpt
) {
1255 logstd("%-32s softlink-ok\n", (dpath
? dpath
: spath
));
1257 hc_umask(&DstHost
, 000);
1258 CountWriteBytes
+= n1
;
1262 if (VerboseOpt
>= 3)
1263 logstd("%-32s nochange", (dpath
? dpath
: spath
));
1264 if (!OwnerMatch(&st1
, &st2
)) {
1265 hc_lchown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
);
1266 if (VerboseOpt
>= 3)
1267 logstd(" (uid/gid differ)");
1269 if (VerboseOpt
>= 3)
1272 CountSourceBytes
+= n1
;
1273 CountSourceReadBytes
+= n1
;
1275 CountTargetReadBytes
+= n2
;
1279 logerr("%-32s softlink-failed\n", (dpath
? dpath
: spath
));
1284 } else if ((S_ISCHR(st1
.st_mode
) || S_ISBLK(st1
.st_mode
)) && DeviceOpt
) {
1289 st1
.st_mode
!= st2
.st_mode
||
1290 st1
.st_rdev
!= st2
.st_rdev
||
1291 !OwnerMatch(&st1
, &st2
)
1294 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1295 xremove(&DstHost
, path
);
1297 path
= mprintf("%s", dpath
);
1300 if (hc_mknod(&DstHost
, path
, st1
.st_mode
, st1
.st_rdev
) == 0) {
1301 hc_chmod(&DstHost
, path
, st1
.st_mode
);
1302 hc_chown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1304 xremove(&DstHost
, dpath
);
1305 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1306 logerr("%-32s dev-rename-after-create failed: %s\n",
1307 (dpath
? dpath
: spath
),
1310 } else if (VerboseOpt
) {
1311 logstd("%-32s dev-ok\n", (dpath
? dpath
: spath
));
1316 logerr("%-32s dev failed: %s\n",
1317 (dpath
? dpath
: spath
), strerror(errno
)
1321 if (VerboseOpt
>= 3)
1322 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1330 if (hln
->dino
== (ino_t
)-1) {
1332 /*hln = NULL; unneeded */
1341 ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
1342 int64_t *CountReadBytes
, int n
)
1347 if ((dir
= hc_opendir(host
, path
)) == NULL
)
1352 * scan .cpignore file for files/directories to ignore
1353 * (only in the source directory, i.e. if n == 0).
1359 char *buf
= malloc(GETBUFSIZE
);
1363 if (UseCpFile
[0] == '/') {
1364 fpath
= mprintf("%s", UseCpFile
);
1366 fpath
= mprintf("%s/%s", path
, UseCpFile
);
1368 AddList(list
, strrchr(fpath
, '/') + 1, 1);
1369 if ((fd
= hc_open(host
, fpath
, O_RDONLY
, 0)) >= 0) {
1371 while ((nread
= hc_read(host
, fd
, buf
+ bufused
,
1372 GETIOSIZE
- bufused
- 1)) > 0) {
1373 *CountReadBytes
+= nread
;
1376 for (next
= buf
; (nl
= strchr(next
, '\n')); next
= nl
+1) {
1378 AddList(list
, next
, 1);
1380 bufused
= strlen(next
);
1382 bcopy(next
, buf
, bufused
);
1385 /* last line has no trailing newline */
1387 AddList(list
, buf
, 1);
1396 * Automatically exclude MD5CacheFile that we create on the
1397 * source from the copy to the destination.
1399 * Automatically exclude a FSMIDCacheFile on the source that
1400 * would otherwise overwrite the one we maintain on the target.
1403 AddList(list
, MD5CacheFile
, 1);
1405 AddList(list
, FSMIDCacheFile
, 1);
1408 while ((den
= hc_readdir(host
, dir
)) != NULL
) {
1412 if (strcmp(den
->d_name
, ".") != 0 && strcmp(den
->d_name
, "..") != 0)
1413 AddList(list
, den
->d_name
, n
);
1415 hc_closedir(host
, dir
);
1425 RemoveRecur(const char *dpath
, dev_t devNo
)
1429 if (hc_lstat(&DstHost
, dpath
, &st
) == 0) {
1430 if (devNo
== (dev_t
)-1)
1432 if (st
.st_dev
== devNo
) {
1433 if (S_ISDIR(st
.st_mode
)) {
1436 if ((dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1438 while ((den
= hc_readdir(&DstHost
, dir
)) != NULL
) {
1441 if (strcmp(den
->d_name
, ".") == 0)
1443 if (strcmp(den
->d_name
, "..") == 0)
1445 ndpath
= mprintf("%s/%s", dpath
, den
->d_name
);
1446 RemoveRecur(ndpath
, devNo
);
1449 hc_closedir(&DstHost
, dir
);
1451 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1453 if (hc_rmdir(&DstHost
, dpath
) < 0) {
1454 logerr("%-32s rmdir failed: %s\n",
1455 dpath
, strerror(errno
)
1458 CountRemovedItems
++;
1463 logstd("%-32s not-removed\n", dpath
);
1464 } else if (hc_rmdir(&DstHost
, dpath
) == 0) {
1466 logstd("%-32s rmdir-ok\n", dpath
);
1467 CountRemovedItems
++;
1469 logerr("%-32s rmdir failed: %s\n",
1470 dpath
, strerror(errno
)
1475 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1477 if (xremove(&DstHost
, dpath
) < 0) {
1478 logerr("%-32s remove failed: %s\n",
1479 dpath
, strerror(errno
)
1482 CountRemovedItems
++;
1487 logstd("%-32s not-removed\n", dpath
);
1488 } else if (xremove(&DstHost
, dpath
) == 0) {
1490 logstd("%-32s remove-ok\n", dpath
);
1491 CountRemovedItems
++;
1493 logerr("%-32s remove failed: %s\n",
1494 dpath
, strerror(errno
)
1504 InitList(List
*list
)
1506 bzero(list
, sizeof(List
));
1507 list
->li_Node
.no_Next
= &list
->li_Node
;
1511 ResetList(List
*list
)
1515 while ((node
= list
->li_Node
.no_Next
) != &list
->li_Node
) {
1516 list
->li_Node
.no_Next
= node
->no_Next
;
1523 IterateList(List
*list
, Node
**nodeptr
, int n
)
1525 Node
*node
= *nodeptr
;
1528 node
= list
->li_Node
.no_Next
;
1529 while (node
!= &list
->li_Node
) {
1530 if (node
->no_Value
== n
) {
1531 *nodeptr
= node
->no_Next
;
1532 return (node
->no_Name
);
1534 node
= node
->no_Next
;
1540 AddList(List
*list
, const char *name
, int n
)
1548 * Scan against wildcards. Only a node value of 1 can be a wildcard
1549 * ( usually scanned from .cpignore )
1552 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1553 if (strcmp(name
, node
->no_Name
) == 0 ||
1554 (n
!= 1 && node
->no_Value
== 1 &&
1555 fnmatch(node
->no_Name
, name
, 0) == 0)
1557 return(node
->no_Value
);
1562 * Look for exact match
1565 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1566 if (strcmp(name
, node
->no_Name
) == 0) {
1567 return(node
->no_Value
);
1570 node
= malloc(sizeof(Node
) + strlen(name
) + 1);
1572 fprintf(stderr
, "out of memory\n");
1576 node
->no_Next
= list
->li_Node
.no_Next
;
1577 list
->li_Node
.no_Next
= node
;
1579 node
->no_HNext
= list
->li_Hash
[hv
];
1580 list
->li_Hash
[hv
] = node
;
1582 strcpy(node
->no_Name
, name
);
1589 shash(const char *s
)
1596 if (*s
== '*' || *s
== '?' ||
1597 *s
== '{' || *s
== '}' ||
1598 *s
== '[' || *s
== ']' ||
1603 hv
= (hv
<< 5) ^ *s
^ (hv
>> 23);
1606 return(((hv
>> 16) ^ hv
) & HMASK
);
1610 YesNo(const char *path
)
1614 fprintf(stderr
, "remove %s (Yes/No) [No]? ", path
);
1617 first
= ch
= getchar();
1618 while (ch
!= '\n' && ch
!= EOF
)
1620 return ((first
== 'y' || first
== 'Y'));
1624 * xrename() - rename with override
1626 * If the rename fails, attempt to override st_flags on the
1627 * destination and rename again. If that fails too, try to
1628 * set the flags back the way they were and give up.
1632 xrename(const char *src
, const char *dst
, u_long flags
)
1636 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1637 #ifdef _ST_FLAGS_PRESENT_
1638 hc_chflags(&DstHost
, dst
, 0);
1639 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0)
1640 hc_chflags(&DstHost
, dst
, flags
);
1647 xlink(const char *src
, const char *dst
, u_long flags
)
1650 #ifdef _ST_FLAGS_PRESENT_
1654 if ((r
= hc_link(&DstHost
, src
, dst
)) < 0) {
1655 #ifdef _ST_FLAGS_PRESENT_
1656 hc_chflags(&DstHost
, src
, 0);
1657 r
= hc_link(&DstHost
, src
, dst
);
1659 hc_chflags(&DstHost
, src
, flags
);
1669 xremove(struct HostConf
*host
, const char *path
)
1673 res
= hc_remove(host
, path
);
1674 #ifdef _ST_FLAGS_PRESENT_
1675 if (res
== -EPERM
) {
1676 hc_chflags(host
, path
, 0);
1677 res
= hc_remove(host
, path
);