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).
50 * Example: cc -O cpdup.c -o cpdup -lcrypto
52 * ".MD5.CHECKSUMS" contains md5 checksumms for the current directory.
53 * This file is stored on the source.
61 #define HMASK (HSIZE-1)
63 #define HLMASK (HLSIZE - 1)
65 #define GETBUFSIZE 8192
66 #define GETPATHSIZE 2048
67 #define GETLINKSIZE 1024
68 #define GETIOSIZE 65536
70 #ifndef _ST_FLAGS_PRESENT_
71 #define st_flags st_mode
76 struct Node
*no_HNext
;
97 typedef struct copy_info
{
104 static struct hlink
*hltable
[HLSIZE
];
106 static void RemoveRecur(const char *dpath
, dev_t devNo
, struct stat
*dstat
);
107 static void InitList(List
*list
);
108 static void ResetList(List
*list
);
109 static Node
*IterateList(List
*list
, Node
*node
, int n
);
110 static int AddList(List
*list
, const char *name
, int n
, struct stat
*st
);
111 static int CheckList(List
*list
, const char *path
, const char *name
);
112 static int getbool(const char *str
);
113 static char *SplitRemote(char **pathp
);
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 static 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 xrmdir(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
);
136 static int mtimecmp(struct stat
*st1
, struct stat
*st2
);
137 static int symlink_mfo_test(struct HostConf
*hc
, struct stat
*st1
,
140 int AskConfirmation
= 1;
156 const char *ssh_argv
[16];
159 const char *UseCpFile
;
160 const char *MD5CacheFile
;
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
, ":CdF:fH:hIi:j:lM:mnoqRSs:uVvX:x")) != -1) {
205 fatal("too many -F options");
206 ssh_argv
[ssh_argc
++] = optarg
;
222 AskConfirmation
= getbool(optarg
);
225 DeviceOpt
= getbool(optarg
);
233 MD5CacheFile
= optarg
;
237 MD5CacheFile
= ".MD5.CHECKSUMS";
255 SafetyOpt
= getbool(optarg
);
258 setvbuf(stdout
, NULL
, _IOLBF
, 0);
270 UseCpFile
= ".cpignore";
273 fatal("missing argument for option: -%c\n", optopt
);
277 fatal("illegal option: -%c\n", optopt
);
293 fatal("too many arguments");
296 * If we are told to go into slave mode, run the HC protocol
299 DstRootPrivs
= (geteuid() == 0);
305 * Extract the source and/or/neither target [user@]host and
306 * make any required connections.
308 if (src
&& (ptr
= SplitRemote(&src
)) != NULL
) {
312 fatal("The MD5 options are not currently supported for remote sources");
313 if (hc_connect(&SrcHost
, ReadOnlyOpt
) < 0)
316 SrcHost
.version
= HCPROTO_VERSION
;
318 fatal("The -R option is only supported for remote sources");
321 if (dst
&& (ptr
= SplitRemote(&dst
)) != NULL
) {
324 if (hc_connect(&DstHost
, 0) < 0)
327 DstHost
.version
= HCPROTO_VERSION
;
331 * dst may be NULL only if -m option is specified,
332 * which forces an update of the MD5 checksums
334 if (dst
== NULL
&& UseMD5Opt
== 0) {
340 DstRootPrivs
= (hc_geteuid(&DstHost
) == 0);
342 GroupCount
= hc_getgroups(&DstHost
, &GroupList
);
346 fprintf(stderr
, "DstRootPrivs == %s\n", DstRootPrivs
? "true" : "false");
347 fprintf(stderr
, "GroupCount == %d\n", GroupCount
);
348 for (i
= 0; i
< GroupCount
; i
++)
349 fprintf(stderr
, "Group[%d] == %d\n", i
, GroupList
[i
]);
352 bzero(&info
, sizeof(info
));
354 DstBaseLen
= strlen(dst
);
357 info
.sdevNo
= (dev_t
)-1;
358 info
.ddevNo
= (dev_t
)-1;
359 i
= DoCopy(&info
, NULL
, -1);
363 info
.sdevNo
= (dev_t
)-1;
364 info
.ddevNo
= (dev_t
)-1;
365 i
= DoCopy(&info
, NULL
, -1);
371 if (SummaryOpt
&& i
== 0) {
375 gettimeofday(&end
, NULL
);
377 /* don't count stat's in our byte statistics */
378 CountSourceBytes
+= sizeof(struct stat
) * CountSourceItems
;
379 CountSourceReadBytes
+= sizeof(struct stat
) * CountSourceItems
;
380 CountWriteBytes
+= sizeof(struct stat
) * CountCopiedItems
;
381 CountWriteBytes
+= sizeof(struct stat
) * CountRemovedItems
;
384 duration
= (end
.tv_sec
- start
.tv_sec
);
385 duration
+= (double)(end
.tv_usec
- start
.tv_usec
) / 1000000.0;
388 logstd("cpdup completed successfully\n");
389 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
390 "%lld bytes written (%.1fX speedup)\n",
391 (long long)CountSourceBytes
,
392 (long long)CountSourceReadBytes
,
393 (long long)CountTargetReadBytes
,
394 (long long)CountWriteBytes
,
395 ((double)CountSourceBytes
* 2.0) / ((double)(CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
)));
396 logstd("%lld source items, %lld items copied, %lld items linked, "
397 "%lld things deleted\n",
398 (long long)CountSourceItems
,
399 (long long)CountCopiedItems
,
400 (long long)CountLinkedItems
,
401 (long long)CountRemovedItems
);
402 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
404 (int)((CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
) / duration
/ 1024.0),
405 (int)(CountSourceBytes
/ duration
/ 1024.0));
407 exit((i
== 0) ? 0 : 1);
411 getbool(const char *str
)
413 if (strcmp(str
, "0") == 0)
415 if (strcmp(str
, "1") == 0)
417 fatal("option requires boolean argument (0 or 1): -%c\n", optopt
);
423 * Check if path specifies a remote path, using the same syntax as scp(1),
424 * i.e. a path is considered remote if the first colon is not preceded by
425 * a slash, so e.g. "./foo:bar" is considered local.
426 * If a remote path is detected, the colon is replaced with a null byte,
427 * and the return value is a pointer to the next character.
428 * Otherwise NULL is returned.
430 * A path prefix of localhost is the same as a locally specified file or
431 * directory path, but prevents any further interpretation of the path
432 * as being a remote hostname (for paths that have colons in them).
435 SplitRemote(char **pathp
)
440 if (path
[(cindex
= strcspn(path
, ":/"))] == ':') {
442 if (strcmp(path
, "localhost") != 0)
443 return (path
+ cindex
);
444 *pathp
= path
+ cindex
;
450 * Check if group g is in our GroupList.
452 * Typically the number of groups a user belongs to isn't large
453 * enough to warrant more effort than a simple linear search.
454 * However, we perform an optimization by moving a group to the
455 * top of the list when we have a hit. This assumes that there
456 * isn't much variance in the gids of files that a non-root user
457 * copies. So most of the time the search will terminate on the
458 * first element of the list.
461 ChgrpAllowed(gid_t g
)
465 for (i
= 0; i
< GroupCount
; i
++)
466 if (GroupList
[i
] == g
) {
468 /* Optimize: Move g to the front of the list. */
470 GroupList
[i
] = GroupList
[i
- 1];
479 * The following two functions return true if the ownership (UID + GID)
480 * or the flags of two files match, respectively.
482 * Only perform weak checking if we don't have sufficient privileges on
483 * the target machine, so we don't waste transfers with things that are
484 * bound to fail anyway.
487 OwnerMatch(struct stat
*st1
, struct stat
*st2
)
490 /* Both UID and GID must match. */
491 return (st1
->st_uid
== st2
->st_uid
&& st1
->st_gid
== st2
->st_gid
);
493 /* Ignore UID, and also ignore GID if we can't chgrp to that group. */
494 return (st1
->st_gid
== st2
->st_gid
|| !ChgrpAllowed(st1
->st_gid
));
497 #ifdef _ST_FLAGS_PRESENT_
499 FlagsMatch(struct stat
*st1
, struct stat
*st2
)
502 * Ignore UF_ARCHIVE. It gets set automatically by the filesystem, for
503 * filesystems that support it. If the destination filesystem supports it, but
504 * it's cleared on the source file, then multiple invocations of cpdup would
505 * all try to copy the file because the flags wouldn't match.
507 * When unpriveleged, ignore flags we can't set
509 u_long ignored
= DstRootPrivs
? 0 : SF_SETTABLE
;
512 ignored
|= UF_ARCHIVE
;
514 return (((st1
->st_flags
^ st2
->st_flags
) & ~ignored
) == 0);
519 static struct hlink
*
520 hltlookup(struct stat
*stp
)
525 n
= stp
->st_ino
& HLMASK
;
527 for (hl
= hltable
[n
]; hl
; hl
= hl
->next
) {
528 if (hl
->ino
== stp
->st_ino
) {
537 static struct hlink
*
538 hltadd(struct stat
*stp
, const char *path
)
541 int plen
= strlen(path
);
544 new = malloc(offsetof(struct hlink
, name
[plen
+ 1]));
546 fatal("out of memory");
549 /* initialize and link the new element into the table */
550 new->ino
= stp
->st_ino
;
551 new->dino
= (ino_t
)-1;
553 bcopy(path
, new->name
, plen
+ 1);
556 n
= stp
->st_ino
& HLMASK
;
557 new->next
= hltable
[n
];
559 hltable
[n
]->prev
= new;
566 hltsetdino(struct hlink
*hl
, ino_t inum
)
572 hltdelete(struct hlink
*hl
)
574 assert(hl
->refs
== 1);
578 hl
->next
->prev
= hl
->prev
;
579 hl
->prev
->next
= hl
->next
;
582 hl
->next
->prev
= NULL
;
584 hltable
[hl
->ino
& HLMASK
] = hl
->next
;
591 hltrels(struct hlink
*hl
)
593 assert(hl
->refs
== 1);
598 * If UseHLPath is defined check to see if the file in question is
599 * the same as the source file, and if it is return a pointer to the
600 * -H path based file for hardlinking. Else return NULL.
603 checkHLPath(struct stat
*st1
, const char *spath
, const char *dpath
)
609 if (asprintf(&hpath
, "%s%s", UseHLPath
, dpath
+ DstBaseLen
) < 0)
610 fatal("out of memory");
613 * stat info matches ?
615 if (hc_stat(&DstHost
, hpath
, &sthl
) < 0 ||
616 st1
->st_size
!= sthl
.st_size
||
617 mtimecmp(st1
, &sthl
) != 0 ||
618 !OwnerMatch(st1
, &sthl
) ||
619 !FlagsMatch(st1
, &sthl
)
626 * If ForceOpt or ValidateOpt is set we have to compare the files
628 if (ForceOpt
|| ValidateOpt
) {
629 error
= validate_check(spath
, hpath
);
639 * Return 0 if the contents of the file <spath> matches the contents of
643 validate_check(const char *spath
, const char *dpath
)
649 fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0);
650 fd2
= hc_open(&DstHost
, dpath
, O_RDONLY
, 0);
653 if (fd1
>= 0 && fd2
>= 0) {
656 char *iobuf1
= malloc(GETIOSIZE
);
657 char *iobuf2
= malloc(GETIOSIZE
);
659 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
660 CountSourceReadBytes
+= n
;
661 x
= hc_read(&DstHost
, fd2
, iobuf2
, GETIOSIZE
);
663 CountTargetReadBytes
+= x
;
666 if (bcmp(iobuf1
, iobuf2
, n
) != 0)
675 hc_close(&SrcHost
, fd1
);
677 hc_close(&DstHost
, fd2
);
682 DoCopy(copy_info_t info
, struct stat
*stat1
, int depth
)
684 const char *spath
= info
->spath
;
685 const char *dpath
= info
->dpath
;
686 dev_t sdevNo
= info
->sdevNo
;
687 dev_t ddevNo
= info
->ddevNo
;
690 unsigned long st2_flags
;
691 int r
, mres
, fres
, st2Valid
;
695 r
= mres
= fres
= st2Valid
= 0;
701 if (hc_lstat(&SrcHost
, spath
, &st1
) != 0) {
708 /* skip snapshot files because they're sparse and _huge_ */
709 if (stat1
->st_flags
& SF_SNAPSHOT
)
712 st2
.st_mode
= 0; /* in case lstat fails */
713 st2
.st_flags
= 0; /* in case lstat fails */
714 if (dpath
&& hc_lstat(&DstHost
, dpath
, &st2
) == 0) {
716 #ifdef _ST_FLAGS_PRESENT_
717 st2_flags
= st2
.st_flags
;
721 if (S_ISREG(stat1
->st_mode
))
722 size
= stat1
->st_size
;
728 if (S_ISREG(stat1
->st_mode
) && stat1
->st_nlink
> 1 && dpath
) {
729 if ((hln
= hltlookup(stat1
)) != NULL
) {
733 if (st2
.st_ino
== hln
->dino
) {
735 * hard link is already correct, nothing to do
738 logstd("%-32s nochange\n", (dpath
) ? dpath
: spath
);
739 if (hln
->nlinked
== stat1
->st_nlink
) {
748 * hard link is not correct, attempt to unlink it
750 if (xremove(&DstHost
, dpath
) < 0) {
751 logerr("%-32s hardlink: unable to unlink: %s\n",
752 ((dpath
) ? dpath
: spath
), strerror(errno
));
761 if (xlink(hln
->name
, dpath
, stat1
->st_flags
) < 0) {
762 int tryrelink
= (errno
== EMLINK
);
763 logerr("%-32s hardlink: unable to link to %s: %s\n",
764 (dpath
? dpath
: spath
), hln
->name
, strerror(errno
)
769 logerr("%-20s hardlink: will attempt to copy normally\n",
770 (dpath
? dpath
: spath
));
775 if (hln
->nlinked
== stat1
->st_nlink
) {
781 logstd("%-32s hardlink: %s\n",
782 (dpath
? dpath
: spath
),
783 (st2Valid
? "relinked" : "linked")
794 * first instance of hardlink must be copied normally
797 hln
= hltadd(stat1
, dpath
);
802 * Do we need to copy the file/dir/link/whatever? Early termination
803 * if we do not. Always traverse directories. Always redo links.
805 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
810 && stat1
->st_mode
== st2
.st_mode
811 && FlagsMatch(stat1
, &st2
)
813 if (S_ISLNK(stat1
->st_mode
) || S_ISDIR(stat1
->st_mode
)) {
817 stat1
->st_size
== st2
.st_size
&&
818 (ValidateOpt
== 2 || mtimecmp(stat1
, &st2
) == 0) &&
819 OwnerMatch(stat1
, &st2
)
821 && (UseMD5Opt
== 0 || !S_ISREG(stat1
->st_mode
) ||
822 (mres
= md5_check(spath
, dpath
)) == 0)
824 && (ValidateOpt
== 0 || !S_ISREG(stat1
->st_mode
) ||
825 validate_check(spath
, dpath
) == 0)
828 * The files are identical, but if we are running as
829 * root we might need to adjust ownership/group/flags.
832 int changedflags
= 0;
835 hltsetdino(hln
, st2
.st_ino
);
837 if (!OwnerMatch(stat1
, &st2
)) {
838 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
841 #ifdef _ST_FLAGS_PRESENT_
842 if (!FlagsMatch(stat1
, &st2
)) {
843 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
847 if (VerboseOpt
>= 3) {
850 logstd("%-32s md5-nochange",
851 (dpath
? dpath
: spath
));
855 logstd("%-32s nochange (contents validated)",
856 (dpath
? dpath
: spath
));
858 logstd("%-32s nochange", (dpath
? dpath
: spath
));
861 logstd(" (uid/gid differ)");
863 logstd(" (flags differ)");
866 CountSourceBytes
+= size
;
873 if (st2Valid
&& !S_ISDIR(stat1
->st_mode
) && S_ISDIR(st2
.st_mode
)) {
875 logerr("%-32s SAFETY - refusing to copy file over directory\n",
876 (dpath
? dpath
: spath
)
880 goto done
; /* continue with the cpdup anyway */
882 if (QuietOpt
== 0 || AskConfirmation
) {
883 logstd("%-32s WARNING: non-directory source will blow away\n"
884 "%-32s preexisting dest directory, continuing anyway!\n",
885 ((dpath
) ? dpath
: spath
), "");
888 RemoveRecur(dpath
, ddevNo
, &st2
);
893 * The various comparisons failed, copy it.
895 if (S_ISDIR(stat1
->st_mode
)) {
899 if (!st2Valid
|| S_ISDIR(st2
.st_mode
) == 0) {
901 xremove(&DstHost
, dpath
);
902 if (hc_mkdir(&DstHost
, dpath
, stat1
->st_mode
| 0700) != 0) {
903 logerr("%s: mkdir failed: %s\n",
904 (dpath
? dpath
: spath
), strerror(errno
));
908 if (hc_lstat(&DstHost
, dpath
, &st2
) != 0) {
909 if (NotForRealOpt
== 0)
910 logerr("%s: lstat of newly made dir failed: %s\n",
911 (dpath
? dpath
: spath
), strerror(errno
));
918 if (!OwnerMatch(stat1
, &st2
) &&
919 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
) != 0
921 logerr("%s: chown of newly made dir failed: %s\n",
922 (dpath
? dpath
: spath
), strerror(errno
));
924 /* Note that we should not set skipdir = 1 here. */
928 logstd("%-32s mkdir-ok\n", (dpath
? dpath
: spath
));
932 * Directory must be scanable by root for cpdup to
933 * work. We'll fix it later if the directory isn't
934 * supposed to be readable ( which is why we fixup
935 * st2.st_mode to match what we did ).
937 if ((st2
.st_mode
& 0700) != 0700) {
938 hc_chmod(&DstHost
, dpath
, st2
.st_mode
| 0700);
942 logstd("%s\n", dpath
? dpath
: spath
);
947 * When copying a directory, stop if the source crosses a mount
950 if (sdevNo
!= (dev_t
)-1 && stat1
->st_dev
!= sdevNo
)
953 sdevNo
= stat1
->st_dev
;
956 * When copying a directory, stop if the destination crosses
959 * The target directory will have been created and stat'd
960 * for st2 if it did not previously exist. st2Valid is left
961 * as a flag. If the stat failed st2 will still only have its
962 * default initialization.
964 * So we simply assume here that the directory is within the
965 * current target mount if we had to create it (aka st2Valid is 0)
966 * and we leave ddevNo alone.
969 if (ddevNo
!= (dev_t
)-1 && st2
.st_dev
!= ddevNo
)
976 List
*list
= malloc(sizeof(List
));
980 logstd("Scanning %s ...\n", spath
);
982 if (ScanDir(list
, &SrcHost
, spath
, &CountSourceReadBytes
, 0) == 0) {
984 while ((node
= IterateList(list
, node
, 0)) != NULL
) {
988 nspath
= mprintf("%s/%s", spath
, node
->no_Name
);
990 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
992 info
->spath
= nspath
;
993 info
->dpath
= ndpath
;
994 info
->sdevNo
= sdevNo
;
995 info
->ddevNo
= ddevNo
;
997 r
+= DoCopy(info
, node
->no_Stat
, depth
);
999 r
+= DoCopy(info
, node
->no_Stat
, depth
+ 1);
1008 * Remove files/directories from destination that do not appear
1011 if (dpath
&& ScanDir(list
, &DstHost
, dpath
,
1012 &CountTargetReadBytes
, 3) == 0) {
1014 while ((node
= 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
, node
->no_Name
);
1022 RemoveRecur(ndpath
, ddevNo
, node
->no_Stat
);
1031 if (dpath
&& st2Valid
) {
1032 struct timeval tv
[2];
1034 if (ForceOpt
|| !OwnerMatch(stat1
, &st2
))
1035 hc_chown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1036 if (stat1
->st_mode
!= st2
.st_mode
)
1037 hc_chmod(&DstHost
, dpath
, stat1
->st_mode
);
1038 #ifdef _ST_FLAGS_PRESENT_
1039 if (!FlagsMatch(stat1
, &st2
))
1040 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1042 if (ForceOpt
|| mtimecmp(stat1
, &st2
) != 0) {
1043 bzero(tv
, sizeof(tv
));
1044 tv
[0].tv_sec
= stat1
->st_mtime
;
1045 tv
[1].tv_sec
= stat1
->st_mtime
;
1046 #if defined(st_mtime) /* A macro, so very likely on modern POSIX */
1047 tv
[0].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1048 tv
[1].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1050 hc_utimes(&DstHost
, dpath
, tv
);
1053 } else if (dpath
== NULL
) {
1055 * If dpath is NULL, we are just updating the MD5
1058 if (UseMD5Opt
&& S_ISREG(stat1
->st_mode
)) {
1059 mres
= md5_check(spath
, NULL
);
1061 if (VerboseOpt
> 1) {
1063 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1065 logstd("%-32s md5-ok\n", (dpath
) ? dpath
: spath
);
1066 } else if (!QuietOpt
&& mres
< 0) {
1067 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1071 } else if (S_ISREG(stat1
->st_mode
)) {
1078 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1080 path
= mprintf("%s", dpath
);
1083 * Handle check failure message.
1087 logerr("%-32s md5-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1091 * Not quite ready to do the copy yet. If UseHLPath is defined,
1092 * see if we can hardlink instead.
1094 * If we can hardlink, and the target exists, we have to remove it
1095 * first or the hardlink will fail. This can occur in a number of
1096 * situations but most typically when the '-f -H' combination is
1099 if (UseHLPath
&& (hpath
= checkHLPath(stat1
, spath
, dpath
)) != NULL
) {
1101 xremove(&DstHost
, dpath
);
1102 if (hc_link(&DstHost
, hpath
, dpath
) == 0) {
1105 logstd("%-32s hardlinked(-H)\n",
1106 (dpath
? dpath
: spath
));
1112 * Shucks, we may have hit a filesystem hard linking limit,
1113 * we have to copy instead.
1118 if ((fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0)) >= 0) {
1119 if ((fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600)) < 0) {
1121 * There could be a .tmp file from a previously interrupted
1122 * run, delete and retry. Fail if we still can't get at it.
1124 #ifdef _ST_FLAGS_PRESENT_
1125 hc_chflags(&DstHost
, path
, 0);
1127 hc_remove(&DstHost
, path
);
1128 fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_TRUNC
, 0600);
1132 char *iobuf1
= malloc(GETIOSIZE
);
1136 * Matt: What about holes?
1139 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
1141 if (hc_write(&DstHost
, fd2
, iobuf1
, n
) != n
)
1145 hc_close(&DstHost
, fd2
);
1147 struct timeval tv
[2];
1149 bzero(tv
, sizeof(tv
));
1150 tv
[0].tv_sec
= stat1
->st_mtime
;
1151 tv
[1].tv_sec
= stat1
->st_mtime
;
1152 #if defined(st_mtime)
1153 tv
[0].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1154 tv
[1].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1157 if (DstRootPrivs
|| ChgrpAllowed(stat1
->st_gid
))
1158 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1159 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1160 #ifdef _ST_FLAGS_PRESENT_
1161 if (stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
1162 hc_utimes(&DstHost
, path
, tv
);
1164 hc_utimes(&DstHost
, path
, tv
);
1166 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1167 logerr("%-32s rename-after-copy failed: %s\n",
1168 (dpath
? dpath
: spath
), strerror(errno
)
1170 xremove(&DstHost
, path
);
1174 logstd("%-32s copy-ok\n", (dpath
? dpath
: spath
));
1175 #ifdef _ST_FLAGS_PRESENT_
1176 if (DstRootPrivs
? stat1
->st_flags
: stat1
->st_flags
& UF_SETTABLE
)
1177 hc_chflags(&DstHost
, dpath
, stat1
->st_flags
);
1180 #ifdef _ST_FLAGS_PRESENT_
1181 if ((stat1
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
)) == 0)
1182 hc_utimes(&DstHost
, dpath
, tv
);
1184 CountSourceReadBytes
+= size
;
1185 CountWriteBytes
+= size
;
1186 CountSourceBytes
+= size
;
1190 logerr("%-32s %s failed: %s\n",
1191 (dpath
? dpath
: spath
), op
, strerror(errno
)
1193 hc_remove(&DstHost
, path
);
1198 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1199 (dpath
? dpath
: spath
), getuid(), geteuid(),
1204 hc_close(&SrcHost
, fd1
);
1206 logerr("%-32s copy: open failed: %s\n",
1207 (dpath
? dpath
: spath
),
1216 if (!r
&& hc_stat(&DstHost
, dpath
, &st2
) == 0) {
1217 hltsetdino(hln
, st2
.st_ino
);
1223 } else if (S_ISLNK(stat1
->st_mode
)) {
1224 char *link1
= malloc(GETLINKSIZE
);
1225 char *link2
= malloc(GETLINKSIZE
);
1230 n1
= hc_readlink(&SrcHost
, spath
, link1
, GETLINKSIZE
- 1);
1232 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1233 n2
= hc_readlink(&DstHost
, dpath
, link2
, GETLINKSIZE
- 1);
1235 path
= mprintf("%s", dpath
);
1239 if (ForceOpt
|| n1
!= n2
|| bcmp(link1
, link2
, n1
) != 0 ||
1240 (st2Valid
&& symlink_mfo_test(&DstHost
, stat1
, &st2
))
1242 struct timeval tv
[2];
1244 bzero(tv
, sizeof(tv
));
1245 tv
[0].tv_sec
= stat1
->st_mtime
;
1246 tv
[1].tv_sec
= stat1
->st_mtime
;
1247 #if defined(st_mtime)
1248 tv
[0].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
1249 tv
[1].tv_usec
= stat1
->st_mtim
.tv_nsec
/ 1000;
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
);
1266 * lutimes, lchmod if supported by destination.
1268 if (DstHost
.version
>= HCPROTO_VERSION_LUCC
) {
1269 hc_lchmod(&DstHost
, path
, stat1
->st_mode
);
1270 hc_lutimes(&DstHost
, path
, tv
);
1274 * rename (and set flags if supported by destination)
1276 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1277 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1278 (dpath
? dpath
: spath
),
1279 path
, dpath
, strerror(errno
));
1280 xremove(&DstHost
, path
);
1282 #ifdef _ST_FLAGS_PRESENT_
1283 if (DstHost
.version
>= HCPROTO_VERSION_LUCC
)
1284 hc_lchflags(&DstHost
, dpath
, stat1
->st_flags
);
1287 logstd("%-32s softlink-ok\n",
1288 (dpath
? dpath
: spath
));
1291 hc_umask(&DstHost
, 000);
1292 CountWriteBytes
+= n1
;
1296 if (VerboseOpt
>= 3)
1297 logstd("%-32s nochange", (dpath
? dpath
: spath
));
1298 if (!OwnerMatch(stat1
, &st2
)) {
1299 hc_lchown(&DstHost
, dpath
, stat1
->st_uid
, stat1
->st_gid
);
1300 if (VerboseOpt
>= 3)
1301 logstd(" (uid/gid differ)");
1303 if (VerboseOpt
>= 3)
1306 CountSourceBytes
+= n1
;
1307 CountSourceReadBytes
+= n1
;
1309 CountTargetReadBytes
+= n2
;
1313 logerr("%-32s softlink-failed\n", (dpath
? dpath
: spath
));
1318 } else if ((S_ISCHR(stat1
->st_mode
) || S_ISBLK(stat1
->st_mode
)) && DeviceOpt
) {
1323 stat1
->st_mode
!= st2
.st_mode
||
1324 stat1
->st_rdev
!= st2
.st_rdev
||
1325 !OwnerMatch(stat1
, &st2
)
1328 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1329 xremove(&DstHost
, path
);
1331 path
= mprintf("%s", dpath
);
1334 if (hc_mknod(&DstHost
, path
, stat1
->st_mode
, stat1
->st_rdev
) == 0) {
1335 hc_chmod(&DstHost
, path
, stat1
->st_mode
);
1336 hc_chown(&DstHost
, path
, stat1
->st_uid
, stat1
->st_gid
);
1338 xremove(&DstHost
, dpath
);
1339 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1340 logerr("%-32s dev-rename-after-create failed: %s\n",
1341 (dpath
? dpath
: spath
),
1344 } else if (VerboseOpt
) {
1345 logstd("%-32s dev-ok\n", (dpath
? dpath
: spath
));
1350 logerr("%-32s dev failed: %s\n",
1351 (dpath
? dpath
: spath
), strerror(errno
)
1355 if (VerboseOpt
>= 3)
1356 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1364 if (hln
->dino
== (ino_t
)-1) {
1366 /*hln = NULL; unneeded */
1375 ScanDir(List
*list
, struct HostConf
*host
, const char *path
,
1376 int64_t *CountReadBytes
, int n
)
1379 struct HostConf
*cphost
;
1380 struct HCDirEntry
*den
;
1381 struct stat
*statptr
;
1385 * scan .cpignore file for files/directories to ignore
1386 * (only in the source directory, i.e. if n == 0).
1392 char *buf
= malloc(GETBUFSIZE
);
1396 if (UseCpFile
[0] == '/') {
1397 fpath
= mprintf("%s", UseCpFile
);
1400 fpath
= mprintf("%s/%s", path
, UseCpFile
);
1401 AddList(list
, strrchr(fpath
, '/') + 1, 1, NULL
);
1404 fd
= hc_open(cphost
, fpath
, O_RDONLY
, 0);
1407 while ((nread
= hc_read(cphost
, fd
, buf
+ bufused
,
1408 GETBUFSIZE
- bufused
- 1)) > 0) {
1409 *CountReadBytes
+= nread
;
1412 for (next
= buf
; (nl
= strchr(next
, '\n')); next
= nl
+1) {
1414 AddList(list
, next
, 1, NULL
);
1416 bufused
= strlen(next
);
1418 bcopy(next
, buf
, bufused
);
1421 /* last line has no trailing newline */
1423 AddList(list
, buf
, 1, NULL
);
1425 hc_close(cphost
, fd
);
1432 * Automatically exclude MD5CacheFile that we create on the
1433 * source from the copy to the destination.
1436 AddList(list
, MD5CacheFile
, 1, NULL
);
1439 if ((dir
= hc_opendir(host
, path
)) == NULL
)
1441 while ((den
= hc_readdir(host
, dir
, &statptr
)) != NULL
) {
1445 if (strcmp(den
->d_name
, ".") != 0 && strcmp(den
->d_name
, "..") != 0) {
1446 if (UseCpFile
&& UseCpFile
[0] == '/') {
1447 if (CheckList(list
, path
, den
->d_name
) == 0)
1450 AddList(list
, den
->d_name
, n
, statptr
);
1453 hc_closedir(host
, dir
);
1463 RemoveRecur(const char *dpath
, dev_t devNo
, struct stat
*dstat
)
1467 if (dstat
== NULL
) {
1468 if (hc_lstat(&DstHost
, dpath
, &st
) == 0)
1471 if (dstat
!= NULL
) {
1472 if (devNo
== (dev_t
)-1)
1473 devNo
= dstat
->st_dev
;
1474 if (dstat
->st_dev
== devNo
) {
1475 if (S_ISDIR(dstat
->st_mode
)) {
1478 if ((dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1479 List
*list
= malloc(sizeof(List
));
1481 struct HCDirEntry
*den
;
1484 while ((den
= hc_readdir(&DstHost
, dir
, &dstat
)) != NULL
) {
1485 if (strcmp(den
->d_name
, ".") == 0)
1487 if (strcmp(den
->d_name
, "..") == 0)
1489 AddList(list
, den
->d_name
, 3, dstat
);
1491 hc_closedir(&DstHost
, dir
);
1492 while ((node
= IterateList(list
, node
, 3)) != NULL
) {
1495 ndpath
= mprintf("%s/%s", dpath
, node
->no_Name
);
1496 RemoveRecur(ndpath
, devNo
, node
->no_Stat
);
1502 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1504 if (xrmdir(&DstHost
, dpath
) < 0) {
1505 logerr("%-32s rmdir failed: %s\n",
1506 dpath
, strerror(errno
)
1509 CountRemovedItems
++;
1514 logstd("%-32s not-removed\n", dpath
);
1515 } else if (xrmdir(&DstHost
, dpath
) == 0) {
1517 logstd("%-32s rmdir-ok\n", dpath
);
1518 CountRemovedItems
++;
1520 logerr("%-32s rmdir failed: %s\n",
1521 dpath
, strerror(errno
)
1526 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1528 if (xremove(&DstHost
, dpath
) < 0) {
1529 logerr("%-32s remove failed: %s\n",
1530 dpath
, strerror(errno
)
1533 CountRemovedItems
++;
1538 logstd("%-32s not-removed\n", dpath
);
1539 } else if (xremove(&DstHost
, dpath
) == 0) {
1541 logstd("%-32s remove-ok\n", dpath
);
1542 CountRemovedItems
++;
1544 logerr("%-32s remove failed: %s\n",
1545 dpath
, strerror(errno
)
1555 InitList(List
*list
)
1557 bzero(list
, sizeof(List
));
1558 list
->li_Node
.no_Next
= &list
->li_Node
;
1562 ResetList(List
*list
)
1566 while ((node
= list
->li_Node
.no_Next
) != &list
->li_Node
) {
1567 list
->li_Node
.no_Next
= node
->no_Next
;
1568 if (node
->no_Stat
!= NULL
)
1569 free(node
->no_Stat
);
1576 IterateList(List
*list
, Node
*node
, int n
)
1579 node
= list
->li_Node
.no_Next
;
1581 node
= node
->no_Next
;
1582 while (node
->no_Value
!= n
&& node
!= &list
->li_Node
)
1583 node
= node
->no_Next
;
1584 return (node
== &list
->li_Node
? NULL
: node
);
1588 AddList(List
*list
, const char *name
, int n
, struct stat
*st
)
1594 * Scan against wildcards. Only a node value of 1 can be a wildcard
1595 * ( usually scanned from .cpignore )
1597 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1598 if (strcmp(name
, node
->no_Name
) == 0 ||
1599 (n
!= 1 && node
->no_Value
== 1 &&
1600 fnmatch(node
->no_Name
, name
, 0) == 0)
1602 return(node
->no_Value
);
1607 * Look for exact match
1611 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1612 if (strcmp(name
, node
->no_Name
) == 0) {
1613 return(node
->no_Value
);
1616 node
= malloc(sizeof(Node
) + strlen(name
) + 1);
1618 fatal("out of memory");
1620 node
->no_Next
= list
->li_Node
.no_Next
;
1621 list
->li_Node
.no_Next
= node
;
1623 node
->no_HNext
= list
->li_Hash
[hv
];
1624 list
->li_Hash
[hv
] = node
;
1626 strcpy(node
->no_Name
, name
);
1634 * Match against n=1 (cpignore) entries
1636 * Returns 0 on match, non-zero if no match
1639 CheckList(List
*list
, const char *path
, const char *name
)
1645 if (asprintf(&fpath
, "%s/%s", path
, name
) < 0)
1646 fatal("out of memory");
1649 * Scan against wildcards. Only a node value of 1 can be a wildcard
1650 * ( usually scanned from .cpignore )
1652 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1653 if (node
->no_Value
!= 1)
1655 if (fnmatch(node
->no_Name
, fpath
, 0) == 0) {
1662 * Look for exact match
1665 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1666 if (node
->no_Value
!= 1)
1668 if (strcmp(fpath
, node
->no_Name
) == 0) {
1679 shash(const char *s
)
1686 if (*s
== '*' || *s
== '?' ||
1687 *s
== '{' || *s
== '}' ||
1688 *s
== '[' || *s
== ']' ||
1693 hv
= (hv
<< 5) ^ *s
^ (hv
>> 23);
1696 return(((hv
>> 16) ^ hv
) & HMASK
);
1700 YesNo(const char *path
)
1704 fprintf(stderr
, "remove %s (Yes/No) [No]? ", path
);
1707 first
= ch
= getchar();
1708 while (ch
!= '\n' && ch
!= EOF
)
1710 return ((first
== 'y' || first
== 'Y'));
1714 * xrename() - rename with override
1716 * If the rename fails, attempt to override st_flags on the
1717 * destination and rename again. If that fails too, try to
1718 * set the flags back the way they were and give up.
1722 xrename(const char *src
, const char *dst
, u_long flags
)
1726 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1727 #ifdef _ST_FLAGS_PRESENT_
1728 if (DstHost
.version
>= HCPROTO_VERSION_LUCC
)
1729 hc_lchflags(&DstHost
, dst
, 0);
1731 hc_chflags(&DstHost
, dst
, 0);
1733 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1734 if (DstHost
.version
>= HCPROTO_VERSION_LUCC
)
1735 hc_lchflags(&DstHost
, dst
, flags
);
1737 hc_chflags(&DstHost
, dst
, flags
);
1745 xlink(const char *src
, const char *dst
, u_long flags
)
1748 #ifdef _ST_FLAGS_PRESENT_
1752 if ((r
= hc_link(&DstHost
, src
, dst
)) < 0) {
1753 #ifdef _ST_FLAGS_PRESENT_
1754 if (DstHost
.version
>= HCPROTO_VERSION_LUCC
)
1755 hc_lchflags(&DstHost
, src
, 0);
1757 hc_chflags(&DstHost
, src
, 0);
1758 r
= hc_link(&DstHost
, src
, dst
);
1760 hc_chflags(&DstHost
, src
, flags
);
1770 xremove(struct HostConf
*host
, const char *path
)
1774 res
= hc_remove(host
, path
);
1775 #ifdef _ST_FLAGS_PRESENT_
1776 if (res
== -EPERM
) {
1777 if (host
->version
>= HCPROTO_VERSION_LUCC
)
1778 hc_lchflags(host
, path
, 0);
1780 hc_chflags(host
, path
, 0);
1781 res
= hc_remove(host
, path
);
1788 xrmdir(struct HostConf
*host
, const char *path
)
1792 res
= hc_rmdir(host
, path
);
1793 #ifdef _ST_FLAGS_PRESENT_
1794 if (res
== -EPERM
) {
1795 hc_chflags(host
, path
, 0);
1796 res
= hc_rmdir(host
, path
);
1803 * Compare mtimes. By default cpdup only compares the seconds field
1804 * because different operating systems and filesystems will store time
1805 * fields with varying amounts of precision.
1807 * This subroutine can be adjusted to also compare to microseconds or
1808 * nanoseconds precision. However, since cpdup() uses utimes() to
1809 * set a file's timestamp and utimes() only takes timeval's (usec precision),
1810 * I strongly recommend only comparing down to usec precision at best.
1813 mtimecmp(struct stat
*st1
, struct stat
*st2
)
1815 if (st1
->st_mtime
< st2
->st_mtime
)
1817 if (st1
->st_mtime
== st2
->st_mtime
)
1823 * Check to determine if a symlink's mtime, flags, or mode differ.
1825 * This is only supported on targets that support lchflags, lutimes,
1829 symlink_mfo_test(struct HostConf
*hc
, struct stat
*st1
, struct stat
*st2
)
1833 if (hc
->version
>= HCPROTO_VERSION_LUCC
) {
1834 if (!FlagsMatch(st1
, st2
))
1836 if (mtimecmp(st1
, st2
) != 0)
1838 if (st1
->st_mode
!= st2
->st_mode
)