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;
152 const char *UseCpFile;
153 const char *UseHLPath;
154 const char *MD5CacheFile;
155 const char *FSMIDCacheFile;
157 int64_t CountSourceBytes;
158 int64_t CountSourceItems;
159 int64_t CountCopiedItems;
160 int64_t CountSourceReadBytes;
161 int64_t CountTargetReadBytes;
162 int64_t CountWriteBytes;
163 int64_t CountRemovedItems;
164 int64_t CountLinkedItems;
166 struct HostConf SrcHost;
167 struct HostConf DstHost;
170 pthread_mutex_t MasterMutex;
174 main(int ac, char **av)
180 struct timeval start;
181 struct copy_info info;
183 signal(SIGPIPE, SIG_IGN);
185 RunningAsUser = (geteuid() != 0);
186 RunningAsRoot = !RunningAsUser;
189 for (i = 0; i < HCTHASH_SIZE; ++i) {
190 pthread_mutex_init(&SrcHost.hct_mutex[i], NULL);
191 pthread_mutex_init(&DstHost.hct_mutex[i], NULL);
193 pthread_mutex_init(&MasterMutex, NULL);
194 pthread_mutex_lock(&MasterMutex);
197 gettimeofday(&start, NULL);
198 for (i = 1; i < ac; ++i) {
205 } else if (dst == NULL) {
208 fatal("too many arguments");
216 v = strtol(ptr, NULL, 0);
224 while (*ptr == 'v') {
228 if (*ptr >= '0' && *ptr <= '9')
229 VerboseOpt = strtol(ptr, NULL, 0);
245 UseCpFile = ".cpignore";
248 UseCpFile = (*ptr) ? ptr : av[++i];
251 UseHLPath = (*ptr) ? ptr : av[++i];
276 FSMIDCacheFile = ".FSMID.CHECK";
280 FSMIDCacheFile = av[++i];
284 MD5CacheFile = av[++i];
288 MD5CacheFile = ".MD5.CHECKSUMS";
291 setvbuf(stdout, NULL, _IOLBF, 0);
294 fatal("illegal option: %s\n", ptr - 2);
301 * If we are told to go into slave mode, run the HC protocol
309 * Extract the source and/or/neither target [user@]host and
310 * make any required connections.
312 if (src && (ptr = strchr(src, ':')) != NULL) {
313 asprintf(&SrcHost.host, "%*.*s", (int)(ptr - src), (int)(ptr - src), src);
316 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
320 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
323 if (hc_connect(&SrcHost) < 0)
326 if (dst && (ptr = strchr(dst, ':')) != NULL) {
327 asprintf(&DstHost.host, "%*.*s", (int)(ptr - dst), (int)(ptr - dst), dst);
330 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
333 if (hc_connect(&DstHost) < 0)
338 * dst may be NULL only if -m option is specified,
339 * which forces an update of the MD5 checksums
341 if (dst == NULL && UseMD5Opt == 0) {
345 bzero(&info, sizeof(info));
349 pthread_cond_init(&info.cond, NULL);
352 DstBaseLen = strlen(dst);
355 info.sdevNo = (dev_t)-1;
356 info.ddevNo = (dev_t)-1;
357 i = DoCopy(&info, -1);
361 info.sdevNo = (dev_t)-1;
362 info.ddevNo = (dev_t)-1;
363 i = DoCopy(&info, -1);
366 pthread_cond_destroy(&info.cond);
373 if (SummaryOpt && i == 0) {
377 gettimeofday(&end, NULL);
379 /* don't count stat's in our byte statistics */
380 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
381 CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
382 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
383 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
386 duration = (end.tv_sec - start.tv_sec);
387 duration += (double)(end.tv_usec - start.tv_usec) / 1000000.0;
390 logstd("cpdup completed successfully\n");
391 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
392 "%lld bytes written (%.1fX speedup)\n",
393 (long long)CountSourceBytes,
394 (long long)CountSourceReadBytes,
395 (long long)CountTargetReadBytes,
396 (long long)CountWriteBytes,
397 ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
398 logstd("%lld source items, %lld items copied, %lld items linked, "
399 "%lld things deleted\n",
400 (long long)CountSourceItems,
401 (long long)CountCopiedItems,
402 (long long)CountLinkedItems,
403 (long long)CountRemovedItems);
404 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
406 (int)((CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration / 1024.0),
407 (int)(CountSourceBytes / duration / 1024.0));
409 exit((i == 0) ? 0 : 1);
412 static struct hlink *
413 hltlookup(struct stat *stp)
416 struct timespec ts = { 0, 100000 };
421 n = stp->st_ino & HLMASK;
426 for (hl = hltable[n]; hl; hl = hl->next) {
427 if (hl->ino == stp->st_ino) {
430 * If the hl entry is still in the process of being created
431 * by another thread we have to wait until it has either been
432 * deleted or completed.
435 pthread_mutex_unlock(&MasterMutex);
436 nanosleep(&ts, NULL);
437 pthread_mutex_lock(&MasterMutex);
449 static struct hlink *
450 hltadd(struct stat *stp, const char *path)
453 int plen = strlen(path);
456 new = malloc(offsetof(struct hlink, name[plen + 1]));
458 fprintf(stderr, "out of memory\n");
463 /* initialize and link the new element into the table */
464 new->ino = stp->st_ino;
465 new->dino = (ino_t)-1;
467 bcopy(path, new->name, plen + 1);
470 n = stp->st_ino & HLMASK;
471 new->next = hltable[n];
473 hltable[n]->prev = new;
480 hltsetdino(struct hlink *hl, ino_t inum)
486 hltdelete(struct hlink *hl)
488 assert(hl->refs == 1);
492 hl->next->prev = hl->prev;
493 hl->prev->next = hl->next;
496 hl->next->prev = NULL;
498 hltable[hl->ino & HLMASK] = hl->next;
505 hltrels(struct hlink *hl)
507 assert(hl->refs == 1);
512 * If UseHLPath is defined check to see if the file in question is
513 * the same as the source file, and if it is return a pointer to the
514 * -H path based file for hardlinking. Else return NULL.
517 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
523 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
526 * stat info matches ?
528 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
529 st1->st_size != sthl.st_size ||
530 st1->st_mtime != sthl.st_mtime ||
531 (RunningAsRoot && (st1->st_uid != sthl.st_uid ||
532 st1->st_gid != sthl.st_gid))
539 * If ForceOpt or ValidateOpt is set we have to compare the files
541 if (ForceOpt || ValidateOpt) {
542 error = validate_check(spath, hpath);
552 * Return 0 if the contents of the file <spath> matches the contents of
556 validate_check(const char *spath, const char *dpath)
562 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
563 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
566 if (fd1 >= 0 && fd2 >= 0) {
569 char *iobuf1 = malloc(GETIOSIZE);
570 char *iobuf2 = malloc(GETIOSIZE);
572 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
573 CountSourceReadBytes += n;
574 x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
576 CountTargetReadBytes += x;
579 if (bcmp(iobuf1, iobuf2, n) != 0)
588 hc_close(&SrcHost, fd1);
590 hc_close(&DstHost, fd2);
596 DoCopyThread(void *arg)
598 copy_info_t cinfo = arg;
599 char *spath = cinfo->spath;
600 char *dpath = cinfo->dpath;
603 r = pthread_detach(pthread_self());
605 pthread_cond_init(&cinfo->cond, NULL);
606 pthread_mutex_lock(&MasterMutex);
607 cinfo->r += DoCopy(cinfo, 0);
608 /* cinfo arguments invalid on return */
609 --cinfo->parent->children;
611 pthread_cond_signal(&cinfo->parent->cond);
615 pthread_cond_destroy(&cinfo->cond);
617 hcc_free_trans(&SrcHost);
618 hcc_free_trans(&DstHost);
619 pthread_mutex_unlock(&MasterMutex);
626 DoCopy(copy_info_t info, int depth)
628 const char *spath = info->spath;
629 const char *dpath = info->dpath;
630 dev_t sdevNo = info->sdevNo;
631 dev_t ddevNo = info->ddevNo;
634 unsigned long st2_flags;
635 int r, mres, fres, st2Valid;
637 List *list = malloc(sizeof(List));
641 r = mres = fres = st2Valid = 0;
646 if (hc_lstat(&SrcHost, spath, &st1) != 0) {
650 st2.st_mode = 0; /* in case lstat fails */
651 st2.st_flags = 0; /* in case lstat fails */
652 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0) {
654 #ifdef _ST_FLAGS_PRESENT_
655 st2_flags = st2.st_flags;
659 if (S_ISREG(st1.st_mode)) {
667 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
668 if ((hln = hltlookup(&st1)) != NULL) {
672 if (st2.st_ino == hln->dino) {
674 * hard link is already correct, nothing to do
677 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
678 if (hln->nlinked == st1.st_nlink) {
687 * hard link is not correct, attempt to unlink it
689 if (xremove(&DstHost, dpath) < 0) {
690 logerr("%-32s hardlink: unable to unlink: %s\n",
691 ((dpath) ? dpath : spath), strerror(errno));
700 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
701 int tryrelink = (errno == EMLINK);
702 logerr("%-32s hardlink: unable to link to %s: %s\n",
703 (dpath ? dpath : spath), hln->name, strerror(errno)
708 logerr("%-20s hardlink: will attempt to copy normally\n");
713 if (hln->nlinked == st1.st_nlink) {
719 logstd("%-32s hardlink: %s\n",
720 (dpath ? dpath : spath),
721 (st2Valid ? "relinked" : "linked")
732 * first instance of hardlink must be copied normally
735 hln = hltadd(&st1, dpath);
740 * Do we need to copy the file/dir/link/whatever? Early termination
741 * if we do not. Always redo links. Directories are always traversed
742 * except when the FSMID options are used.
744 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
749 && st1.st_mode == st2.st_mode
750 #ifdef _ST_FLAGS_PRESENT_
751 && (RunningAsUser || st1.st_flags == st2.st_flags)
754 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
756 * If FSMID tracking is turned on we can avoid recursing through
757 * an entire directory subtree if the FSMID matches.
759 #ifdef _ST_FSMID_PRESENT_
761 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
763 if (VerboseOpt >= 3) {
765 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
767 logstd("%-32s nochange\n", (dpath ? dpath : spath));
775 st1.st_size == st2.st_size &&
776 st1.st_mtime == st2.st_mtime &&
777 (RunningAsUser || (st1.st_uid == st2.st_uid &&
778 st1.st_gid == st2.st_gid))
780 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
781 (mres = md5_check(spath, dpath)) == 0)
783 #ifdef _ST_FSMID_PRESENT_
784 && (UseFSMIDOpt == 0 ||
785 (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
787 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
788 validate_check(spath, dpath) == 0)
791 * The files are identical, but if we are not running as
792 * root we might need to adjust ownership/group/flags.
795 int changedflags = 0;
797 hltsetdino(hln, st2.st_ino);
799 if (RunningAsUser && (st1.st_uid != st2.st_uid ||
800 st1.st_gid != st2.st_gid)) {
801 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
804 #ifdef _ST_FLAGS_PRESENT_
805 if (RunningAsUser && st1.st_flags != st2.st_flags) {
806 hc_chflags(&DstHost, dpath, st1.st_flags);
810 if (VerboseOpt >= 3) {
813 logstd("%-32s md5-nochange",
814 (dpath ? dpath : spath));
818 logstd("%-32s fsmid-nochange",
819 (dpath ? dpath : spath));
820 } else if (ValidateOpt) {
821 logstd("%-32s nochange (contents validated)",
822 (dpath ? dpath : spath));
824 logstd("%-32s nochange", (dpath ? dpath : spath));
827 logstd(" (uid/gid differ)");
829 logstd(" (flags differ)");
832 CountSourceBytes += size;
839 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
841 logerr("%-32s SAFETY - refusing to copy file over directory\n",
842 (dpath ? dpath : spath)
846 goto done; /* continue with the cpdup anyway */
848 if (QuietOpt == 0 || AskConfirmation) {
849 logstd("%-32s WARNING: non-directory source will blow away\n"
850 "%-32s preexisting dest directory, continuing anyway!\n",
851 ((dpath) ? dpath : spath), "");
854 RemoveRecur(dpath, ddevNo);
858 * The various comparisons failed, copy it.
860 if (S_ISDIR(st1.st_mode)) {
864 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
865 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
870 if (S_ISDIR(st2.st_mode) == 0) {
871 xremove(&DstHost, dpath);
872 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
873 logerr("%s: mkdir failed: %s\n",
874 (dpath ? dpath : spath), strerror(errno));
880 * Matt: why don't you check error codes here?
881 * (Because I'm an idiot... checks added!)
883 if (hc_lstat(&DstHost, dpath, &st2) != 0) {
884 logerr("%s: lstat of newly made dir failed: %s\n",
885 (dpath ? dpath : spath), strerror(errno));
889 if (hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid) != 0){
890 logerr("%s: chown of newly made dir failed: %s\n",
891 (dpath ? dpath : spath), strerror(errno));
898 * Directory must be scanable by root for cpdup to
899 * work. We'll fix it later if the directory isn't
900 * supposed to be readable ( which is why we fixup
901 * st2.st_mode to match what we did ).
903 if ((st2.st_mode & 0700) != 0700) {
904 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
908 logstd("%s\n", dpath ? dpath : spath);
912 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
918 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
925 * scan .cpignore file for files/directories
931 char *buf = malloc(GETBUFSIZE);
934 if (UseCpFile[0] == '/') {
935 fpath = mprintf("%s", UseCpFile);
937 fpath = mprintf("%s/%s", spath, UseCpFile);
939 AddList(list, strrchr(fpath, '/') + 1, 1);
940 if ((fi = fopen(fpath, "r")) != NULL) {
941 while (fgets(buf, GETBUFSIZE, fi) != NULL) {
943 CountSourceReadBytes += l;
944 if (l && buf[l-1] == '\n')
947 AddList(list, buf, 1);
956 * Automatically exclude MD5CacheFile that we create on the
957 * source from the copy to the destination.
959 * Automatically exclude a FSMIDCacheFile on the source that
960 * would otherwise overwrite the one we maintain on the target.
963 AddList(list, MD5CacheFile, 1);
965 AddList(list, FSMIDCacheFile, 1);
967 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
974 if (strcmp(den->d_name, ".") == 0 ||
975 strcmp(den->d_name, "..") == 0
980 * ignore if on .cpignore list
982 if (AddList(list, den->d_name, 0) == 1) {
985 nspath = mprintf("%s/%s", spath, den->d_name);
987 ndpath = mprintf("%s/%s", dpath, den->d_name);
990 if (CurParallel < MaxParallel || depth > MAXDEPTH) {
991 copy_info_t cinfo = malloc(sizeof(*cinfo));
994 bzero(cinfo, sizeof(*cinfo));
995 cinfo->spath = nspath;
996 cinfo->dpath = ndpath;
997 cinfo->sdevNo = sdevNo;
998 cinfo->ddevNo = ddevNo;
999 cinfo->parent = info;
1002 pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
1006 info->spath = nspath;
1007 info->dpath = ndpath;
1008 info->sdevNo = sdevNo;
1009 info->ddevNo = ddevNo;
1011 r += DoCopy(info, depth);
1013 r += DoCopy(info, depth + 1);
1022 hc_closedir(&SrcHost, dir);
1026 * Wait for our children to finish
1028 while (info->children) {
1029 pthread_cond_wait(&info->cond, &MasterMutex);
1036 * Remove files/directories from destination that do not appear
1039 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
1040 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
1044 if (strcmp(den->d_name, ".") == 0 ||
1045 strcmp(den->d_name, "..") == 0
1050 * If object does not exist in source or .cpignore
1051 * then recursively remove it.
1053 if (AddList(list, den->d_name, 3) == 3) {
1056 ndpath = mprintf("%s/%s", dpath, den->d_name);
1057 RemoveRecur(ndpath, ddevNo);
1061 hc_closedir(&DstHost, dir);
1065 struct timeval tv[2];
1069 st1.st_uid != st2.st_uid ||
1070 st1.st_gid != st2.st_gid
1072 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
1074 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
1075 hc_chmod(&DstHost, dpath, st1.st_mode);
1077 #ifdef _ST_FLAGS_PRESENT_
1078 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
1079 hc_chflags(&DstHost, dpath, st1.st_flags);
1084 st1.st_mtime != st2.st_mtime
1086 bzero(tv, sizeof(tv));
1087 tv[0].tv_sec = st1.st_mtime;
1088 tv[1].tv_sec = st1.st_mtime;
1089 hc_utimes(&DstHost, dpath, tv);
1093 } else if (dpath == NULL) {
1095 * If dpath is NULL, we are just updating the MD5
1098 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
1099 mres = md5_check(spath, NULL);
1101 if (VerboseOpt > 1) {
1103 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1105 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
1106 } else if (!QuietOpt && mres < 0) {
1107 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1111 } else if (S_ISREG(st1.st_mode)) {
1117 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1120 * Handle check failure message.
1124 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1128 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1131 * Not quite ready to do the copy yet. If UseHLPath is defined,
1132 * see if we can hardlink instead.
1134 * If we can hardlink, and the target exists, we have to remove it
1135 * first or the hardlink will fail. This can occur in a number of
1136 * situations but must typically when the '-f -H' combination is
1139 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1141 xremove(&DstHost, dpath);
1142 if (hc_link(&DstHost, hpath, dpath) == 0) {
1145 logstd("%-32s hardlinked(-H)\n",
1146 (dpath ? dpath : spath));
1152 * Shucks, we may have hit a filesystem hard linking limit,
1153 * we have to copy instead.
1158 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1159 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1161 * There could be a .tmp file from a previously interrupted
1162 * run, delete and retry. Fail if we still can't get at it.
1164 #ifdef _ST_FLAGS_PRESENT_
1165 hc_chflags(&DstHost, path, 0);
1167 hc_remove(&DstHost, path);
1168 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1172 char *iobuf1 = malloc(GETIOSIZE);
1176 * Matt: What about holes?
1179 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1181 if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1185 hc_close(&DstHost, fd2);
1187 struct timeval tv[2];
1189 bzero(tv, sizeof(tv));
1190 tv[0].tv_sec = st1.st_mtime;
1191 tv[1].tv_sec = st1.st_mtime;
1193 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1194 hc_chmod(&DstHost, path, st1.st_mode);
1195 #ifdef _ST_FLAGS_PRESENT_
1196 if (st1.st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
1197 hc_utimes(&DstHost, path, tv);
1199 hc_utimes(&DstHost, path, tv);
1201 if (xrename(path, dpath, st2_flags) != 0) {
1202 logerr("%-32s rename-after-copy failed: %s\n",
1203 (dpath ? dpath : spath), strerror(errno)
1208 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1209 #ifdef _ST_FLAGS_PRESENT_
1211 hc_chflags(&DstHost, dpath, st1.st_flags);
1214 #ifdef _ST_FLAGS_PRESENT_
1215 if ((st1.st_flags & (UF_IMMUTABLE|SF_IMMUTABLE)) == 0)
1216 hc_utimes(&DstHost, dpath, tv);
1218 CountSourceReadBytes += size;
1219 CountWriteBytes += size;
1220 CountSourceBytes += size;
1224 logerr("%-32s %s failed: %s\n",
1225 (dpath ? dpath : spath), op, strerror(errno)
1227 hc_remove(&DstHost, path);
1232 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1233 (dpath ? dpath : spath), getuid(), geteuid(),
1238 hc_close(&SrcHost, fd1);
1240 logerr("%-32s copy: open failed: %s\n",
1241 (dpath ? dpath : spath),
1250 if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1251 hltsetdino(hln, st2.st_ino);
1257 } else if (S_ISLNK(st1.st_mode)) {
1258 char *link1 = malloc(GETLINKSIZE);
1259 char *link2 = malloc(GETLINKSIZE);
1260 char *path = malloc(GETPATHSIZE);
1264 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1265 n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1266 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1268 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1269 hc_umask(&DstHost, ~st1.st_mode);
1270 xremove(&DstHost, path);
1272 if (hc_symlink(&DstHost, link1, path) < 0) {
1273 logerr("%-32s symlink (%s->%s) failed: %s\n",
1274 (dpath ? dpath : spath), link1, path,
1279 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1281 * there is no lchmod() or lchflags(), we
1282 * cannot chmod or chflags a softlink.
1284 if (xrename(path, dpath, st2_flags) != 0) {
1285 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1286 (dpath ? dpath : spath),
1287 path, dpath, strerror(errno));
1288 } else if (VerboseOpt) {
1289 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1291 hc_umask(&DstHost, 000);
1292 CountWriteBytes += n1;
1296 if (VerboseOpt >= 3)
1297 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1299 CountSourceBytes += n1;
1300 CountSourceReadBytes += n1;
1302 CountTargetReadBytes += n2;
1306 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1311 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1312 char *path = malloc(GETPATHSIZE);
1316 st1.st_mode != st2.st_mode ||
1317 st1.st_rdev != st2.st_rdev ||
1318 st1.st_uid != st2.st_uid ||
1319 st1.st_gid != st2.st_gid
1321 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1323 xremove(&DstHost, path);
1324 if (hc_mknod(&DstHost, path, st1.st_mode, st1.st_rdev) == 0) {
1325 hc_chmod(&DstHost, path, st1.st_mode);
1326 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1327 xremove(&DstHost, dpath);
1328 if (xrename(path, dpath, st2_flags) != 0) {
1329 logerr("%-32s dev-rename-after-create failed: %s\n",
1330 (dpath ? dpath : spath),
1333 } else if (VerboseOpt) {
1334 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1339 logerr("%-32s dev failed: %s\n",
1340 (dpath ? dpath : spath), strerror(errno)
1344 if (VerboseOpt >= 3)
1345 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1352 if (hln->dino == (ino_t)-1) {
1354 /*hln = NULL; unneeded */
1369 RemoveRecur(const char *dpath, dev_t devNo)
1373 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1376 if (st.st_dev == devNo) {
1377 if (S_ISDIR(st.st_mode)) {
1380 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1382 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1385 if (strcmp(den->d_name, ".") == 0)
1387 if (strcmp(den->d_name, "..") == 0)
1389 ndpath = mprintf("%s/%s", dpath, den->d_name);
1390 RemoveRecur(ndpath, devNo);
1393 hc_closedir(&DstHost, dir);
1395 if (AskConfirmation && NoRemoveOpt == 0) {
1397 if (hc_rmdir(&DstHost, dpath) < 0) {
1398 logerr("%-32s rmdir failed: %s\n",
1399 dpath, strerror(errno)
1402 CountRemovedItems++;
1407 logstd("%-32s not-removed\n", dpath);
1408 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1410 logstd("%-32s rmdir-ok\n", dpath);
1411 CountRemovedItems++;
1413 logerr("%-32s rmdir failed: %s\n",
1414 dpath, strerror(errno)
1419 if (AskConfirmation && NoRemoveOpt == 0) {
1421 if (xremove(&DstHost, dpath) < 0) {
1422 logerr("%-32s remove failed: %s\n",
1423 dpath, strerror(errno)
1426 CountRemovedItems++;
1431 logstd("%-32s not-removed\n", dpath);
1432 } else if (xremove(&DstHost, dpath) == 0) {
1434 logstd("%-32s remove-ok\n", dpath);
1435 CountRemovedItems++;
1437 logerr("%-32s remove failed: %s\n",
1438 dpath, strerror(errno)
1448 InitList(List *list)
1450 bzero(list, sizeof(List));
1451 list->li_Node.no_Next = &list->li_Node;
1455 ResetList(List *list)
1459 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1460 list->li_Node.no_Next = node->no_Next;
1467 AddList(List *list, const char *name, int n)
1475 * Scan against wildcards. Only a node value of 1 can be a wildcard
1476 * ( usually scanned from .cpignore )
1479 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1480 if (strcmp(name, node->no_Name) == 0 ||
1481 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1483 return(node->no_Value);
1488 * Look for exact match
1491 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1492 if (strcmp(name, node->no_Name) == 0) {
1493 return(node->no_Value);
1496 node = malloc(sizeof(Node) + strlen(name) + 1);
1498 fprintf(stderr, "out of memory\n");
1502 node->no_Next = list->li_Node.no_Next;
1503 list->li_Node.no_Next = node;
1505 node->no_HNext = list->li_Hash[hv];
1506 list->li_Hash[hv] = node;
1508 strcpy(node->no_Name, name);
1515 shash(const char *s)
1522 if (*s == '*' || *s == '?' ||
1523 *s == '{' || *s == '}' ||
1524 *s == '[' || *s == ']' ||
1529 hv = (hv << 5) ^ *s ^ (hv >> 23);
1532 return(((hv >> 16) ^ hv) & HMASK);
1536 * WildCmp() - compare wild string to sane string
1538 * Return 0 on success, -1 on failure.
1542 WildCmp(const char *w, const char *s)
1545 * skip fixed portion
1551 if (w[1] == 0) /* optimize wild* case */
1557 for (i = 0; i <= l; ++i) {
1558 if (WildCmp(w + 1, s + i) == 0)
1572 if (*w == 0) /* terminator */
1584 YesNo(const char *path)
1588 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1591 first = ch = getchar();
1592 while (ch != '\n' && ch != EOF)
1594 return ((first == 'y' || first == 'Y'));
1598 * xrename() - rename with override
1600 * If the rename fails, attempt to override st_flags on the
1601 * destination and rename again. If that fails too, try to
1602 * set the flags back the way they were and give up.
1606 xrename(const char *src, const char *dst, u_long flags)
1612 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1613 #ifdef _ST_FLAGS_PRESENT_
1614 hc_chflags(&DstHost, dst, 0);
1615 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1616 hc_chflags(&DstHost, dst, flags);
1623 xlink(const char *src, const char *dst, u_long flags)
1626 #ifdef _ST_FLAGS_PRESENT_
1632 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1633 #ifdef _ST_FLAGS_PRESENT_
1634 hc_chflags(&DstHost, src, 0);
1635 r = hc_link(&DstHost, src, dst);
1637 hc_chflags(&DstHost, src, flags);
1647 xremove(struct HostConf *host, const char *path)
1651 res = hc_remove(host, path);
1652 #ifdef _ST_FLAGS_PRESENT_
1653 if (res == -EPERM) {
1654 hc_chflags(host, path, 0);
1655 res = hc_remove(host, path);