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 xrmdir(struct HostConf
*host
, const char *path
);
134 static int DoCopy(copy_info_t info
, struct stat
*stat1
, int depth
);
135 static int ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
136 int64_t *CountReadBytes
, int n
);
138 int AskConfirmation
= 1;
155 const char *ssh_argv
[16];
158 const char *UseCpFile
;
159 const char *MD5CacheFile
;
160 const char *FSMIDCacheFile
;
161 const char *UseHLPath
;
163 static int DstBaseLen
;
164 static int HardLinkCount
;
165 static int GroupCount
;
166 static gid_t
*GroupList
;
168 int64_t CountSourceBytes
;
169 int64_t CountSourceItems
;
170 int64_t CountCopiedItems
;
171 int64_t CountSourceReadBytes
;
172 int64_t CountTargetReadBytes
;
173 int64_t CountWriteBytes
;
174 int64_t CountRemovedItems
;
175 int64_t CountLinkedItems
;
177 static struct HostConf SrcHost
;
178 static struct HostConf DstHost
;
181 main(int ac
, char **av
)
188 struct timeval start
;
189 struct copy_info info
;
191 signal(SIGPIPE
, SIG_IGN
);
193 gettimeofday(&start
, NULL
);
195 while ((opt
= getopt(ac
, av
, ":CdnF:fH:Ii:j:K:klM:mopqRSs:uVvX:x")) != -1) {
197 /* TODO: sort the branches */
224 UseCpFile
= ".cpignore";
234 fatal("too many -F options");
235 ssh_argv
[ssh_argc
++] = optarg
;
247 AskConfirmation
= getbool(optarg
);
250 DeviceOpt
= getbool(optarg
);
253 SafetyOpt
= getbool(optarg
);
260 FSMIDCacheFile
= ".FSMID.CHECK";
264 FSMIDCacheFile
= optarg
;
268 MD5CacheFile
= optarg
;
272 MD5CacheFile
= ".MD5.CHECKSUMS";
275 setvbuf(stdout
, NULL
, _IOLBF
, 0);
278 fatal("missing argument for option: -%c\n", optopt
);
282 fatal("illegal option: -%c\n", optopt
);
298 fatal("too many arguments");
301 * If we are told to go into slave mode, run the HC protocol
304 DstRootPrivs
= (geteuid() == 0);
310 * Extract the source and/or/neither target [user@]host and
311 * make any required connections.
313 if (src
&& (ptr
= SplitRemote(&src
)) != NULL
) {
317 fatal("The MD5 options are not currently supported for remote sources");
318 if (hc_connect(&SrcHost
, ReadOnlyOpt
) < 0)
320 } else if (ReadOnlyOpt
)
321 fatal("The -R option is only supported for remote sources");
323 if (dst
&& (ptr
= SplitRemote(&dst
)) != NULL
) {
327 fatal("The FSMID options are not currently supported for remote targets");
328 if (hc_connect(&DstHost
, 0) < 0)
333 * dst may be NULL only if -m option is specified,
334 * which forces an update of the MD5 checksums
336 if (dst
== NULL
&& UseMD5Opt
== 0) {
342 DstRootPrivs
= (hc_geteuid(&DstHost
) == 0);
344 GroupCount
= hc_getgroups(&DstHost
, &GroupList
);
348 fprintf(stderr
, "DstRootPrivs == %s\n", DstRootPrivs
? "true" : "false");
349 fprintf(stderr
, "GroupCount == %d\n", GroupCount
);
350 for (i
= 0; i
< GroupCount
; i
++)
351 fprintf(stderr
, "Group[%d] == %d\n", i
, GroupList
[i
]);
354 bzero(&info
, sizeof(info
));
356 DstBaseLen
= strlen(dst
);
359 info
.sdevNo
= (dev_t
)-1;
360 info
.ddevNo
= (dev_t
)-1;
361 i
= DoCopy(&info
, NULL
, -1);
365 info
.sdevNo
= (dev_t
)-1;
366 info
.ddevNo
= (dev_t
)-1;
367 i
= DoCopy(&info
, NULL
, -1);
374 if (SummaryOpt
&& i
== 0) {
378 gettimeofday(&end
, NULL
);
380 /* don't count stat's in our byte statistics */
381 CountSourceBytes
+= sizeof(struct stat
) * CountSourceItems
;
382 CountSourceReadBytes
+= sizeof(struct stat
) * CountSourceItems
;
383 CountWriteBytes
+= sizeof(struct stat
) * CountCopiedItems
;
384 CountWriteBytes
+= sizeof(struct stat
) * CountRemovedItems
;
387 duration
= (end
.tv_sec
- start
.tv_sec
);
388 duration
+= (double)(end
.tv_usec
- start
.tv_usec
) / 1000000.0;
391 logstd("cpdup completed successfully\n");
392 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
393 "%lld bytes written (%.1fX speedup)\n",
394 (long long)CountSourceBytes
,
395 (long long)CountSourceReadBytes
,
396 (long long)CountTargetReadBytes
,
397 (long long)CountWriteBytes
,
398 ((double)CountSourceBytes
* 2.0) / ((double)(CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
)));
399 logstd("%lld source items, %lld items copied, %lld items linked, "
400 "%lld things deleted\n",
401 (long long)CountSourceItems
,
402 (long long)CountCopiedItems
,
403 (long long)CountLinkedItems
,
404 (long long)CountRemovedItems
);
405 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
407 (int)((CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
) / duration
/ 1024.0),
408 (int)(CountSourceBytes
/ duration
/ 1024.0));
410 exit((i
== 0) ? 0 : 1);
414 getbool(const char *str
)
416 if (strcmp(str
, "0") == 0)
418 if (strcmp(str
, "1") == 0)
420 fatal("option requires boolean argument (0 or 1): -%c\n", optopt
);
426 * Check if path specifies a remote path, using the same syntax as scp(1),
427 * i.e. a path is considered remote if the first colon is not preceded by
428 * a slash, so e.g. "./foo:bar" is considered local.
429 * If a remote path is detected, the colon is replaced with a null byte,
430 * and the return value is a pointer to the next character.
431 * Otherwise NULL is returned.
433 * A path prefix of localhost is the same as a locally specified file or
434 * directory path, but prevents any further interpretation of the path
435 * as being a remote hostname (for paths that have colons in them).
438 SplitRemote(char **pathp
)
443 if (path
[(cindex
= strcspn(path
, ":/"))] == ':') {
445 if (strcmp(path
, "localhost") != 0)
446 return (path
+ cindex
);
447 *pathp
= path
+ cindex
;
453 * Check if group g is in our GroupList.
455 * Typically the number of groups a user belongs to isn't large
456 * enough to warrant more effort than a simple linear search.
457 * However, we perform an optimization by moving a group to the
458 * top of the list when we have a hit. This assumes that there
459 * isn't much variance in the gids of files that a non-root user
460 * copies. So most of the time the search will terminate on the
461 * first element of the list.
464 ChgrpAllowed(gid_t g
)
468 for (i
= 0; i
< GroupCount
; i
++)
469 if (GroupList
[i
] == g
) {
471 /* Optimize: Move g to the front of the list. */
473 GroupList
[i
] = GroupList
[i
- 1];
482 * The following two functions return true if the ownership (UID + GID)
483 * or the flags of two files match, respectively.
485 * Only perform weak checking if we don't have sufficient privileges on
486 * the target machine, so we don't waste transfers with things that are
487 * bound to fail anyway.
490 OwnerMatch(struct stat
*st1
, struct stat
*st2
)
493 /* Both UID and GID must match. */
494 return (st1
->st_uid
== st2
->st_uid
&& st1
->st_gid
== st2
->st_gid
);
496 /* Ignore UID, and also ignore GID if we can't chgrp to that group. */
497 return (st1
->st_gid
== st2
->st_gid
|| !ChgrpAllowed(st1
->st_gid
));
500 #ifdef _ST_FLAGS_PRESENT_
502 FlagsMatch(struct stat
*st1
, struct stat
*st2
)
505 * Ignore UF_ARCHIVE. It gets set automatically by the filesystem, for
506 * filesystems that support it. If the destination filesystem supports it, but
507 * it's cleared on the source file, then multiple invocations of cpdup would
508 * all try to copy the file because the flags wouldn't match.
510 * When unpriveleged, ignore flags we can't set
512 u_long ignored
= DstRootPrivs
? 0 : SF_SETTABLE
;
515 ignored
|= UF_ARCHIVE
;
517 return (((st1
->st_flags
^ st2
->st_flags
) & ~ignored
) == 0);
522 static struct hlink
*
523 hltlookup(struct stat
*stp
)
528 n
= stp
->st_ino
& HLMASK
;
530 for (hl
= hltable
[n
]; hl
; hl
= hl
->next
) {
531 if (hl
->ino
== stp
->st_ino
) {
540 static struct hlink
*
541 hltadd(struct stat
*stp
, const char *path
)
544 int plen
= strlen(path
);
547 new = malloc(offsetof(struct hlink
, name
[plen
+ 1]));
549 fatal("out of memory");
552 /* initialize and link the new element into the table */
553 new->ino
= stp
->st_ino
;
554 new->dino
= (ino_t
)-1;
556 bcopy(path
, new->name
, plen
+ 1);
559 n
= stp
->st_ino
& HLMASK
;
560 new->next
= hltable
[n
];
562 hltable
[n
]->prev
= new;
569 hltsetdino(struct hlink
*hl
, ino_t inum
)
575 hltdelete(struct hlink
*hl
)
577 assert(hl
->refs
== 1);
581 hl
->next
->prev
= hl
->prev
;
582 hl
->prev
->next
= hl
->next
;
585 hl
->next
->prev
= NULL
;
587 hltable
[hl
->ino
& HLMASK
] = hl
->next
;
594 hltrels(struct hlink
*hl
)
596 assert(hl
->refs
== 1);
601 * If UseHLPath is defined check to see if the file in question is
602 * the same as the source file, and if it is return a pointer to the
603 * -H path based file for hardlinking. Else return NULL.
606 checkHLPath(struct stat
*st1
, const char *spath
, const char *dpath
)
612 asprintf(&hpath
, "%s%s", UseHLPath
, dpath
+ DstBaseLen
);
615 * stat info matches ?
617 if (hc_stat(&DstHost
, hpath
, &sthl
) < 0 ||
618 st1
->st_size
!= sthl
.st_size
||
619 st1
->st_mtime
!= sthl
.st_mtime
||
620 !OwnerMatch(st1
, &sthl
) ||
621 !FlagsMatch(st1
, &sthl
)
628 * If ForceOpt or ValidateOpt is set we have to compare the files
630 if (ForceOpt
|| ValidateOpt
) {
631 error
= validate_check(spath
, hpath
);
641 * Return 0 if the contents of the file <spath> matches the contents of
645 validate_check(const char *spath
, const char *dpath
)
651 fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0);
652 fd2
= hc_open(&DstHost
, dpath
, O_RDONLY
, 0);
655 if (fd1
>= 0 && fd2
>= 0) {
658 char *iobuf1
= malloc(GETIOSIZE
);
659 char *iobuf2
= malloc(GETIOSIZE
);
661 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
662 CountSourceReadBytes
+= n
;
663 x
= hc_read(&DstHost
, fd2
, iobuf2
, GETIOSIZE
);
665 CountTargetReadBytes
+= x
;
668 if (bcmp(iobuf1
, iobuf2
, n
) != 0)
677 hc_close(&SrcHost
, fd1
);
679 hc_close(&DstHost
, fd2
);
684 DoCopy(copy_info_t info
, struct stat
*stat1
, int depth
)
686 const char *spath
= info
->spath
;
687 const char *dpath
= info
->dpath
;
688 dev_t sdevNo
= info
->sdevNo
;
689 dev_t ddevNo
= info
->ddevNo
;
692 unsigned long st2_flags
;
693 int r
, mres
, fres
, st2Valid
;
697 r
= mres
= fres
= st2Valid
= 0;
703 if (hc_lstat(&SrcHost
, spath
, &st1
) != 0) {
710 /* skip snapshot files because they're sparse and _huge_ */
711 if (stat1
->st_flags
& SF_SNAPSHOT
)
714 st2
.st_mode
= 0; /* in case lstat fails */
715 st2
.st_flags
= 0; /* in case lstat fails */
716 if (dpath
&& hc_lstat(&DstHost
, dpath
, &st2
) == 0) {
718 #ifdef _ST_FLAGS_PRESENT_
719 st2_flags
= st2
.st_flags
;
723 if (S_ISREG(stat1
->st_mode
))
724 size
= stat1
->st_size
;
730 if (S_ISREG(stat1
->st_mode
) && stat1
->st_nlink
> 1 && dpath
) {
731 if ((hln
= hltlookup(stat1
)) != NULL
) {
735 if (st2
.st_ino
== hln
->dino
) {
737 * hard link is already correct, nothing to do
740 logstd("%-32s nochange\n", (dpath
) ? dpath
: spath
);
741 if (hln
->nlinked
== stat1
->st_nlink
) {
750 * hard link is not correct, attempt to unlink it
752 if (xremove(&DstHost
, dpath
) < 0) {
753 logerr("%-32s hardlink: unable to unlink: %s\n",
754 ((dpath
) ? dpath
: spath
), strerror(errno
));
763 if (xlink(hln
->name
, dpath
, stat1
->st_flags
) < 0) {
764 int tryrelink
= (errno
== EMLINK
);
765 logerr("%-32s hardlink: unable to link to %s: %s\n",
766 (dpath
? dpath
: spath
), hln
->name
, strerror(errno
)
771 logerr("%-20s hardlink: will attempt to copy normally\n",
772 (dpath
? dpath
: spath
));
777 if (hln
->nlinked
== stat1
->st_nlink
) {
783 logstd("%-32s hardlink: %s\n",
784 (dpath
? dpath
: spath
),
785 (st2Valid
? "relinked" : "linked")
796 * first instance of hardlink must be copied normally
799 hln
= hltadd(stat1
, dpath
);
804 * Do we need to copy the file/dir/link/whatever? Early termination
805 * if we do not. Always redo links. Directories are always traversed
806 * except when the FSMID options are used.
808 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
813 && stat1
->st_mode
== st2
.st_mode
814 && FlagsMatch(stat1
, &st2
)
816 if (S_ISLNK(stat1
->st_mode
) || S_ISDIR(stat1
->st_mode
)) {
818 * If FSMID tracking is turned on we can avoid recursing through
819 * an entire directory subtree if the FSMID matches.
821 #ifdef _ST_FSMID_PRESENT_
823 (UseFSMIDOpt
&& (fres
= fsmid_check(stat1
->st_fsmid
, dpath
)) == 0)
825 if (VerboseOpt
>= 3) {
826 if (UseFSMIDOpt
) /* always true!?! */
827 logstd("%-32s fsmid-nochange\n", (dpath
? dpath
: spath
));
829 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
837 stat1
->st_size
== st2
.st_size
&&
838 (ValidateOpt
== 2 || stat1
->st_mtime
== st2
.st_mtime
) &&
839 OwnerMatch(stat1
, &st2
)
841 && (UseMD5Opt
== 0 || !S_ISREG(stat1
->st_mode
) ||
842 (mres
= md5_check(spath
, dpath
)) == 0)
844 #ifdef _ST_FSMID_PRESENT_
845 && (UseFSMIDOpt
== 0 ||
846 (fres
= fsmid_check(stat1
->st_fsmid
, dpath
)) == 0)
848 && (ValidateOpt
== 0 || !S_ISREG(stat1
->st_mode
) ||
849 validate_check(spath
, dpath
) == 0)
852 * The files are identical, but if we are running as
853 * root we might need to adjust ownership/group/flags.
856 int changedflags
= 0;
859 hltsetdino(hln
, st2
.st_ino
);
861 if (!OwnerMatch(stat1
, &st2
)) {
862 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
865 #ifdef _ST_FLAGS_PRESENT_
866 if (!FlagsMatch(stat1
, &st2
)) {
867 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
871 if (VerboseOpt
>= 3) {
874 logstd("%-32s md5-nochange",
875 (dpath
? dpath
: spath
));
879 logstd("%-32s fsmid-nochange",
880 (dpath
? dpath
: spath
));
881 } else if (ValidateOpt
) {
882 logstd("%-32s nochange (contents validated)",
883 (dpath
? dpath
: spath
));
885 logstd("%-32s nochange", (dpath
? dpath
: spath
));
888 logstd(" (uid/gid differ)");
890 logstd(" (flags differ)");
893 CountSourceBytes
+= size
;
900 if (st2Valid
&& !S_ISDIR(stat1
->st_mode
) && S_ISDIR(st2
.st_mode
)) {
902 logerr("%-32s SAFETY - refusing to copy file over directory\n",
903 (dpath
? dpath
: spath
)
907 goto done
; /* continue with the cpdup anyway */
909 if (QuietOpt
== 0 || AskConfirmation
) {
910 logstd("%-32s WARNING: non-directory source will blow away\n"
911 "%-32s preexisting dest directory, continuing anyway!\n",
912 ((dpath
) ? dpath
: spath
), "");
915 RemoveRecur(dpath
, ddevNo
, &st2
);
920 * The various comparisons failed, copy it.
922 if (S_ISDIR(stat1
->st_mode
)) {
926 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
929 if (!st2Valid
|| S_ISDIR(st2
.st_mode
) == 0) {
931 xremove(&DstHost
, dpath
);
932 if (hc_mkdir(&DstHost
, dpath
, stat1
->st_mode
| 0700) != 0) {
933 logerr("%s: mkdir failed: %s\n",
934 (dpath
? dpath
: spath
), strerror(errno
));
938 if (hc_lstat(&DstHost
, dpath
, &st2
) != 0) {
939 if (NotForRealOpt
== 0)
940 logerr("%s: lstat of newly made dir failed: %s\n",
941 (dpath
? dpath
: spath
), strerror(errno
));
948 if (!OwnerMatch(stat1
, &st2
) &&
949 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
) != 0
951 logerr("%s: chown of newly made dir failed: %s\n",
952 (dpath
? dpath
: spath
), strerror(errno
));
954 /* Note that we should not set skipdir = 1 here. */
958 logstd("%-32s mkdir-ok\n", (dpath
? dpath
: spath
));
962 * Directory must be scanable by root for cpdup to
963 * work. We'll fix it later if the directory isn't
964 * supposed to be readable ( which is why we fixup
965 * st2.st_mode to match what we did ).
967 if ((st2
.st_mode
& 0700) != 0700) {
968 hc_chmod(&DstHost
, dpath
, st2
.st_mode
| 0700);
972 logstd("%s\n", dpath
? dpath
: spath
);
977 * When copying a directory, stop if the source crosses a mount
980 if (sdevNo
!= (dev_t
)-1 && stat1
->st_dev
!= sdevNo
)
983 sdevNo
= stat1
->st_dev
;
986 * When copying a directory, stop if the destination crosses
989 * The target directory will have been created and stat'd
990 * for st2 if it did not previously exist. st2Valid is left
991 * as a flag. If the stat failed st2 will still only have its
992 * default initialization.
994 * So we simply assume here that the directory is within the
995 * current target mount if we had to create it (aka st2Valid is 0)
996 * and we leave ddevNo alone.
999 if (ddevNo
!= (dev_t
)-1 && st2
.st_dev
!= ddevNo
)
1002 ddevNo
= st2
.st_dev
;
1006 List
*list
= malloc(sizeof(List
));
1010 logstd("Scanning %s ...\n", spath
);
1012 if (ScanDir(list
, &SrcHost
, spath
, &CountSourceReadBytes
, 0) == 0) {
1014 while ((node
= IterateList(list
, node
, 0)) != NULL
) {
1016 char *ndpath
= NULL
;
1018 nspath
= mprintf("%s/%s", spath
, node
->no_Name
);
1020 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1022 info
->spath
= nspath
;
1023 info
->dpath
= ndpath
;
1024 info
->sdevNo
= sdevNo
;
1025 info
->ddevNo
= ddevNo
;
1027 r
+= DoCopy(info
, node
->no_Stat
, depth
);
1029 r
+= DoCopy(info
, node
->no_Stat
, depth
+ 1);
1038 * Remove files/directories from destination that do not appear
1041 if (dpath
&& ScanDir(list
, &DstHost
, dpath
,
1042 &CountTargetReadBytes
, 3) == 0) {
1044 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1046 * If object does not exist in source or .cpignore
1047 * then recursively remove it.
1051 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1052 RemoveRecur(ndpath
, ddevNo
, node
->no_Stat
);
1061 if (dpath
&& st2Valid
) {
1062 struct timeval tv
[2];
1064 if (ForceOpt
|| !OwnerMatch(stat1
, &st2
))
1065 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1066 if (stat1
->st_mode
!= st2
.st_mode
)
1067 hc_chmod(&DstHost
, dpath
, stat1
->st_mode
);
1068 #ifdef _ST_FLAGS_PRESENT_
1069 if (!FlagsMatch(stat1
, &st2
))
1070 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1072 if (ForceOpt
|| stat1
->st_mtime
!= st2
.st_mtime
) {
1073 bzero(tv
, sizeof(tv
));
1074 tv
[0].tv_sec
= stat1
->st_mtime
;
1075 tv
[1].tv_sec
= stat1
->st_mtime
;
1076 hc_utimes(&DstHost
, dpath
, tv
);
1079 } else if (dpath
== NULL
) {
1081 * If dpath is NULL, we are just updating the MD5
1084 if (UseMD5Opt
&& S_ISREG(stat1
->st_mode
)) {
1085 mres
= md5_check(spath
, NULL
);
1087 if (VerboseOpt
> 1) {
1089 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1091 logstd("%-32s md5-ok\n", (dpath
) ? dpath
: spath
);
1092 } else if (!QuietOpt
&& mres
< 0) {
1093 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1097 } else if (S_ISREG(stat1
->st_mode
)) {
1104 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1106 path
= mprintf("%s", dpath
);
1109 * Handle check failure message.
1113 logerr("%-32s md5-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1117 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1120 * Not quite ready to do the copy yet. If UseHLPath is defined,
1121 * see if we can hardlink instead.
1123 * If we can hardlink, and the target exists, we have to remove it
1124 * first or the hardlink will fail. This can occur in a number of
1125 * situations but most typically when the '-f -H' combination is
1128 if (UseHLPath
&& (hpath
= checkHLPath(stat1
, spath
, dpath
)) != NULL
) {
1130 xremove(&DstHost
, dpath
);
1131 if (hc_link(&DstHost
, hpath
, dpath
) == 0) {
1134 logstd("%-32s hardlinked(-H)\n",
1135 (dpath
? dpath
: spath
));
1141 * Shucks, we may have hit a filesystem hard linking limit,
1142 * we have to copy instead.
1147 if ((fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0)) >= 0) {
1148 if ((fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600)) < 0) {
1150 * There could be a .tmp file from a previously interrupted
1151 * run, delete and retry. Fail if we still can't get at it.
1153 #ifdef _ST_FLAGS_PRESENT_
1154 hc_chflags(&DstHost
, path
, 0);
1156 hc_remove(&DstHost
, path
);
1157 fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_TRUNC
, 0600);
1161 char *iobuf1
= malloc(GETIOSIZE
);
1165 * Matt: What about holes?
1168 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
1170 if (hc_write(&DstHost
, fd2
, iobuf1
, n
) != n
)
1174 hc_close(&DstHost
, fd2
);
1176 struct timeval tv
[2];
1178 bzero(tv
, sizeof(tv
));
1179 tv
[0].tv_sec
= stat1
->st_mtime
;
1180 tv
[1].tv_sec
= stat1
->st_mtime
;
1182 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1183 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1184 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1185 #ifdef _ST_FLAGS_PRESENT_
1186 if (stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
1187 hc_utimes(&DstHost
, path
, tv
);
1189 hc_utimes(&DstHost
, path
, tv
);
1191 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1192 logerr("%-32s rename-after-copy failed: %s\n",
1193 (dpath
? dpath
: spath
), strerror(errno
)
1198 logstd("%-32s copy-ok\n", (dpath
? dpath
: spath
));
1199 #ifdef _ST_FLAGS_PRESENT_
1200 if (DstRootPrivs
? stat1
->st_flags
: stat1
->st_flags
& UF_SETTABLE
)
1201 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1204 #ifdef _ST_FLAGS_PRESENT_
1205 if ((stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
)) == 0)
1206 hc_utimes(&DstHost
, dpath
, tv
);
1208 CountSourceReadBytes
+= size
;
1209 CountWriteBytes
+= size
;
1210 CountSourceBytes
+= size
;
1214 logerr("%-32s %s failed: %s\n",
1215 (dpath
? dpath
: spath
), op
, strerror(errno
)
1217 hc_remove(&DstHost
, path
);
1222 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1223 (dpath
? dpath
: spath
), getuid(), geteuid(),
1228 hc_close(&SrcHost
, fd1
);
1230 logerr("%-32s copy: open failed: %s\n",
1231 (dpath
? dpath
: spath
),
1240 if (!r
&& hc_stat(&DstHost
, dpath
, &st2
) == 0) {
1241 hltsetdino(hln
, st2
.st_ino
);
1247 } else if (S_ISLNK(stat1
->st_mode
)) {
1248 char *link1
= malloc(GETLINKSIZE
);
1249 char *link2
= malloc(GETLINKSIZE
);
1254 n1
= hc_readlink(&SrcHost
, spath
, link1
, GETLINKSIZE
- 1);
1256 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1257 n2
= hc_readlink(&DstHost
, dpath
, link2
, GETLINKSIZE
- 1);
1259 path
= mprintf("%s", dpath
);
1263 if (ForceOpt
|| n1
!= n2
|| bcmp(link1
, link2
, n1
) != 0) {
1264 hc_umask(&DstHost
, ~stat1
->st_mode
);
1265 xremove(&DstHost
, path
);
1267 if (hc_symlink(&DstHost
, link1
, path
) < 0) {
1268 logerr("%-32s symlink (%s->%s) failed: %s\n",
1269 (dpath
? dpath
: spath
), link1
, path
,
1274 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1275 hc_lchown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1277 * there is no lchmod() or lchflags(), we
1278 * cannot chmod or chflags a softlink.
1280 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1281 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1282 (dpath
? dpath
: spath
),
1283 path
, dpath
, strerror(errno
));
1284 } else if (VerboseOpt
) {
1285 logstd("%-32s softlink-ok\n", (dpath
? dpath
: spath
));
1287 hc_umask(&DstHost
, 000);
1288 CountWriteBytes
+= n1
;
1292 if (VerboseOpt
>= 3)
1293 logstd("%-32s nochange", (dpath
? dpath
: spath
));
1294 if (!OwnerMatch(stat1
, &st2
)) {
1295 hc_lchown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1296 if (VerboseOpt
>= 3)
1297 logstd(" (uid/gid differ)");
1299 if (VerboseOpt
>= 3)
1302 CountSourceBytes
+= n1
;
1303 CountSourceReadBytes
+= n1
;
1305 CountTargetReadBytes
+= n2
;
1309 logerr("%-32s softlink-failed\n", (dpath
? dpath
: spath
));
1314 } else if ((S_ISCHR(stat1
->st_mode
) || S_ISBLK(stat1
->st_mode
)) && DeviceOpt
) {
1319 stat1
->st_mode
!= st2
.st_mode
||
1320 stat1
->st_rdev
!= st2
.st_rdev
||
1321 !OwnerMatch(stat1
, &st2
)
1324 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1325 xremove(&DstHost
, path
);
1327 path
= mprintf("%s", dpath
);
1330 if (hc_mknod(&DstHost
, path
, stat1
->st_mode
, stat1
->st_rdev
) == 0) {
1331 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1332 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1334 xremove(&DstHost
, dpath
);
1335 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1336 logerr("%-32s dev-rename-after-create failed: %s\n",
1337 (dpath
? dpath
: spath
),
1340 } else if (VerboseOpt
) {
1341 logstd("%-32s dev-ok\n", (dpath
? dpath
: spath
));
1346 logerr("%-32s dev failed: %s\n",
1347 (dpath
? dpath
: spath
), strerror(errno
)
1351 if (VerboseOpt
>= 3)
1352 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1360 if (hln
->dino
== (ino_t
)-1) {
1362 /*hln = NULL; unneeded */
1371 ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
1372 int64_t *CountReadBytes
, int n
)
1375 struct HCDirEntry
*den
;
1376 struct stat
*statptr
;
1380 * scan .cpignore file for files/directories to ignore
1381 * (only in the source directory, i.e. if n == 0).
1387 char *buf
= malloc(GETBUFSIZE
);
1391 if (UseCpFile
[0] == '/') {
1392 fpath
= mprintf("%s", UseCpFile
);
1394 fpath
= mprintf("%s/%s", path
, UseCpFile
);
1396 AddList(list
, strrchr(fpath
, '/') + 1, 1, NULL
);
1397 if ((fd
= hc_open(host
, fpath
, O_RDONLY
, 0)) >= 0) {
1399 while ((nread
= hc_read(host
, fd
, buf
+ bufused
,
1400 GETBUFSIZE
- bufused
- 1)) > 0) {
1401 *CountReadBytes
+= nread
;
1404 for (next
= buf
; (nl
= strchr(next
, '\n')); next
= nl
+1) {
1406 AddList(list
, next
, 1, NULL
);
1408 bufused
= strlen(next
);
1410 bcopy(next
, buf
, bufused
);
1413 /* last line has no trailing newline */
1415 AddList(list
, buf
, 1, NULL
);
1424 * Automatically exclude MD5CacheFile that we create on the
1425 * source from the copy to the destination.
1427 * Automatically exclude a FSMIDCacheFile on the source that
1428 * would otherwise overwrite the one we maintain on the target.
1431 AddList(list
, MD5CacheFile
, 1, NULL
);
1433 AddList(list
, FSMIDCacheFile
, 1, NULL
);
1436 if ((dir
= hc_opendir(host
, path
)) == NULL
)
1438 while ((den
= hc_readdir(host
, dir
, &statptr
)) != NULL
) {
1442 if (strcmp(den
->d_name
, ".") != 0 && strcmp(den
->d_name
, "..") != 0)
1443 AddList(list
, den
->d_name
, n
, statptr
);
1445 hc_closedir(host
, dir
);
1455 RemoveRecur(const char *dpath
, dev_t devNo
, struct stat
*dstat
)
1459 if (dstat
== NULL
) {
1460 if (hc_lstat(&DstHost
, dpath
, &st
) == 0)
1463 if (dstat
!= NULL
) {
1464 if (devNo
== (dev_t
)-1)
1465 devNo
= dstat
->st_dev
;
1466 if (dstat
->st_dev
== devNo
) {
1467 if (S_ISDIR(dstat
->st_mode
)) {
1470 if ((dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1471 List
*list
= malloc(sizeof(List
));
1473 struct HCDirEntry
*den
;
1476 while ((den
= hc_readdir(&DstHost
, dir
, &dstat
)) != NULL
) {
1477 if (strcmp(den
->d_name
, ".") == 0)
1479 if (strcmp(den
->d_name
, "..") == 0)
1481 AddList(list
, den
->d_name
, 3, dstat
);
1483 hc_closedir(&DstHost
, dir
);
1484 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1487 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1488 RemoveRecur(ndpath
, devNo
, node
->no_Stat
);
1494 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1496 if (xrmdir(&DstHost
, dpath
) < 0) {
1497 logerr("%-32s rmdir failed: %s\n",
1498 dpath
, strerror(errno
)
1501 CountRemovedItems
++;
1506 logstd("%-32s not-removed\n", dpath
);
1507 } else if (xrmdir(&DstHost
, dpath
) == 0) {
1509 logstd("%-32s rmdir-ok\n", dpath
);
1510 CountRemovedItems
++;
1512 logerr("%-32s rmdir failed: %s\n",
1513 dpath
, strerror(errno
)
1518 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1520 if (xremove(&DstHost
, dpath
) < 0) {
1521 logerr("%-32s remove failed: %s\n",
1522 dpath
, strerror(errno
)
1525 CountRemovedItems
++;
1530 logstd("%-32s not-removed\n", dpath
);
1531 } else if (xremove(&DstHost
, dpath
) == 0) {
1533 logstd("%-32s remove-ok\n", dpath
);
1534 CountRemovedItems
++;
1536 logerr("%-32s remove failed: %s\n",
1537 dpath
, strerror(errno
)
1547 InitList(List
*list
)
1549 bzero(list
, sizeof(List
));
1550 list
->li_Node
.no_Next
= &list
->li_Node
;
1554 ResetList(List
*list
)
1558 while ((node
= list
->li_Node
.no_Next
) != &list
->li_Node
) {
1559 list
->li_Node
.no_Next
= node
->no_Next
;
1560 if (node
->no_Stat
!= NULL
)
1561 free(node
->no_Stat
);
1568 IterateList(List
*list
, Node
*node
, int n
)
1571 node
= list
->li_Node
.no_Next
;
1573 node
= node
->no_Next
;
1574 while (node
->no_Value
!= n
&& node
!= &list
->li_Node
)
1575 node
= node
->no_Next
;
1576 return (node
== &list
->li_Node
? NULL
: node
);
1580 AddList(List
*list
, const char *name
, int n
, struct stat
*st
)
1586 * Scan against wildcards. Only a node value of 1 can be a wildcard
1587 * ( usually scanned from .cpignore )
1590 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1591 if (strcmp(name
, node
->no_Name
) == 0 ||
1592 (n
!= 1 && node
->no_Value
== 1 &&
1593 fnmatch(node
->no_Name
, name
, 0) == 0)
1595 return(node
->no_Value
);
1600 * Look for exact match
1604 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1605 if (strcmp(name
, node
->no_Name
) == 0) {
1606 return(node
->no_Value
);
1609 node
= malloc(sizeof(Node
) + strlen(name
) + 1);
1611 fatal("out of memory");
1613 node
->no_Next
= list
->li_Node
.no_Next
;
1614 list
->li_Node
.no_Next
= node
;
1616 node
->no_HNext
= list
->li_Hash
[hv
];
1617 list
->li_Hash
[hv
] = node
;
1619 strcpy(node
->no_Name
, name
);
1627 shash(const char *s
)
1634 if (*s
== '*' || *s
== '?' ||
1635 *s
== '{' || *s
== '}' ||
1636 *s
== '[' || *s
== ']' ||
1641 hv
= (hv
<< 5) ^ *s
^ (hv
>> 23);
1644 return(((hv
>> 16) ^ hv
) & HMASK
);
1648 YesNo(const char *path
)
1652 fprintf(stderr
, "remove %s (Yes/No) [No]? ", path
);
1655 first
= ch
= getchar();
1656 while (ch
!= '\n' && ch
!= EOF
)
1658 return ((first
== 'y' || first
== 'Y'));
1662 * xrename() - rename with override
1664 * If the rename fails, attempt to override st_flags on the
1665 * destination and rename again. If that fails too, try to
1666 * set the flags back the way they were and give up.
1670 xrename(const char *src
, const char *dst
, u_long flags
)
1674 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1675 #ifdef _ST_FLAGS_PRESENT_
1676 hc_chflags(&DstHost
, dst
, 0);
1677 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0)
1678 hc_chflags(&DstHost
, dst
, flags
);
1685 xlink(const char *src
, const char *dst
, u_long flags
)
1688 #ifdef _ST_FLAGS_PRESENT_
1692 if ((r
= hc_link(&DstHost
, src
, dst
)) < 0) {
1693 #ifdef _ST_FLAGS_PRESENT_
1694 hc_chflags(&DstHost
, src
, 0);
1695 r
= hc_link(&DstHost
, src
, dst
);
1697 hc_chflags(&DstHost
, src
, flags
);
1707 xremove(struct HostConf
*host
, const char *path
)
1711 res
= hc_remove(host
, path
);
1712 #ifdef _ST_FLAGS_PRESENT_
1713 if (res
== -EPERM
) {
1714 hc_chflags(host
, path
, 0);
1715 res
= hc_remove(host
, path
);
1722 xrmdir(struct HostConf
*host
, const char *path
)
1726 res
= hc_rmdir(host
, path
);
1727 #ifdef _ST_FLAGS_PRESENT_
1728 if (res
== -EPERM
) {
1729 hc_chflags(host
, path
, 0);
1730 res
= hc_rmdir(host
, path
);