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 MAXDEPTH 32 /* max copy depth for thread */
68 #define GETBUFSIZE 8192
69 #define GETPATHSIZE 2048
70 #define GETLINKSIZE 1024
71 #define GETIOSIZE 65536
73 #ifndef _ST_FLAGS_PRESENT_
74 #define st_flags st_mode
79 struct Node *no_HNext;
99 typedef struct copy_info {
105 struct copy_info *parent;
112 struct hlink *hltable[HLSIZE];
114 void RemoveRecur(const char *dpath, dev_t devNo);
115 void InitList(List *list);
116 void ResetList(List *list);
117 int AddList(List *list, const char *name, int n);
118 static struct hlink *hltlookup(struct stat *);
119 static struct hlink *hltadd(struct stat *, const char *);
120 static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
121 static int validate_check(const char *spath, const char *dpath);
122 static int shash(const char *s);
123 static void hltdelete(struct hlink *);
124 static void hltsetdino(struct hlink *, ino_t);
125 int YesNo(const char *path);
126 static int xrename(const char *src, const char *dst, u_long flags);
127 static int xlink(const char *src, const char *dst, u_long flags);
128 static int xremove(struct HostConf *host, const char *path);
129 int WildCmp(const char *s1, const char *s2);
130 static int DoCopy(copy_info_t info, int depth);
132 int AskConfirmation = 1;
144 int EnableDirectoryRetries;
148 int MaxParallel = -1;
151 const char *ssh_argv[16];
154 const char *UseCpFile;
155 const char *UseHLPath;
156 const char *MD5CacheFile;
157 const char *FSMIDCacheFile;
159 int64_t CountSourceBytes;
160 int64_t CountSourceItems;
161 int64_t CountCopiedItems;
162 int64_t CountSourceReadBytes;
163 int64_t CountTargetReadBytes;
164 int64_t CountWriteBytes;
165 int64_t CountRemovedItems;
166 int64_t CountLinkedItems;
168 struct HostConf SrcHost;
169 struct HostConf DstHost;
172 pthread_mutex_t MasterMutex;
176 main(int ac, char **av)
182 struct timeval start;
183 struct copy_info info;
185 signal(SIGPIPE, SIG_IGN);
187 RunningAsUser = (geteuid() != 0);
188 RunningAsRoot = !RunningAsUser;
191 for (i = 0; i < HCTHASH_SIZE; ++i) {
192 pthread_mutex_init(&SrcHost.hct_mutex[i], NULL);
193 pthread_mutex_init(&DstHost.hct_mutex[i], NULL);
195 pthread_mutex_init(&MasterMutex, NULL);
196 pthread_mutex_lock(&MasterMutex);
199 gettimeofday(&start, NULL);
200 for (i = 1; i < ac; ++i) {
207 } else if (dst == NULL) {
210 fatal("too many arguments");
218 v = strtol(ptr, NULL, 0);
226 while (*ptr == 'v') {
230 if (*ptr >= '0' && *ptr <= '9')
231 VerboseOpt = strtol(ptr, NULL, 0);
247 UseCpFile = ".cpignore";
250 UseCpFile = (*ptr) ? ptr : av[++i];
253 UseHLPath = (*ptr) ? ptr : av[++i];
257 fatal("too many -F options");
258 ssh_argv[ssh_argc++] = (*ptr) ? ptr : av[++i];
283 FSMIDCacheFile = ".FSMID.CHECK";
287 FSMIDCacheFile = av[++i];
291 MD5CacheFile = av[++i];
295 MD5CacheFile = ".MD5.CHECKSUMS";
298 setvbuf(stdout, NULL, _IOLBF, 0);
301 fatal("illegal option: %s\n", ptr - 2);
308 * If we are told to go into slave mode, run the HC protocol
316 * Extract the source and/or/neither target [user@]host and
317 * make any required connections.
319 if (src && (ptr = strchr(src, ':')) != NULL) {
320 asprintf(&SrcHost.host, "%*.*s", (int)(ptr - src), (int)(ptr - src), src);
323 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
327 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
330 if (hc_connect(&SrcHost) < 0)
333 if (dst && (ptr = strchr(dst, ':')) != NULL) {
334 asprintf(&DstHost.host, "%*.*s", (int)(ptr - dst), (int)(ptr - dst), dst);
337 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
340 if (hc_connect(&DstHost) < 0)
345 * dst may be NULL only if -m option is specified,
346 * which forces an update of the MD5 checksums
348 if (dst == NULL && UseMD5Opt == 0) {
352 bzero(&info, sizeof(info));
356 pthread_cond_init(&info.cond, NULL);
359 DstBaseLen = strlen(dst);
362 info.sdevNo = (dev_t)-1;
363 info.ddevNo = (dev_t)-1;
364 i = DoCopy(&info, -1);
368 info.sdevNo = (dev_t)-1;
369 info.ddevNo = (dev_t)-1;
370 i = DoCopy(&info, -1);
373 pthread_cond_destroy(&info.cond);
380 if (SummaryOpt && i == 0) {
384 gettimeofday(&end, NULL);
386 /* don't count stat's in our byte statistics */
387 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
388 CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
389 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
390 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
393 duration = (end.tv_sec - start.tv_sec);
394 duration += (double)(end.tv_usec - start.tv_usec) / 1000000.0;
397 logstd("cpdup completed successfully\n");
398 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
399 "%lld bytes written (%.1fX speedup)\n",
400 (long long)CountSourceBytes,
401 (long long)CountSourceReadBytes,
402 (long long)CountTargetReadBytes,
403 (long long)CountWriteBytes,
404 ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
405 logstd("%lld source items, %lld items copied, %lld items linked, "
406 "%lld things deleted\n",
407 (long long)CountSourceItems,
408 (long long)CountCopiedItems,
409 (long long)CountLinkedItems,
410 (long long)CountRemovedItems);
411 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
413 (int)((CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration / 1024.0),
414 (int)(CountSourceBytes / duration / 1024.0));
416 exit((i == 0) ? 0 : 1);
419 static struct hlink *
420 hltlookup(struct stat *stp)
423 struct timespec ts = { 0, 100000 };
428 n = stp->st_ino & HLMASK;
433 for (hl = hltable[n]; hl; hl = hl->next) {
434 if (hl->ino == stp->st_ino) {
437 * If the hl entry is still in the process of being created
438 * by another thread we have to wait until it has either been
439 * deleted or completed.
442 pthread_mutex_unlock(&MasterMutex);
443 nanosleep(&ts, NULL);
444 pthread_mutex_lock(&MasterMutex);
456 static struct hlink *
457 hltadd(struct stat *stp, const char *path)
460 int plen = strlen(path);
463 new = malloc(offsetof(struct hlink, name[plen + 1]));
465 fprintf(stderr, "out of memory\n");
470 /* initialize and link the new element into the table */
471 new->ino = stp->st_ino;
472 new->dino = (ino_t)-1;
474 bcopy(path, new->name, plen + 1);
477 n = stp->st_ino & HLMASK;
478 new->next = hltable[n];
480 hltable[n]->prev = new;
487 hltsetdino(struct hlink *hl, ino_t inum)
493 hltdelete(struct hlink *hl)
495 assert(hl->refs == 1);
499 hl->next->prev = hl->prev;
500 hl->prev->next = hl->next;
503 hl->next->prev = NULL;
505 hltable[hl->ino & HLMASK] = hl->next;
512 hltrels(struct hlink *hl)
514 assert(hl->refs == 1);
519 * If UseHLPath is defined check to see if the file in question is
520 * the same as the source file, and if it is return a pointer to the
521 * -H path based file for hardlinking. Else return NULL.
524 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
530 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
533 * stat info matches ?
535 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
536 st1->st_size != sthl.st_size ||
537 st1->st_mtime != sthl.st_mtime ||
538 (RunningAsRoot && (st1->st_uid != sthl.st_uid ||
539 st1->st_gid != sthl.st_gid))
546 * If ForceOpt or ValidateOpt is set we have to compare the files
548 if (ForceOpt || ValidateOpt) {
549 error = validate_check(spath, hpath);
559 * Return 0 if the contents of the file <spath> matches the contents of
563 validate_check(const char *spath, const char *dpath)
569 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
570 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
573 if (fd1 >= 0 && fd2 >= 0) {
576 char *iobuf1 = malloc(GETIOSIZE);
577 char *iobuf2 = malloc(GETIOSIZE);
579 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
580 CountSourceReadBytes += n;
581 x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
583 CountTargetReadBytes += x;
586 if (bcmp(iobuf1, iobuf2, n) != 0)
595 hc_close(&SrcHost, fd1);
597 hc_close(&DstHost, fd2);
603 DoCopyThread(void *arg)
605 copy_info_t cinfo = arg;
606 char *spath = cinfo->spath;
607 char *dpath = cinfo->dpath;
610 r = pthread_detach(pthread_self());
612 pthread_cond_init(&cinfo->cond, NULL);
613 pthread_mutex_lock(&MasterMutex);
614 cinfo->r += DoCopy(cinfo, 0);
615 /* cinfo arguments invalid on return */
616 --cinfo->parent->children;
618 pthread_cond_signal(&cinfo->parent->cond);
622 pthread_cond_destroy(&cinfo->cond);
624 hcc_free_trans(&SrcHost);
625 hcc_free_trans(&DstHost);
626 pthread_mutex_unlock(&MasterMutex);
633 DoCopy(copy_info_t info, int depth)
635 const char *spath = info->spath;
636 const char *dpath = info->dpath;
637 dev_t sdevNo = info->sdevNo;
638 dev_t ddevNo = info->ddevNo;
641 unsigned long st2_flags;
642 int r, mres, fres, st2Valid;
644 List *list = malloc(sizeof(List));
648 r = mres = fres = st2Valid = 0;
653 if (hc_lstat(&SrcHost, spath, &st1) != 0) {
658 /* skip snapshot files because they're sparse and _huge_ */
659 if (st1.st_flags & SF_SNAPSHOT)
662 st2.st_mode = 0; /* in case lstat fails */
663 st2.st_flags = 0; /* in case lstat fails */
664 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0) {
666 #ifdef _ST_FLAGS_PRESENT_
667 st2_flags = st2.st_flags;
671 if (S_ISREG(st1.st_mode)) {
679 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
680 if ((hln = hltlookup(&st1)) != NULL) {
684 if (st2.st_ino == hln->dino) {
686 * hard link is already correct, nothing to do
689 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
690 if (hln->nlinked == st1.st_nlink) {
699 * hard link is not correct, attempt to unlink it
701 if (xremove(&DstHost, dpath) < 0) {
702 logerr("%-32s hardlink: unable to unlink: %s\n",
703 ((dpath) ? dpath : spath), strerror(errno));
712 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
713 int tryrelink = (errno == EMLINK);
714 logerr("%-32s hardlink: unable to link to %s: %s\n",
715 (dpath ? dpath : spath), hln->name, strerror(errno)
720 logerr("%-20s hardlink: will attempt to copy normally\n");
725 if (hln->nlinked == st1.st_nlink) {
731 logstd("%-32s hardlink: %s\n",
732 (dpath ? dpath : spath),
733 (st2Valid ? "relinked" : "linked")
744 * first instance of hardlink must be copied normally
747 hln = hltadd(&st1, dpath);
752 * Do we need to copy the file/dir/link/whatever? Early termination
753 * if we do not. Always redo links. Directories are always traversed
754 * except when the FSMID options are used.
756 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
761 && st1.st_mode == st2.st_mode
762 #ifdef _ST_FLAGS_PRESENT_
763 && (RunningAsUser || st1.st_flags == st2.st_flags)
766 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
768 * If FSMID tracking is turned on we can avoid recursing through
769 * an entire directory subtree if the FSMID matches.
771 #ifdef _ST_FSMID_PRESENT_
773 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
775 if (VerboseOpt >= 3) {
777 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
779 logstd("%-32s nochange\n", (dpath ? dpath : spath));
787 st1.st_size == st2.st_size &&
788 st1.st_mtime == st2.st_mtime &&
789 (RunningAsUser || (st1.st_uid == st2.st_uid &&
790 st1.st_gid == st2.st_gid))
792 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
793 (mres = md5_check(spath, dpath)) == 0)
795 #ifdef _ST_FSMID_PRESENT_
796 && (UseFSMIDOpt == 0 ||
797 (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
799 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
800 validate_check(spath, dpath) == 0)
803 * The files are identical, but if we are running as
804 * root we might need to adjust ownership/group/flags.
807 int changedflags = 0;
808 #ifdef _ST_FLAGS_PRESENT_
813 hltsetdino(hln, st2.st_ino);
815 if (RunningAsRoot && (st1.st_uid != st2.st_uid ||
816 st1.st_gid != st2.st_gid)) {
817 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
820 #ifdef _ST_FLAGS_PRESENT_
821 /* XXX also check secure-level and jail status? */
822 mask = (RunningAsRoot) ? (u_int32_t)-1 : UF_SETTABLE;
823 if ((st1.st_flags & mask) != (st2.st_flags & mask)) {
824 hc_chflags(&DstHost, dpath, st1.st_flags);
828 if (VerboseOpt >= 3) {
831 logstd("%-32s md5-nochange",
832 (dpath ? dpath : spath));
836 logstd("%-32s fsmid-nochange",
837 (dpath ? dpath : spath));
838 } else if (ValidateOpt) {
839 logstd("%-32s nochange (contents validated)",
840 (dpath ? dpath : spath));
842 logstd("%-32s nochange", (dpath ? dpath : spath));
845 logstd(" (uid/gid differ)");
847 logstd(" (flags differ)");
850 CountSourceBytes += size;
857 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
859 logerr("%-32s SAFETY - refusing to copy file over directory\n",
860 (dpath ? dpath : spath)
864 goto done; /* continue with the cpdup anyway */
866 if (QuietOpt == 0 || AskConfirmation) {
867 logstd("%-32s WARNING: non-directory source will blow away\n"
868 "%-32s preexisting dest directory, continuing anyway!\n",
869 ((dpath) ? dpath : spath), "");
872 RemoveRecur(dpath, ddevNo);
877 * The various comparisons failed, copy it.
879 if (S_ISDIR(st1.st_mode)) {
883 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
884 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
889 if (S_ISDIR(st2.st_mode) == 0) {
890 xremove(&DstHost, dpath);
891 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
892 logerr("%s: mkdir failed: %s\n",
893 (dpath ? dpath : spath), strerror(errno));
899 * Matt: why don't you check error codes here?
900 * (Because I'm an idiot... checks added!)
902 if (hc_lstat(&DstHost, dpath, &st2) != 0) {
903 logerr("%s: lstat of newly made dir failed: %s\n",
904 (dpath ? dpath : spath), strerror(errno));
908 if (hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid) != 0){
909 logerr("%s: chown of newly made dir failed: %s\n",
910 (dpath ? dpath : spath), strerror(errno));
917 * Directory must be scanable by root for cpdup to
918 * work. We'll fix it later if the directory isn't
919 * supposed to be readable ( which is why we fixup
920 * st2.st_mode to match what we did ).
922 if ((st2.st_mode & 0700) != 0700) {
923 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
927 logstd("%s\n", dpath ? dpath : spath);
932 * When copying a directory, stop if the source crosses a mount
935 if (sdevNo != (dev_t)-1 && st1.st_dev != sdevNo) {
942 * When copying a directory, stop if the destination crosses
945 * The target directory will have been created and stat'd
946 * for st2 if it did not previously exist. st2Valid is left
947 * as a flag. If the stat failed st2 will still only have its
948 * default initialization.
950 * So we simply assume here that the directory is within the
951 * current target mount if we had to create it (aka st2Valid is 0)
952 * and we leave ddevNo alone.
955 if (ddevNo != (dev_t)-1 && st2.st_dev != ddevNo) {
963 * scan .cpignore file for files/directories
969 char *buf = malloc(GETBUFSIZE);
972 if (UseCpFile[0] == '/') {
973 fpath = mprintf("%s", UseCpFile);
975 fpath = mprintf("%s/%s", spath, UseCpFile);
977 AddList(list, strrchr(fpath, '/') + 1, 1);
978 if ((fi = fopen(fpath, "r")) != NULL) {
979 while (fgets(buf, GETBUFSIZE, fi) != NULL) {
981 CountSourceReadBytes += l;
982 if (l && buf[l-1] == '\n')
985 AddList(list, buf, 1);
994 * Automatically exclude MD5CacheFile that we create on the
995 * source from the copy to the destination.
997 * Automatically exclude a FSMIDCacheFile on the source that
998 * would otherwise overwrite the one we maintain on the target.
1001 AddList(list, MD5CacheFile, 1);
1003 AddList(list, FSMIDCacheFile, 1);
1005 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
1010 char *ndpath = NULL;
1012 if (strcmp(den->d_name, ".") == 0 ||
1013 strcmp(den->d_name, "..") == 0
1018 * ignore if on .cpignore list
1020 if (AddList(list, den->d_name, 0) == 1) {
1023 nspath = mprintf("%s/%s", spath, den->d_name);
1025 ndpath = mprintf("%s/%s", dpath, den->d_name);
1028 if (CurParallel < MaxParallel || depth > MAXDEPTH) {
1029 copy_info_t cinfo = malloc(sizeof(*cinfo));
1030 pthread_t dummy_thr;
1032 bzero(cinfo, sizeof(*cinfo));
1033 cinfo->spath = nspath;
1034 cinfo->dpath = ndpath;
1035 cinfo->sdevNo = sdevNo;
1036 cinfo->ddevNo = ddevNo;
1037 cinfo->parent = info;
1040 pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
1044 info->spath = nspath;
1045 info->dpath = ndpath;
1046 info->sdevNo = sdevNo;
1047 info->ddevNo = ddevNo;
1049 r += DoCopy(info, depth);
1051 r += DoCopy(info, depth + 1);
1060 hc_closedir(&SrcHost, dir);
1064 * Wait for our children to finish
1066 while (info->children) {
1067 pthread_cond_wait(&info->cond, &MasterMutex);
1074 * Remove files/directories from destination that do not appear
1077 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
1078 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
1082 if (strcmp(den->d_name, ".") == 0 ||
1083 strcmp(den->d_name, "..") == 0
1088 * If object does not exist in source or .cpignore
1089 * then recursively remove it.
1091 if (AddList(list, den->d_name, 3) == 3) {
1094 ndpath = mprintf("%s/%s", dpath, den->d_name);
1095 RemoveRecur(ndpath, ddevNo);
1099 hc_closedir(&DstHost, dir);
1103 struct timeval tv[2];
1107 st1.st_uid != st2.st_uid ||
1108 st1.st_gid != st2.st_gid
1110 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
1112 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
1113 hc_chmod(&DstHost, dpath, st1.st_mode);
1115 #ifdef _ST_FLAGS_PRESENT_
1116 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
1117 hc_chflags(&DstHost, dpath, st1.st_flags);
1122 st1.st_mtime != st2.st_mtime
1124 bzero(tv, sizeof(tv));
1125 tv[0].tv_sec = st1.st_mtime;
1126 tv[1].tv_sec = st1.st_mtime;
1127 hc_utimes(&DstHost, dpath, tv);
1131 } else if (dpath == NULL) {
1133 * If dpath is NULL, we are just updating the MD5
1136 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
1137 mres = md5_check(spath, NULL);
1139 if (VerboseOpt > 1) {
1141 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1143 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
1144 } else if (!QuietOpt && mres < 0) {
1145 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1149 } else if (S_ISREG(st1.st_mode)) {
1156 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1158 path = mprintf("%s", dpath);
1161 * Handle check failure message.
1165 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1169 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1172 * Not quite ready to do the copy yet. If UseHLPath is defined,
1173 * see if we can hardlink instead.
1175 * If we can hardlink, and the target exists, we have to remove it
1176 * first or the hardlink will fail. This can occur in a number of
1177 * situations but must typically when the '-f -H' combination is
1180 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1182 xremove(&DstHost, dpath);
1183 if (hc_link(&DstHost, hpath, dpath) == 0) {
1186 logstd("%-32s hardlinked(-H)\n",
1187 (dpath ? dpath : spath));
1193 * Shucks, we may have hit a filesystem hard linking limit,
1194 * we have to copy instead.
1199 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1200 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1202 * There could be a .tmp file from a previously interrupted
1203 * run, delete and retry. Fail if we still can't get at it.
1205 #ifdef _ST_FLAGS_PRESENT_
1206 hc_chflags(&DstHost, path, 0);
1208 hc_remove(&DstHost, path);
1209 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1213 char *iobuf1 = malloc(GETIOSIZE);
1217 * Matt: What about holes?
1220 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1222 if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1226 hc_close(&DstHost, fd2);
1228 struct timeval tv[2];
1230 bzero(tv, sizeof(tv));
1231 tv[0].tv_sec = st1.st_mtime;
1232 tv[1].tv_sec = st1.st_mtime;
1234 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1235 hc_chmod(&DstHost, path, st1.st_mode);
1236 #ifdef _ST_FLAGS_PRESENT_
1237 if (st1.st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
1238 hc_utimes(&DstHost, path, tv);
1240 hc_utimes(&DstHost, path, tv);
1242 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1243 logerr("%-32s rename-after-copy failed: %s\n",
1244 (dpath ? dpath : spath), strerror(errno)
1249 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1250 #ifdef _ST_FLAGS_PRESENT_
1252 hc_chflags(&DstHost, dpath, st1.st_flags);
1255 #ifdef _ST_FLAGS_PRESENT_
1256 if ((st1.st_flags & (UF_IMMUTABLE|SF_IMMUTABLE)) == 0)
1257 hc_utimes(&DstHost, dpath, tv);
1259 CountSourceReadBytes += size;
1260 CountWriteBytes += size;
1261 CountSourceBytes += size;
1265 logerr("%-32s %s failed: %s\n",
1266 (dpath ? dpath : spath), op, strerror(errno)
1268 hc_remove(&DstHost, path);
1273 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1274 (dpath ? dpath : spath), getuid(), geteuid(),
1279 hc_close(&SrcHost, fd1);
1281 logerr("%-32s copy: open failed: %s\n",
1282 (dpath ? dpath : spath),
1291 if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1292 hltsetdino(hln, st2.st_ino);
1298 } else if (S_ISLNK(st1.st_mode)) {
1299 char *link1 = malloc(GETLINKSIZE);
1300 char *link2 = malloc(GETLINKSIZE);
1305 n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1307 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1308 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1310 path = mprintf("%s", dpath);
1314 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1315 hc_umask(&DstHost, ~st1.st_mode);
1316 xremove(&DstHost, path);
1318 if (hc_symlink(&DstHost, link1, path) < 0) {
1319 logerr("%-32s symlink (%s->%s) failed: %s\n",
1320 (dpath ? dpath : spath), link1, path,
1325 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1327 * there is no lchmod() or lchflags(), we
1328 * cannot chmod or chflags a softlink.
1330 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1331 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1332 (dpath ? dpath : spath),
1333 path, dpath, strerror(errno));
1334 } else if (VerboseOpt) {
1335 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1337 hc_umask(&DstHost, 000);
1338 CountWriteBytes += n1;
1342 if (VerboseOpt >= 3)
1343 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1345 CountSourceBytes += n1;
1346 CountSourceReadBytes += n1;
1348 CountTargetReadBytes += n2;
1352 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1357 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1362 st1.st_mode != st2.st_mode ||
1363 st1.st_rdev != st2.st_rdev ||
1364 st1.st_uid != st2.st_uid ||
1365 st1.st_gid != st2.st_gid
1368 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1369 xremove(&DstHost, path);
1371 path = mprintf("%s", dpath);
1374 if (hc_mknod(&DstHost, path, st1.st_mode, st1.st_rdev) == 0) {
1375 hc_chmod(&DstHost, path, st1.st_mode);
1376 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1378 xremove(&DstHost, dpath);
1379 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1380 logerr("%-32s dev-rename-after-create failed: %s\n",
1381 (dpath ? dpath : spath),
1384 } else if (VerboseOpt) {
1385 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1390 logerr("%-32s dev failed: %s\n",
1391 (dpath ? dpath : spath), strerror(errno)
1395 if (VerboseOpt >= 3)
1396 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1404 if (hln->dino == (ino_t)-1) {
1406 /*hln = NULL; unneeded */
1421 RemoveRecur(const char *dpath, dev_t devNo)
1425 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1426 if (devNo == (dev_t)-1)
1428 if (st.st_dev == devNo) {
1429 if (S_ISDIR(st.st_mode)) {
1432 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1434 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1437 if (strcmp(den->d_name, ".") == 0)
1439 if (strcmp(den->d_name, "..") == 0)
1441 ndpath = mprintf("%s/%s", dpath, den->d_name);
1442 RemoveRecur(ndpath, devNo);
1445 hc_closedir(&DstHost, dir);
1447 if (AskConfirmation && NoRemoveOpt == 0) {
1449 if (hc_rmdir(&DstHost, dpath) < 0) {
1450 logerr("%-32s rmdir failed: %s\n",
1451 dpath, strerror(errno)
1454 CountRemovedItems++;
1459 logstd("%-32s not-removed\n", dpath);
1460 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1462 logstd("%-32s rmdir-ok\n", dpath);
1463 CountRemovedItems++;
1465 logerr("%-32s rmdir failed: %s\n",
1466 dpath, strerror(errno)
1471 if (AskConfirmation && NoRemoveOpt == 0) {
1473 if (xremove(&DstHost, dpath) < 0) {
1474 logerr("%-32s remove failed: %s\n",
1475 dpath, strerror(errno)
1478 CountRemovedItems++;
1483 logstd("%-32s not-removed\n", dpath);
1484 } else if (xremove(&DstHost, dpath) == 0) {
1486 logstd("%-32s remove-ok\n", dpath);
1487 CountRemovedItems++;
1489 logerr("%-32s remove failed: %s\n",
1490 dpath, strerror(errno)
1500 InitList(List *list)
1502 bzero(list, sizeof(List));
1503 list->li_Node.no_Next = &list->li_Node;
1507 ResetList(List *list)
1511 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1512 list->li_Node.no_Next = node->no_Next;
1519 AddList(List *list, const char *name, int n)
1527 * Scan against wildcards. Only a node value of 1 can be a wildcard
1528 * ( usually scanned from .cpignore )
1531 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1532 if (strcmp(name, node->no_Name) == 0 ||
1533 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1535 return(node->no_Value);
1540 * Look for exact match
1543 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1544 if (strcmp(name, node->no_Name) == 0) {
1545 return(node->no_Value);
1548 node = malloc(sizeof(Node) + strlen(name) + 1);
1550 fprintf(stderr, "out of memory\n");
1554 node->no_Next = list->li_Node.no_Next;
1555 list->li_Node.no_Next = node;
1557 node->no_HNext = list->li_Hash[hv];
1558 list->li_Hash[hv] = node;
1560 strcpy(node->no_Name, name);
1567 shash(const char *s)
1574 if (*s == '*' || *s == '?' ||
1575 *s == '{' || *s == '}' ||
1576 *s == '[' || *s == ']' ||
1581 hv = (hv << 5) ^ *s ^ (hv >> 23);
1584 return(((hv >> 16) ^ hv) & HMASK);
1588 * WildCmp() - compare wild string to sane string
1590 * Return 0 on success, -1 on failure.
1594 WildCmp(const char *w, const char *s)
1597 * skip fixed portion
1603 if (w[1] == 0) /* optimize wild* case */
1609 for (i = 0; i <= l; ++i) {
1610 if (WildCmp(w + 1, s + i) == 0)
1624 if (*w == 0) /* terminator */
1636 YesNo(const char *path)
1640 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1643 first = ch = getchar();
1644 while (ch != '\n' && ch != EOF)
1646 return ((first == 'y' || first == 'Y'));
1650 * xrename() - rename with override
1652 * If the rename fails, attempt to override st_flags on the
1653 * destination and rename again. If that fails too, try to
1654 * set the flags back the way they were and give up.
1658 xrename(const char *src, const char *dst, u_long flags)
1662 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1663 #ifdef _ST_FLAGS_PRESENT_
1664 hc_chflags(&DstHost, dst, 0);
1665 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1666 hc_chflags(&DstHost, dst, flags);
1673 xlink(const char *src, const char *dst, u_long flags)
1676 #ifdef _ST_FLAGS_PRESENT_
1680 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1681 #ifdef _ST_FLAGS_PRESENT_
1682 hc_chflags(&DstHost, src, 0);
1683 r = hc_link(&DstHost, src, dst);
1685 hc_chflags(&DstHost, src, flags);
1695 xremove(struct HostConf *host, const char *path)
1699 res = hc_remove(host, path);
1700 #ifdef _ST_FLAGS_PRESENT_
1701 if (res == -EPERM) {
1702 hc_chflags(host, path, 0);
1703 res = hc_remove(host, path);