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.20 2008/04/10 22:09:08 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 #ifndef _ST_FLAGS_PRESENT_
68 #define st_flags st_mode
73 struct Node *no_HNext;
92 typedef struct copy_info {
98 struct copy_info *parent;
105 struct hlink *hltable[HLSIZE];
107 void RemoveRecur(const char *dpath, dev_t devNo);
108 void InitList(List *list);
109 void ResetList(List *list);
110 int AddList(List *list, const char *name, int n);
111 static struct hlink *hltlookup(struct stat *);
112 static struct hlink *hltadd(struct stat *, const char *);
113 static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
114 static int validate_check(const char *spath, const char *dpath);
115 static int shash(const char *s);
116 static void hltdelete(struct hlink *);
117 int YesNo(const char *path);
118 static int xrename(const char *src, const char *dst, u_long flags);
119 static int xlink(const char *src, const char *dst, u_long flags);
120 int WildCmp(const char *s1, const char *s2);
121 static int DoCopy(copy_info_t info);
123 int AskConfirmation = 1;
134 int EnableDirectoryRetries;
138 int MaxParallel = -1;
141 const char *UseCpFile;
142 const char *UseHLPath;
143 const char *MD5CacheFile;
144 const char *FSMIDCacheFile;
146 int64_t CountSourceBytes;
147 int64_t CountSourceItems;
148 int64_t CountCopiedItems;
149 int64_t CountSourceReadBytes;
150 int64_t CountTargetReadBytes;
151 int64_t CountWriteBytes;
152 int64_t CountRemovedItems;
153 int64_t CountLinkedItems;
155 struct HostConf SrcHost;
156 struct HostConf DstHost;
159 pthread_mutex_t MasterMutex;
163 main(int ac, char **av)
169 struct timeval start;
170 struct copy_info info;
172 signal(SIGPIPE, SIG_IGN);
175 pthread_mutex_init(&SrcHost.read_mutex, NULL);
176 pthread_mutex_init(&DstHost.read_mutex, NULL);
177 pthread_mutex_init(&MasterMutex, NULL);
178 pthread_mutex_lock(&MasterMutex);
181 gettimeofday(&start, NULL);
182 for (i = 1; i < ac; ++i) {
189 } else if (dst == NULL) {
192 fatal("too many arguments");
200 v = strtol(ptr, NULL, 0);
205 while (*ptr == 'v') {
209 if (*ptr >= '0' && *ptr <= '9')
210 VerboseOpt = strtol(ptr, NULL, 0);
226 UseCpFile = ".cpignore";
229 UseCpFile = (*ptr) ? ptr : av[++i];
232 UseHLPath = (*ptr) ? ptr : av[++i];
257 FSMIDCacheFile = ".FSMID.CHECK";
261 FSMIDCacheFile = av[++i];
265 MD5CacheFile = av[++i];
269 MD5CacheFile = ".MD5.CHECKSUMS";
272 setvbuf(stdout, NULL, _IOLBF, 0);
275 fatal("illegal option: %s\n", ptr - 2);
282 * If we are told to go into slave mode, run the HC protocol
290 * Extract the source and/or/neither target [user@]host and
291 * make any required connections.
293 if (src && (ptr = strchr(src, ':')) != NULL) {
294 asprintf(&SrcHost.host, "%*.*s", ptr - src, ptr - src, src);
297 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
301 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
304 if (hc_connect(&SrcHost) < 0)
305 fprintf(stderr, "Unable to connect to %s\n", SrcHost.host);
307 if (dst && (ptr = strchr(dst, ':')) != NULL) {
308 asprintf(&DstHost.host, "%*.*s", ptr - dst, ptr - dst, dst);
311 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
314 if (hc_connect(&DstHost) < 0)
315 fprintf(stderr, "Unable to connect to %s\n", DstHost.host);
319 * dst may be NULL only if -m option is specified,
320 * which forces an update of the MD5 checksums
322 if (dst == NULL && UseMD5Opt == 0) {
329 pthread_cond_init(&info.cond, NULL);
332 DstBaseLen = strlen(dst);
335 info.sdevNo = (dev_t)-1;
336 info.ddevNo = (dev_t)-1;
341 info.sdevNo = (dev_t)-1;
342 info.ddevNo = (dev_t)-1;
346 pthread_cond_destroy(&info.cond);
353 if (SummaryOpt && i == 0) {
357 gettimeofday(&end, NULL);
359 /* don't count stat's in our byte statistics */
360 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
361 CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
362 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
363 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
366 duration = end.tv_sec - start.tv_sec;
368 duration += end.tv_usec - start.tv_usec;
369 if (duration == 0) duration = 1;
370 logstd("cpdup completed successfully\n");
371 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
372 "%lld bytes written (%.1fX speedup)\n",
373 (long long)CountSourceBytes,
374 (long long)CountSourceReadBytes,
375 (long long)CountTargetReadBytes,
376 (long long)CountWriteBytes,
377 ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
378 logstd("%lld source items, %lld items copied, %lld items linked, "
379 "%lld things deleted\n",
380 (long long)CountSourceItems,
381 (long long)CountCopiedItems,
382 (long long)CountLinkedItems,
383 (long long)CountRemovedItems);
384 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
385 (float)duration / (float)1000000,
386 (long)((long)1000000 * (CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration / 1024.0),
387 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
389 exit((i == 0) ? 0 : 1);
392 static struct hlink *
393 hltlookup(struct stat *stp)
398 n = stp->st_ino & HLMASK;
400 for (hl = hltable[n]; hl; hl = hl->next)
401 if (hl->ino == stp->st_ino)
407 static struct hlink *
408 hltadd(struct stat *stp, const char *path)
411 int plen = strlen(path);
414 new = malloc(offsetof(struct hlink, name[plen + 1]));
416 fprintf(stderr, "out of memory\n");
420 /* initialize and link the new element into the table */
421 new->ino = stp->st_ino;
423 bcopy(path, new->name, plen + 1);
426 n = stp->st_ino & HLMASK;
427 new->next = hltable[n];
429 hltable[n]->prev = new;
436 hltdelete(struct hlink *hl)
440 hl->next->prev = hl->prev;
441 hl->prev->next = hl->next;
444 hl->next->prev = NULL;
446 hltable[hl->ino & HLMASK] = hl->next;
453 * If UseHLPath is defined check to see if the file in question is
454 * the same as the source file, and if it is return a pointer to the
455 * -H path based file for hardlinking. Else return NULL.
458 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
464 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
467 * stat info matches ?
469 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
470 st1->st_size != sthl.st_size ||
471 st1->st_uid != sthl.st_uid ||
472 st1->st_gid != sthl.st_gid ||
473 st1->st_mtime != sthl.st_mtime
480 * If ForceOpt or ValidateOpt is set we have to compare the files
482 if (ForceOpt || ValidateOpt) {
483 error = validate_check(spath, hpath);
493 * Return 0 if the contents of the file <spath> matches the contents of
497 validate_check(const char *spath, const char *dpath)
503 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
504 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
507 if (fd1 >= 0 && fd2 >= 0) {
511 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
512 CountSourceReadBytes += n;
513 x = hc_read(&DstHost, fd2, IOBuf2, sizeof(IOBuf2));
515 CountTargetReadBytes += x;
518 if (bcmp(IOBuf1, IOBuf2, n) != 0)
525 hc_close(&SrcHost, fd1);
527 hc_close(&DstHost, fd2);
533 DoCopyThread(void *arg)
535 copy_info_t cinfo = arg;
536 char *spath = cinfo->spath;
537 char *dpath = cinfo->dpath;
539 pthread_cond_init(&cinfo->cond, NULL);
540 pthread_mutex_lock(&MasterMutex);
541 cinfo->r += DoCopy(cinfo);
542 /* cinfo arguments invalid on return */
543 --cinfo->parent->children;
545 pthread_cond_signal(&cinfo->parent->cond);
546 pthread_mutex_unlock(&MasterMutex);
550 pthread_cond_destroy(&cinfo->cond);
558 DoCopy(copy_info_t info)
560 const char *spath = info->spath;
561 const char *dpath = info->dpath;
562 dev_t sdevNo = info->sdevNo;
563 dev_t ddevNo = info->ddevNo;
566 int r, mres, fres, st2Valid;
568 List *list = malloc(sizeof(List));
572 r = mres = fres = st2Valid = 0;
576 if (hc_lstat(&SrcHost, spath, &st1) != 0) {
580 st2.st_mode = 0; /* in case lstat fails */
581 st2.st_flags = 0; /* in case lstat fails */
582 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
585 if (S_ISREG(st1.st_mode)) {
593 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
594 if ((hln = hltlookup(&st1)) != NULL) {
598 if (st2.st_ino == hln->dino) {
600 * hard link is already correct, nothing to do
603 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
604 if (hln->nlinked == st1.st_nlink)
611 * hard link is not correct, attempt to unlink it
613 if (hc_remove(&DstHost, dpath) < 0) {
614 logerr("%-32s hardlink: unable to unlink: %s\n",
615 ((dpath) ? dpath : spath), strerror(errno));
622 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
623 int tryrelink = (errno == EMLINK);
624 logerr("%-32s hardlink: unable to link to %s: %s\n",
625 (dpath ? dpath : spath), hln->name, strerror(errno)
630 logerr("%-20s hardlink: will attempt to copy normally\n");
635 if (hln->nlinked == st1.st_nlink) {
641 logstd("%-32s hardlink: %s\n",
642 (dpath ? dpath : spath),
643 (st2Valid ? "relinked" : "linked")
654 * first instance of hardlink must be copied normally
657 hln = hltadd(&st1, dpath);
662 * Do we need to copy the file/dir/link/whatever? Early termination
663 * if we do not. Always redo links. Directories are always traversed
664 * except when the FSMID options are used.
666 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
671 && st1.st_mode == st2.st_mode
672 #ifdef _ST_FLAGS_PRESENT_
673 && st1.st_flags == st2.st_flags
676 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
678 * If FSMID tracking is turned on we can avoid recursing through
679 * an entire directory subtree if the FSMID matches.
681 #ifdef _ST_FSMID_PRESENT_
683 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
685 if (VerboseOpt >= 3) {
687 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
689 logstd("%-32s nochange\n", (dpath ? dpath : spath));
697 st1.st_size == st2.st_size &&
698 st1.st_uid == st2.st_uid &&
699 st1.st_gid == st2.st_gid &&
700 st1.st_mtime == st2.st_mtime
702 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
703 (mres = md5_check(spath, dpath)) == 0)
705 #ifdef _ST_FSMID_PRESENT_
706 && (UseFSMIDOpt == 0 ||
707 (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
709 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
710 validate_check(spath, dpath) == 0)
713 hln->dino = st2.st_ino;
714 if (VerboseOpt >= 3) {
717 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
721 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
722 else if (ValidateOpt)
723 logstd("%-32s nochange (contents validated)\n", (dpath ? dpath : spath));
725 logstd("%-32s nochange\n", (dpath ? dpath : spath));
727 CountSourceBytes += size;
734 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
736 logerr("%-32s SAFETY - refusing to copy file over directory\n",
737 (dpath ? dpath : spath)
741 goto done; /* continue with the cpdup anyway */
743 if (QuietOpt == 0 || AskConfirmation) {
744 logstd("%-32s WARNING: non-directory source will blow away\n"
745 "%-32s preexisting dest directory, continuing anyway!\n",
746 ((dpath) ? dpath : spath), "");
749 RemoveRecur(dpath, ddevNo);
753 * The various comparisons failed, copy it.
755 if (S_ISDIR(st1.st_mode)) {
759 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
760 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
765 if (S_ISDIR(st2.st_mode) == 0) {
766 hc_remove(&DstHost, dpath);
767 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
768 logerr("%s: mkdir failed: %s\n",
769 (dpath ? dpath : spath), strerror(errno));
774 * Matt: why don't you check error codes here?
776 hc_lstat(&DstHost, dpath, &st2);
777 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
781 * Directory must be scanable by root for cpdup to
782 * work. We'll fix it later if the directory isn't
783 * supposed to be readable ( which is why we fixup
784 * st2.st_mode to match what we did ).
786 if ((st2.st_mode & 0700) != 0700) {
787 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
791 logstd("%s\n", dpath ? dpath : spath);
795 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
801 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
808 * scan .cpignore file for files/directories
817 if (UseCpFile[0] == '/') {
818 fpath = mprintf("%s", UseCpFile);
820 fpath = mprintf("%s/%s", spath, UseCpFile);
822 AddList(list, strrchr(fpath, '/') + 1, 1);
823 if ((fi = fopen(fpath, "r")) != NULL) {
824 while (fgets(buf, sizeof(buf), fi) != NULL) {
826 CountSourceReadBytes += l;
827 if (l && buf[l-1] == '\n')
829 if (buf[0] && buf[0] != '#')
830 AddList(list, buf, 1);
838 * Automatically exclude MD5CacheFile that we create on the
839 * source from the copy to the destination.
841 * Automatically exclude a FSMIDCacheFile on the source that
842 * would otherwise overwrite the one we maintain on the target.
845 AddList(list, MD5CacheFile, 1);
847 AddList(list, FSMIDCacheFile, 1);
849 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
856 if (strcmp(den->d_name, ".") == 0 ||
857 strcmp(den->d_name, "..") == 0
862 * ignore if on .cpignore list
864 if (AddList(list, den->d_name, 0) == 1) {
867 nspath = mprintf("%s/%s", spath, den->d_name);
869 ndpath = mprintf("%s/%s", dpath, den->d_name);
872 if (CurParallel < MaxParallel) {
873 copy_info_t cinfo = malloc(sizeof(*cinfo));
874 pthread_t dummy_thr = NULL;
876 bzero(cinfo, sizeof(*cinfo));
877 cinfo->spath = nspath;
878 cinfo->dpath = ndpath;
879 cinfo->sdevNo = sdevNo;
880 cinfo->ddevNo = ddevNo;
881 cinfo->parent = info;
884 pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
888 info->spath = nspath;
889 info->dpath = ndpath;
890 info->sdevNo = sdevNo;
891 info->ddevNo = ddevNo;
899 hc_closedir(&SrcHost, dir);
903 * Wait for our children to finish
905 while (info->children) {
906 pthread_cond_wait(&info->cond, &MasterMutex);
913 * Remove files/directories from destination that do not appear
916 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
917 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
921 if (strcmp(den->d_name, ".") == 0 ||
922 strcmp(den->d_name, "..") == 0
927 * If object does not exist in source or .cpignore
928 * then recursively remove it.
930 if (AddList(list, den->d_name, 3) == 3) {
933 ndpath = mprintf("%s/%s", dpath, den->d_name);
934 RemoveRecur(ndpath, ddevNo);
938 hc_closedir(&DstHost, dir);
942 struct timeval tv[2];
946 st1.st_uid != st2.st_uid ||
947 st1.st_gid != st2.st_gid
949 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
951 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
952 hc_chmod(&DstHost, dpath, st1.st_mode);
954 #ifdef _ST_FLAGS_PRESENT_
955 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
956 hc_chflags(&DstHost, dpath, st1.st_flags);
961 st1.st_mtime != st2.st_mtime
963 bzero(tv, sizeof(tv));
964 tv[0].tv_sec = st1.st_mtime;
965 tv[1].tv_sec = st1.st_mtime;
966 hc_utimes(&DstHost, dpath, tv);
970 } else if (dpath == NULL) {
972 * If dpath is NULL, we are just updating the MD5
975 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
976 mres = md5_check(spath, NULL);
978 if (VerboseOpt > 1) {
980 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
982 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
983 } else if (!QuietOpt && mres < 0) {
984 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
988 } else if (S_ISREG(st1.st_mode)) {
994 path = mprintf("%s.tmp", dpath);
997 * Handle check failure message.
1001 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1005 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1008 * Not quite ready to do the copy yet. If UseHLPath is defined,
1009 * see if we can hardlink instead.
1011 * If we can hardlink, and the target exists, we have to remove it
1012 * first or the hardlink will fail. This can occur in a number of
1013 * situations but must typically when the '-f -H' combination is
1016 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1018 hc_remove(&DstHost, dpath);
1019 if (hc_link(&DstHost, hpath, dpath) == 0) {
1022 logstd("%-32s hardlinked(-H)\n",
1023 (dpath ? dpath : spath));
1029 * Shucks, we may have hit a filesystem hard linking limit,
1030 * we have to copy instead.
1035 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1036 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1038 * There could be a .tmp file from a previously interrupted
1039 * run, delete and retry. Fail if we still can't get at it.
1041 #ifdef _ST_FLAGS_PRESENT_
1042 hc_chflags(&DstHost, path, 0);
1044 hc_remove(&DstHost, path);
1045 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1052 * Matt: What about holes?
1055 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
1057 if (hc_write(&DstHost, fd2, IOBuf1, n) != n)
1061 hc_close(&DstHost, fd2);
1063 struct timeval tv[2];
1065 bzero(tv, sizeof(tv));
1066 tv[0].tv_sec = st1.st_mtime;
1067 tv[1].tv_sec = st1.st_mtime;
1069 hc_utimes(&DstHost, path, tv);
1070 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1071 hc_chmod(&DstHost, path, st1.st_mode);
1072 if (xrename(path, dpath, st2.st_flags) != 0) {
1073 logerr("%-32s rename-after-copy failed: %s\n",
1074 (dpath ? dpath : spath), strerror(errno)
1079 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1080 #ifdef _ST_FLAGS_PRESENT_
1082 hc_chflags(&DstHost, dpath, st1.st_flags);
1085 CountSourceReadBytes += size;
1086 CountWriteBytes += size;
1087 CountSourceBytes += size;
1091 logerr("%-32s %s failed: %s\n",
1092 (dpath ? dpath : spath), op, strerror(errno)
1094 hc_remove(&DstHost, path);
1098 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1099 (dpath ? dpath : spath), getuid(), geteuid(),
1104 hc_close(&SrcHost, fd1);
1106 logerr("%-32s copy: open failed: %s\n",
1107 (dpath ? dpath : spath),
1116 if (!r && hc_stat(&DstHost, dpath, &st2) == 0)
1117 hln->dino = st2.st_ino;
1121 } else if (S_ISLNK(st1.st_mode)) {
1128 snprintf(path, sizeof(path), "%s.tmp", dpath);
1129 n1 = hc_readlink(&SrcHost, spath, link1, sizeof(link1) - 1);
1130 n2 = hc_readlink(&DstHost, dpath, link2, sizeof(link2) - 1);
1132 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1133 hc_umask(&DstHost, ~st1.st_mode);
1134 hc_remove(&DstHost, path);
1136 if (hc_symlink(&DstHost, link1, path) < 0) {
1137 logerr("%-32s symlink (%s->%s) failed: %s\n",
1138 (dpath ? dpath : spath), link1, path,
1143 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1145 * there is no lchmod() or lchflags(), we
1146 * cannot chmod or chflags a softlink.
1148 if (xrename(path, dpath, st2.st_flags) != 0) {
1149 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1150 (dpath ? dpath : spath),
1151 path, dpath, strerror(errno));
1152 } else if (VerboseOpt) {
1153 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1155 hc_umask(&DstHost, 000);
1156 CountWriteBytes += n1;
1160 if (VerboseOpt >= 3)
1161 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1163 CountSourceBytes += n1;
1164 CountSourceReadBytes += n1;
1166 CountTargetReadBytes += n2;
1170 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1172 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1177 st1.st_mode != st2.st_mode ||
1178 st1.st_rdev != st2.st_rdev ||
1179 st1.st_uid != st2.st_uid ||
1180 st1.st_gid != st2.st_gid
1182 snprintf(path, sizeof(path), "%s.tmp", dpath);
1184 hc_remove(&DstHost, path);
1185 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
1186 hc_chmod(&DstHost, path, st1.st_mode);
1187 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1188 hc_remove(&DstHost, dpath);
1189 if (xrename(path, dpath, st2.st_flags) != 0) {
1190 logerr("%-32s dev-rename-after-create failed: %s\n",
1191 (dpath ? dpath : spath),
1194 } else if (VerboseOpt) {
1195 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1200 logerr("%-32s dev failed: %s\n",
1201 (dpath ? dpath : spath), strerror(errno)
1205 if (VerboseOpt >= 3)
1206 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1221 RemoveRecur(const char *dpath, dev_t devNo)
1225 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1228 if (st.st_dev == devNo) {
1229 if (S_ISDIR(st.st_mode)) {
1232 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1234 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1237 if (strcmp(den->d_name, ".") == 0)
1239 if (strcmp(den->d_name, "..") == 0)
1241 ndpath = mprintf("%s/%s", dpath, den->d_name);
1242 RemoveRecur(ndpath, devNo);
1245 hc_closedir(&DstHost, dir);
1247 if (AskConfirmation && NoRemoveOpt == 0) {
1249 if (hc_rmdir(&DstHost, dpath) < 0) {
1250 logerr("%-32s rmdir failed: %s\n",
1251 dpath, strerror(errno)
1254 CountRemovedItems++;
1259 logstd("%-32s not-removed\n", dpath);
1260 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1262 logstd("%-32s rmdir-ok\n", dpath);
1263 CountRemovedItems++;
1265 logerr("%-32s rmdir failed: %s\n",
1266 dpath, strerror(errno)
1271 if (AskConfirmation && NoRemoveOpt == 0) {
1273 if (hc_remove(&DstHost, dpath) < 0) {
1274 logerr("%-32s remove failed: %s\n",
1275 dpath, strerror(errno)
1278 CountRemovedItems++;
1283 logstd("%-32s not-removed\n", dpath);
1284 } else if (hc_remove(&DstHost, dpath) == 0) {
1286 logstd("%-32s remove-ok\n", dpath);
1287 CountRemovedItems++;
1289 logerr("%-32s remove failed: %s\n",
1290 dpath, strerror(errno)
1300 InitList(List *list)
1302 bzero(list, sizeof(List));
1303 list->li_Node.no_Next = &list->li_Node;
1307 ResetList(List *list)
1311 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1312 list->li_Node.no_Next = node->no_Next;
1319 AddList(List *list, const char *name, int n)
1327 * Scan against wildcards. Only a node value of 1 can be a wildcard
1328 * ( usually scanned from .cpignore )
1331 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1332 if (strcmp(name, node->no_Name) == 0 ||
1333 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1335 return(node->no_Value);
1340 * Look for exact match
1343 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1344 if (strcmp(name, node->no_Name) == 0) {
1345 return(node->no_Value);
1348 node = malloc(sizeof(Node) + strlen(name) + 1);
1350 fprintf(stderr, "out of memory\n");
1354 node->no_Next = list->li_Node.no_Next;
1355 list->li_Node.no_Next = node;
1357 node->no_HNext = list->li_Hash[hv];
1358 list->li_Hash[hv] = node;
1360 strcpy(node->no_Name, name);
1367 shash(const char *s)
1374 if (*s == '*' || *s == '?' ||
1375 *s == '{' || *s == '}' ||
1376 *s == '[' || *s == ']' ||
1381 hv = (hv << 5) ^ *s ^ (hv >> 23);
1384 return(((hv >> 16) ^ hv) & HMASK);
1388 * WildCmp() - compare wild string to sane string
1390 * Return 0 on success, -1 on failure.
1394 WildCmp(const char *w, const char *s)
1397 * skip fixed portion
1403 if (w[1] == 0) /* optimize wild* case */
1409 for (i = 0; i <= l; ++i) {
1410 if (WildCmp(w + 1, s + i) == 0)
1424 if (*w == 0) /* terminator */
1436 YesNo(const char *path)
1440 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1443 first = ch = getchar();
1444 while (ch != '\n' && ch != EOF)
1446 return ((first == 'y' || first == 'Y'));
1450 * xrename() - rename with override
1452 * If the rename fails, attempt to override st_flags on the
1453 * destination and rename again. If that fails too, try to
1454 * set the flags back the way they were and give up.
1458 xrename(const char *src, const char *dst, u_long flags)
1464 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1465 #ifdef _ST_FLAGS_PRESENT_
1466 hc_chflags(&DstHost, dst, 0);
1467 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1468 hc_chflags(&DstHost, dst, flags);
1475 xlink(const char *src, const char *dst, u_long flags)
1478 #ifdef _ST_FLAGS_PRESENT_
1484 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1485 #ifdef _ST_FLAGS_PRESENT_
1486 hc_chflags(&DstHost, src, 0);
1487 r = hc_link(&DstHost, src, dst);
1489 hc_chflags(&DstHost, src, flags);