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.14 2006/08/14 02:41:10 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)
345 if (!(new = malloc(sizeof (struct hlink)))) {
346 fprintf(stderr, "out of memory\n");
350 /* initialize and link the new element into the table */
351 new->ino = stp->st_ino;
353 strncpy(new->name, path, 2048);
356 n = stp->st_ino & HLMASK;
357 new->next = hltable[n];
359 hltable[n]->prev = new;
366 hltdelete(struct hlink *hl)
370 hl->next->prev = hl->prev;
371 hl->prev->next = hl->next;
374 hl->next->prev = NULL;
376 hltable[hl->ino & HLMASK] = hl->next;
383 * If UseHLPath is defined check to see if the file in question is
384 * the same as the source file, and if it is return a pointer to the
385 * -H path based file for hardlinking. Else return NULL.
388 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
396 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
399 * stat info matches ?
401 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
402 st1->st_size != sthl.st_size ||
403 st1->st_uid != sthl.st_uid ||
404 st1->st_gid != sthl.st_gid ||
405 st1->st_mtime != sthl.st_mtime
412 * If ForceOpt is set we have to compare the files
415 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
416 fd2 = hc_open(&DstHost, hpath, O_RDONLY, 0);
419 if (fd1 >= 0 && fd2 >= 0) {
422 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
423 if (hc_read(&DstHost, fd2, IOBuf2, sizeof(IOBuf2)) != n)
425 if (bcmp(IOBuf1, IOBuf2, n) != 0)
432 hc_close(&SrcHost, fd1);
434 hc_close(&DstHost, fd2);
444 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
448 int r, mres, fres, st2Valid;
454 r = mres = fres = st2Valid = 0;
458 if (hc_lstat(&SrcHost, spath, &st1) != 0)
460 st2.st_mode = 0; /* in case lstat fails */
461 st2.st_flags = 0; /* in case lstat fails */
462 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
465 if (S_ISREG(st1.st_mode)) {
466 size = st1.st_blocks * 512;
467 if (st1.st_size % 512)
468 size += st1.st_size % 512 - 512;
475 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
476 if ((hln = hltlookup(&st1)) != NULL) {
480 if (st2.st_ino == hln->dino) {
482 * hard link is already correct, nothing to do
485 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
486 if (hln->nlinked == st1.st_nlink)
492 * hard link is not correct, attempt to unlink it
494 if (hc_remove(&DstHost, dpath) < 0) {
495 logerr("%-32s hardlink: unable to unlink: %s\n",
496 ((dpath) ? dpath : spath), strerror(errno));
503 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
504 int tryrelink = (errno == EMLINK);
505 logerr("%-32s hardlink: unable to link to %s: %s\n",
506 (dpath ? dpath : spath), hln->name, strerror(errno)
511 logerr("%-20s hardlink: will attempt to copy normally\n");
516 if (hln->nlinked == st1.st_nlink) {
522 logstd("%-32s hardlink: %s\n",
523 (dpath ? dpath : spath),
524 (st2Valid ? "relinked" : "linked")
534 * first instance of hardlink must be copied normally
537 hln = hltadd(&st1, dpath);
542 * Do we need to copy the file/dir/link/whatever? Early termination
543 * if we do not. Always redo links. Directories are always traversed
544 * except when the FSMID options are used.
546 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
551 && st1.st_mode == st2.st_mode
552 #ifdef _ST_FLAGS_PRESENT_
553 && st1.st_flags == st2.st_flags
556 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
558 * If FSMID tracking is turned on we can avoid recursing through
559 * an entire directory subtree if the FSMID matches.
561 #ifdef _ST_FSMID_PRESENT_
563 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
565 if (VerboseOpt >= 3) {
567 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
569 logstd("%-32s nochange\n", (dpath ? dpath : spath));
576 st1.st_size == st2.st_size &&
577 st1.st_uid == st2.st_uid &&
578 st1.st_gid == st2.st_gid &&
579 st1.st_mtime == st2.st_mtime
581 && (UseMD5Opt == 0 || (mres = md5_check(spath, dpath)) == 0)
583 #ifdef _ST_FSMID_PRESENT_
584 && (UseFSMIDOpt == 0 || (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
588 hln->dino = st2.st_ino;
589 if (VerboseOpt >= 3) {
592 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
596 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
598 logstd("%-32s nochange\n", (dpath ? dpath : spath));
600 CountSourceBytes += size;
607 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
609 logerr("%-32s SAFETY - refusing to copy file over directory\n",
610 (dpath ? dpath : spath)
613 return(0); /* continue with the cpdup anyway */
615 if (QuietOpt == 0 || AskConfirmation) {
616 logstd("%-32s WARNING: non-directory source will blow away\n"
617 "%-32s preexisting dest directory, continuing anyway!\n",
618 ((dpath) ? dpath : spath), "");
621 RemoveRecur(dpath, ddevNo);
625 * The various comparisons failed, copy it.
627 if (S_ISDIR(st1.st_mode)) {
631 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
632 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
637 if (S_ISDIR(st2.st_mode) == 0) {
638 hc_remove(&DstHost, dpath);
639 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
640 logerr("%s: mkdir failed: %s\n",
641 (dpath ? dpath : spath), strerror(errno));
646 * Matt: why don't you check error codes here?
648 hc_lstat(&DstHost, dpath, &st2);
649 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
653 * Directory must be scanable by root for cpdup to
654 * work. We'll fix it later if the directory isn't
655 * supposed to be readable ( which is why we fixup
656 * st2.st_mode to match what we did ).
658 if ((st2.st_mode & 0700) != 0700) {
659 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
663 logstd("%s\n", dpath ? dpath : spath);
667 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
673 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
680 * scan .cpignore file for files/directories
689 if (UseCpFile[0] == '/') {
690 fpath = mprintf("%s", UseCpFile);
692 fpath = mprintf("%s/%s", spath, UseCpFile);
694 AddList(&list, strrchr(fpath, '/') + 1, 1);
695 if ((fi = fopen(fpath, "r")) != NULL) {
696 while (fgets(buf, sizeof(buf), fi) != NULL) {
699 if (l && buf[l-1] == '\n')
701 if (buf[0] && buf[0] != '#')
702 AddList(&list, buf, 1);
710 * Automatically exclude MD5CacheFile that we create on the
711 * source from the copy to the destination.
713 * Automatically exclude a FSMIDCacheFile on the source that
714 * would otherwise overwrite the one we maintain on the target.
717 AddList(&list, MD5CacheFile, 1);
719 AddList(&list, FSMIDCacheFile, 1);
721 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
728 if (strcmp(den->d_name, ".") == 0 ||
729 strcmp(den->d_name, "..") == 0
734 * ignore if on .cpignore list
736 if (AddList(&list, den->d_name, 0) == 1) {
739 nspath = mprintf("%s/%s", spath, den->d_name);
741 ndpath = mprintf("%s/%s", dpath, den->d_name);
753 hc_closedir(&SrcHost, dir);
756 * Remove files/directories from destination that do not appear
759 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
760 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
764 if (strcmp(den->d_name, ".") == 0 ||
765 strcmp(den->d_name, "..") == 0
770 * If object does not exist in source or .cpignore
771 * then recursively remove it.
773 if (AddList(&list, den->d_name, 3) == 3) {
776 ndpath = mprintf("%s/%s", dpath, den->d_name);
777 RemoveRecur(ndpath, ddevNo);
781 hc_closedir(&DstHost, dir);
787 st1.st_uid != st2.st_uid ||
788 st1.st_gid != st2.st_gid
790 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
792 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
793 hc_chmod(&DstHost, dpath, st1.st_mode);
795 #ifdef _ST_FLAGS_PRESENT_
796 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
797 hc_chflags(&DstHost, dpath, st1.st_flags);
802 } else if (dpath == NULL) {
804 * If dpath is NULL, we are just updating the MD5
807 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
808 mres = md5_check(spath, NULL);
810 if (VerboseOpt > 1) {
812 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
814 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
815 } else if (!QuietOpt && mres < 0) {
816 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
820 } else if (S_ISREG(st1.st_mode)) {
826 path = mprintf("%s.tmp", dpath);
829 * Handle check failure message.
833 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
837 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
840 * Not quite ready to do the copy yet. If UseHLPath is defined,
841 * see if we can hardlink instead.
844 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
845 if (hc_link(&DstHost, hpath, dpath) == 0) {
847 logstd("%-32s hardlinked(-H)\n",
848 (dpath ? dpath : spath));
854 * Shucks, we may have hit a filesystem hard linking limit,
855 * we have to copy instead.
860 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
861 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
863 * There could be a .tmp file from a previously interrupted
864 * run, delete and retry. Fail if we still can't get at it.
866 #ifdef _ST_FLAGS_PRESENT_
867 hc_chflags(&DstHost, path, 0);
869 hc_remove(&DstHost, path);
870 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
877 * Matt: What about holes?
880 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
882 if (hc_write(&DstHost, fd2, IOBuf1, n) != n)
886 hc_close(&DstHost, fd2);
888 struct timeval tv[2];
890 bzero(tv, sizeof(tv));
891 tv[0].tv_sec = st1.st_mtime;
892 tv[1].tv_sec = st1.st_mtime;
894 hc_utimes(&DstHost, path, tv);
895 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
896 hc_chmod(&DstHost, path, st1.st_mode);
897 if (xrename(path, dpath, st2.st_flags) != 0) {
898 logerr("%-32s rename-after-copy failed: %s\n",
899 (dpath ? dpath : spath), strerror(errno)
904 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
905 #ifdef _ST_FLAGS_PRESENT_
907 hc_chflags(&DstHost, dpath, st1.st_flags);
910 CountReadBytes += size;
911 CountWriteBytes += size;
912 CountSourceBytes += size;
916 logerr("%-32s %s failed: %s\n",
917 (dpath ? dpath : spath), op, strerror(errno)
919 hc_remove(&DstHost, path);
923 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
924 (dpath ? dpath : spath), getuid(), geteuid(),
929 hc_close(&SrcHost, fd1);
931 logerr("%-32s copy: open failed: %s\n",
932 (dpath ? dpath : spath),
941 if (!r && hc_stat(&DstHost, dpath, &st2) == 0)
942 hln->dino = st2.st_ino;
946 } else if (S_ISLNK(st1.st_mode)) {
953 snprintf(path, sizeof(path), "%s.tmp", dpath);
954 n1 = hc_readlink(&SrcHost, spath, link1, sizeof(link1) - 1);
955 n2 = hc_readlink(&DstHost, dpath, link2, sizeof(link2) - 1);
957 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
958 hc_umask(&DstHost, ~st1.st_mode);
959 hc_remove(&DstHost, path);
961 if (hc_symlink(&DstHost, link1, path) < 0) {
962 logerr("%-32s symlink (%s->%s) failed: %s\n",
963 (dpath ? dpath : spath), link1, path,
968 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
970 * there is no lchmod() or lchflags(), we
971 * cannot chmod or chflags a softlink.
973 if (xrename(path, dpath, st2.st_flags) != 0) {
974 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
975 (dpath ? dpath : spath),
976 path, dpath, strerror(errno));
977 } else if (VerboseOpt) {
978 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
980 hc_umask(&DstHost, 000);
981 CountWriteBytes += n1;
986 logstd("%-32s nochange\n", (dpath ? dpath : spath));
988 CountSourceBytes += n1;
989 CountReadBytes += n1;
990 if (n2 > 0) CountReadBytes += n2;
994 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
996 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
1001 st1.st_mode != st2.st_mode ||
1002 st1.st_rdev != st2.st_rdev ||
1003 st1.st_uid != st2.st_uid ||
1004 st1.st_gid != st2.st_gid
1006 snprintf(path, sizeof(path), "%s.tmp", dpath);
1008 hc_remove(&DstHost, path);
1009 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
1010 hc_chmod(&DstHost, path, st1.st_mode);
1011 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1012 hc_remove(&DstHost, dpath);
1013 if (xrename(path, dpath, st2.st_flags) != 0) {
1014 logerr("%-32s dev-rename-after-create failed: %s\n",
1015 (dpath ? dpath : spath),
1018 } else if (VerboseOpt) {
1019 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1024 logerr("%-32s dev failed: %s\n",
1025 (dpath ? dpath : spath), strerror(errno)
1029 if (VerboseOpt >= 3)
1030 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1043 RemoveRecur(const char *dpath, dev_t devNo)
1047 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1050 if (st.st_dev == devNo) {
1051 if (S_ISDIR(st.st_mode)) {
1054 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1056 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1059 if (strcmp(den->d_name, ".") == 0)
1061 if (strcmp(den->d_name, "..") == 0)
1063 ndpath = mprintf("%s/%s", dpath, den->d_name);
1064 RemoveRecur(ndpath, devNo);
1067 hc_closedir(&DstHost, dir);
1069 if (AskConfirmation && NoRemoveOpt == 0) {
1071 if (hc_rmdir(&DstHost, dpath) < 0) {
1072 logerr("%-32s rmdir failed: %s\n",
1073 dpath, strerror(errno)
1076 CountRemovedItems++;
1081 logstd("%-32s not-removed\n", dpath);
1082 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1084 logstd("%-32s rmdir-ok\n", dpath);
1085 CountRemovedItems++;
1087 logerr("%-32s rmdir failed: %s\n",
1088 dpath, strerror(errno)
1093 if (AskConfirmation && NoRemoveOpt == 0) {
1095 if (hc_remove(&DstHost, dpath) < 0) {
1096 logerr("%-32s remove failed: %s\n",
1097 dpath, strerror(errno)
1100 CountRemovedItems++;
1105 logstd("%-32s not-removed\n", dpath);
1106 } else if (hc_remove(&DstHost, dpath) == 0) {
1108 logstd("%-32s remove-ok\n", dpath);
1109 CountRemovedItems++;
1111 logerr("%-32s remove failed: %s\n",
1112 dpath, strerror(errno)
1122 InitList(List *list)
1124 bzero(list, sizeof(List));
1125 list->li_Node.no_Next = &list->li_Node;
1129 ResetList(List *list)
1133 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1134 list->li_Node.no_Next = node->no_Next;
1141 AddList(List *list, const char *name, int n)
1149 * Scan against wildcards. Only a node value of 1 can be a wildcard
1150 * ( usually scanned from .cpignore )
1153 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1154 if (strcmp(name, node->no_Name) == 0 ||
1155 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1157 return(node->no_Value);
1162 * Look for exact match
1165 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1166 if (strcmp(name, node->no_Name) == 0) {
1167 return(node->no_Value);
1170 node = malloc(sizeof(Node) + strlen(name) + 1);
1172 fprintf(stderr, "out of memory\n");
1176 node->no_Next = list->li_Node.no_Next;
1177 list->li_Node.no_Next = node;
1179 node->no_HNext = list->li_Hash[hv];
1180 list->li_Hash[hv] = node;
1182 strcpy(node->no_Name, name);
1189 shash(const char *s)
1196 if (*s == '*' || *s == '?' ||
1197 *s == '{' || *s == '}' ||
1198 *s == '[' || *s == ']' ||
1203 hv = (hv << 5) ^ *s ^ (hv >> 23);
1206 return(((hv >> 16) ^ hv) & HMASK);
1210 * WildCmp() - compare wild string to sane string
1212 * Return 0 on success, -1 on failure.
1216 WildCmp(const char *w, const char *s)
1219 * skip fixed portion
1225 if (w[1] == 0) /* optimize wild* case */
1231 for (i = 0; i <= l; ++i) {
1232 if (WildCmp(w + 1, s + i) == 0)
1246 if (*w == 0) /* terminator */
1258 YesNo(const char *path)
1262 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1265 first = ch = getchar();
1266 while (ch != '\n' && ch != EOF)
1268 return ((first == 'y' || first == 'Y'));
1272 * xrename() - rename with override
1274 * If the rename fails, attempt to override st_flags on the
1275 * destination and rename again. If that fails too, try to
1276 * set the flags back the way they were and give up.
1280 xrename(const char *src, const char *dst, u_long flags)
1286 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1287 #ifdef _ST_FLAGS_PRESENT_
1288 hc_chflags(&DstHost, dst, 0);
1289 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1290 hc_chflags(&DstHost, dst, flags);
1297 xlink(const char *src, const char *dst, u_long flags)
1300 #ifdef _ST_FLAGS_PRESENT_
1306 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1307 #ifdef _ST_FLAGS_PRESENT_
1308 hc_chflags(&DstHost, src, 0);
1309 r = hc_link(&DstHost, src, dst);
1311 hc_chflags(&DstHost, src, flags);