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;
150 const char *UseCpFile;
151 const char *UseHLPath;
152 const char *MD5CacheFile;
153 const char *FSMIDCacheFile;
155 int64_t CountSourceBytes;
156 int64_t CountSourceItems;
157 int64_t CountCopiedItems;
158 int64_t CountSourceReadBytes;
159 int64_t CountTargetReadBytes;
160 int64_t CountWriteBytes;
161 int64_t CountRemovedItems;
162 int64_t CountLinkedItems;
164 struct HostConf SrcHost;
165 struct HostConf DstHost;
168 pthread_mutex_t MasterMutex;
172 main(int ac, char **av)
178 struct timeval start;
179 struct copy_info info;
181 signal(SIGPIPE, SIG_IGN);
184 for (i = 0; i < HCTHASH_SIZE; ++i) {
185 pthread_mutex_init(&SrcHost.hct_mutex[i], NULL);
186 pthread_mutex_init(&DstHost.hct_mutex[i], NULL);
188 pthread_mutex_init(&MasterMutex, NULL);
189 pthread_mutex_lock(&MasterMutex);
192 gettimeofday(&start, NULL);
193 for (i = 1; i < ac; ++i) {
200 } else if (dst == NULL) {
203 fatal("too many arguments");
211 v = strtol(ptr, NULL, 0);
219 while (*ptr == 'v') {
223 if (*ptr >= '0' && *ptr <= '9')
224 VerboseOpt = strtol(ptr, NULL, 0);
240 UseCpFile = ".cpignore";
243 UseCpFile = (*ptr) ? ptr : av[++i];
246 UseHLPath = (*ptr) ? ptr : av[++i];
271 FSMIDCacheFile = ".FSMID.CHECK";
275 FSMIDCacheFile = av[++i];
279 MD5CacheFile = av[++i];
283 MD5CacheFile = ".MD5.CHECKSUMS";
286 setvbuf(stdout, NULL, _IOLBF, 0);
289 fatal("illegal option: %s\n", ptr - 2);
296 * If we are told to go into slave mode, run the HC protocol
304 * Extract the source and/or/neither target [user@]host and
305 * make any required connections.
307 if (src && (ptr = strchr(src, ':')) != NULL) {
308 asprintf(&SrcHost.host, "%*.*s", (int)(ptr - src), (int)(ptr - src), src);
311 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
315 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
318 if (hc_connect(&SrcHost) < 0)
321 if (dst && (ptr = strchr(dst, ':')) != NULL) {
322 asprintf(&DstHost.host, "%*.*s", (int)(ptr - dst), (int)(ptr - dst), dst);
325 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
328 if (hc_connect(&DstHost) < 0)
333 * dst may be NULL only if -m option is specified,
334 * which forces an update of the MD5 checksums
336 if (dst == NULL && UseMD5Opt == 0) {
340 bzero(&info, sizeof(info));
344 pthread_cond_init(&info.cond, NULL);
347 DstBaseLen = strlen(dst);
350 info.sdevNo = (dev_t)-1;
351 info.ddevNo = (dev_t)-1;
352 i = DoCopy(&info, -1);
356 info.sdevNo = (dev_t)-1;
357 info.ddevNo = (dev_t)-1;
358 i = DoCopy(&info, -1);
361 pthread_cond_destroy(&info.cond);
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);
407 static struct hlink *
408 hltlookup(struct stat *stp)
411 struct timespec ts = { 0, 100000 };
416 n = stp->st_ino & HLMASK;
421 for (hl = hltable[n]; hl; hl = hl->next) {
422 if (hl->ino == stp->st_ino) {
425 * If the hl entry is still in the process of being created
426 * by another thread we have to wait until it has either been
427 * deleted or completed.
430 pthread_mutex_unlock(&MasterMutex);
431 nanosleep(&ts, NULL);
432 pthread_mutex_lock(&MasterMutex);
444 static struct hlink *
445 hltadd(struct stat *stp, const char *path)
448 int plen = strlen(path);
451 new = malloc(offsetof(struct hlink, name[plen + 1]));
453 fprintf(stderr, "out of memory\n");
458 /* initialize and link the new element into the table */
459 new->ino = stp->st_ino;
460 new->dino = (ino_t)-1;
462 bcopy(path, new->name, plen + 1);
465 n = stp->st_ino & HLMASK;
466 new->next = hltable[n];
468 hltable[n]->prev = new;
475 hltsetdino(struct hlink *hl, ino_t inum)
481 hltdelete(struct hlink *hl)
483 assert(hl->refs == 1);
487 hl->next->prev = hl->prev;
488 hl->prev->next = hl->next;
491 hl->next->prev = NULL;
493 hltable[hl->ino & HLMASK] = hl->next;
500 hltrels(struct hlink *hl)
502 assert(hl->refs == 1);
507 * If UseHLPath is defined check to see if the file in question is
508 * the same as the source file, and if it is return a pointer to the
509 * -H path based file for hardlinking. Else return NULL.
512 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
518 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
521 * stat info matches ?
523 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
524 st1->st_size != sthl.st_size ||
525 st1->st_uid != sthl.st_uid ||
526 st1->st_gid != sthl.st_gid ||
527 st1->st_mtime != sthl.st_mtime
534 * If ForceOpt or ValidateOpt is set we have to compare the files
536 if (ForceOpt || ValidateOpt) {
537 error = validate_check(spath, hpath);
547 * Return 0 if the contents of the file <spath> matches the contents of
551 validate_check(const char *spath, const char *dpath)
557 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
558 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
561 if (fd1 >= 0 && fd2 >= 0) {
564 char *iobuf1 = malloc(GETIOSIZE);
565 char *iobuf2 = malloc(GETIOSIZE);
567 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
568 CountSourceReadBytes += n;
569 x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
571 CountTargetReadBytes += x;
574 if (bcmp(iobuf1, iobuf2, n) != 0)
583 hc_close(&SrcHost, fd1);
585 hc_close(&DstHost, fd2);
591 DoCopyThread(void *arg)
593 copy_info_t cinfo = arg;
594 char *spath = cinfo->spath;
595 char *dpath = cinfo->dpath;
598 r = pthread_detach(pthread_self());
600 pthread_cond_init(&cinfo->cond, NULL);
601 pthread_mutex_lock(&MasterMutex);
602 cinfo->r += DoCopy(cinfo, 0);
603 /* cinfo arguments invalid on return */
604 --cinfo->parent->children;
606 pthread_cond_signal(&cinfo->parent->cond);
610 pthread_cond_destroy(&cinfo->cond);
612 hcc_free_trans(&SrcHost);
613 hcc_free_trans(&DstHost);
614 pthread_mutex_unlock(&MasterMutex);
621 DoCopy(copy_info_t info, int depth)
623 const char *spath = info->spath;
624 const char *dpath = info->dpath;
625 dev_t sdevNo = info->sdevNo;
626 dev_t ddevNo = info->ddevNo;
629 int r, mres, fres, st2Valid;
631 List *list = malloc(sizeof(List));
635 r = mres = fres = st2Valid = 0;
639 if (hc_lstat(&SrcHost, spath, &st1) != 0) {
643 st2.st_mode = 0; /* in case lstat fails */
644 st2.st_flags = 0; /* in case lstat fails */
645 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
648 if (S_ISREG(st1.st_mode)) {
656 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
657 if ((hln = hltlookup(&st1)) != NULL) {
661 if (st2.st_ino == hln->dino) {
663 * hard link is already correct, nothing to do
666 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
667 if (hln->nlinked == st1.st_nlink) {
676 * hard link is not correct, attempt to unlink it
678 if (xremove(&DstHost, dpath) < 0) {
679 logerr("%-32s hardlink: unable to unlink: %s\n",
680 ((dpath) ? dpath : spath), strerror(errno));
689 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
690 int tryrelink = (errno == EMLINK);
691 logerr("%-32s hardlink: unable to link to %s: %s\n",
692 (dpath ? dpath : spath), hln->name, strerror(errno)
697 logerr("%-20s hardlink: will attempt to copy normally\n");
702 if (hln->nlinked == st1.st_nlink) {
708 logstd("%-32s hardlink: %s\n",
709 (dpath ? dpath : spath),
710 (st2Valid ? "relinked" : "linked")
721 * first instance of hardlink must be copied normally
724 hln = hltadd(&st1, dpath);
729 * Do we need to copy the file/dir/link/whatever? Early termination
730 * if we do not. Always redo links. Directories are always traversed
731 * except when the FSMID options are used.
733 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
738 && st1.st_mode == st2.st_mode
739 #ifdef _ST_FLAGS_PRESENT_
740 && st1.st_flags == st2.st_flags
743 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
745 * If FSMID tracking is turned on we can avoid recursing through
746 * an entire directory subtree if the FSMID matches.
748 #ifdef _ST_FSMID_PRESENT_
750 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
752 if (VerboseOpt >= 3) {
754 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
756 logstd("%-32s nochange\n", (dpath ? dpath : spath));
764 st1.st_size == st2.st_size &&
765 st1.st_uid == st2.st_uid &&
766 st1.st_gid == st2.st_gid &&
767 st1.st_mtime == st2.st_mtime
769 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
770 (mres = md5_check(spath, dpath)) == 0)
772 #ifdef _ST_FSMID_PRESENT_
773 && (UseFSMIDOpt == 0 ||
774 (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
776 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
777 validate_check(spath, dpath) == 0)
780 hltsetdino(hln, st2.st_ino);
781 if (VerboseOpt >= 3) {
784 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
788 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
789 else if (ValidateOpt)
790 logstd("%-32s nochange (contents validated)\n", (dpath ? dpath : spath));
792 logstd("%-32s nochange\n", (dpath ? dpath : spath));
794 CountSourceBytes += size;
801 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
803 logerr("%-32s SAFETY - refusing to copy file over directory\n",
804 (dpath ? dpath : spath)
808 goto done; /* continue with the cpdup anyway */
810 if (QuietOpt == 0 || AskConfirmation) {
811 logstd("%-32s WARNING: non-directory source will blow away\n"
812 "%-32s preexisting dest directory, continuing anyway!\n",
813 ((dpath) ? dpath : spath), "");
816 RemoveRecur(dpath, ddevNo);
820 * The various comparisons failed, copy it.
822 if (S_ISDIR(st1.st_mode)) {
826 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
827 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
832 if (S_ISDIR(st2.st_mode) == 0) {
833 xremove(&DstHost, dpath);
834 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
835 logerr("%s: mkdir failed: %s\n",
836 (dpath ? dpath : spath), strerror(errno));
841 * Matt: why don't you check error codes here?
843 hc_lstat(&DstHost, dpath, &st2);
844 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
848 * Directory must be scanable by root for cpdup to
849 * work. We'll fix it later if the directory isn't
850 * supposed to be readable ( which is why we fixup
851 * st2.st_mode to match what we did ).
853 if ((st2.st_mode & 0700) != 0700) {
854 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
858 logstd("%s\n", dpath ? dpath : spath);
862 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
868 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
875 * scan .cpignore file for files/directories
881 char *buf = malloc(GETBUFSIZE);
884 if (UseCpFile[0] == '/') {
885 fpath = mprintf("%s", UseCpFile);
887 fpath = mprintf("%s/%s", spath, UseCpFile);
889 AddList(list, strrchr(fpath, '/') + 1, 1);
890 if ((fi = fopen(fpath, "r")) != NULL) {
891 while (fgets(buf, GETBUFSIZE, fi) != NULL) {
893 CountSourceReadBytes += l;
894 if (l && buf[l-1] == '\n')
896 if (buf[0] && buf[0] != '#')
897 AddList(list, buf, 1);
906 * Automatically exclude MD5CacheFile that we create on the
907 * source from the copy to the destination.
909 * Automatically exclude a FSMIDCacheFile on the source that
910 * would otherwise overwrite the one we maintain on the target.
913 AddList(list, MD5CacheFile, 1);
915 AddList(list, FSMIDCacheFile, 1);
917 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
924 if (strcmp(den->d_name, ".") == 0 ||
925 strcmp(den->d_name, "..") == 0
930 * ignore if on .cpignore list
932 if (AddList(list, den->d_name, 0) == 1) {
935 nspath = mprintf("%s/%s", spath, den->d_name);
937 ndpath = mprintf("%s/%s", dpath, den->d_name);
940 if (CurParallel < MaxParallel || depth > MAXDEPTH) {
941 copy_info_t cinfo = malloc(sizeof(*cinfo));
944 bzero(cinfo, sizeof(*cinfo));
945 cinfo->spath = nspath;
946 cinfo->dpath = ndpath;
947 cinfo->sdevNo = sdevNo;
948 cinfo->ddevNo = ddevNo;
949 cinfo->parent = info;
952 pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
956 info->spath = nspath;
957 info->dpath = ndpath;
958 info->sdevNo = sdevNo;
959 info->ddevNo = ddevNo;
961 r += DoCopy(info, depth);
963 r += DoCopy(info, depth + 1);
972 hc_closedir(&SrcHost, dir);
976 * Wait for our children to finish
978 while (info->children) {
979 pthread_cond_wait(&info->cond, &MasterMutex);
986 * Remove files/directories from destination that do not appear
989 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
990 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
994 if (strcmp(den->d_name, ".") == 0 ||
995 strcmp(den->d_name, "..") == 0
1000 * If object does not exist in source or .cpignore
1001 * then recursively remove it.
1003 if (AddList(list, den->d_name, 3) == 3) {
1006 ndpath = mprintf("%s/%s", dpath, den->d_name);
1007 RemoveRecur(ndpath, ddevNo);
1011 hc_closedir(&DstHost, dir);
1015 struct timeval tv[2];
1019 st1.st_uid != st2.st_uid ||
1020 st1.st_gid != st2.st_gid
1022 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
1024 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
1025 hc_chmod(&DstHost, dpath, st1.st_mode);
1027 #ifdef _ST_FLAGS_PRESENT_
1028 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
1029 hc_chflags(&DstHost, dpath, st1.st_flags);
1034 st1.st_mtime != st2.st_mtime
1036 bzero(tv, sizeof(tv));
1037 tv[0].tv_sec = st1.st_mtime;
1038 tv[1].tv_sec = st1.st_mtime;
1039 hc_utimes(&DstHost, dpath, tv);
1043 } else if (dpath == NULL) {
1045 * If dpath is NULL, we are just updating the MD5
1048 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
1049 mres = md5_check(spath, NULL);
1051 if (VerboseOpt > 1) {
1053 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1055 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
1056 } else if (!QuietOpt && mres < 0) {
1057 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1061 } else if (S_ISREG(st1.st_mode)) {
1067 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1070 * Handle check failure message.
1074 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1078 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1081 * Not quite ready to do the copy yet. If UseHLPath is defined,
1082 * see if we can hardlink instead.
1084 * If we can hardlink, and the target exists, we have to remove it
1085 * first or the hardlink will fail. This can occur in a number of
1086 * situations but must typically when the '-f -H' combination is
1089 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1091 xremove(&DstHost, dpath);
1092 if (hc_link(&DstHost, hpath, dpath) == 0) {
1095 logstd("%-32s hardlinked(-H)\n",
1096 (dpath ? dpath : spath));
1102 * Shucks, we may have hit a filesystem hard linking limit,
1103 * we have to copy instead.
1108 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1109 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1111 * There could be a .tmp file from a previously interrupted
1112 * run, delete and retry. Fail if we still can't get at it.
1114 #ifdef _ST_FLAGS_PRESENT_
1115 hc_chflags(&DstHost, path, 0);
1117 hc_remove(&DstHost, path);
1118 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1122 char *iobuf1 = malloc(GETIOSIZE);
1126 * Matt: What about holes?
1129 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1131 if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1135 hc_close(&DstHost, fd2);
1137 struct timeval tv[2];
1139 bzero(tv, sizeof(tv));
1140 tv[0].tv_sec = st1.st_mtime;
1141 tv[1].tv_sec = st1.st_mtime;
1143 hc_utimes(&DstHost, path, tv);
1144 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1145 hc_chmod(&DstHost, path, st1.st_mode);
1146 if (xrename(path, dpath, st2.st_flags) != 0) {
1147 logerr("%-32s rename-after-copy failed: %s\n",
1148 (dpath ? dpath : spath), strerror(errno)
1153 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1154 #ifdef _ST_FLAGS_PRESENT_
1156 hc_chflags(&DstHost, dpath, st1.st_flags);
1159 CountSourceReadBytes += size;
1160 CountWriteBytes += size;
1161 CountSourceBytes += size;
1165 logerr("%-32s %s failed: %s\n",
1166 (dpath ? dpath : spath), op, strerror(errno)
1168 hc_remove(&DstHost, path);
1173 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1174 (dpath ? dpath : spath), getuid(), geteuid(),
1179 hc_close(&SrcHost, fd1);
1181 logerr("%-32s copy: open failed: %s\n",
1182 (dpath ? dpath : spath),
1191 if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1192 hltsetdino(hln, st2.st_ino);
1198 } else if (S_ISLNK(st1.st_mode)) {
1199 char *link1 = malloc(GETLINKSIZE);
1200 char *link2 = malloc(GETLINKSIZE);
1201 char *path = malloc(GETPATHSIZE);
1205 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1206 n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1207 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1209 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1210 hc_umask(&DstHost, ~st1.st_mode);
1211 xremove(&DstHost, path);
1213 if (hc_symlink(&DstHost, link1, path) < 0) {
1214 logerr("%-32s symlink (%s->%s) failed: %s\n",
1215 (dpath ? dpath : spath), link1, path,
1220 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1222 * there is no lchmod() or lchflags(), we
1223 * cannot chmod or chflags a softlink.
1225 if (xrename(path, dpath, st2.st_flags) != 0) {
1226 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1227 (dpath ? dpath : spath),
1228 path, dpath, strerror(errno));
1229 } else if (VerboseOpt) {
1230 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1232 hc_umask(&DstHost, 000);
1233 CountWriteBytes += n1;
1237 if (VerboseOpt >= 3)
1238 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1240 CountSourceBytes += n1;
1241 CountSourceReadBytes += n1;
1243 CountTargetReadBytes += n2;
1247 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1252 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1253 char *path = malloc(GETPATHSIZE);
1257 st1.st_mode != st2.st_mode ||
1258 st1.st_rdev != st2.st_rdev ||
1259 st1.st_uid != st2.st_uid ||
1260 st1.st_gid != st2.st_gid
1262 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1264 xremove(&DstHost, path);
1265 if (hc_mknod(&DstHost, path, st1.st_mode, st1.st_rdev) == 0) {
1266 hc_chmod(&DstHost, path, st1.st_mode);
1267 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1268 xremove(&DstHost, dpath);
1269 if (xrename(path, dpath, st2.st_flags) != 0) {
1270 logerr("%-32s dev-rename-after-create failed: %s\n",
1271 (dpath ? dpath : spath),
1274 } else if (VerboseOpt) {
1275 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1280 logerr("%-32s dev failed: %s\n",
1281 (dpath ? dpath : spath), strerror(errno)
1285 if (VerboseOpt >= 3)
1286 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1293 if (hln->dino == (ino_t)-1) {
1295 /*hln = NULL; unneeded */
1310 RemoveRecur(const char *dpath, dev_t devNo)
1314 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1317 if (st.st_dev == devNo) {
1318 if (S_ISDIR(st.st_mode)) {
1321 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1323 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1326 if (strcmp(den->d_name, ".") == 0)
1328 if (strcmp(den->d_name, "..") == 0)
1330 ndpath = mprintf("%s/%s", dpath, den->d_name);
1331 RemoveRecur(ndpath, devNo);
1334 hc_closedir(&DstHost, dir);
1336 if (AskConfirmation && NoRemoveOpt == 0) {
1338 if (hc_rmdir(&DstHost, dpath) < 0) {
1339 logerr("%-32s rmdir failed: %s\n",
1340 dpath, strerror(errno)
1343 CountRemovedItems++;
1348 logstd("%-32s not-removed\n", dpath);
1349 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1351 logstd("%-32s rmdir-ok\n", dpath);
1352 CountRemovedItems++;
1354 logerr("%-32s rmdir failed: %s\n",
1355 dpath, strerror(errno)
1360 if (AskConfirmation && NoRemoveOpt == 0) {
1362 if (xremove(&DstHost, dpath) < 0) {
1363 logerr("%-32s remove failed: %s\n",
1364 dpath, strerror(errno)
1367 CountRemovedItems++;
1372 logstd("%-32s not-removed\n", dpath);
1373 } else if (xremove(&DstHost, dpath) == 0) {
1375 logstd("%-32s remove-ok\n", dpath);
1376 CountRemovedItems++;
1378 logerr("%-32s remove failed: %s\n",
1379 dpath, strerror(errno)
1389 InitList(List *list)
1391 bzero(list, sizeof(List));
1392 list->li_Node.no_Next = &list->li_Node;
1396 ResetList(List *list)
1400 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1401 list->li_Node.no_Next = node->no_Next;
1408 AddList(List *list, const char *name, int n)
1416 * Scan against wildcards. Only a node value of 1 can be a wildcard
1417 * ( usually scanned from .cpignore )
1420 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1421 if (strcmp(name, node->no_Name) == 0 ||
1422 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1424 return(node->no_Value);
1429 * Look for exact match
1432 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1433 if (strcmp(name, node->no_Name) == 0) {
1434 return(node->no_Value);
1437 node = malloc(sizeof(Node) + strlen(name) + 1);
1439 fprintf(stderr, "out of memory\n");
1443 node->no_Next = list->li_Node.no_Next;
1444 list->li_Node.no_Next = node;
1446 node->no_HNext = list->li_Hash[hv];
1447 list->li_Hash[hv] = node;
1449 strcpy(node->no_Name, name);
1456 shash(const char *s)
1463 if (*s == '*' || *s == '?' ||
1464 *s == '{' || *s == '}' ||
1465 *s == '[' || *s == ']' ||
1470 hv = (hv << 5) ^ *s ^ (hv >> 23);
1473 return(((hv >> 16) ^ hv) & HMASK);
1477 * WildCmp() - compare wild string to sane string
1479 * Return 0 on success, -1 on failure.
1483 WildCmp(const char *w, const char *s)
1486 * skip fixed portion
1492 if (w[1] == 0) /* optimize wild* case */
1498 for (i = 0; i <= l; ++i) {
1499 if (WildCmp(w + 1, s + i) == 0)
1513 if (*w == 0) /* terminator */
1525 YesNo(const char *path)
1529 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1532 first = ch = getchar();
1533 while (ch != '\n' && ch != EOF)
1535 return ((first == 'y' || first == 'Y'));
1539 * xrename() - rename with override
1541 * If the rename fails, attempt to override st_flags on the
1542 * destination and rename again. If that fails too, try to
1543 * set the flags back the way they were and give up.
1547 xrename(const char *src, const char *dst, u_long flags)
1553 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1554 #ifdef _ST_FLAGS_PRESENT_
1555 hc_chflags(&DstHost, dst, 0);
1556 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1557 hc_chflags(&DstHost, dst, flags);
1564 xlink(const char *src, const char *dst, u_long flags)
1567 #ifdef _ST_FLAGS_PRESENT_
1573 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1574 #ifdef _ST_FLAGS_PRESENT_
1575 hc_chflags(&DstHost, src, 0);
1576 r = hc_link(&DstHost, src, dst);
1578 hc_chflags(&DstHost, src, flags);
1588 xremove(struct HostConf *host, const char *path)
1592 res = hc_remove(host, path);
1593 #ifdef _ST_FLAGS_PRESENT_
1594 if (res == -EPERM) {
1595 hc_chflags(host, path, 0);
1596 res = hc_remove(host, path);