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.16 2006/08/18 16:05: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 #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 signal(SIGPIPE, SIG_IGN);
149 gettimeofday(&start, NULL);
150 for (i = 1; i < ac; ++i) {
157 } else if (dst == NULL) {
160 fatal("too many arguments");
168 v = strtol(ptr, NULL, 0);
173 while (*ptr == 'v') {
177 if (*ptr >= '0' && *ptr <= '9')
178 VerboseOpt = strtol(ptr, NULL, 0);
187 UseCpFile = ".cpignore";
190 UseCpFile = (*ptr) ? ptr : av[++i];
193 UseHLPath = (*ptr) ? ptr : av[++i];
212 FSMIDCacheFile = ".FSMID.CHECK";
216 FSMIDCacheFile = av[++i];
220 MD5CacheFile = av[++i];
224 MD5CacheFile = ".MD5.CHECKSUMS";
227 setvbuf(stdout, NULL, _IOLBF, 0);
230 fatal("illegal option: %s\n", ptr - 2);
237 * If we are told to go into slave mode, run the HC protocol
245 * Extract the source and/or/neither target [user@]host and
246 * make any required connections.
248 if (src && (ptr = strchr(src, ':')) != NULL) {
249 asprintf(&SrcHost.host, "%*.*s", ptr - src, ptr - src, src);
252 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
256 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
259 if (hc_connect(&SrcHost) < 0)
260 fprintf(stderr, "Unable to connect to %s\n", SrcHost.host);
262 if (dst && (ptr = strchr(dst, ':')) != NULL) {
263 asprintf(&DstHost.host, "%*.*s", ptr - dst, ptr - dst, dst);
266 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
269 if (hc_connect(&DstHost) < 0)
270 fprintf(stderr, "Unable to connect to %s\n", DstHost.host);
274 * dst may be NULL only if -m option is specified,
275 * which forces an update of the MD5 checksums
277 if (dst == NULL && UseMD5Opt == 0) {
282 DstBaseLen = strlen(dst);
283 i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
285 i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
292 if (SummaryOpt && i == 0) {
296 gettimeofday(&end, NULL);
297 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
298 CountReadBytes += sizeof(struct stat) * CountSourceItems;
299 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
300 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
302 duration = end.tv_sec - start.tv_sec;
304 duration += end.tv_usec - start.tv_usec;
305 if (duration == 0) duration = 1;
306 logstd("cpdup completed successfully\n");
307 logstd("%lld bytes source %lld bytes read %lld bytes written (%.1fX speedup)\n",
308 (long long)CountSourceBytes,
309 (long long)CountReadBytes,
310 (long long)CountWriteBytes,
311 ((double)CountSourceBytes * 2.0) / ((double)(CountReadBytes + CountWriteBytes)));
312 logstd("%lld source items %lld items copied %lld things deleted\n",
313 (long long)CountSourceItems,
314 (long long)CountCopiedItems,
315 (long long)CountRemovedItems);
316 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
317 (float)duration / (float)1000000,
318 (long)((long)1000000 * (CountReadBytes + CountWriteBytes) / duration / 1024.0),
319 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
321 exit((i == 0) ? 0 : 1);
324 static struct hlink *
325 hltlookup(struct stat *stp)
330 n = stp->st_ino & HLMASK;
332 for (hl = hltable[n]; hl; hl = hl->next)
333 if (hl->ino == stp->st_ino)
339 static struct hlink *
340 hltadd(struct stat *stp, const char *path)
343 int plen = strlen(path);
346 new = malloc(offsetof(struct hlink, name[plen + 1]));
348 fprintf(stderr, "out of memory\n");
352 /* initialize and link the new element into the table */
353 new->ino = stp->st_ino;
355 bcopy(path, new->name, plen + 1);
358 n = stp->st_ino & HLMASK;
359 new->next = hltable[n];
361 hltable[n]->prev = new;
368 hltdelete(struct hlink *hl)
372 hl->next->prev = hl->prev;
373 hl->prev->next = hl->next;
376 hl->next->prev = NULL;
378 hltable[hl->ino & HLMASK] = hl->next;
385 * If UseHLPath is defined check to see if the file in question is
386 * the same as the source file, and if it is return a pointer to the
387 * -H path based file for hardlinking. Else return NULL.
390 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
398 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
401 * stat info matches ?
403 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
404 st1->st_size != sthl.st_size ||
405 st1->st_uid != sthl.st_uid ||
406 st1->st_gid != sthl.st_gid ||
407 st1->st_mtime != sthl.st_mtime
414 * If ForceOpt is set we have to compare the files
417 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
418 fd2 = hc_open(&DstHost, hpath, O_RDONLY, 0);
421 if (fd1 >= 0 && fd2 >= 0) {
424 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
425 if (hc_read(&DstHost, fd2, IOBuf2, sizeof(IOBuf2)) != n)
427 if (bcmp(IOBuf1, IOBuf2, n) != 0)
434 hc_close(&SrcHost, fd1);
436 hc_close(&DstHost, fd2);
446 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
450 int r, mres, fres, st2Valid;
456 r = mres = fres = st2Valid = 0;
460 if (hc_lstat(&SrcHost, spath, &st1) != 0)
462 st2.st_mode = 0; /* in case lstat fails */
463 st2.st_flags = 0; /* in case lstat fails */
464 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
467 if (S_ISREG(st1.st_mode)) {
468 size = st1.st_blocks * 512;
469 if (st1.st_size % 512)
470 size += st1.st_size % 512 - 512;
477 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
478 if ((hln = hltlookup(&st1)) != NULL) {
482 if (st2.st_ino == hln->dino) {
484 * hard link is already correct, nothing to do
487 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
488 if (hln->nlinked == st1.st_nlink)
494 * hard link is not correct, attempt to unlink it
496 if (hc_remove(&DstHost, dpath) < 0) {
497 logerr("%-32s hardlink: unable to unlink: %s\n",
498 ((dpath) ? dpath : spath), strerror(errno));
505 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
506 int tryrelink = (errno == EMLINK);
507 logerr("%-32s hardlink: unable to link to %s: %s\n",
508 (dpath ? dpath : spath), hln->name, strerror(errno)
513 logerr("%-20s hardlink: will attempt to copy normally\n");
518 if (hln->nlinked == st1.st_nlink) {
524 logstd("%-32s hardlink: %s\n",
525 (dpath ? dpath : spath),
526 (st2Valid ? "relinked" : "linked")
536 * first instance of hardlink must be copied normally
539 hln = hltadd(&st1, dpath);
544 * Do we need to copy the file/dir/link/whatever? Early termination
545 * if we do not. Always redo links. Directories are always traversed
546 * except when the FSMID options are used.
548 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
553 && st1.st_mode == st2.st_mode
554 #ifdef _ST_FLAGS_PRESENT_
555 && st1.st_flags == st2.st_flags
558 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
560 * If FSMID tracking is turned on we can avoid recursing through
561 * an entire directory subtree if the FSMID matches.
563 #ifdef _ST_FSMID_PRESENT_
565 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
567 if (VerboseOpt >= 3) {
569 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
571 logstd("%-32s nochange\n", (dpath ? dpath : spath));
578 st1.st_size == st2.st_size &&
579 st1.st_uid == st2.st_uid &&
580 st1.st_gid == st2.st_gid &&
581 st1.st_mtime == st2.st_mtime
583 && (UseMD5Opt == 0 || (mres = md5_check(spath, dpath)) == 0)
585 #ifdef _ST_FSMID_PRESENT_
586 && (UseFSMIDOpt == 0 || (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
590 hln->dino = st2.st_ino;
591 if (VerboseOpt >= 3) {
594 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
598 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
600 logstd("%-32s nochange\n", (dpath ? dpath : spath));
602 CountSourceBytes += size;
609 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
611 logerr("%-32s SAFETY - refusing to copy file over directory\n",
612 (dpath ? dpath : spath)
615 return(0); /* continue with the cpdup anyway */
617 if (QuietOpt == 0 || AskConfirmation) {
618 logstd("%-32s WARNING: non-directory source will blow away\n"
619 "%-32s preexisting dest directory, continuing anyway!\n",
620 ((dpath) ? dpath : spath), "");
623 RemoveRecur(dpath, ddevNo);
627 * The various comparisons failed, copy it.
629 if (S_ISDIR(st1.st_mode)) {
633 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
634 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
639 if (S_ISDIR(st2.st_mode) == 0) {
640 hc_remove(&DstHost, dpath);
641 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
642 logerr("%s: mkdir failed: %s\n",
643 (dpath ? dpath : spath), strerror(errno));
648 * Matt: why don't you check error codes here?
650 hc_lstat(&DstHost, dpath, &st2);
651 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
655 * Directory must be scanable by root for cpdup to
656 * work. We'll fix it later if the directory isn't
657 * supposed to be readable ( which is why we fixup
658 * st2.st_mode to match what we did ).
660 if ((st2.st_mode & 0700) != 0700) {
661 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
665 logstd("%s\n", dpath ? dpath : spath);
669 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
675 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
682 * scan .cpignore file for files/directories
691 if (UseCpFile[0] == '/') {
692 fpath = mprintf("%s", UseCpFile);
694 fpath = mprintf("%s/%s", spath, UseCpFile);
696 AddList(&list, strrchr(fpath, '/') + 1, 1);
697 if ((fi = fopen(fpath, "r")) != NULL) {
698 while (fgets(buf, sizeof(buf), fi) != NULL) {
701 if (l && buf[l-1] == '\n')
703 if (buf[0] && buf[0] != '#')
704 AddList(&list, buf, 1);
712 * Automatically exclude MD5CacheFile that we create on the
713 * source from the copy to the destination.
715 * Automatically exclude a FSMIDCacheFile on the source that
716 * would otherwise overwrite the one we maintain on the target.
719 AddList(&list, MD5CacheFile, 1);
721 AddList(&list, FSMIDCacheFile, 1);
723 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
730 if (strcmp(den->d_name, ".") == 0 ||
731 strcmp(den->d_name, "..") == 0
736 * ignore if on .cpignore list
738 if (AddList(&list, den->d_name, 0) == 1) {
741 nspath = mprintf("%s/%s", spath, den->d_name);
743 ndpath = mprintf("%s/%s", dpath, den->d_name);
755 hc_closedir(&SrcHost, dir);
758 * Remove files/directories from destination that do not appear
761 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
762 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
766 if (strcmp(den->d_name, ".") == 0 ||
767 strcmp(den->d_name, "..") == 0
772 * If object does not exist in source or .cpignore
773 * then recursively remove it.
775 if (AddList(&list, den->d_name, 3) == 3) {
778 ndpath = mprintf("%s/%s", dpath, den->d_name);
779 RemoveRecur(ndpath, ddevNo);
783 hc_closedir(&DstHost, dir);
787 struct timeval tv[2];
791 st1.st_uid != st2.st_uid ||
792 st1.st_gid != st2.st_gid
794 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
796 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
797 hc_chmod(&DstHost, dpath, st1.st_mode);
799 #ifdef _ST_FLAGS_PRESENT_
800 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
801 hc_chflags(&DstHost, dpath, st1.st_flags);
806 st1.st_mtime != st2.st_mtime
808 bzero(tv, sizeof(tv));
809 tv[0].tv_sec = st1.st_mtime;
810 tv[1].tv_sec = st1.st_mtime;
811 hc_utimes(&DstHost, dpath, tv);
815 } else if (dpath == NULL) {
817 * If dpath is NULL, we are just updating the MD5
820 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
821 mres = md5_check(spath, NULL);
823 if (VerboseOpt > 1) {
825 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
827 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
828 } else if (!QuietOpt && mres < 0) {
829 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
833 } else if (S_ISREG(st1.st_mode)) {
839 path = mprintf("%s.tmp", dpath);
842 * Handle check failure message.
846 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
850 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
853 * Not quite ready to do the copy yet. If UseHLPath is defined,
854 * see if we can hardlink instead.
857 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
858 if (hc_link(&DstHost, hpath, dpath) == 0) {
860 logstd("%-32s hardlinked(-H)\n",
861 (dpath ? dpath : spath));
867 * Shucks, we may have hit a filesystem hard linking limit,
868 * we have to copy instead.
873 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
874 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
876 * There could be a .tmp file from a previously interrupted
877 * run, delete and retry. Fail if we still can't get at it.
879 #ifdef _ST_FLAGS_PRESENT_
880 hc_chflags(&DstHost, path, 0);
882 hc_remove(&DstHost, path);
883 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
890 * Matt: What about holes?
893 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
895 if (hc_write(&DstHost, fd2, IOBuf1, n) != n)
899 hc_close(&DstHost, fd2);
901 struct timeval tv[2];
903 bzero(tv, sizeof(tv));
904 tv[0].tv_sec = st1.st_mtime;
905 tv[1].tv_sec = st1.st_mtime;
907 hc_utimes(&DstHost, path, tv);
908 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
909 hc_chmod(&DstHost, path, st1.st_mode);
910 if (xrename(path, dpath, st2.st_flags) != 0) {
911 logerr("%-32s rename-after-copy failed: %s\n",
912 (dpath ? dpath : spath), strerror(errno)
917 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
918 #ifdef _ST_FLAGS_PRESENT_
920 hc_chflags(&DstHost, dpath, st1.st_flags);
923 CountReadBytes += size;
924 CountWriteBytes += size;
925 CountSourceBytes += size;
929 logerr("%-32s %s failed: %s\n",
930 (dpath ? dpath : spath), op, strerror(errno)
932 hc_remove(&DstHost, path);
936 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
937 (dpath ? dpath : spath), getuid(), geteuid(),
942 hc_close(&SrcHost, fd1);
944 logerr("%-32s copy: open failed: %s\n",
945 (dpath ? dpath : spath),
954 if (!r && hc_stat(&DstHost, dpath, &st2) == 0)
955 hln->dino = st2.st_ino;
959 } else if (S_ISLNK(st1.st_mode)) {
966 snprintf(path, sizeof(path), "%s.tmp", dpath);
967 n1 = hc_readlink(&SrcHost, spath, link1, sizeof(link1) - 1);
968 n2 = hc_readlink(&DstHost, dpath, link2, sizeof(link2) - 1);
970 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
971 hc_umask(&DstHost, ~st1.st_mode);
972 hc_remove(&DstHost, path);
974 if (hc_symlink(&DstHost, link1, path) < 0) {
975 logerr("%-32s symlink (%s->%s) failed: %s\n",
976 (dpath ? dpath : spath), link1, path,
981 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
983 * there is no lchmod() or lchflags(), we
984 * cannot chmod or chflags a softlink.
986 if (xrename(path, dpath, st2.st_flags) != 0) {
987 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
988 (dpath ? dpath : spath),
989 path, dpath, strerror(errno));
990 } else if (VerboseOpt) {
991 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
993 hc_umask(&DstHost, 000);
994 CountWriteBytes += n1;
999 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1001 CountSourceBytes += n1;
1002 CountReadBytes += n1;
1003 if (n2 > 0) CountReadBytes += n2;
1007 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1009 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
1014 st1.st_mode != st2.st_mode ||
1015 st1.st_rdev != st2.st_rdev ||
1016 st1.st_uid != st2.st_uid ||
1017 st1.st_gid != st2.st_gid
1019 snprintf(path, sizeof(path), "%s.tmp", dpath);
1021 hc_remove(&DstHost, path);
1022 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
1023 hc_chmod(&DstHost, path, st1.st_mode);
1024 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1025 hc_remove(&DstHost, dpath);
1026 if (xrename(path, dpath, st2.st_flags) != 0) {
1027 logerr("%-32s dev-rename-after-create failed: %s\n",
1028 (dpath ? dpath : spath),
1031 } else if (VerboseOpt) {
1032 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1037 logerr("%-32s dev failed: %s\n",
1038 (dpath ? dpath : spath), strerror(errno)
1042 if (VerboseOpt >= 3)
1043 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1056 RemoveRecur(const char *dpath, dev_t devNo)
1060 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1063 if (st.st_dev == devNo) {
1064 if (S_ISDIR(st.st_mode)) {
1067 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1069 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1072 if (strcmp(den->d_name, ".") == 0)
1074 if (strcmp(den->d_name, "..") == 0)
1076 ndpath = mprintf("%s/%s", dpath, den->d_name);
1077 RemoveRecur(ndpath, devNo);
1080 hc_closedir(&DstHost, dir);
1082 if (AskConfirmation && NoRemoveOpt == 0) {
1084 if (hc_rmdir(&DstHost, dpath) < 0) {
1085 logerr("%-32s rmdir failed: %s\n",
1086 dpath, strerror(errno)
1089 CountRemovedItems++;
1094 logstd("%-32s not-removed\n", dpath);
1095 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1097 logstd("%-32s rmdir-ok\n", dpath);
1098 CountRemovedItems++;
1100 logerr("%-32s rmdir failed: %s\n",
1101 dpath, strerror(errno)
1106 if (AskConfirmation && NoRemoveOpt == 0) {
1108 if (hc_remove(&DstHost, dpath) < 0) {
1109 logerr("%-32s remove failed: %s\n",
1110 dpath, strerror(errno)
1113 CountRemovedItems++;
1118 logstd("%-32s not-removed\n", dpath);
1119 } else if (hc_remove(&DstHost, dpath) == 0) {
1121 logstd("%-32s remove-ok\n", dpath);
1122 CountRemovedItems++;
1124 logerr("%-32s remove failed: %s\n",
1125 dpath, strerror(errno)
1135 InitList(List *list)
1137 bzero(list, sizeof(List));
1138 list->li_Node.no_Next = &list->li_Node;
1142 ResetList(List *list)
1146 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1147 list->li_Node.no_Next = node->no_Next;
1154 AddList(List *list, const char *name, int n)
1162 * Scan against wildcards. Only a node value of 1 can be a wildcard
1163 * ( usually scanned from .cpignore )
1166 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1167 if (strcmp(name, node->no_Name) == 0 ||
1168 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1170 return(node->no_Value);
1175 * Look for exact match
1178 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1179 if (strcmp(name, node->no_Name) == 0) {
1180 return(node->no_Value);
1183 node = malloc(sizeof(Node) + strlen(name) + 1);
1185 fprintf(stderr, "out of memory\n");
1189 node->no_Next = list->li_Node.no_Next;
1190 list->li_Node.no_Next = node;
1192 node->no_HNext = list->li_Hash[hv];
1193 list->li_Hash[hv] = node;
1195 strcpy(node->no_Name, name);
1202 shash(const char *s)
1209 if (*s == '*' || *s == '?' ||
1210 *s == '{' || *s == '}' ||
1211 *s == '[' || *s == ']' ||
1216 hv = (hv << 5) ^ *s ^ (hv >> 23);
1219 return(((hv >> 16) ^ hv) & HMASK);
1223 * WildCmp() - compare wild string to sane string
1225 * Return 0 on success, -1 on failure.
1229 WildCmp(const char *w, const char *s)
1232 * skip fixed portion
1238 if (w[1] == 0) /* optimize wild* case */
1244 for (i = 0; i <= l; ++i) {
1245 if (WildCmp(w + 1, s + i) == 0)
1259 if (*w == 0) /* terminator */
1271 YesNo(const char *path)
1275 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1278 first = ch = getchar();
1279 while (ch != '\n' && ch != EOF)
1281 return ((first == 'y' || first == 'Y'));
1285 * xrename() - rename with override
1287 * If the rename fails, attempt to override st_flags on the
1288 * destination and rename again. If that fails too, try to
1289 * set the flags back the way they were and give up.
1293 xrename(const char *src, const char *dst, u_long flags)
1299 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1300 #ifdef _ST_FLAGS_PRESENT_
1301 hc_chflags(&DstHost, dst, 0);
1302 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1303 hc_chflags(&DstHost, dst, flags);
1310 xlink(const char *src, const char *dst, u_long flags)
1313 #ifdef _ST_FLAGS_PRESENT_
1319 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1320 #ifdef _ST_FLAGS_PRESENT_
1321 hc_chflags(&DstHost, src, 0);
1322 r = hc_link(&DstHost, src, dst);
1324 hc_chflags(&DstHost, src, flags);