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 struct hlink *hltable[HLSIZE];
108 void RemoveRecur(const char *dpath, dev_t devNo, struct stat *dstat);
109 void InitList(List *list);
110 void ResetList(List *list);
111 Node *IterateList(List *list, Node *node, int n);
112 int AddList(List *list, const char *name, int n, struct stat *st);
113 static int getbool(const char *str);
114 static char *SplitRemote(char *path);
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 int YesNo(const char *path);
130 static int xrename(const char *src, const char *dst, u_long flags);
131 static int xlink(const char *src, const char *dst, u_long flags);
132 static int xremove(struct HostConf *host, const char *path);
133 static int DoCopy(copy_info_t info, struct stat *stat1, int depth);
134 static int ScanDir(List *list, struct HostConf *host, const char *path,
135 int64_t *CountReadBytes, int n);
137 int AskConfirmation = 1;
151 int EnableDirectoryRetries;
156 const char *ssh_argv[16];
160 const char *UseCpFile;
161 const char *UseHLPath;
162 const char *MD5CacheFile;
163 const char *FSMIDCacheFile;
165 int64_t CountSourceBytes;
166 int64_t CountSourceItems;
167 int64_t CountCopiedItems;
168 int64_t CountSourceReadBytes;
169 int64_t CountTargetReadBytes;
170 int64_t CountWriteBytes;
171 int64_t CountRemovedItems;
172 int64_t CountLinkedItems;
174 struct HostConf SrcHost;
175 struct HostConf DstHost;
178 main(int ac, char **av)
185 struct timeval start;
186 struct copy_info info;
188 signal(SIGPIPE, SIG_IGN);
190 gettimeofday(&start, NULL);
192 while ((opt = getopt(ac, av, ":CdF:fH:Ii:j:K:klM:mopqRSs:uVvX:x")) != -1) {
194 /* TODO: sort the branches */
218 UseCpFile = ".cpignore";
228 fatal("too many -F options");
229 ssh_argv[ssh_argc++] = optarg;
241 AskConfirmation = getbool(optarg);
244 DeviceOpt = getbool(optarg);
247 SafetyOpt = getbool(optarg);
254 FSMIDCacheFile = ".FSMID.CHECK";
258 FSMIDCacheFile = optarg;
262 MD5CacheFile = optarg;
266 MD5CacheFile = ".MD5.CHECKSUMS";
269 setvbuf(stdout, NULL, _IOLBF, 0);
272 fatal("missing argument for option: -%c\n", optopt);
276 fatal("illegal option: -%c\n", optopt);
292 fatal("too many arguments");
295 * If we are told to go into slave mode, run the HC protocol
298 DstRootPrivs = (geteuid() == 0);
304 * Extract the source and/or/neither target [user@]host and
305 * make any required connections.
307 if (src && (ptr = SplitRemote(src)) != NULL) {
311 fatal("The MD5 options are not currently supported for remote sources");
312 if (hc_connect(&SrcHost, ReadOnlyOpt) < 0)
314 } else if (ReadOnlyOpt)
315 fatal("The -R option is only supported for remote sources");
317 if (dst && (ptr = SplitRemote(dst)) != NULL) {
321 fatal("The FSMID options are not currently supported for remote targets");
322 if (hc_connect(&DstHost, 0) < 0)
327 * dst may be NULL only if -m option is specified,
328 * which forces an update of the MD5 checksums
330 if (dst == NULL && UseMD5Opt == 0) {
336 DstRootPrivs = (hc_geteuid(&DstHost) == 0);
338 GroupCount = hc_getgroups(&DstHost, &GroupList);
342 fprintf(stderr, "DstRootPrivs == %s\n", DstRootPrivs ? "true" : "false");
343 fprintf(stderr, "GroupCount == %d\n", GroupCount);
344 for (i = 0; i < GroupCount; i++)
345 fprintf(stderr, "Group[%d] == %d\n", i, GroupList[i]);
348 bzero(&info, sizeof(info));
350 DstBaseLen = strlen(dst);
353 info.sdevNo = (dev_t)-1;
354 info.ddevNo = (dev_t)-1;
355 i = DoCopy(&info, NULL, -1);
359 info.sdevNo = (dev_t)-1;
360 info.ddevNo = (dev_t)-1;
361 i = DoCopy(&info, NULL, -1);
368 if (SummaryOpt && i == 0) {
372 gettimeofday(&end, NULL);
374 /* don't count stat's in our byte statistics */
375 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
376 CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
377 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
378 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
381 duration = (end.tv_sec - start.tv_sec);
382 duration += (double)(end.tv_usec - start.tv_usec) / 1000000.0;
385 logstd("cpdup completed successfully\n");
386 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
387 "%lld bytes written (%.1fX speedup)\n",
388 (long long)CountSourceBytes,
389 (long long)CountSourceReadBytes,
390 (long long)CountTargetReadBytes,
391 (long long)CountWriteBytes,
392 ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
393 logstd("%lld source items, %lld items copied, %lld items linked, "
394 "%lld things deleted\n",
395 (long long)CountSourceItems,
396 (long long)CountCopiedItems,
397 (long long)CountLinkedItems,
398 (long long)CountRemovedItems);
399 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
401 (int)((CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration / 1024.0),
402 (int)(CountSourceBytes / duration / 1024.0));
404 exit((i == 0) ? 0 : 1);
408 getbool(const char *str)
410 if (strcmp(str, "0") == 0)
412 if (strcmp(str, "1") == 0)
414 fatal("option requires boolean argument (0 or 1): -%c\n", optopt);
420 * Check if path specifies a remote path, using the same syntax as scp(1),
421 * i.e. a path is considered remote if the first colon is not preceded by
422 * a slash, so e.g. "./foo:bar" is considered local.
423 * If a remote path is detected, the colon is replaced with a null byte,
424 * and the return value is a pointer to the next character.
425 * Otherwise NULL is returned.
428 SplitRemote(char *path)
432 if (path[(cindex = strcspn(path, ":/"))] == ':') {
434 return (path + cindex);
440 * Check if group g is in our GroupList.
442 * Typically the number of groups a user belongs to isn't large
443 * enough to warrant more effort than a simple linear search.
444 * However, we perform an optimization by moving a group to the
445 * top of the list when we have a hit. This assumes that there
446 * isn't much variance in the gids of files that a non-root user
447 * copies. So most of the time the search will terminate on the
448 * first element of the list.
451 ChgrpAllowed(gid_t g)
455 for (i = 0; i < GroupCount; i++)
456 if (GroupList[i] == g) {
458 /* Optimize: Move g to the front of the list. */
460 GroupList[i] = GroupList[i - 1];
469 * The following two functions return true if the ownership (UID + GID)
470 * or the flags of two files match, respectively.
472 * Only perform weak checking if we don't have sufficient privileges on
473 * the target machine, so we don't waste transfers with things that are
474 * bound to fail anyway.
477 OwnerMatch(struct stat *st1, struct stat *st2)
480 /* Both UID and GID must match. */
481 return (st1->st_uid == st2->st_uid && st1->st_gid == st2->st_gid);
483 /* Ignore UID, and also ignore GID if we can't chgrp to that group. */
484 return (st1->st_gid == st2->st_gid || !ChgrpAllowed(st1->st_gid));
487 #ifdef _ST_FLAGS_PRESENT_
489 FlagsMatch(struct stat *st1, struct stat *st2)
492 return (st1->st_flags == st2->st_flags);
494 /* Only consider the user-settable flags. */
495 return (((st1->st_flags ^ st2->st_flags) & UF_SETTABLE) == 0);
500 static struct hlink *
501 hltlookup(struct stat *stp)
506 n = stp->st_ino & HLMASK;
508 for (hl = hltable[n]; hl; hl = hl->next) {
509 if (hl->ino == stp->st_ino) {
518 static struct hlink *
519 hltadd(struct stat *stp, const char *path)
522 int plen = strlen(path);
525 new = malloc(offsetof(struct hlink, name[plen + 1]));
527 fatal("out of memory");
530 /* initialize and link the new element into the table */
531 new->ino = stp->st_ino;
532 new->dino = (ino_t)-1;
534 bcopy(path, new->name, plen + 1);
537 n = stp->st_ino & HLMASK;
538 new->next = hltable[n];
540 hltable[n]->prev = new;
547 hltsetdino(struct hlink *hl, ino_t inum)
553 hltdelete(struct hlink *hl)
555 assert(hl->refs == 1);
559 hl->next->prev = hl->prev;
560 hl->prev->next = hl->next;
563 hl->next->prev = NULL;
565 hltable[hl->ino & HLMASK] = hl->next;
572 hltrels(struct hlink *hl)
574 assert(hl->refs == 1);
579 * If UseHLPath is defined check to see if the file in question is
580 * the same as the source file, and if it is return a pointer to the
581 * -H path based file for hardlinking. Else return NULL.
584 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
590 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
593 * stat info matches ?
595 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
596 st1->st_size != sthl.st_size ||
597 st1->st_mtime != sthl.st_mtime ||
598 !OwnerMatch(st1, &sthl) ||
599 !FlagsMatch(st1, &sthl)
606 * If ForceOpt or ValidateOpt is set we have to compare the files
608 if (ForceOpt || ValidateOpt) {
609 error = validate_check(spath, hpath);
619 * Return 0 if the contents of the file <spath> matches the contents of
623 validate_check(const char *spath, const char *dpath)
629 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
630 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
633 if (fd1 >= 0 && fd2 >= 0) {
636 char *iobuf1 = malloc(GETIOSIZE);
637 char *iobuf2 = malloc(GETIOSIZE);
639 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
640 CountSourceReadBytes += n;
641 x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
643 CountTargetReadBytes += x;
646 if (bcmp(iobuf1, iobuf2, n) != 0)
655 hc_close(&SrcHost, fd1);
657 hc_close(&DstHost, fd2);
662 DoCopy(copy_info_t info, struct stat *stat1, int depth)
664 const char *spath = info->spath;
665 const char *dpath = info->dpath;
666 dev_t sdevNo = info->sdevNo;
667 dev_t ddevNo = info->ddevNo;
670 unsigned long st2_flags;
671 int r, mres, fres, st2Valid;
675 r = mres = fres = st2Valid = 0;
681 if (hc_lstat(&SrcHost, spath, &st1) != 0) {
688 /* skip snapshot files because they're sparse and _huge_ */
689 if (stat1->st_flags & SF_SNAPSHOT)
692 st2.st_mode = 0; /* in case lstat fails */
693 st2.st_flags = 0; /* in case lstat fails */
694 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0) {
696 #ifdef _ST_FLAGS_PRESENT_
697 st2_flags = st2.st_flags;
701 if (S_ISREG(stat1->st_mode))
702 size = stat1->st_size;
708 if (S_ISREG(stat1->st_mode) && stat1->st_nlink > 1 && dpath) {
709 if ((hln = hltlookup(stat1)) != NULL) {
713 if (st2.st_ino == hln->dino) {
715 * hard link is already correct, nothing to do
718 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
719 if (hln->nlinked == stat1->st_nlink) {
728 * hard link is not correct, attempt to unlink it
730 if (xremove(&DstHost, dpath) < 0) {
731 logerr("%-32s hardlink: unable to unlink: %s\n",
732 ((dpath) ? dpath : spath), strerror(errno));
741 if (xlink(hln->name, dpath, stat1->st_flags) < 0) {
742 int tryrelink = (errno == EMLINK);
743 logerr("%-32s hardlink: unable to link to %s: %s\n",
744 (dpath ? dpath : spath), hln->name, strerror(errno)
749 logerr("%-20s hardlink: will attempt to copy normally\n");
754 if (hln->nlinked == stat1->st_nlink) {
760 logstd("%-32s hardlink: %s\n",
761 (dpath ? dpath : spath),
762 (st2Valid ? "relinked" : "linked")
773 * first instance of hardlink must be copied normally
776 hln = hltadd(stat1, dpath);
781 * Do we need to copy the file/dir/link/whatever? Early termination
782 * if we do not. Always redo links. Directories are always traversed
783 * except when the FSMID options are used.
785 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
790 && stat1->st_mode == st2.st_mode
791 && FlagsMatch(stat1, &st2)
793 if (S_ISLNK(stat1->st_mode) || S_ISDIR(stat1->st_mode)) {
795 * If FSMID tracking is turned on we can avoid recursing through
796 * an entire directory subtree if the FSMID matches.
798 #ifdef _ST_FSMID_PRESENT_
800 (UseFSMIDOpt && (fres = fsmid_check(stat1->st_fsmid, dpath)) == 0)
802 if (VerboseOpt >= 3) {
803 if (UseFSMIDOpt) /* always true!?! */
804 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
806 logstd("%-32s nochange\n", (dpath ? dpath : spath));
814 stat1->st_size == st2.st_size &&
815 (ValidateOpt == 2 || stat1->st_mtime == st2.st_mtime) &&
816 OwnerMatch(stat1, &st2)
818 && (UseMD5Opt == 0 || !S_ISREG(stat1->st_mode) ||
819 (mres = md5_check(spath, dpath)) == 0)
821 #ifdef _ST_FSMID_PRESENT_
822 && (UseFSMIDOpt == 0 ||
823 (fres = fsmid_check(stat1->st_fsmid, dpath)) == 0)
825 && (ValidateOpt == 0 || !S_ISREG(stat1->st_mode) ||
826 validate_check(spath, dpath) == 0)
829 * The files are identical, but if we are running as
830 * root we might need to adjust ownership/group/flags.
833 int changedflags = 0;
836 hltsetdino(hln, st2.st_ino);
838 if (!OwnerMatch(stat1, &st2)) {
839 hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
842 #ifdef _ST_FLAGS_PRESENT_
843 if (!FlagsMatch(stat1, &st2)) {
844 hc_chflags(&DstHost, dpath, stat1->st_flags);
848 if (VerboseOpt >= 3) {
851 logstd("%-32s md5-nochange",
852 (dpath ? dpath : spath));
856 logstd("%-32s fsmid-nochange",
857 (dpath ? dpath : spath));
858 } else if (ValidateOpt) {
859 logstd("%-32s nochange (contents validated)",
860 (dpath ? dpath : spath));
862 logstd("%-32s nochange", (dpath ? dpath : spath));
865 logstd(" (uid/gid differ)");
867 logstd(" (flags differ)");
870 CountSourceBytes += size;
877 if (st2Valid && !S_ISDIR(stat1->st_mode) && S_ISDIR(st2.st_mode)) {
879 logerr("%-32s SAFETY - refusing to copy file over directory\n",
880 (dpath ? dpath : spath)
884 goto done; /* continue with the cpdup anyway */
886 if (QuietOpt == 0 || AskConfirmation) {
887 logstd("%-32s WARNING: non-directory source will blow away\n"
888 "%-32s preexisting dest directory, continuing anyway!\n",
889 ((dpath) ? dpath : spath), "");
892 RemoveRecur(dpath, ddevNo, &st2);
897 * The various comparisons failed, copy it.
899 if (S_ISDIR(stat1->st_mode)) {
903 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
906 if (!st2Valid || S_ISDIR(st2.st_mode) == 0) {
908 xremove(&DstHost, dpath);
909 if (hc_mkdir(&DstHost, dpath, stat1->st_mode | 0700) != 0) {
910 logerr("%s: mkdir failed: %s\n",
911 (dpath ? dpath : spath), strerror(errno));
915 if (hc_lstat(&DstHost, dpath, &st2) != 0) {
916 logerr("%s: lstat of newly made dir failed: %s\n",
917 (dpath ? dpath : spath), strerror(errno));
924 if (!OwnerMatch(stat1, &st2) &&
925 hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid) != 0
927 logerr("%s: chown of newly made dir failed: %s\n",
928 (dpath ? dpath : spath), strerror(errno));
930 /* Note that we should not set skipdir = 1 here. */
936 * Directory must be scanable by root for cpdup to
937 * work. We'll fix it later if the directory isn't
938 * supposed to be readable ( which is why we fixup
939 * st2.st_mode to match what we did ).
941 if ((st2.st_mode & 0700) != 0700) {
942 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
946 logstd("%s\n", dpath ? dpath : spath);
951 * When copying a directory, stop if the source crosses a mount
954 if (sdevNo != (dev_t)-1 && stat1->st_dev != sdevNo)
957 sdevNo = stat1->st_dev;
960 * When copying a directory, stop if the destination crosses
963 * The target directory will have been created and stat'd
964 * for st2 if it did not previously exist. st2Valid is left
965 * as a flag. If the stat failed st2 will still only have its
966 * default initialization.
968 * So we simply assume here that the directory is within the
969 * current target mount if we had to create it (aka st2Valid is 0)
970 * and we leave ddevNo alone.
973 if (ddevNo != (dev_t)-1 && st2.st_dev != ddevNo)
980 List *list = malloc(sizeof(List));
984 logstd("Scanning %s ...\n", spath);
986 if (ScanDir(list, &SrcHost, spath, &CountSourceReadBytes, 0) == 0) {
988 while ((node = IterateList(list, node, 0)) != NULL) {
992 nspath = mprintf("%s/%s", spath, node->no_Name);
994 ndpath = mprintf("%s/%s", dpath, node->no_Name);
996 info->spath = nspath;
997 info->dpath = ndpath;
998 info->sdevNo = sdevNo;
999 info->ddevNo = ddevNo;
1001 r += DoCopy(info, node->no_Stat, depth);
1003 r += DoCopy(info, node->no_Stat, depth + 1);
1012 * Remove files/directories from destination that do not appear
1015 if (dpath && ScanDir(list, &DstHost, dpath,
1016 &CountTargetReadBytes, 3) == 0) {
1018 while ((node = IterateList(list, node, 3)) != NULL) {
1020 * If object does not exist in source or .cpignore
1021 * then recursively remove it.
1025 ndpath = mprintf("%s/%s", dpath, node->no_Name);
1026 RemoveRecur(ndpath, ddevNo, node->no_Stat);
1035 if (dpath && st2Valid) {
1036 struct timeval tv[2];
1038 if (ForceOpt || !OwnerMatch(stat1, &st2))
1039 hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
1040 if (stat1->st_mode != st2.st_mode)
1041 hc_chmod(&DstHost, dpath, stat1->st_mode);
1042 #ifdef _ST_FLAGS_PRESENT_
1043 if (!FlagsMatch(stat1, &st2))
1044 hc_chflags(&DstHost, dpath, stat1->st_flags);
1046 if (ForceOpt || stat1->st_mtime != st2.st_mtime) {
1047 bzero(tv, sizeof(tv));
1048 tv[0].tv_sec = stat1->st_mtime;
1049 tv[1].tv_sec = stat1->st_mtime;
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 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1094 * Not quite ready to do the copy yet. If UseHLPath is defined,
1095 * see if we can hardlink instead.
1097 * If we can hardlink, and the target exists, we have to remove it
1098 * first or the hardlink will fail. This can occur in a number of
1099 * situations but most typically when the '-f -H' combination is
1102 if (UseHLPath && (hpath = checkHLPath(stat1, spath, dpath)) != NULL) {
1104 xremove(&DstHost, dpath);
1105 if (hc_link(&DstHost, hpath, dpath) == 0) {
1108 logstd("%-32s hardlinked(-H)\n",
1109 (dpath ? dpath : spath));
1115 * Shucks, we may have hit a filesystem hard linking limit,
1116 * we have to copy instead.
1121 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1122 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1124 * There could be a .tmp file from a previously interrupted
1125 * run, delete and retry. Fail if we still can't get at it.
1127 #ifdef _ST_FLAGS_PRESENT_
1128 hc_chflags(&DstHost, path, 0);
1130 hc_remove(&DstHost, path);
1131 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1135 char *iobuf1 = malloc(GETIOSIZE);
1139 * Matt: What about holes?
1142 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1144 if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1148 hc_close(&DstHost, fd2);
1150 struct timeval tv[2];
1152 bzero(tv, sizeof(tv));
1153 tv[0].tv_sec = stat1->st_mtime;
1154 tv[1].tv_sec = stat1->st_mtime;
1156 if (DstRootPrivs || ChgrpAllowed(stat1->st_gid))
1157 hc_chown(&DstHost, path, stat1->st_uid, stat1->st_gid);
1158 hc_chmod(&DstHost, path, stat1->st_mode);
1159 #ifdef _ST_FLAGS_PRESENT_
1160 if (stat1->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
1161 hc_utimes(&DstHost, path, tv);
1163 hc_utimes(&DstHost, path, tv);
1165 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1166 logerr("%-32s rename-after-copy failed: %s\n",
1167 (dpath ? dpath : spath), strerror(errno)
1172 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1173 #ifdef _ST_FLAGS_PRESENT_
1174 if (DstRootPrivs ? stat1->st_flags : stat1->st_flags & UF_SETTABLE)
1175 hc_chflags(&DstHost, dpath, stat1->st_flags);
1178 #ifdef _ST_FLAGS_PRESENT_
1179 if ((stat1->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE)) == 0)
1180 hc_utimes(&DstHost, dpath, tv);
1182 CountSourceReadBytes += size;
1183 CountWriteBytes += size;
1184 CountSourceBytes += size;
1188 logerr("%-32s %s failed: %s\n",
1189 (dpath ? dpath : spath), op, strerror(errno)
1191 hc_remove(&DstHost, path);
1196 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1197 (dpath ? dpath : spath), getuid(), geteuid(),
1202 hc_close(&SrcHost, fd1);
1204 logerr("%-32s copy: open failed: %s\n",
1205 (dpath ? dpath : spath),
1214 if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1215 hltsetdino(hln, st2.st_ino);
1221 } else if (S_ISLNK(stat1->st_mode)) {
1222 char *link1 = malloc(GETLINKSIZE);
1223 char *link2 = malloc(GETLINKSIZE);
1228 n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1230 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1231 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1233 path = mprintf("%s", dpath);
1237 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1238 hc_umask(&DstHost, ~stat1->st_mode);
1239 xremove(&DstHost, path);
1241 if (hc_symlink(&DstHost, link1, path) < 0) {
1242 logerr("%-32s symlink (%s->%s) failed: %s\n",
1243 (dpath ? dpath : spath), link1, path,
1248 if (DstRootPrivs || ChgrpAllowed(stat1->st_gid))
1249 hc_lchown(&DstHost, path, stat1->st_uid, stat1->st_gid);
1251 * there is no lchmod() or lchflags(), we
1252 * cannot chmod or chflags a softlink.
1254 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1255 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1256 (dpath ? dpath : spath),
1257 path, dpath, strerror(errno));
1258 } else if (VerboseOpt) {
1259 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1261 hc_umask(&DstHost, 000);
1262 CountWriteBytes += n1;
1266 if (VerboseOpt >= 3)
1267 logstd("%-32s nochange", (dpath ? dpath : spath));
1268 if (!OwnerMatch(stat1, &st2)) {
1269 hc_lchown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
1270 if (VerboseOpt >= 3)
1271 logstd(" (uid/gid differ)");
1273 if (VerboseOpt >= 3)
1276 CountSourceBytes += n1;
1277 CountSourceReadBytes += n1;
1279 CountTargetReadBytes += n2;
1283 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1288 } else if ((S_ISCHR(stat1->st_mode) || S_ISBLK(stat1->st_mode)) && DeviceOpt) {
1293 stat1->st_mode != st2.st_mode ||
1294 stat1->st_rdev != st2.st_rdev ||
1295 !OwnerMatch(stat1, &st2)
1298 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1299 xremove(&DstHost, path);
1301 path = mprintf("%s", dpath);
1304 if (hc_mknod(&DstHost, path, stat1->st_mode, stat1->st_rdev) == 0) {
1305 hc_chmod(&DstHost, path, stat1->st_mode);
1306 hc_chown(&DstHost, path, stat1->st_uid, stat1->st_gid);
1308 xremove(&DstHost, dpath);
1309 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1310 logerr("%-32s dev-rename-after-create failed: %s\n",
1311 (dpath ? dpath : spath),
1314 } else if (VerboseOpt) {
1315 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1320 logerr("%-32s dev failed: %s\n",
1321 (dpath ? dpath : spath), strerror(errno)
1325 if (VerboseOpt >= 3)
1326 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1334 if (hln->dino == (ino_t)-1) {
1336 /*hln = NULL; unneeded */
1345 ScanDir(List *list, struct HostConf *host, const char *path,
1346 int64_t *CountReadBytes, int n)
1349 struct HCDirEntry *den;
1350 struct stat *statptr;
1354 * scan .cpignore file for files/directories to ignore
1355 * (only in the source directory, i.e. if n == 0).
1361 char *buf = malloc(GETBUFSIZE);
1365 if (UseCpFile[0] == '/') {
1366 fpath = mprintf("%s", UseCpFile);
1368 fpath = mprintf("%s/%s", path, UseCpFile);
1370 AddList(list, strrchr(fpath, '/') + 1, 1, NULL);
1371 if ((fd = hc_open(host, fpath, O_RDONLY, 0)) >= 0) {
1373 while ((nread = hc_read(host, fd, buf + bufused,
1374 GETBUFSIZE - bufused - 1)) > 0) {
1375 *CountReadBytes += nread;
1378 for (next = buf; (nl = strchr(next, '\n')); next = nl+1) {
1380 AddList(list, next, 1, NULL);
1382 bufused = strlen(next);
1384 bcopy(next, buf, bufused);
1387 /* last line has no trailing newline */
1389 AddList(list, buf, 1, NULL);
1398 * Automatically exclude MD5CacheFile that we create on the
1399 * source from the copy to the destination.
1401 * Automatically exclude a FSMIDCacheFile on the source that
1402 * would otherwise overwrite the one we maintain on the target.
1405 AddList(list, MD5CacheFile, 1, NULL);
1407 AddList(list, FSMIDCacheFile, 1, NULL);
1410 if ((dir = hc_opendir(host, path)) == NULL)
1412 while ((den = hc_readdir(host, dir, &statptr)) != NULL) {
1416 if (strcmp(den->d_name, ".") != 0 && strcmp(den->d_name, "..") != 0)
1417 AddList(list, den->d_name, n, statptr);
1419 hc_closedir(host, dir);
1429 RemoveRecur(const char *dpath, dev_t devNo, struct stat *dstat)
1433 if (dstat == NULL) {
1434 if (hc_lstat(&DstHost, dpath, &st) == 0)
1437 if (dstat != NULL) {
1438 if (devNo == (dev_t)-1)
1439 devNo = dstat->st_dev;
1440 if (dstat->st_dev == devNo) {
1441 if (S_ISDIR(dstat->st_mode)) {
1444 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1445 List *list = malloc(sizeof(List));
1447 struct HCDirEntry *den;
1450 while ((den = hc_readdir(&DstHost, dir, &dstat)) != NULL) {
1451 if (strcmp(den->d_name, ".") == 0)
1453 if (strcmp(den->d_name, "..") == 0)
1455 AddList(list, den->d_name, 3, dstat);
1457 hc_closedir(&DstHost, dir);
1458 while ((node = IterateList(list, node, 3)) != NULL) {
1461 ndpath = mprintf("%s/%s", dpath, node->no_Name);
1462 RemoveRecur(ndpath, devNo, node->no_Stat);
1468 if (AskConfirmation && NoRemoveOpt == 0) {
1470 if (hc_rmdir(&DstHost, dpath) < 0) {
1471 logerr("%-32s rmdir failed: %s\n",
1472 dpath, strerror(errno)
1475 CountRemovedItems++;
1480 logstd("%-32s not-removed\n", dpath);
1481 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1483 logstd("%-32s rmdir-ok\n", dpath);
1484 CountRemovedItems++;
1486 logerr("%-32s rmdir failed: %s\n",
1487 dpath, strerror(errno)
1492 if (AskConfirmation && NoRemoveOpt == 0) {
1494 if (xremove(&DstHost, dpath) < 0) {
1495 logerr("%-32s remove failed: %s\n",
1496 dpath, strerror(errno)
1499 CountRemovedItems++;
1504 logstd("%-32s not-removed\n", dpath);
1505 } else if (xremove(&DstHost, dpath) == 0) {
1507 logstd("%-32s remove-ok\n", dpath);
1508 CountRemovedItems++;
1510 logerr("%-32s remove failed: %s\n",
1511 dpath, strerror(errno)
1521 InitList(List *list)
1523 bzero(list, sizeof(List));
1524 list->li_Node.no_Next = &list->li_Node;
1528 ResetList(List *list)
1532 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1533 list->li_Node.no_Next = node->no_Next;
1534 if (node->no_Stat != NULL)
1535 free(node->no_Stat);
1542 IterateList(List *list, Node *node, int n)
1545 node = list->li_Node.no_Next;
1547 node = node->no_Next;
1548 while (node->no_Value != n && node != &list->li_Node)
1549 node = node->no_Next;
1550 return (node == &list->li_Node ? NULL : node);
1554 AddList(List *list, const char *name, int n, struct stat *st)
1560 * Scan against wildcards. Only a node value of 1 can be a wildcard
1561 * ( usually scanned from .cpignore )
1564 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1565 if (strcmp(name, node->no_Name) == 0 ||
1566 (n != 1 && node->no_Value == 1 &&
1567 fnmatch(node->no_Name, name, 0) == 0)
1569 return(node->no_Value);
1574 * Look for exact match
1578 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1579 if (strcmp(name, node->no_Name) == 0) {
1580 return(node->no_Value);
1583 node = malloc(sizeof(Node) + strlen(name) + 1);
1585 fatal("out of memory");
1587 node->no_Next = list->li_Node.no_Next;
1588 list->li_Node.no_Next = node;
1590 node->no_HNext = list->li_Hash[hv];
1591 list->li_Hash[hv] = node;
1593 strcpy(node->no_Name, name);
1601 shash(const char *s)
1608 if (*s == '*' || *s == '?' ||
1609 *s == '{' || *s == '}' ||
1610 *s == '[' || *s == ']' ||
1615 hv = (hv << 5) ^ *s ^ (hv >> 23);
1618 return(((hv >> 16) ^ hv) & HMASK);
1622 YesNo(const char *path)
1626 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1629 first = ch = getchar();
1630 while (ch != '\n' && ch != EOF)
1632 return ((first == 'y' || first == 'Y'));
1636 * xrename() - rename with override
1638 * If the rename fails, attempt to override st_flags on the
1639 * destination and rename again. If that fails too, try to
1640 * set the flags back the way they were and give up.
1644 xrename(const char *src, const char *dst, u_long flags)
1648 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1649 #ifdef _ST_FLAGS_PRESENT_
1650 hc_chflags(&DstHost, dst, 0);
1651 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1652 hc_chflags(&DstHost, dst, flags);
1659 xlink(const char *src, const char *dst, u_long flags)
1662 #ifdef _ST_FLAGS_PRESENT_
1666 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1667 #ifdef _ST_FLAGS_PRESENT_
1668 hc_chflags(&DstHost, src, 0);
1669 r = hc_link(&DstHost, src, dst);
1671 hc_chflags(&DstHost, src, flags);
1681 xremove(struct HostConf *host, const char *path)
1685 res = hc_remove(host, path);
1686 #ifdef _ST_FLAGS_PRESENT_
1687 if (res == -EPERM) {
1688 hc_chflags(host, path, 0);
1689 res = hc_remove(host, path);