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.13 2006/08/13 20:51:40 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 struct hlink *hltable[HLSIZE];
94 void RemoveRecur(const char *dpath, dev_t devNo);
95 void InitList(List *list);
96 void ResetList(List *list);
97 int AddList(List *list, const char *name, int n);
98 static struct hlink *hltlookup(struct stat *);
99 static struct hlink *hltadd(struct stat *, const char *);
100 static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
101 static int shash(const char *s);
102 static void hltdelete(struct hlink *);
103 int YesNo(const char *path);
104 static int xrename(const char *src, const char *dst, u_long flags);
105 static int xlink(const char *src, const char *dst, u_long flags);
106 int WildCmp(const char *s1, const char *s2);
107 int DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo);
109 int AskConfirmation = 1;
119 int EnableDirectoryRetries;
123 const char *UseCpFile;
124 const char *UseHLPath;
125 const char *MD5CacheFile;
126 const char *FSMIDCacheFile;
128 int64_t CountSourceBytes;
129 int64_t CountSourceItems;
130 int64_t CountCopiedItems;
131 int64_t CountReadBytes;
132 int64_t CountWriteBytes;
133 int64_t CountRemovedItems;
135 struct HostConf SrcHost;
136 struct HostConf DstHost;
139 main(int ac, char **av)
145 struct timeval start;
147 gettimeofday(&start, NULL);
148 for (i = 1; i < ac; ++i) {
155 } else if (dst == NULL) {
158 fatal("too many arguments");
166 v = strtol(ptr, NULL, 0);
171 while (*ptr == 'v') {
175 if (*ptr >= '0' && *ptr <= '9')
176 VerboseOpt = strtol(ptr, NULL, 0);
185 UseCpFile = ".cpignore";
188 UseCpFile = (*ptr) ? ptr : av[++i];
191 UseHLPath = (*ptr) ? ptr : av[++i];
210 FSMIDCacheFile = ".FSMID.CHECK";
214 FSMIDCacheFile = av[++i];
218 MD5CacheFile = av[++i];
222 MD5CacheFile = ".MD5.CHECKSUMS";
225 setvbuf(stdout, NULL, _IOLBF, 0);
228 fatal("illegal option: %s\n", ptr - 2);
235 * If we are told to go into slave mode, run the HC protocol
243 * Extract the source and/or/neither target [user@]host and
244 * make any required connections.
246 if (src && (ptr = strchr(src, ':')) != NULL) {
247 asprintf(&SrcHost.host, "%*.*s", ptr - src, ptr - src, src);
250 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
254 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
257 if (hc_connect(&SrcHost) < 0)
258 fprintf(stderr, "Unable to connect to %s\n", SrcHost.host);
260 if (dst && (ptr = strchr(dst, ':')) != NULL) {
261 asprintf(&DstHost.host, "%*.*s", ptr - dst, ptr - dst, dst);
264 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
267 if (hc_connect(&DstHost) < 0)
268 fprintf(stderr, "Unable to connect to %s\n", DstHost.host);
272 * dst may be NULL only if -m option is specified,
273 * which forces an update of the MD5 checksums
275 if (dst == NULL && UseMD5Opt == 0) {
280 DstBaseLen = strlen(dst);
281 i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
283 i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
290 if (SummaryOpt && i == 0) {
294 gettimeofday(&end, NULL);
295 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
296 CountReadBytes += sizeof(struct stat) * CountSourceItems;
297 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
298 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
300 duration = end.tv_sec - start.tv_sec;
302 duration += end.tv_usec - start.tv_usec;
303 if (duration == 0) duration = 1;
304 logstd("cpdup completed successfully\n");
305 logstd("%lld bytes source %lld bytes read %lld bytes written (%.1fX speedup)\n",
306 (long long)CountSourceBytes,
307 (long long)CountReadBytes,
308 (long long)CountWriteBytes,
309 ((double)CountSourceBytes * 2.0) / ((double)(CountReadBytes + CountWriteBytes)));
310 logstd("%lld source items %lld items copied %lld things deleted\n",
311 (long long)CountSourceItems,
312 (long long)CountCopiedItems,
313 (long long)CountRemovedItems);
314 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
315 (float)duration / (float)1000000,
316 (long)((long)1000000 * (CountReadBytes + CountWriteBytes) / duration / 1024.0),
317 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
319 exit((i == 0) ? 0 : 1);
322 static struct hlink *
323 hltlookup(struct stat *stp)
328 n = stp->st_ino & HLMASK;
330 for (hl = hltable[n]; hl; hl = hl->next)
331 if (hl->ino == stp->st_ino)
337 static struct hlink *
338 hltadd(struct stat *stp, const char *path)
343 if (!(new = malloc(sizeof (struct hlink)))) {
344 fprintf(stderr, "out of memory\n");
348 /* initialize and link the new element into the table */
349 new->ino = stp->st_ino;
351 strncpy(new->name, path, 2048);
354 n = stp->st_ino & HLMASK;
355 new->next = hltable[n];
357 hltable[n]->prev = new;
364 hltdelete(struct hlink *hl)
368 hl->next->prev = hl->prev;
369 hl->prev->next = hl->next;
372 hl->next->prev = NULL;
374 hltable[hl->ino & HLMASK] = hl->next;
381 * If UseHLPath is defined check to see if the file in question is
382 * the same as the source file, and if it is return a pointer to the
383 * -H path based file for hardlinking. Else return NULL.
386 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
394 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
397 * stat info matches ?
399 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
400 st1->st_size != sthl.st_size ||
401 st1->st_uid != sthl.st_uid ||
402 st1->st_gid != sthl.st_gid ||
403 st1->st_mtime != sthl.st_mtime
410 * If ForceOpt is set we have to compare the files
413 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
414 fd2 = hc_open(&DstHost, hpath, O_RDONLY, 0);
417 if (fd1 >= 0 && fd2 >= 0) {
420 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
421 if (hc_read(&DstHost, fd2, IOBuf2, sizeof(IOBuf2)) != n)
423 if (bcmp(IOBuf1, IOBuf2, n) != 0)
430 hc_close(&SrcHost, fd1);
432 hc_close(&DstHost, fd2);
442 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
446 int r, mres, fres, st2Valid;
452 r = mres = fres = st2Valid = 0;
456 if (hc_lstat(&SrcHost, spath, &st1) != 0)
458 st2.st_mode = 0; /* in case lstat fails */
459 st2.st_flags = 0; /* in case lstat fails */
460 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
463 if (S_ISREG(st1.st_mode)) {
464 size = st1.st_blocks * 512;
465 if (st1.st_size % 512)
466 size += st1.st_size % 512 - 512;
473 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
474 if ((hln = hltlookup(&st1)) != NULL) {
478 if (st2.st_ino == hln->dino) {
480 * hard link is already correct, nothing to do
483 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
484 if (hln->nlinked == st1.st_nlink)
490 * hard link is not correct, attempt to unlink it
492 if (hc_remove(&DstHost, dpath) < 0) {
493 logerr("%-32s hardlink: unable to unlink: %s\n",
494 ((dpath) ? dpath : spath), strerror(errno));
501 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
502 int tryrelink = (errno == EMLINK);
503 logerr("%-32s hardlink: unable to link to %s: %s\n",
504 (dpath ? dpath : spath), hln->name, strerror(errno)
509 logerr("%-20s hardlink: will attempt to copy normally\n");
514 if (hln->nlinked == st1.st_nlink) {
520 logstd("%-32s hardlink: %s\n",
521 (dpath ? dpath : spath),
522 (st2Valid ? "relinked" : "linked")
532 * first instance of hardlink must be copied normally
535 hln = hltadd(&st1, dpath);
540 * Do we need to copy the file/dir/link/whatever? Early termination
541 * if we do not. Always redo links. Directories are always traversed
542 * except when the FSMID options are used.
544 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
549 && st1.st_mode == st2.st_mode
550 #ifdef _ST_FLAGS_PRESENT_
551 && st1.st_flags == st2.st_flags
554 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
556 * If FSMID tracking is turned on we can avoid recursing through
557 * an entire directory subtree if the FSMID matches.
559 #ifdef _ST_FSMID_PRESENT_
561 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
563 if (VerboseOpt >= 3) {
565 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
567 logstd("%-32s nochange\n", (dpath ? dpath : spath));
574 st1.st_size == st2.st_size &&
575 st1.st_uid == st2.st_uid &&
576 st1.st_gid == st2.st_gid &&
577 st1.st_mtime == st2.st_mtime
579 && (UseMD5Opt == 0 || (mres = md5_check(spath, dpath)) == 0)
581 #ifdef _ST_FSMID_PRESENT_
582 && (UseFSMIDOpt == 0 || (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
586 hln->dino = st2.st_ino;
587 if (VerboseOpt >= 3) {
590 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
594 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
596 logstd("%-32s nochange\n", (dpath ? dpath : spath));
598 CountSourceBytes += size;
605 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
607 logerr("%-32s SAFETY - refusing to copy file over directory\n",
608 (dpath ? dpath : spath)
611 return(0); /* continue with the cpdup anyway */
613 if (QuietOpt == 0 || AskConfirmation) {
614 logstd("%-32s WARNING: non-directory source will blow away\n"
615 "%-32s preexisting dest directory, continuing anyway!\n",
616 ((dpath) ? dpath : spath), "");
619 RemoveRecur(dpath, ddevNo);
623 * The various comparisons failed, copy it.
625 if (S_ISDIR(st1.st_mode)) {
629 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
630 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
635 if (S_ISDIR(st2.st_mode) == 0) {
636 hc_remove(&DstHost, dpath);
637 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
638 logerr("%s: mkdir failed: %s\n",
639 (dpath ? dpath : spath), strerror(errno));
644 * Matt: why don't you check error codes here?
646 hc_lstat(&DstHost, dpath, &st2);
647 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
651 * Directory must be scanable by root for cpdup to
652 * work. We'll fix it later if the directory isn't
653 * supposed to be readable ( which is why we fixup
654 * st2.st_mode to match what we did ).
656 if ((st2.st_mode & 0700) != 0700) {
657 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
661 logstd("%s\n", dpath ? dpath : spath);
665 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
671 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
678 * scan .cpignore file for files/directories
687 if (UseCpFile[0] == '/') {
688 fpath = mprintf("%s", UseCpFile);
690 fpath = mprintf("%s/%s", spath, UseCpFile);
692 AddList(&list, strrchr(fpath, '/') + 1, 1);
693 if ((fi = fopen(fpath, "r")) != NULL) {
694 while (fgets(buf, sizeof(buf), fi) != NULL) {
697 if (l && buf[l-1] == '\n')
699 if (buf[0] && buf[0] != '#')
700 AddList(&list, buf, 1);
708 * Automatically exclude MD5CacheFile that we create on the
709 * source from the copy to the destination.
711 * Automatically exclude a FSMIDCacheFile on the source that
712 * would otherwise overwrite the one we maintain on the target.
715 AddList(&list, MD5CacheFile, 1);
717 AddList(&list, FSMIDCacheFile, 1);
719 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
726 if (strcmp(den->d_name, ".") == 0 ||
727 strcmp(den->d_name, "..") == 0
732 * ignore if on .cpignore list
734 if (AddList(&list, den->d_name, 0) == 1) {
737 nspath = mprintf("%s/%s", spath, den->d_name);
739 ndpath = mprintf("%s/%s", dpath, den->d_name);
751 hc_closedir(&SrcHost, dir);
754 * Remove files/directories from destination that do not appear
757 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
758 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
762 if (strcmp(den->d_name, ".") == 0 ||
763 strcmp(den->d_name, "..") == 0
768 * If object does not exist in source or .cpignore
769 * then recursively remove it.
771 if (AddList(&list, den->d_name, 3) == 3) {
774 ndpath = mprintf("%s/%s", dpath, den->d_name);
775 RemoveRecur(ndpath, ddevNo);
779 hc_closedir(&DstHost, dir);
785 st1.st_uid != st2.st_uid ||
786 st1.st_gid != st2.st_gid
788 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
790 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
791 hc_chmod(&DstHost, dpath, st1.st_mode);
793 #ifdef _ST_FLAGS_PRESENT_
794 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
795 hc_chflags(&DstHost, dpath, st1.st_flags);
800 } else if (dpath == NULL) {
802 * If dpath is NULL, we are just updating the MD5
805 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
806 mres = md5_check(spath, NULL);
808 if (VerboseOpt > 1) {
810 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
812 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
813 } else if (!QuietOpt && mres < 0) {
814 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
818 } else if (S_ISREG(st1.st_mode)) {
824 path = mprintf("%s.tmp", dpath);
827 * Handle check failure message.
831 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
835 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
838 * Not quite ready to do the copy yet. If UseHLPath is defined,
839 * see if we can hardlink instead.
842 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
843 if (hc_link(&DstHost, hpath, dpath) == 0) {
845 logstd("%-32s hardlinked(-H)\n",
846 (dpath ? dpath : spath));
852 * Shucks, we may have hit a filesystem hard linking limit,
853 * we have to copy instead.
858 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
859 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
861 * There could be a .tmp file from a previously interrupted
862 * run, delete and retry. Fail if we still can't get at it.
864 #ifdef _ST_FLAGS_PRESENT_
865 hc_chflags(&DstHost, path, 0);
867 hc_remove(&DstHost, path);
868 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
875 * Matt: What about holes?
878 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
880 if (hc_write(&DstHost, fd2, IOBuf1, n) != n)
884 hc_close(&DstHost, fd2);
886 struct timeval tv[2];
888 bzero(tv, sizeof(tv));
889 tv[0].tv_sec = st1.st_mtime;
890 tv[1].tv_sec = st1.st_mtime;
892 hc_utimes(&DstHost, path, tv);
893 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
894 hc_chmod(&DstHost, path, st1.st_mode);
895 if (xrename(path, dpath, st2.st_flags) != 0) {
896 logerr("%-32s rename-after-copy failed: %s\n",
897 (dpath ? dpath : spath), strerror(errno)
902 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
903 #ifdef _ST_FLAGS_PRESENT_
905 hc_chflags(&DstHost, dpath, st1.st_flags);
908 CountReadBytes += size;
909 CountWriteBytes += size;
910 CountSourceBytes += size;
914 logerr("%-32s %s failed: %s\n",
915 (dpath ? dpath : spath), op, strerror(errno)
917 hc_remove(&DstHost, path);
921 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
922 (dpath ? dpath : spath), getuid(), geteuid(),
927 hc_close(&SrcHost, fd1);
929 logerr("%-32s copy: open failed: %s\n",
930 (dpath ? dpath : spath),
939 if (!r && hc_stat(&DstHost, dpath, &st2) == 0)
940 hln->dino = st2.st_ino;
944 } else if (S_ISLNK(st1.st_mode)) {
951 snprintf(path, sizeof(path), "%s.tmp", dpath);
952 n1 = hc_readlink(&SrcHost, spath, link1, sizeof(link1) - 1);
953 n2 = hc_readlink(&DstHost, dpath, link2, sizeof(link2) - 1);
955 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
956 hc_umask(&DstHost, ~st1.st_mode);
957 hc_remove(&DstHost, path);
959 if (hc_symlink(&DstHost, link1, path) < 0) {
960 logerr("%-32s symlink (%s->%s) failed: %s\n",
961 (dpath ? dpath : spath), link1, path,
966 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
968 * there is no lchmod() or lchflags(), we
969 * cannot chmod or chflags a softlink.
971 if (xrename(path, dpath, st2.st_flags) != 0) {
972 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
973 (dpath ? dpath : spath),
974 path, dpath, strerror(errno));
975 } else if (VerboseOpt) {
976 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
978 hc_umask(&DstHost, 000);
979 CountWriteBytes += n1;
984 logstd("%-32s nochange\n", (dpath ? dpath : spath));
986 CountSourceBytes += n1;
987 CountReadBytes += n1;
988 if (n2 > 0) CountReadBytes += n2;
992 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
994 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
999 st1.st_mode != st2.st_mode ||
1000 st1.st_rdev != st2.st_rdev ||
1001 st1.st_uid != st2.st_uid ||
1002 st1.st_gid != st2.st_gid
1004 snprintf(path, sizeof(path), "%s.tmp", dpath);
1006 hc_remove(&DstHost, path);
1007 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
1008 hc_chmod(&DstHost, path, st1.st_mode);
1009 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1010 hc_remove(&DstHost, dpath);
1011 if (xrename(path, dpath, st2.st_flags) != 0) {
1012 logerr("%-32s dev-rename-after-create failed: %s\n",
1013 (dpath ? dpath : spath),
1016 } else if (VerboseOpt) {
1017 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1022 logerr("%-32s dev failed: %s\n",
1023 (dpath ? dpath : spath), strerror(errno)
1027 if (VerboseOpt >= 3)
1028 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1041 RemoveRecur(const char *dpath, dev_t devNo)
1045 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1048 if (st.st_dev == devNo) {
1049 if (S_ISDIR(st.st_mode)) {
1052 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1054 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1057 if (strcmp(den->d_name, ".") == 0)
1059 if (strcmp(den->d_name, "..") == 0)
1061 ndpath = mprintf("%s/%s", dpath, den->d_name);
1062 RemoveRecur(ndpath, devNo);
1065 hc_closedir(&DstHost, dir);
1067 if (AskConfirmation && NoRemoveOpt == 0) {
1069 if (hc_rmdir(&DstHost, dpath) < 0) {
1070 logerr("%-32s rmdir failed: %s\n",
1071 dpath, strerror(errno)
1074 CountRemovedItems++;
1079 logstd("%-32s not-removed\n", dpath);
1080 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1082 logstd("%-32s rmdir-ok\n", dpath);
1083 CountRemovedItems++;
1085 logerr("%-32s rmdir failed: %s\n",
1086 dpath, strerror(errno)
1091 if (AskConfirmation && NoRemoveOpt == 0) {
1093 if (hc_remove(&DstHost, dpath) < 0) {
1094 logerr("%-32s remove failed: %s\n",
1095 dpath, strerror(errno)
1098 CountRemovedItems++;
1103 logstd("%-32s not-removed\n", dpath);
1104 } else if (hc_remove(&DstHost, dpath) == 0) {
1106 logstd("%-32s remove-ok\n", dpath);
1107 CountRemovedItems++;
1109 logerr("%-32s remove failed: %s\n",
1110 dpath, strerror(errno)
1120 InitList(List *list)
1122 bzero(list, sizeof(List));
1123 list->li_Node.no_Next = &list->li_Node;
1127 ResetList(List *list)
1131 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1132 list->li_Node.no_Next = node->no_Next;
1139 AddList(List *list, const char *name, int n)
1147 * Scan against wildcards. Only a node value of 1 can be a wildcard
1148 * ( usually scanned from .cpignore )
1151 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1152 if (strcmp(name, node->no_Name) == 0 ||
1153 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1155 return(node->no_Value);
1160 * Look for exact match
1163 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1164 if (strcmp(name, node->no_Name) == 0) {
1165 return(node->no_Value);
1168 node = malloc(sizeof(Node) + strlen(name) + 1);
1170 fprintf(stderr, "out of memory\n");
1174 node->no_Next = list->li_Node.no_Next;
1175 list->li_Node.no_Next = node;
1177 node->no_HNext = list->li_Hash[hv];
1178 list->li_Hash[hv] = node;
1180 strcpy(node->no_Name, name);
1187 shash(const char *s)
1194 if (*s == '*' || *s == '?' ||
1195 *s == '{' || *s == '}' ||
1196 *s == '[' || *s == ']' ||
1201 hv = (hv << 5) ^ *s ^ (hv >> 23);
1204 return(((hv >> 16) ^ hv) & HMASK);
1208 * WildCmp() - compare wild string to sane string
1210 * Return 0 on success, -1 on failure.
1214 WildCmp(const char *w, const char *s)
1217 * skip fixed portion
1223 if (w[1] == 0) /* optimize wild* case */
1229 for (i = 0; i <= l; ++i) {
1230 if (WildCmp(w + 1, s + i) == 0)
1244 if (*w == 0) /* terminator */
1256 YesNo(const char *path)
1260 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1263 first = ch = getchar();
1264 while (ch != '\n' && ch != EOF)
1266 return ((first == 'y' || first == 'Y'));
1270 * xrename() - rename with override
1272 * If the rename fails, attempt to override st_flags on the
1273 * destination and rename again. If that fails too, try to
1274 * set the flags back the way they were and give up.
1278 xrename(const char *src, const char *dst, u_long flags)
1284 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1285 #ifdef _ST_FLAGS_PRESENT_
1286 hc_chflags(&DstHost, dst, 0);
1287 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1288 hc_chflags(&DstHost, dst, flags);
1295 xlink(const char *src, const char *dst, u_long flags)
1301 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1302 #ifdef _ST_FLAGS_PRESENT_
1303 hc_chflags(&DstHost, src, 0);
1304 r = hc_link(&DstHost, src, dst);
1306 hc_chflags(&DstHost, src, flags);