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.30 2008/06/27 01:15:39 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 #define MAXDEPTH 32 /* max copy depth for thread */
68 #define GETBUFSIZE 8192
69 #define GETPATHSIZE 2048
70 #define GETLINKSIZE 1024
71 #define GETIOSIZE 65536
73 #ifndef _ST_FLAGS_PRESENT_
74 #define st_flags st_mode
79 struct Node *no_HNext;
99 typedef struct copy_info {
105 struct copy_info *parent;
112 struct hlink *hltable[HLSIZE];
114 void RemoveRecur(const char *dpath, dev_t devNo);
115 void InitList(List *list);
116 void ResetList(List *list);
117 int AddList(List *list, const char *name, int n);
118 static struct hlink *hltlookup(struct stat *);
119 static struct hlink *hltadd(struct stat *, const char *);
120 static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
121 static int validate_check(const char *spath, const char *dpath);
122 static int shash(const char *s);
123 static void hltdelete(struct hlink *);
124 static void hltsetdino(struct hlink *, ino_t);
125 int YesNo(const char *path);
126 static int xrename(const char *src, const char *dst, u_long flags);
127 static int xlink(const char *src, const char *dst, u_long flags);
128 int WildCmp(const char *s1, const char *s2);
129 static int DoCopy(copy_info_t info, int depth);
131 int AskConfirmation = 1;
143 int EnableDirectoryRetries;
147 int MaxParallel = -1;
149 const char *UseCpFile;
150 const char *UseHLPath;
151 const char *MD5CacheFile;
152 const char *FSMIDCacheFile;
154 int64_t CountSourceBytes;
155 int64_t CountSourceItems;
156 int64_t CountCopiedItems;
157 int64_t CountSourceReadBytes;
158 int64_t CountTargetReadBytes;
159 int64_t CountWriteBytes;
160 int64_t CountRemovedItems;
161 int64_t CountLinkedItems;
163 struct HostConf SrcHost;
164 struct HostConf DstHost;
167 pthread_mutex_t MasterMutex;
171 main(int ac, char **av)
177 struct timeval start;
178 struct copy_info info;
180 signal(SIGPIPE, SIG_IGN);
183 for (i = 0; i < HCTHASH_SIZE; ++i) {
184 pthread_mutex_init(&SrcHost.hct_mutex[i], NULL);
185 pthread_mutex_init(&DstHost.hct_mutex[i], NULL);
187 pthread_mutex_init(&MasterMutex, NULL);
188 pthread_mutex_lock(&MasterMutex);
191 gettimeofday(&start, NULL);
192 for (i = 1; i < ac; ++i) {
199 } else if (dst == NULL) {
202 fatal("too many arguments");
210 v = strtol(ptr, NULL, 0);
218 while (*ptr == 'v') {
222 if (*ptr >= '0' && *ptr <= '9')
223 VerboseOpt = strtol(ptr, NULL, 0);
239 UseCpFile = ".cpignore";
242 UseCpFile = (*ptr) ? ptr : av[++i];
245 UseHLPath = (*ptr) ? ptr : av[++i];
270 FSMIDCacheFile = ".FSMID.CHECK";
274 FSMIDCacheFile = av[++i];
278 MD5CacheFile = av[++i];
282 MD5CacheFile = ".MD5.CHECKSUMS";
285 setvbuf(stdout, NULL, _IOLBF, 0);
288 fatal("illegal option: %s\n", ptr - 2);
295 * If we are told to go into slave mode, run the HC protocol
303 * Extract the source and/or/neither target [user@]host and
304 * make any required connections.
306 if (src && (ptr = strchr(src, ':')) != NULL) {
307 asprintf(&SrcHost.host, "%*.*s", ptr - src, ptr - src, src);
310 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
314 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
317 if (hc_connect(&SrcHost) < 0)
320 if (dst && (ptr = strchr(dst, ':')) != NULL) {
321 asprintf(&DstHost.host, "%*.*s", ptr - dst, ptr - dst, dst);
324 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
327 if (hc_connect(&DstHost) < 0)
332 * dst may be NULL only if -m option is specified,
333 * which forces an update of the MD5 checksums
335 if (dst == NULL && UseMD5Opt == 0) {
339 bzero(&info, sizeof(info));
343 pthread_cond_init(&info.cond, NULL);
346 DstBaseLen = strlen(dst);
349 info.sdevNo = (dev_t)-1;
350 info.ddevNo = (dev_t)-1;
351 i = DoCopy(&info, -1);
355 info.sdevNo = (dev_t)-1;
356 info.ddevNo = (dev_t)-1;
357 i = DoCopy(&info, -1);
360 pthread_cond_destroy(&info.cond);
367 if (SummaryOpt && i == 0) {
371 gettimeofday(&end, NULL);
373 /* don't count stat's in our byte statistics */
374 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
375 CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
376 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
377 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
380 duration = (end.tv_sec - start.tv_sec);
381 duration += (double)(end.tv_usec - start.tv_usec) / 1000000.0;
384 logstd("cpdup completed successfully\n");
385 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
386 "%lld bytes written (%.1fX speedup)\n",
387 (long long)CountSourceBytes,
388 (long long)CountSourceReadBytes,
389 (long long)CountTargetReadBytes,
390 (long long)CountWriteBytes,
391 ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
392 logstd("%lld source items, %lld items copied, %lld items linked, "
393 "%lld things deleted\n",
394 (long long)CountSourceItems,
395 (long long)CountCopiedItems,
396 (long long)CountLinkedItems,
397 (long long)CountRemovedItems);
398 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
400 (int)((CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration / 1024.0),
401 (int)(CountSourceBytes / duration / 1024.0));
403 exit((i == 0) ? 0 : 1);
406 static struct hlink *
407 hltlookup(struct stat *stp)
410 struct timespec ts = { 0, 100000 };
415 n = stp->st_ino & HLMASK;
420 for (hl = hltable[n]; hl; hl = hl->next) {
421 if (hl->ino == stp->st_ino) {
424 * If the hl entry is still in the process of being created
425 * by another thread we have to wait until it has either been
426 * deleted or completed.
429 pthread_mutex_unlock(&MasterMutex);
430 nanosleep(&ts, NULL);
431 pthread_mutex_lock(&MasterMutex);
443 static struct hlink *
444 hltadd(struct stat *stp, const char *path)
447 int plen = strlen(path);
450 new = malloc(offsetof(struct hlink, name[plen + 1]));
452 fprintf(stderr, "out of memory\n");
457 /* initialize and link the new element into the table */
458 new->ino = stp->st_ino;
459 new->dino = (ino_t)-1;
461 bcopy(path, new->name, plen + 1);
464 n = stp->st_ino & HLMASK;
465 new->next = hltable[n];
467 hltable[n]->prev = new;
474 hltsetdino(struct hlink *hl, ino_t inum)
480 hltdelete(struct hlink *hl)
482 assert(hl->refs == 1);
486 hl->next->prev = hl->prev;
487 hl->prev->next = hl->next;
490 hl->next->prev = NULL;
492 hltable[hl->ino & HLMASK] = hl->next;
499 hltrels(struct hlink *hl)
501 assert(hl->refs == 1);
506 * If UseHLPath is defined check to see if the file in question is
507 * the same as the source file, and if it is return a pointer to the
508 * -H path based file for hardlinking. Else return NULL.
511 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
517 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
520 * stat info matches ?
522 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
523 st1->st_size != sthl.st_size ||
524 st1->st_uid != sthl.st_uid ||
525 st1->st_gid != sthl.st_gid ||
526 st1->st_mtime != sthl.st_mtime
533 * If ForceOpt or ValidateOpt is set we have to compare the files
535 if (ForceOpt || ValidateOpt) {
536 error = validate_check(spath, hpath);
546 * Return 0 if the contents of the file <spath> matches the contents of
550 validate_check(const char *spath, const char *dpath)
556 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
557 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
560 if (fd1 >= 0 && fd2 >= 0) {
563 char *iobuf1 = malloc(GETIOSIZE);
564 char *iobuf2 = malloc(GETIOSIZE);
566 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
567 CountSourceReadBytes += n;
568 x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
570 CountTargetReadBytes += x;
573 if (bcmp(iobuf1, iobuf2, n) != 0)
582 hc_close(&SrcHost, fd1);
584 hc_close(&DstHost, fd2);
590 DoCopyThread(void *arg)
592 copy_info_t cinfo = arg;
593 char *spath = cinfo->spath;
594 char *dpath = cinfo->dpath;
597 r = pthread_detach(pthread_self());
599 pthread_cond_init(&cinfo->cond, NULL);
600 pthread_mutex_lock(&MasterMutex);
601 cinfo->r += DoCopy(cinfo, 0);
602 /* cinfo arguments invalid on return */
603 --cinfo->parent->children;
605 pthread_cond_signal(&cinfo->parent->cond);
609 pthread_cond_destroy(&cinfo->cond);
611 hcc_free_trans(&SrcHost);
612 hcc_free_trans(&DstHost);
613 pthread_mutex_unlock(&MasterMutex);
620 DoCopy(copy_info_t info, int depth)
622 const char *spath = info->spath;
623 const char *dpath = info->dpath;
624 dev_t sdevNo = info->sdevNo;
625 dev_t ddevNo = info->ddevNo;
628 int r, mres, fres, st2Valid;
630 List *list = malloc(sizeof(List));
634 r = mres = fres = st2Valid = 0;
638 if (hc_lstat(&SrcHost, spath, &st1) != 0) {
642 st2.st_mode = 0; /* in case lstat fails */
643 st2.st_flags = 0; /* in case lstat fails */
644 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
647 if (S_ISREG(st1.st_mode)) {
655 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
656 if ((hln = hltlookup(&st1)) != NULL) {
660 if (st2.st_ino == hln->dino) {
662 * hard link is already correct, nothing to do
665 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
666 if (hln->nlinked == st1.st_nlink) {
675 * hard link is not correct, attempt to unlink it
677 if (hc_remove(&DstHost, dpath) < 0) {
678 logerr("%-32s hardlink: unable to unlink: %s\n",
679 ((dpath) ? dpath : spath), strerror(errno));
688 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
689 int tryrelink = (errno == EMLINK);
690 logerr("%-32s hardlink: unable to link to %s: %s\n",
691 (dpath ? dpath : spath), hln->name, strerror(errno)
696 logerr("%-20s hardlink: will attempt to copy normally\n");
701 if (hln->nlinked == st1.st_nlink) {
707 logstd("%-32s hardlink: %s\n",
708 (dpath ? dpath : spath),
709 (st2Valid ? "relinked" : "linked")
720 * first instance of hardlink must be copied normally
723 hln = hltadd(&st1, dpath);
728 * Do we need to copy the file/dir/link/whatever? Early termination
729 * if we do not. Always redo links. Directories are always traversed
730 * except when the FSMID options are used.
732 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
737 && st1.st_mode == st2.st_mode
738 #ifdef _ST_FLAGS_PRESENT_
739 && st1.st_flags == st2.st_flags
742 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
744 * If FSMID tracking is turned on we can avoid recursing through
745 * an entire directory subtree if the FSMID matches.
747 #ifdef _ST_FSMID_PRESENT_
749 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
751 if (VerboseOpt >= 3) {
753 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
755 logstd("%-32s nochange\n", (dpath ? dpath : spath));
763 st1.st_size == st2.st_size &&
764 st1.st_uid == st2.st_uid &&
765 st1.st_gid == st2.st_gid &&
766 st1.st_mtime == st2.st_mtime
768 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
769 (mres = md5_check(spath, dpath)) == 0)
771 #ifdef _ST_FSMID_PRESENT_
772 && (UseFSMIDOpt == 0 ||
773 (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
775 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
776 validate_check(spath, dpath) == 0)
779 hltsetdino(hln, st2.st_ino);
780 if (VerboseOpt >= 3) {
783 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
787 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
788 else if (ValidateOpt)
789 logstd("%-32s nochange (contents validated)\n", (dpath ? dpath : spath));
791 logstd("%-32s nochange\n", (dpath ? dpath : spath));
793 CountSourceBytes += size;
800 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
802 logerr("%-32s SAFETY - refusing to copy file over directory\n",
803 (dpath ? dpath : spath)
807 goto done; /* continue with the cpdup anyway */
809 if (QuietOpt == 0 || AskConfirmation) {
810 logstd("%-32s WARNING: non-directory source will blow away\n"
811 "%-32s preexisting dest directory, continuing anyway!\n",
812 ((dpath) ? dpath : spath), "");
815 RemoveRecur(dpath, ddevNo);
819 * The various comparisons failed, copy it.
821 if (S_ISDIR(st1.st_mode)) {
825 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
826 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
831 if (S_ISDIR(st2.st_mode) == 0) {
832 hc_remove(&DstHost, dpath);
833 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
834 logerr("%s: mkdir failed: %s\n",
835 (dpath ? dpath : spath), strerror(errno));
840 * Matt: why don't you check error codes here?
842 hc_lstat(&DstHost, dpath, &st2);
843 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
847 * Directory must be scanable by root for cpdup to
848 * work. We'll fix it later if the directory isn't
849 * supposed to be readable ( which is why we fixup
850 * st2.st_mode to match what we did ).
852 if ((st2.st_mode & 0700) != 0700) {
853 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
857 logstd("%s\n", dpath ? dpath : spath);
861 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
867 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
874 * scan .cpignore file for files/directories
880 char *buf = malloc(GETBUFSIZE);
883 if (UseCpFile[0] == '/') {
884 fpath = mprintf("%s", UseCpFile);
886 fpath = mprintf("%s/%s", spath, UseCpFile);
888 AddList(list, strrchr(fpath, '/') + 1, 1);
889 if ((fi = fopen(fpath, "r")) != NULL) {
890 while (fgets(buf, GETBUFSIZE, fi) != NULL) {
892 CountSourceReadBytes += l;
893 if (l && buf[l-1] == '\n')
895 if (buf[0] && buf[0] != '#')
896 AddList(list, buf, 1);
905 * Automatically exclude MD5CacheFile that we create on the
906 * source from the copy to the destination.
908 * Automatically exclude a FSMIDCacheFile on the source that
909 * would otherwise overwrite the one we maintain on the target.
912 AddList(list, MD5CacheFile, 1);
914 AddList(list, FSMIDCacheFile, 1);
916 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
923 if (strcmp(den->d_name, ".") == 0 ||
924 strcmp(den->d_name, "..") == 0
929 * ignore if on .cpignore list
931 if (AddList(list, den->d_name, 0) == 1) {
934 nspath = mprintf("%s/%s", spath, den->d_name);
936 ndpath = mprintf("%s/%s", dpath, den->d_name);
939 if (CurParallel < MaxParallel || depth > MAXDEPTH) {
940 copy_info_t cinfo = malloc(sizeof(*cinfo));
943 bzero(cinfo, sizeof(*cinfo));
944 cinfo->spath = nspath;
945 cinfo->dpath = ndpath;
946 cinfo->sdevNo = sdevNo;
947 cinfo->ddevNo = ddevNo;
948 cinfo->parent = info;
951 pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
955 info->spath = nspath;
956 info->dpath = ndpath;
957 info->sdevNo = sdevNo;
958 info->ddevNo = ddevNo;
960 r += DoCopy(info, depth);
962 r += DoCopy(info, depth + 1);
971 hc_closedir(&SrcHost, dir);
975 * Wait for our children to finish
977 while (info->children) {
978 pthread_cond_wait(&info->cond, &MasterMutex);
985 * Remove files/directories from destination that do not appear
988 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
989 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
993 if (strcmp(den->d_name, ".") == 0 ||
994 strcmp(den->d_name, "..") == 0
999 * If object does not exist in source or .cpignore
1000 * then recursively remove it.
1002 if (AddList(list, den->d_name, 3) == 3) {
1005 ndpath = mprintf("%s/%s", dpath, den->d_name);
1006 RemoveRecur(ndpath, ddevNo);
1010 hc_closedir(&DstHost, dir);
1014 struct timeval tv[2];
1018 st1.st_uid != st2.st_uid ||
1019 st1.st_gid != st2.st_gid
1021 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
1023 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
1024 hc_chmod(&DstHost, dpath, st1.st_mode);
1026 #ifdef _ST_FLAGS_PRESENT_
1027 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
1028 hc_chflags(&DstHost, dpath, st1.st_flags);
1033 st1.st_mtime != st2.st_mtime
1035 bzero(tv, sizeof(tv));
1036 tv[0].tv_sec = st1.st_mtime;
1037 tv[1].tv_sec = st1.st_mtime;
1038 hc_utimes(&DstHost, dpath, tv);
1042 } else if (dpath == NULL) {
1044 * If dpath is NULL, we are just updating the MD5
1047 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
1048 mres = md5_check(spath, NULL);
1050 if (VerboseOpt > 1) {
1052 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1054 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
1055 } else if (!QuietOpt && mres < 0) {
1056 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1060 } else if (S_ISREG(st1.st_mode)) {
1066 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1069 * Handle check failure message.
1073 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1077 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1080 * Not quite ready to do the copy yet. If UseHLPath is defined,
1081 * see if we can hardlink instead.
1083 * If we can hardlink, and the target exists, we have to remove it
1084 * first or the hardlink will fail. This can occur in a number of
1085 * situations but must typically when the '-f -H' combination is
1088 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1090 hc_remove(&DstHost, dpath);
1091 if (hc_link(&DstHost, hpath, dpath) == 0) {
1094 logstd("%-32s hardlinked(-H)\n",
1095 (dpath ? dpath : spath));
1101 * Shucks, we may have hit a filesystem hard linking limit,
1102 * we have to copy instead.
1107 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1108 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1110 * There could be a .tmp file from a previously interrupted
1111 * run, delete and retry. Fail if we still can't get at it.
1113 #ifdef _ST_FLAGS_PRESENT_
1114 hc_chflags(&DstHost, path, 0);
1116 hc_remove(&DstHost, path);
1117 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1121 char *iobuf1 = malloc(GETIOSIZE);
1125 * Matt: What about holes?
1128 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1130 if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1134 hc_close(&DstHost, fd2);
1136 struct timeval tv[2];
1138 bzero(tv, sizeof(tv));
1139 tv[0].tv_sec = st1.st_mtime;
1140 tv[1].tv_sec = st1.st_mtime;
1142 hc_utimes(&DstHost, path, tv);
1143 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1144 hc_chmod(&DstHost, path, st1.st_mode);
1145 if (xrename(path, dpath, st2.st_flags) != 0) {
1146 logerr("%-32s rename-after-copy failed: %s\n",
1147 (dpath ? dpath : spath), strerror(errno)
1152 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1153 #ifdef _ST_FLAGS_PRESENT_
1155 hc_chflags(&DstHost, dpath, st1.st_flags);
1158 CountSourceReadBytes += size;
1159 CountWriteBytes += size;
1160 CountSourceBytes += size;
1164 logerr("%-32s %s failed: %s\n",
1165 (dpath ? dpath : spath), op, strerror(errno)
1167 hc_remove(&DstHost, path);
1172 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1173 (dpath ? dpath : spath), getuid(), geteuid(),
1178 hc_close(&SrcHost, fd1);
1180 logerr("%-32s copy: open failed: %s\n",
1181 (dpath ? dpath : spath),
1190 if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1191 hltsetdino(hln, st2.st_ino);
1197 } else if (S_ISLNK(st1.st_mode)) {
1198 char *link1 = malloc(GETLINKSIZE);
1199 char *link2 = malloc(GETLINKSIZE);
1200 char *path = malloc(GETPATHSIZE);
1204 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1205 n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1206 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1208 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1209 hc_umask(&DstHost, ~st1.st_mode);
1210 hc_remove(&DstHost, path);
1212 if (hc_symlink(&DstHost, link1, path) < 0) {
1213 logerr("%-32s symlink (%s->%s) failed: %s\n",
1214 (dpath ? dpath : spath), link1, path,
1219 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1221 * there is no lchmod() or lchflags(), we
1222 * cannot chmod or chflags a softlink.
1224 if (xrename(path, dpath, st2.st_flags) != 0) {
1225 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1226 (dpath ? dpath : spath),
1227 path, dpath, strerror(errno));
1228 } else if (VerboseOpt) {
1229 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1231 hc_umask(&DstHost, 000);
1232 CountWriteBytes += n1;
1236 if (VerboseOpt >= 3)
1237 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1239 CountSourceBytes += n1;
1240 CountSourceReadBytes += n1;
1242 CountTargetReadBytes += n2;
1246 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1251 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1252 char *path = malloc(GETPATHSIZE);
1256 st1.st_mode != st2.st_mode ||
1257 st1.st_rdev != st2.st_rdev ||
1258 st1.st_uid != st2.st_uid ||
1259 st1.st_gid != st2.st_gid
1261 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1263 hc_remove(&DstHost, path);
1264 if (hc_mknod(&DstHost, path, st1.st_mode, st1.st_rdev) == 0) {
1265 hc_chmod(&DstHost, path, st1.st_mode);
1266 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1267 hc_remove(&DstHost, dpath);
1268 if (xrename(path, dpath, st2.st_flags) != 0) {
1269 logerr("%-32s dev-rename-after-create failed: %s\n",
1270 (dpath ? dpath : spath),
1273 } else if (VerboseOpt) {
1274 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1279 logerr("%-32s dev failed: %s\n",
1280 (dpath ? dpath : spath), strerror(errno)
1284 if (VerboseOpt >= 3)
1285 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1292 if (hln->dino == (ino_t)-1) {
1294 /*hln = NULL; unneeded */
1309 RemoveRecur(const char *dpath, dev_t devNo)
1313 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1316 if (st.st_dev == devNo) {
1317 if (S_ISDIR(st.st_mode)) {
1320 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1322 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1325 if (strcmp(den->d_name, ".") == 0)
1327 if (strcmp(den->d_name, "..") == 0)
1329 ndpath = mprintf("%s/%s", dpath, den->d_name);
1330 RemoveRecur(ndpath, devNo);
1333 hc_closedir(&DstHost, dir);
1335 if (AskConfirmation && NoRemoveOpt == 0) {
1337 if (hc_rmdir(&DstHost, dpath) < 0) {
1338 logerr("%-32s rmdir failed: %s\n",
1339 dpath, strerror(errno)
1342 CountRemovedItems++;
1347 logstd("%-32s not-removed\n", dpath);
1348 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1350 logstd("%-32s rmdir-ok\n", dpath);
1351 CountRemovedItems++;
1353 logerr("%-32s rmdir failed: %s\n",
1354 dpath, strerror(errno)
1359 if (AskConfirmation && NoRemoveOpt == 0) {
1361 if (hc_remove(&DstHost, dpath) < 0) {
1362 logerr("%-32s remove failed: %s\n",
1363 dpath, strerror(errno)
1366 CountRemovedItems++;
1371 logstd("%-32s not-removed\n", dpath);
1372 } else if (hc_remove(&DstHost, dpath) == 0) {
1374 logstd("%-32s remove-ok\n", dpath);
1375 CountRemovedItems++;
1377 logerr("%-32s remove failed: %s\n",
1378 dpath, strerror(errno)
1388 InitList(List *list)
1390 bzero(list, sizeof(List));
1391 list->li_Node.no_Next = &list->li_Node;
1395 ResetList(List *list)
1399 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1400 list->li_Node.no_Next = node->no_Next;
1407 AddList(List *list, const char *name, int n)
1415 * Scan against wildcards. Only a node value of 1 can be a wildcard
1416 * ( usually scanned from .cpignore )
1419 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1420 if (strcmp(name, node->no_Name) == 0 ||
1421 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1423 return(node->no_Value);
1428 * Look for exact match
1431 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1432 if (strcmp(name, node->no_Name) == 0) {
1433 return(node->no_Value);
1436 node = malloc(sizeof(Node) + strlen(name) + 1);
1438 fprintf(stderr, "out of memory\n");
1442 node->no_Next = list->li_Node.no_Next;
1443 list->li_Node.no_Next = node;
1445 node->no_HNext = list->li_Hash[hv];
1446 list->li_Hash[hv] = node;
1448 strcpy(node->no_Name, name);
1455 shash(const char *s)
1462 if (*s == '*' || *s == '?' ||
1463 *s == '{' || *s == '}' ||
1464 *s == '[' || *s == ']' ||
1469 hv = (hv << 5) ^ *s ^ (hv >> 23);
1472 return(((hv >> 16) ^ hv) & HMASK);
1476 * WildCmp() - compare wild string to sane string
1478 * Return 0 on success, -1 on failure.
1482 WildCmp(const char *w, const char *s)
1485 * skip fixed portion
1491 if (w[1] == 0) /* optimize wild* case */
1497 for (i = 0; i <= l; ++i) {
1498 if (WildCmp(w + 1, s + i) == 0)
1512 if (*w == 0) /* terminator */
1524 YesNo(const char *path)
1528 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1531 first = ch = getchar();
1532 while (ch != '\n' && ch != EOF)
1534 return ((first == 'y' || first == 'Y'));
1538 * xrename() - rename with override
1540 * If the rename fails, attempt to override st_flags on the
1541 * destination and rename again. If that fails too, try to
1542 * set the flags back the way they were and give up.
1546 xrename(const char *src, const char *dst, u_long flags)
1552 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1553 #ifdef _ST_FLAGS_PRESENT_
1554 hc_chflags(&DstHost, dst, 0);
1555 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1556 hc_chflags(&DstHost, dst, flags);
1563 xlink(const char *src, const char *dst, u_long flags)
1566 #ifdef _ST_FLAGS_PRESENT_
1572 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1573 #ifdef _ST_FLAGS_PRESENT_
1574 hc_chflags(&DstHost, src, 0);
1575 r = hc_link(&DstHost, src, dst);
1577 hc_chflags(&DstHost, src, flags);