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.17 2006/09/16 16:47:29 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.
856 * If we can hardlink, and the target exists, we have to remove it
857 * first or the hardlink will fail. This can occur in a number of
858 * situations but must typically when the '-f -H' combination is
861 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
863 hc_remove(&DstHost, dpath);
864 if (hc_link(&DstHost, hpath, dpath) == 0) {
866 logstd("%-32s hardlinked(-H)\n",
867 (dpath ? dpath : spath));
873 * Shucks, we may have hit a filesystem hard linking limit,
874 * we have to copy instead.
879 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
880 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
882 * There could be a .tmp file from a previously interrupted
883 * run, delete and retry. Fail if we still can't get at it.
885 #ifdef _ST_FLAGS_PRESENT_
886 hc_chflags(&DstHost, path, 0);
888 hc_remove(&DstHost, path);
889 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
896 * Matt: What about holes?
899 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
901 if (hc_write(&DstHost, fd2, IOBuf1, n) != n)
905 hc_close(&DstHost, fd2);
907 struct timeval tv[2];
909 bzero(tv, sizeof(tv));
910 tv[0].tv_sec = st1.st_mtime;
911 tv[1].tv_sec = st1.st_mtime;
913 hc_utimes(&DstHost, path, tv);
914 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
915 hc_chmod(&DstHost, path, st1.st_mode);
916 if (xrename(path, dpath, st2.st_flags) != 0) {
917 logerr("%-32s rename-after-copy failed: %s\n",
918 (dpath ? dpath : spath), strerror(errno)
923 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
924 #ifdef _ST_FLAGS_PRESENT_
926 hc_chflags(&DstHost, dpath, st1.st_flags);
929 CountReadBytes += size;
930 CountWriteBytes += size;
931 CountSourceBytes += size;
935 logerr("%-32s %s failed: %s\n",
936 (dpath ? dpath : spath), op, strerror(errno)
938 hc_remove(&DstHost, path);
942 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
943 (dpath ? dpath : spath), getuid(), geteuid(),
948 hc_close(&SrcHost, fd1);
950 logerr("%-32s copy: open failed: %s\n",
951 (dpath ? dpath : spath),
960 if (!r && hc_stat(&DstHost, dpath, &st2) == 0)
961 hln->dino = st2.st_ino;
965 } else if (S_ISLNK(st1.st_mode)) {
972 snprintf(path, sizeof(path), "%s.tmp", dpath);
973 n1 = hc_readlink(&SrcHost, spath, link1, sizeof(link1) - 1);
974 n2 = hc_readlink(&DstHost, dpath, link2, sizeof(link2) - 1);
976 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
977 hc_umask(&DstHost, ~st1.st_mode);
978 hc_remove(&DstHost, path);
980 if (hc_symlink(&DstHost, link1, path) < 0) {
981 logerr("%-32s symlink (%s->%s) failed: %s\n",
982 (dpath ? dpath : spath), link1, path,
987 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
989 * there is no lchmod() or lchflags(), we
990 * cannot chmod or chflags a softlink.
992 if (xrename(path, dpath, st2.st_flags) != 0) {
993 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
994 (dpath ? dpath : spath),
995 path, dpath, strerror(errno));
996 } else if (VerboseOpt) {
997 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
999 hc_umask(&DstHost, 000);
1000 CountWriteBytes += n1;
1004 if (VerboseOpt >= 3)
1005 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1007 CountSourceBytes += n1;
1008 CountReadBytes += n1;
1009 if (n2 > 0) CountReadBytes += n2;
1013 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1015 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
1020 st1.st_mode != st2.st_mode ||
1021 st1.st_rdev != st2.st_rdev ||
1022 st1.st_uid != st2.st_uid ||
1023 st1.st_gid != st2.st_gid
1025 snprintf(path, sizeof(path), "%s.tmp", dpath);
1027 hc_remove(&DstHost, path);
1028 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
1029 hc_chmod(&DstHost, path, st1.st_mode);
1030 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1031 hc_remove(&DstHost, dpath);
1032 if (xrename(path, dpath, st2.st_flags) != 0) {
1033 logerr("%-32s dev-rename-after-create failed: %s\n",
1034 (dpath ? dpath : spath),
1037 } else if (VerboseOpt) {
1038 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1043 logerr("%-32s dev failed: %s\n",
1044 (dpath ? dpath : spath), strerror(errno)
1048 if (VerboseOpt >= 3)
1049 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1062 RemoveRecur(const char *dpath, dev_t devNo)
1066 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1069 if (st.st_dev == devNo) {
1070 if (S_ISDIR(st.st_mode)) {
1073 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1075 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1078 if (strcmp(den->d_name, ".") == 0)
1080 if (strcmp(den->d_name, "..") == 0)
1082 ndpath = mprintf("%s/%s", dpath, den->d_name);
1083 RemoveRecur(ndpath, devNo);
1086 hc_closedir(&DstHost, dir);
1088 if (AskConfirmation && NoRemoveOpt == 0) {
1090 if (hc_rmdir(&DstHost, dpath) < 0) {
1091 logerr("%-32s rmdir failed: %s\n",
1092 dpath, strerror(errno)
1095 CountRemovedItems++;
1100 logstd("%-32s not-removed\n", dpath);
1101 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1103 logstd("%-32s rmdir-ok\n", dpath);
1104 CountRemovedItems++;
1106 logerr("%-32s rmdir failed: %s\n",
1107 dpath, strerror(errno)
1112 if (AskConfirmation && NoRemoveOpt == 0) {
1114 if (hc_remove(&DstHost, dpath) < 0) {
1115 logerr("%-32s remove failed: %s\n",
1116 dpath, strerror(errno)
1119 CountRemovedItems++;
1124 logstd("%-32s not-removed\n", dpath);
1125 } else if (hc_remove(&DstHost, dpath) == 0) {
1127 logstd("%-32s remove-ok\n", dpath);
1128 CountRemovedItems++;
1130 logerr("%-32s remove failed: %s\n",
1131 dpath, strerror(errno)
1141 InitList(List *list)
1143 bzero(list, sizeof(List));
1144 list->li_Node.no_Next = &list->li_Node;
1148 ResetList(List *list)
1152 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1153 list->li_Node.no_Next = node->no_Next;
1160 AddList(List *list, const char *name, int n)
1168 * Scan against wildcards. Only a node value of 1 can be a wildcard
1169 * ( usually scanned from .cpignore )
1172 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1173 if (strcmp(name, node->no_Name) == 0 ||
1174 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1176 return(node->no_Value);
1181 * Look for exact match
1184 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1185 if (strcmp(name, node->no_Name) == 0) {
1186 return(node->no_Value);
1189 node = malloc(sizeof(Node) + strlen(name) + 1);
1191 fprintf(stderr, "out of memory\n");
1195 node->no_Next = list->li_Node.no_Next;
1196 list->li_Node.no_Next = node;
1198 node->no_HNext = list->li_Hash[hv];
1199 list->li_Hash[hv] = node;
1201 strcpy(node->no_Name, name);
1208 shash(const char *s)
1215 if (*s == '*' || *s == '?' ||
1216 *s == '{' || *s == '}' ||
1217 *s == '[' || *s == ']' ||
1222 hv = (hv << 5) ^ *s ^ (hv >> 23);
1225 return(((hv >> 16) ^ hv) & HMASK);
1229 * WildCmp() - compare wild string to sane string
1231 * Return 0 on success, -1 on failure.
1235 WildCmp(const char *w, const char *s)
1238 * skip fixed portion
1244 if (w[1] == 0) /* optimize wild* case */
1250 for (i = 0; i <= l; ++i) {
1251 if (WildCmp(w + 1, s + i) == 0)
1265 if (*w == 0) /* terminator */
1277 YesNo(const char *path)
1281 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1284 first = ch = getchar();
1285 while (ch != '\n' && ch != EOF)
1287 return ((first == 'y' || first == 'Y'));
1291 * xrename() - rename with override
1293 * If the rename fails, attempt to override st_flags on the
1294 * destination and rename again. If that fails too, try to
1295 * set the flags back the way they were and give up.
1299 xrename(const char *src, const char *dst, u_long flags)
1305 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1306 #ifdef _ST_FLAGS_PRESENT_
1307 hc_chflags(&DstHost, dst, 0);
1308 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1309 hc_chflags(&DstHost, dst, flags);
1316 xlink(const char *src, const char *dst, u_long flags)
1319 #ifdef _ST_FLAGS_PRESENT_
1325 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1326 #ifdef _ST_FLAGS_PRESENT_
1327 hc_chflags(&DstHost, src, 0);
1328 r = hc_link(&DstHost, src, dst);
1330 hc_chflags(&DstHost, src, flags);