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));
842 * Matt: why don't you check error codes here?
843 * (Because I'm an idiot... checks added!)
845 if (hc_lstat(&DstHost, dpath, &st2) != 0) {
846 logerr("%s: lstat of newly made dir failed: %s\n",
847 (dpath ? dpath : spath), strerror(errno));
851 if (hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid) != 0){
852 logerr("%s: chown of newly made dir failed: %s\n",
853 (dpath ? dpath : spath), strerror(errno));
860 * Directory must be scanable by root for cpdup to
861 * work. We'll fix it later if the directory isn't
862 * supposed to be readable ( which is why we fixup
863 * st2.st_mode to match what we did ).
865 if ((st2.st_mode & 0700) != 0700) {
866 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
870 logstd("%s\n", dpath ? dpath : spath);
874 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
880 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
887 * scan .cpignore file for files/directories
893 char *buf = malloc(GETBUFSIZE);
896 if (UseCpFile[0] == '/') {
897 fpath = mprintf("%s", UseCpFile);
899 fpath = mprintf("%s/%s", spath, UseCpFile);
901 AddList(list, strrchr(fpath, '/') + 1, 1);
902 if ((fi = fopen(fpath, "r")) != NULL) {
903 while (fgets(buf, GETBUFSIZE, fi) != NULL) {
905 CountSourceReadBytes += l;
906 if (l && buf[l-1] == '\n')
909 AddList(list, buf, 1);
918 * Automatically exclude MD5CacheFile that we create on the
919 * source from the copy to the destination.
921 * Automatically exclude a FSMIDCacheFile on the source that
922 * would otherwise overwrite the one we maintain on the target.
925 AddList(list, MD5CacheFile, 1);
927 AddList(list, FSMIDCacheFile, 1);
929 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
936 if (strcmp(den->d_name, ".") == 0 ||
937 strcmp(den->d_name, "..") == 0
942 * ignore if on .cpignore list
944 if (AddList(list, den->d_name, 0) == 1) {
947 nspath = mprintf("%s/%s", spath, den->d_name);
949 ndpath = mprintf("%s/%s", dpath, den->d_name);
952 if (CurParallel < MaxParallel || depth > MAXDEPTH) {
953 copy_info_t cinfo = malloc(sizeof(*cinfo));
956 bzero(cinfo, sizeof(*cinfo));
957 cinfo->spath = nspath;
958 cinfo->dpath = ndpath;
959 cinfo->sdevNo = sdevNo;
960 cinfo->ddevNo = ddevNo;
961 cinfo->parent = info;
964 pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
968 info->spath = nspath;
969 info->dpath = ndpath;
970 info->sdevNo = sdevNo;
971 info->ddevNo = ddevNo;
973 r += DoCopy(info, depth);
975 r += DoCopy(info, depth + 1);
984 hc_closedir(&SrcHost, dir);
988 * Wait for our children to finish
990 while (info->children) {
991 pthread_cond_wait(&info->cond, &MasterMutex);
998 * Remove files/directories from destination that do not appear
1001 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
1002 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
1006 if (strcmp(den->d_name, ".") == 0 ||
1007 strcmp(den->d_name, "..") == 0
1012 * If object does not exist in source or .cpignore
1013 * then recursively remove it.
1015 if (AddList(list, den->d_name, 3) == 3) {
1018 ndpath = mprintf("%s/%s", dpath, den->d_name);
1019 RemoveRecur(ndpath, ddevNo);
1023 hc_closedir(&DstHost, dir);
1027 struct timeval tv[2];
1031 st1.st_uid != st2.st_uid ||
1032 st1.st_gid != st2.st_gid
1034 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
1036 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
1037 hc_chmod(&DstHost, dpath, st1.st_mode);
1039 #ifdef _ST_FLAGS_PRESENT_
1040 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
1041 hc_chflags(&DstHost, dpath, st1.st_flags);
1046 st1.st_mtime != st2.st_mtime
1048 bzero(tv, sizeof(tv));
1049 tv[0].tv_sec = st1.st_mtime;
1050 tv[1].tv_sec = st1.st_mtime;
1051 hc_utimes(&DstHost, dpath, tv);
1055 } else if (dpath == NULL) {
1057 * If dpath is NULL, we are just updating the MD5
1060 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
1061 mres = md5_check(spath, NULL);
1063 if (VerboseOpt > 1) {
1065 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1067 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
1068 } else if (!QuietOpt && mres < 0) {
1069 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1073 } else if (S_ISREG(st1.st_mode)) {
1079 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1082 * Handle check failure message.
1086 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1090 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1093 * Not quite ready to do the copy yet. If UseHLPath is defined,
1094 * see if we can hardlink instead.
1096 * If we can hardlink, and the target exists, we have to remove it
1097 * first or the hardlink will fail. This can occur in a number of
1098 * situations but must typically when the '-f -H' combination is
1101 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1103 xremove(&DstHost, dpath);
1104 if (hc_link(&DstHost, hpath, dpath) == 0) {
1107 logstd("%-32s hardlinked(-H)\n",
1108 (dpath ? dpath : spath));
1114 * Shucks, we may have hit a filesystem hard linking limit,
1115 * we have to copy instead.
1120 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1121 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1123 * There could be a .tmp file from a previously interrupted
1124 * run, delete and retry. Fail if we still can't get at it.
1126 #ifdef _ST_FLAGS_PRESENT_
1127 hc_chflags(&DstHost, path, 0);
1129 hc_remove(&DstHost, path);
1130 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1134 char *iobuf1 = malloc(GETIOSIZE);
1138 * Matt: What about holes?
1141 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1143 if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1147 hc_close(&DstHost, fd2);
1149 struct timeval tv[2];
1151 bzero(tv, sizeof(tv));
1152 tv[0].tv_sec = st1.st_mtime;
1153 tv[1].tv_sec = st1.st_mtime;
1155 hc_utimes(&DstHost, path, tv);
1156 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1157 hc_chmod(&DstHost, path, st1.st_mode);
1158 if (xrename(path, dpath, st2.st_flags) != 0) {
1159 logerr("%-32s rename-after-copy failed: %s\n",
1160 (dpath ? dpath : spath), strerror(errno)
1165 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1166 #ifdef _ST_FLAGS_PRESENT_
1168 hc_chflags(&DstHost, dpath, st1.st_flags);
1171 CountSourceReadBytes += size;
1172 CountWriteBytes += size;
1173 CountSourceBytes += size;
1177 logerr("%-32s %s failed: %s\n",
1178 (dpath ? dpath : spath), op, strerror(errno)
1180 hc_remove(&DstHost, path);
1185 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1186 (dpath ? dpath : spath), getuid(), geteuid(),
1191 hc_close(&SrcHost, fd1);
1193 logerr("%-32s copy: open failed: %s\n",
1194 (dpath ? dpath : spath),
1203 if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1204 hltsetdino(hln, st2.st_ino);
1210 } else if (S_ISLNK(st1.st_mode)) {
1211 char *link1 = malloc(GETLINKSIZE);
1212 char *link2 = malloc(GETLINKSIZE);
1213 char *path = malloc(GETPATHSIZE);
1217 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1218 n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1219 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1221 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1222 hc_umask(&DstHost, ~st1.st_mode);
1223 xremove(&DstHost, path);
1225 if (hc_symlink(&DstHost, link1, path) < 0) {
1226 logerr("%-32s symlink (%s->%s) failed: %s\n",
1227 (dpath ? dpath : spath), link1, path,
1232 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1234 * there is no lchmod() or lchflags(), we
1235 * cannot chmod or chflags a softlink.
1237 if (xrename(path, dpath, st2.st_flags) != 0) {
1238 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1239 (dpath ? dpath : spath),
1240 path, dpath, strerror(errno));
1241 } else if (VerboseOpt) {
1242 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1244 hc_umask(&DstHost, 000);
1245 CountWriteBytes += n1;
1249 if (VerboseOpt >= 3)
1250 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1252 CountSourceBytes += n1;
1253 CountSourceReadBytes += n1;
1255 CountTargetReadBytes += n2;
1259 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1264 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1265 char *path = malloc(GETPATHSIZE);
1269 st1.st_mode != st2.st_mode ||
1270 st1.st_rdev != st2.st_rdev ||
1271 st1.st_uid != st2.st_uid ||
1272 st1.st_gid != st2.st_gid
1274 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1276 xremove(&DstHost, path);
1277 if (hc_mknod(&DstHost, path, st1.st_mode, st1.st_rdev) == 0) {
1278 hc_chmod(&DstHost, path, st1.st_mode);
1279 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1280 xremove(&DstHost, dpath);
1281 if (xrename(path, dpath, st2.st_flags) != 0) {
1282 logerr("%-32s dev-rename-after-create failed: %s\n",
1283 (dpath ? dpath : spath),
1286 } else if (VerboseOpt) {
1287 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1292 logerr("%-32s dev failed: %s\n",
1293 (dpath ? dpath : spath), strerror(errno)
1297 if (VerboseOpt >= 3)
1298 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1305 if (hln->dino == (ino_t)-1) {
1307 /*hln = NULL; unneeded */
1322 RemoveRecur(const char *dpath, dev_t devNo)
1326 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1329 if (st.st_dev == devNo) {
1330 if (S_ISDIR(st.st_mode)) {
1333 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1335 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1338 if (strcmp(den->d_name, ".") == 0)
1340 if (strcmp(den->d_name, "..") == 0)
1342 ndpath = mprintf("%s/%s", dpath, den->d_name);
1343 RemoveRecur(ndpath, devNo);
1346 hc_closedir(&DstHost, dir);
1348 if (AskConfirmation && NoRemoveOpt == 0) {
1350 if (hc_rmdir(&DstHost, dpath) < 0) {
1351 logerr("%-32s rmdir failed: %s\n",
1352 dpath, strerror(errno)
1355 CountRemovedItems++;
1360 logstd("%-32s not-removed\n", dpath);
1361 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1363 logstd("%-32s rmdir-ok\n", dpath);
1364 CountRemovedItems++;
1366 logerr("%-32s rmdir failed: %s\n",
1367 dpath, strerror(errno)
1372 if (AskConfirmation && NoRemoveOpt == 0) {
1374 if (xremove(&DstHost, dpath) < 0) {
1375 logerr("%-32s remove failed: %s\n",
1376 dpath, strerror(errno)
1379 CountRemovedItems++;
1384 logstd("%-32s not-removed\n", dpath);
1385 } else if (xremove(&DstHost, dpath) == 0) {
1387 logstd("%-32s remove-ok\n", dpath);
1388 CountRemovedItems++;
1390 logerr("%-32s remove failed: %s\n",
1391 dpath, strerror(errno)
1401 InitList(List *list)
1403 bzero(list, sizeof(List));
1404 list->li_Node.no_Next = &list->li_Node;
1408 ResetList(List *list)
1412 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1413 list->li_Node.no_Next = node->no_Next;
1420 AddList(List *list, const char *name, int n)
1428 * Scan against wildcards. Only a node value of 1 can be a wildcard
1429 * ( usually scanned from .cpignore )
1432 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1433 if (strcmp(name, node->no_Name) == 0 ||
1434 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1436 return(node->no_Value);
1441 * Look for exact match
1444 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1445 if (strcmp(name, node->no_Name) == 0) {
1446 return(node->no_Value);
1449 node = malloc(sizeof(Node) + strlen(name) + 1);
1451 fprintf(stderr, "out of memory\n");
1455 node->no_Next = list->li_Node.no_Next;
1456 list->li_Node.no_Next = node;
1458 node->no_HNext = list->li_Hash[hv];
1459 list->li_Hash[hv] = node;
1461 strcpy(node->no_Name, name);
1468 shash(const char *s)
1475 if (*s == '*' || *s == '?' ||
1476 *s == '{' || *s == '}' ||
1477 *s == '[' || *s == ']' ||
1482 hv = (hv << 5) ^ *s ^ (hv >> 23);
1485 return(((hv >> 16) ^ hv) & HMASK);
1489 * WildCmp() - compare wild string to sane string
1491 * Return 0 on success, -1 on failure.
1495 WildCmp(const char *w, const char *s)
1498 * skip fixed portion
1504 if (w[1] == 0) /* optimize wild* case */
1510 for (i = 0; i <= l; ++i) {
1511 if (WildCmp(w + 1, s + i) == 0)
1525 if (*w == 0) /* terminator */
1537 YesNo(const char *path)
1541 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1544 first = ch = getchar();
1545 while (ch != '\n' && ch != EOF)
1547 return ((first == 'y' || first == 'Y'));
1551 * xrename() - rename with override
1553 * If the rename fails, attempt to override st_flags on the
1554 * destination and rename again. If that fails too, try to
1555 * set the flags back the way they were and give up.
1559 xrename(const char *src, const char *dst, u_long flags)
1565 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1566 #ifdef _ST_FLAGS_PRESENT_
1567 hc_chflags(&DstHost, dst, 0);
1568 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1569 hc_chflags(&DstHost, dst, flags);
1576 xlink(const char *src, const char *dst, u_long flags)
1579 #ifdef _ST_FLAGS_PRESENT_
1585 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1586 #ifdef _ST_FLAGS_PRESENT_
1587 hc_chflags(&DstHost, src, 0);
1588 r = hc_link(&DstHost, src, dst);
1590 hc_chflags(&DstHost, src, flags);
1600 xremove(struct HostConf *host, const char *path)
1604 res = hc_remove(host, path);
1605 #ifdef _ST_FLAGS_PRESENT_
1606 if (res == -EPERM) {
1607 hc_chflags(host, path, 0);
1608 res = hc_remove(host, path);