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.19 2008/03/22 18:09:16 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 validate_check(const char *spath, const char *dpath);
102 static int shash(const char *s);
103 static void hltdelete(struct hlink *);
104 int YesNo(const char *path);
105 static int xrename(const char *src, const char *dst, u_long flags);
106 static int xlink(const char *src, const char *dst, u_long flags);
107 int WildCmp(const char *s1, const char *s2);
108 int DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo);
110 int AskConfirmation = 1;
121 int EnableDirectoryRetries;
126 const char *UseCpFile;
127 const char *UseHLPath;
128 const char *MD5CacheFile;
129 const char *FSMIDCacheFile;
131 int64_t CountSourceBytes;
132 int64_t CountSourceItems;
133 int64_t CountCopiedItems;
134 int64_t CountSourceReadBytes;
135 int64_t CountTargetReadBytes;
136 int64_t CountWriteBytes;
137 int64_t CountRemovedItems;
138 int64_t CountLinkedItems;
140 struct HostConf SrcHost;
141 struct HostConf DstHost;
144 main(int ac, char **av)
150 struct timeval start;
152 signal(SIGPIPE, SIG_IGN);
154 gettimeofday(&start, NULL);
155 for (i = 1; i < ac; ++i) {
162 } else if (dst == NULL) {
165 fatal("too many arguments");
173 v = strtol(ptr, NULL, 0);
178 while (*ptr == 'v') {
182 if (*ptr >= '0' && *ptr <= '9')
183 VerboseOpt = strtol(ptr, NULL, 0);
195 UseCpFile = ".cpignore";
198 UseCpFile = (*ptr) ? ptr : av[++i];
201 UseHLPath = (*ptr) ? ptr : av[++i];
223 FSMIDCacheFile = ".FSMID.CHECK";
227 FSMIDCacheFile = av[++i];
231 MD5CacheFile = av[++i];
235 MD5CacheFile = ".MD5.CHECKSUMS";
238 setvbuf(stdout, NULL, _IOLBF, 0);
241 fatal("illegal option: %s\n", ptr - 2);
248 * If we are told to go into slave mode, run the HC protocol
256 * Extract the source and/or/neither target [user@]host and
257 * make any required connections.
259 if (src && (ptr = strchr(src, ':')) != NULL) {
260 asprintf(&SrcHost.host, "%*.*s", ptr - src, ptr - src, src);
263 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
267 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
270 if (hc_connect(&SrcHost) < 0)
271 fprintf(stderr, "Unable to connect to %s\n", SrcHost.host);
273 if (dst && (ptr = strchr(dst, ':')) != NULL) {
274 asprintf(&DstHost.host, "%*.*s", ptr - dst, ptr - dst, dst);
277 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
280 if (hc_connect(&DstHost) < 0)
281 fprintf(stderr, "Unable to connect to %s\n", DstHost.host);
285 * dst may be NULL only if -m option is specified,
286 * which forces an update of the MD5 checksums
288 if (dst == NULL && UseMD5Opt == 0) {
293 DstBaseLen = strlen(dst);
294 i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
296 i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
303 if (SummaryOpt && i == 0) {
307 gettimeofday(&end, NULL);
309 /* don't count stat's in our byte statistics */
310 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
311 CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
312 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
313 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
316 duration = end.tv_sec - start.tv_sec;
318 duration += end.tv_usec - start.tv_usec;
319 if (duration == 0) duration = 1;
320 logstd("cpdup completed successfully\n");
321 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
322 "%lld bytes written (%.1fX speedup)\n",
323 (long long)CountSourceBytes,
324 (long long)CountSourceReadBytes,
325 (long long)CountTargetReadBytes,
326 (long long)CountWriteBytes,
327 ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
328 logstd("%lld source items, %lld items copied, %lld items linked, "
329 "%lld things deleted\n",
330 (long long)CountSourceItems,
331 (long long)CountCopiedItems,
332 (long long)CountLinkedItems,
333 (long long)CountRemovedItems);
334 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
335 (float)duration / (float)1000000,
336 (long)((long)1000000 * (CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration / 1024.0),
337 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
339 exit((i == 0) ? 0 : 1);
342 static struct hlink *
343 hltlookup(struct stat *stp)
348 n = stp->st_ino & HLMASK;
350 for (hl = hltable[n]; hl; hl = hl->next)
351 if (hl->ino == stp->st_ino)
357 static struct hlink *
358 hltadd(struct stat *stp, const char *path)
361 int plen = strlen(path);
364 new = malloc(offsetof(struct hlink, name[plen + 1]));
366 fprintf(stderr, "out of memory\n");
370 /* initialize and link the new element into the table */
371 new->ino = stp->st_ino;
373 bcopy(path, new->name, plen + 1);
376 n = stp->st_ino & HLMASK;
377 new->next = hltable[n];
379 hltable[n]->prev = new;
386 hltdelete(struct hlink *hl)
390 hl->next->prev = hl->prev;
391 hl->prev->next = hl->next;
394 hl->next->prev = NULL;
396 hltable[hl->ino & HLMASK] = hl->next;
403 * If UseHLPath is defined check to see if the file in question is
404 * the same as the source file, and if it is return a pointer to the
405 * -H path based file for hardlinking. Else return NULL.
408 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
414 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
417 * stat info matches ?
419 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
420 st1->st_size != sthl.st_size ||
421 st1->st_uid != sthl.st_uid ||
422 st1->st_gid != sthl.st_gid ||
423 st1->st_mtime != sthl.st_mtime
430 * If ForceOpt or ValidateOpt is set we have to compare the files
432 if (ForceOpt || ValidateOpt) {
433 error = validate_check(spath, hpath);
443 * Return 0 if the contents of the file <spath> matches the contents of
447 validate_check(const char *spath, const char *dpath)
453 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
454 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
457 if (fd1 >= 0 && fd2 >= 0) {
461 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
462 CountSourceReadBytes += n;
463 x = hc_read(&DstHost, fd2, IOBuf2, sizeof(IOBuf2));
465 CountTargetReadBytes += x;
468 if (bcmp(IOBuf1, IOBuf2, n) != 0)
475 hc_close(&SrcHost, fd1);
477 hc_close(&DstHost, fd2);
482 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
486 int r, mres, fres, st2Valid;
492 r = mres = fres = st2Valid = 0;
496 if (hc_lstat(&SrcHost, spath, &st1) != 0)
498 st2.st_mode = 0; /* in case lstat fails */
499 st2.st_flags = 0; /* in case lstat fails */
500 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
503 if (S_ISREG(st1.st_mode)) {
511 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
512 if ((hln = hltlookup(&st1)) != NULL) {
516 if (st2.st_ino == hln->dino) {
518 * hard link is already correct, nothing to do
521 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
522 if (hln->nlinked == st1.st_nlink)
528 * hard link is not correct, attempt to unlink it
530 if (hc_remove(&DstHost, dpath) < 0) {
531 logerr("%-32s hardlink: unable to unlink: %s\n",
532 ((dpath) ? dpath : spath), strerror(errno));
539 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
540 int tryrelink = (errno == EMLINK);
541 logerr("%-32s hardlink: unable to link to %s: %s\n",
542 (dpath ? dpath : spath), hln->name, strerror(errno)
547 logerr("%-20s hardlink: will attempt to copy normally\n");
552 if (hln->nlinked == st1.st_nlink) {
558 logstd("%-32s hardlink: %s\n",
559 (dpath ? dpath : spath),
560 (st2Valid ? "relinked" : "linked")
570 * first instance of hardlink must be copied normally
573 hln = hltadd(&st1, dpath);
578 * Do we need to copy the file/dir/link/whatever? Early termination
579 * if we do not. Always redo links. Directories are always traversed
580 * except when the FSMID options are used.
582 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
587 && st1.st_mode == st2.st_mode
588 #ifdef _ST_FLAGS_PRESENT_
589 && st1.st_flags == st2.st_flags
592 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
594 * If FSMID tracking is turned on we can avoid recursing through
595 * an entire directory subtree if the FSMID matches.
597 #ifdef _ST_FSMID_PRESENT_
599 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
601 if (VerboseOpt >= 3) {
603 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
605 logstd("%-32s nochange\n", (dpath ? dpath : spath));
612 st1.st_size == st2.st_size &&
613 st1.st_uid == st2.st_uid &&
614 st1.st_gid == st2.st_gid &&
615 st1.st_mtime == st2.st_mtime
617 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
618 (mres = md5_check(spath, dpath)) == 0)
620 #ifdef _ST_FSMID_PRESENT_
621 && (UseFSMIDOpt == 0 ||
622 (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
624 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
625 validate_check(spath, dpath) == 0)
628 hln->dino = st2.st_ino;
629 if (VerboseOpt >= 3) {
632 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
636 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
637 else if (ValidateOpt)
638 logstd("%-32s nochange (contents validated)\n", (dpath ? dpath : spath));
640 logstd("%-32s nochange\n", (dpath ? dpath : spath));
642 CountSourceBytes += size;
649 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
651 logerr("%-32s SAFETY - refusing to copy file over directory\n",
652 (dpath ? dpath : spath)
655 return(0); /* continue with the cpdup anyway */
657 if (QuietOpt == 0 || AskConfirmation) {
658 logstd("%-32s WARNING: non-directory source will blow away\n"
659 "%-32s preexisting dest directory, continuing anyway!\n",
660 ((dpath) ? dpath : spath), "");
663 RemoveRecur(dpath, ddevNo);
667 * The various comparisons failed, copy it.
669 if (S_ISDIR(st1.st_mode)) {
673 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
674 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
679 if (S_ISDIR(st2.st_mode) == 0) {
680 hc_remove(&DstHost, dpath);
681 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
682 logerr("%s: mkdir failed: %s\n",
683 (dpath ? dpath : spath), strerror(errno));
688 * Matt: why don't you check error codes here?
690 hc_lstat(&DstHost, dpath, &st2);
691 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
695 * Directory must be scanable by root for cpdup to
696 * work. We'll fix it later if the directory isn't
697 * supposed to be readable ( which is why we fixup
698 * st2.st_mode to match what we did ).
700 if ((st2.st_mode & 0700) != 0700) {
701 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
705 logstd("%s\n", dpath ? dpath : spath);
709 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
715 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
722 * scan .cpignore file for files/directories
731 if (UseCpFile[0] == '/') {
732 fpath = mprintf("%s", UseCpFile);
734 fpath = mprintf("%s/%s", spath, UseCpFile);
736 AddList(&list, strrchr(fpath, '/') + 1, 1);
737 if ((fi = fopen(fpath, "r")) != NULL) {
738 while (fgets(buf, sizeof(buf), fi) != NULL) {
740 CountSourceReadBytes += l;
741 if (l && buf[l-1] == '\n')
743 if (buf[0] && buf[0] != '#')
744 AddList(&list, buf, 1);
752 * Automatically exclude MD5CacheFile that we create on the
753 * source from the copy to the destination.
755 * Automatically exclude a FSMIDCacheFile on the source that
756 * would otherwise overwrite the one we maintain on the target.
759 AddList(&list, MD5CacheFile, 1);
761 AddList(&list, FSMIDCacheFile, 1);
763 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
770 if (strcmp(den->d_name, ".") == 0 ||
771 strcmp(den->d_name, "..") == 0
776 * ignore if on .cpignore list
778 if (AddList(&list, den->d_name, 0) == 1) {
781 nspath = mprintf("%s/%s", spath, den->d_name);
783 ndpath = mprintf("%s/%s", dpath, den->d_name);
795 hc_closedir(&SrcHost, dir);
798 * Remove files/directories from destination that do not appear
801 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
802 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
806 if (strcmp(den->d_name, ".") == 0 ||
807 strcmp(den->d_name, "..") == 0
812 * If object does not exist in source or .cpignore
813 * then recursively remove it.
815 if (AddList(&list, den->d_name, 3) == 3) {
818 ndpath = mprintf("%s/%s", dpath, den->d_name);
819 RemoveRecur(ndpath, ddevNo);
823 hc_closedir(&DstHost, dir);
827 struct timeval tv[2];
831 st1.st_uid != st2.st_uid ||
832 st1.st_gid != st2.st_gid
834 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
836 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
837 hc_chmod(&DstHost, dpath, st1.st_mode);
839 #ifdef _ST_FLAGS_PRESENT_
840 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
841 hc_chflags(&DstHost, dpath, st1.st_flags);
846 st1.st_mtime != st2.st_mtime
848 bzero(tv, sizeof(tv));
849 tv[0].tv_sec = st1.st_mtime;
850 tv[1].tv_sec = st1.st_mtime;
851 hc_utimes(&DstHost, dpath, tv);
855 } else if (dpath == NULL) {
857 * If dpath is NULL, we are just updating the MD5
860 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
861 mres = md5_check(spath, NULL);
863 if (VerboseOpt > 1) {
865 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
867 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
868 } else if (!QuietOpt && mres < 0) {
869 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
873 } else if (S_ISREG(st1.st_mode)) {
879 path = mprintf("%s.tmp", dpath);
882 * Handle check failure message.
886 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
890 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
893 * Not quite ready to do the copy yet. If UseHLPath is defined,
894 * see if we can hardlink instead.
896 * If we can hardlink, and the target exists, we have to remove it
897 * first or the hardlink will fail. This can occur in a number of
898 * situations but must typically when the '-f -H' combination is
901 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
903 hc_remove(&DstHost, dpath);
904 if (hc_link(&DstHost, hpath, dpath) == 0) {
907 logstd("%-32s hardlinked(-H)\n",
908 (dpath ? dpath : spath));
914 * Shucks, we may have hit a filesystem hard linking limit,
915 * we have to copy instead.
920 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
921 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
923 * There could be a .tmp file from a previously interrupted
924 * run, delete and retry. Fail if we still can't get at it.
926 #ifdef _ST_FLAGS_PRESENT_
927 hc_chflags(&DstHost, path, 0);
929 hc_remove(&DstHost, path);
930 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
937 * Matt: What about holes?
940 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
942 if (hc_write(&DstHost, fd2, IOBuf1, n) != n)
946 hc_close(&DstHost, fd2);
948 struct timeval tv[2];
950 bzero(tv, sizeof(tv));
951 tv[0].tv_sec = st1.st_mtime;
952 tv[1].tv_sec = st1.st_mtime;
954 hc_utimes(&DstHost, path, tv);
955 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
956 hc_chmod(&DstHost, path, st1.st_mode);
957 if (xrename(path, dpath, st2.st_flags) != 0) {
958 logerr("%-32s rename-after-copy failed: %s\n",
959 (dpath ? dpath : spath), strerror(errno)
964 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
965 #ifdef _ST_FLAGS_PRESENT_
967 hc_chflags(&DstHost, dpath, st1.st_flags);
970 CountSourceReadBytes += size;
971 CountWriteBytes += size;
972 CountSourceBytes += size;
976 logerr("%-32s %s failed: %s\n",
977 (dpath ? dpath : spath), op, strerror(errno)
979 hc_remove(&DstHost, path);
983 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
984 (dpath ? dpath : spath), getuid(), geteuid(),
989 hc_close(&SrcHost, fd1);
991 logerr("%-32s copy: open failed: %s\n",
992 (dpath ? dpath : spath),
1001 if (!r && hc_stat(&DstHost, dpath, &st2) == 0)
1002 hln->dino = st2.st_ino;
1006 } else if (S_ISLNK(st1.st_mode)) {
1013 snprintf(path, sizeof(path), "%s.tmp", dpath);
1014 n1 = hc_readlink(&SrcHost, spath, link1, sizeof(link1) - 1);
1015 n2 = hc_readlink(&DstHost, dpath, link2, sizeof(link2) - 1);
1017 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1018 hc_umask(&DstHost, ~st1.st_mode);
1019 hc_remove(&DstHost, path);
1021 if (hc_symlink(&DstHost, link1, path) < 0) {
1022 logerr("%-32s symlink (%s->%s) failed: %s\n",
1023 (dpath ? dpath : spath), link1, path,
1028 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1030 * there is no lchmod() or lchflags(), we
1031 * cannot chmod or chflags a softlink.
1033 if (xrename(path, dpath, st2.st_flags) != 0) {
1034 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1035 (dpath ? dpath : spath),
1036 path, dpath, strerror(errno));
1037 } else if (VerboseOpt) {
1038 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1040 hc_umask(&DstHost, 000);
1041 CountWriteBytes += n1;
1045 if (VerboseOpt >= 3)
1046 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1048 CountSourceBytes += n1;
1049 CountSourceReadBytes += n1;
1051 CountTargetReadBytes += n2;
1055 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1057 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1062 st1.st_mode != st2.st_mode ||
1063 st1.st_rdev != st2.st_rdev ||
1064 st1.st_uid != st2.st_uid ||
1065 st1.st_gid != st2.st_gid
1067 snprintf(path, sizeof(path), "%s.tmp", dpath);
1069 hc_remove(&DstHost, path);
1070 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
1071 hc_chmod(&DstHost, path, st1.st_mode);
1072 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1073 hc_remove(&DstHost, dpath);
1074 if (xrename(path, dpath, st2.st_flags) != 0) {
1075 logerr("%-32s dev-rename-after-create failed: %s\n",
1076 (dpath ? dpath : spath),
1079 } else if (VerboseOpt) {
1080 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1085 logerr("%-32s dev failed: %s\n",
1086 (dpath ? dpath : spath), strerror(errno)
1090 if (VerboseOpt >= 3)
1091 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1104 RemoveRecur(const char *dpath, dev_t devNo)
1108 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1111 if (st.st_dev == devNo) {
1112 if (S_ISDIR(st.st_mode)) {
1115 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1117 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1120 if (strcmp(den->d_name, ".") == 0)
1122 if (strcmp(den->d_name, "..") == 0)
1124 ndpath = mprintf("%s/%s", dpath, den->d_name);
1125 RemoveRecur(ndpath, devNo);
1128 hc_closedir(&DstHost, dir);
1130 if (AskConfirmation && NoRemoveOpt == 0) {
1132 if (hc_rmdir(&DstHost, dpath) < 0) {
1133 logerr("%-32s rmdir failed: %s\n",
1134 dpath, strerror(errno)
1137 CountRemovedItems++;
1142 logstd("%-32s not-removed\n", dpath);
1143 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1145 logstd("%-32s rmdir-ok\n", dpath);
1146 CountRemovedItems++;
1148 logerr("%-32s rmdir failed: %s\n",
1149 dpath, strerror(errno)
1154 if (AskConfirmation && NoRemoveOpt == 0) {
1156 if (hc_remove(&DstHost, dpath) < 0) {
1157 logerr("%-32s remove failed: %s\n",
1158 dpath, strerror(errno)
1161 CountRemovedItems++;
1166 logstd("%-32s not-removed\n", dpath);
1167 } else if (hc_remove(&DstHost, dpath) == 0) {
1169 logstd("%-32s remove-ok\n", dpath);
1170 CountRemovedItems++;
1172 logerr("%-32s remove failed: %s\n",
1173 dpath, strerror(errno)
1183 InitList(List *list)
1185 bzero(list, sizeof(List));
1186 list->li_Node.no_Next = &list->li_Node;
1190 ResetList(List *list)
1194 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1195 list->li_Node.no_Next = node->no_Next;
1202 AddList(List *list, const char *name, int n)
1210 * Scan against wildcards. Only a node value of 1 can be a wildcard
1211 * ( usually scanned from .cpignore )
1214 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1215 if (strcmp(name, node->no_Name) == 0 ||
1216 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1218 return(node->no_Value);
1223 * Look for exact match
1226 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1227 if (strcmp(name, node->no_Name) == 0) {
1228 return(node->no_Value);
1231 node = malloc(sizeof(Node) + strlen(name) + 1);
1233 fprintf(stderr, "out of memory\n");
1237 node->no_Next = list->li_Node.no_Next;
1238 list->li_Node.no_Next = node;
1240 node->no_HNext = list->li_Hash[hv];
1241 list->li_Hash[hv] = node;
1243 strcpy(node->no_Name, name);
1250 shash(const char *s)
1257 if (*s == '*' || *s == '?' ||
1258 *s == '{' || *s == '}' ||
1259 *s == '[' || *s == ']' ||
1264 hv = (hv << 5) ^ *s ^ (hv >> 23);
1267 return(((hv >> 16) ^ hv) & HMASK);
1271 * WildCmp() - compare wild string to sane string
1273 * Return 0 on success, -1 on failure.
1277 WildCmp(const char *w, const char *s)
1280 * skip fixed portion
1286 if (w[1] == 0) /* optimize wild* case */
1292 for (i = 0; i <= l; ++i) {
1293 if (WildCmp(w + 1, s + i) == 0)
1307 if (*w == 0) /* terminator */
1319 YesNo(const char *path)
1323 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1326 first = ch = getchar();
1327 while (ch != '\n' && ch != EOF)
1329 return ((first == 'y' || first == 'Y'));
1333 * xrename() - rename with override
1335 * If the rename fails, attempt to override st_flags on the
1336 * destination and rename again. If that fails too, try to
1337 * set the flags back the way they were and give up.
1341 xrename(const char *src, const char *dst, u_long flags)
1347 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1348 #ifdef _ST_FLAGS_PRESENT_
1349 hc_chflags(&DstHost, dst, 0);
1350 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1351 hc_chflags(&DstHost, dst, flags);
1358 xlink(const char *src, const char *dst, u_long flags)
1361 #ifdef _ST_FLAGS_PRESENT_
1367 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1368 #ifdef _ST_FLAGS_PRESENT_
1369 hc_chflags(&DstHost, src, 0);
1370 r = hc_link(&DstHost, src, dst);
1372 hc_chflags(&DstHost, src, flags);