Fix a pipelining performance issue due to the way reading from the socket
[dragonfly.git] / bin / cpdup / cpdup.c
blob64547be6af83ccc0a2daea5b79ad0a836d43b0ac
1 /*-
2 * CPDUP.C
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.
18 * Safety features:
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
26 * Copying features:
28 * - does not copy file if mtime, flags, perms, and size match unless
29 * forced
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.28 2008/05/23 06:55:11 dillon Exp $
51 /*-
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.
58 #include "cpdup.h"
59 #include "hclink.h"
60 #include "hcproto.h"
62 #define HSIZE 8192
63 #define HMASK (HSIZE-1)
64 #define HLSIZE 8192
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
75 #endif
77 typedef struct Node {
78 struct Node *no_Next;
79 struct Node *no_HNext;
80 int no_Value;
81 char no_Name[4];
82 } Node;
84 typedef struct List {
85 Node li_Node;
86 Node *li_Hash[HSIZE];
87 } List;
89 struct hlink {
90 ino_t ino;
91 ino_t dino;
92 int refs;
93 struct hlink *next;
94 struct hlink *prev;
95 nlink_t nlinked;
96 char name[0];
99 typedef struct copy_info {
100 char *spath;
101 char *dpath;
102 dev_t sdevNo;
103 dev_t ddevNo;
104 #ifdef USE_PTHREADS
105 struct copy_info *parent;
106 pthread_cond_t cond;
107 int children;
108 int r;
109 #endif
110 } *copy_info_t;
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;
132 int SafetyOpt = 1;
133 int ForceOpt;
134 int DeviceOpt = 1;
135 int VerboseOpt;
136 int QuietOpt;
137 int NoRemoveOpt;
138 int UseMD5Opt;
139 int UseFSMIDOpt;
140 int SummaryOpt;
141 int CompressOpt;
142 int SlaveOpt;
143 int EnableDirectoryRetries;
144 int DstBaseLen;
145 int ValidateOpt;
146 int CurParallel;
147 int MaxParallel = -1;
148 int HardLinkCount;
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;
166 #if USE_PTHREADS
167 pthread_mutex_t MasterMutex;
168 #endif
171 main(int ac, char **av)
173 int i;
174 char *src = NULL;
175 char *dst = NULL;
176 char *ptr;
177 struct timeval start;
178 struct copy_info info;
180 signal(SIGPIPE, SIG_IGN);
182 #if USE_PTHREADS
183 pthread_mutex_init(&SrcHost.read_mutex, NULL);
184 pthread_mutex_init(&DstHost.read_mutex, NULL);
185 pthread_mutex_init(&MasterMutex, NULL);
186 pthread_mutex_lock(&MasterMutex);
187 #endif
189 gettimeofday(&start, NULL);
190 for (i = 1; i < ac; ++i) {
191 int v = 1;
193 ptr = av[i];
194 if (*ptr != '-') {
195 if (src == NULL) {
196 src = ptr;
197 } else if (dst == NULL) {
198 dst = ptr;
199 } else {
200 fatal("too many arguments");
201 /* not reached */
203 continue;
205 ptr += 2;
207 if (*ptr)
208 v = strtol(ptr, NULL, 0);
210 switch(ptr[-1]) {
211 case 'C':
212 CompressOpt = 1;
213 break;
214 case 'v':
215 VerboseOpt = 1;
216 while (*ptr == 'v') {
217 ++VerboseOpt;
218 ++ptr;
220 if (*ptr >= '0' && *ptr <= '9')
221 VerboseOpt = strtol(ptr, NULL, 0);
222 break;
223 case 'l':
224 setlinebuf(stdout);
225 setlinebuf(stderr);
226 break;
227 case 'V':
228 ValidateOpt = v;
229 break;
230 case 'I':
231 SummaryOpt = v;
232 break;
233 case 'o':
234 NoRemoveOpt = v;
235 break;
236 case 'x':
237 UseCpFile = ".cpignore";
238 break;
239 case 'X':
240 UseCpFile = (*ptr) ? ptr : av[++i];
241 break;
242 case 'H':
243 UseHLPath = (*ptr) ? ptr : av[++i];
244 break;
245 case 'S':
246 SlaveOpt = v;
247 break;
248 case 'f':
249 ForceOpt = v;
250 break;
251 case 'i':
252 AskConfirmation = v;
253 break;
254 case 'j':
255 DeviceOpt = v;
256 break;
257 case 'p':
258 MaxParallel = v;
259 break;
260 case 's':
261 SafetyOpt = v;
262 break;
263 case 'q':
264 QuietOpt = v;
265 break;
266 case 'k':
267 UseFSMIDOpt = v;
268 FSMIDCacheFile = ".FSMID.CHECK";
269 break;
270 case 'K':
271 UseFSMIDOpt = v;
272 FSMIDCacheFile = av[++i];
273 break;
274 case 'M':
275 UseMD5Opt = v;
276 MD5CacheFile = av[++i];
277 break;
278 case 'm':
279 UseMD5Opt = v;
280 MD5CacheFile = ".MD5.CHECKSUMS";
281 break;
282 case 'u':
283 setvbuf(stdout, NULL, _IOLBF, 0);
284 break;
285 default:
286 fatal("illegal option: %s\n", ptr - 2);
287 /* not reached */
288 break;
293 * If we are told to go into slave mode, run the HC protocol
295 if (SlaveOpt) {
296 hc_slave(0, 1);
297 exit(0);
301 * Extract the source and/or/neither target [user@]host and
302 * make any required connections.
304 if (src && (ptr = strchr(src, ':')) != NULL) {
305 asprintf(&SrcHost.host, "%*.*s", ptr - src, ptr - src, src);
306 src = ptr + 1;
307 if (UseCpFile) {
308 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
309 exit(1);
311 if (UseMD5Opt) {
312 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
313 exit(1);
315 if (hc_connect(&SrcHost) < 0)
316 exit(1);
318 if (dst && (ptr = strchr(dst, ':')) != NULL) {
319 asprintf(&DstHost.host, "%*.*s", ptr - dst, ptr - dst, dst);
320 dst = ptr + 1;
321 if (UseFSMIDOpt) {
322 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
323 exit(1);
325 if (hc_connect(&DstHost) < 0)
326 exit(1);
330 * dst may be NULL only if -m option is specified,
331 * which forces an update of the MD5 checksums
333 if (dst == NULL && UseMD5Opt == 0) {
334 fatal(NULL);
335 /* not reached */
337 bzero(&info, sizeof(info));
338 #if USE_PTHREADS
339 info.r = 0;
340 info.children = 0;
341 pthread_cond_init(&info.cond, NULL);
342 #endif
343 if (dst) {
344 DstBaseLen = strlen(dst);
345 info.spath = src;
346 info.dpath = dst;
347 info.sdevNo = (dev_t)-1;
348 info.ddevNo = (dev_t)-1;
349 i = DoCopy(&info, -1);
350 } else {
351 info.spath = src;
352 info.dpath = NULL;
353 info.sdevNo = (dev_t)-1;
354 info.ddevNo = (dev_t)-1;
355 i = DoCopy(&info, -1);
357 #if USE_PTHREADS
358 pthread_cond_destroy(&info.cond);
359 #endif
360 #ifndef NOMD5
361 md5_flush();
362 #endif
363 fsmid_flush();
365 if (SummaryOpt && i == 0) {
366 double duration;
367 struct timeval end;
369 gettimeofday(&end, NULL);
370 #if 0
371 /* don't count stat's in our byte statistics */
372 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
373 CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
374 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
375 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
376 #endif
378 duration = (end.tv_sec - start.tv_sec);
379 duration += (double)(end.tv_usec - start.tv_usec) / 1000000.0;
380 if (duration == 0.0)
381 duration = 1.0;
382 logstd("cpdup completed successfully\n");
383 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
384 "%lld bytes written (%.1fX speedup)\n",
385 (long long)CountSourceBytes,
386 (long long)CountSourceReadBytes,
387 (long long)CountTargetReadBytes,
388 (long long)CountWriteBytes,
389 ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
390 logstd("%lld source items, %lld items copied, %lld items linked, "
391 "%lld things deleted\n",
392 (long long)CountSourceItems,
393 (long long)CountCopiedItems,
394 (long long)CountLinkedItems,
395 (long long)CountRemovedItems);
396 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
397 duration,
398 (int)((CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration / 1024.0),
399 (int)(CountSourceBytes / duration / 1024.0));
401 exit((i == 0) ? 0 : 1);
404 static struct hlink *
405 hltlookup(struct stat *stp)
407 #if USE_PTHREADS
408 struct timespec ts = { 0, 100000 };
409 #endif
410 struct hlink *hl;
411 int n;
413 n = stp->st_ino & HLMASK;
415 #if USE_PTHREADS
416 again:
417 #endif
418 for (hl = hltable[n]; hl; hl = hl->next) {
419 if (hl->ino == stp->st_ino) {
420 #if USE_PTHREADS
422 * If the hl entry is still in the process of being created
423 * by another thread we have to wait until it has either been
424 * deleted or completed.
426 if (hl->refs) {
427 pthread_mutex_unlock(&MasterMutex);
428 nanosleep(&ts, NULL);
429 pthread_mutex_lock(&MasterMutex);
430 goto again;
432 #endif
433 ++hl->refs;
434 return hl;
438 return NULL;
441 static struct hlink *
442 hltadd(struct stat *stp, const char *path)
444 struct hlink *new;
445 int plen = strlen(path);
446 int n;
448 new = malloc(offsetof(struct hlink, name[plen + 1]));
449 if (new == NULL) {
450 fprintf(stderr, "out of memory\n");
451 exit(EXIT_FAILURE);
453 ++HardLinkCount;
455 /* initialize and link the new element into the table */
456 new->ino = stp->st_ino;
457 new->dino = (ino_t)-1;
458 new->refs = 1;
459 bcopy(path, new->name, plen + 1);
460 new->nlinked = 1;
461 new->prev = NULL;
462 n = stp->st_ino & HLMASK;
463 new->next = hltable[n];
464 if (hltable[n])
465 hltable[n]->prev = new;
466 hltable[n] = new;
468 return new;
471 static void
472 hltsetdino(struct hlink *hl, ino_t inum)
474 hl->dino = inum;
477 static void
478 hltdelete(struct hlink *hl)
480 assert(hl->refs == 1);
481 --hl->refs;
482 if (hl->prev) {
483 if (hl->next)
484 hl->next->prev = hl->prev;
485 hl->prev->next = hl->next;
486 } else {
487 if (hl->next)
488 hl->next->prev = NULL;
490 hltable[hl->ino & HLMASK] = hl->next;
492 --HardLinkCount;
493 free(hl);
496 static void
497 hltrels(struct hlink *hl)
499 assert(hl->refs == 1);
500 --hl->refs;
504 * If UseHLPath is defined check to see if the file in question is
505 * the same as the source file, and if it is return a pointer to the
506 * -H path based file for hardlinking. Else return NULL.
508 static char *
509 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
511 struct stat sthl;
512 char *hpath;
513 int error;
515 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
518 * stat info matches ?
520 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
521 st1->st_size != sthl.st_size ||
522 st1->st_uid != sthl.st_uid ||
523 st1->st_gid != sthl.st_gid ||
524 st1->st_mtime != sthl.st_mtime
526 free(hpath);
527 return(NULL);
531 * If ForceOpt or ValidateOpt is set we have to compare the files
533 if (ForceOpt || ValidateOpt) {
534 error = validate_check(spath, hpath);
535 if (error) {
536 free(hpath);
537 hpath = NULL;
540 return(hpath);
544 * Return 0 if the contents of the file <spath> matches the contents of
545 * the file <dpath>.
547 static int
548 validate_check(const char *spath, const char *dpath)
550 int error;
551 int fd1;
552 int fd2;
554 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
555 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
556 error = -1;
558 if (fd1 >= 0 && fd2 >= 0) {
559 int n;
560 int x;
561 char *iobuf1 = malloc(GETIOSIZE);
562 char *iobuf2 = malloc(GETIOSIZE);
564 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
565 CountSourceReadBytes += n;
566 x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
567 if (x > 0)
568 CountTargetReadBytes += x;
569 if (x != n)
570 break;
571 if (bcmp(iobuf1, iobuf2, n) != 0)
572 break;
574 free(iobuf1);
575 free(iobuf2);
576 if (n == 0)
577 error = 0;
579 if (fd1 >= 0)
580 hc_close(&SrcHost, fd1);
581 if (fd2 >= 0)
582 hc_close(&DstHost, fd2);
583 return (error);
585 #if USE_PTHREADS
587 static void *
588 DoCopyThread(void *arg)
590 copy_info_t cinfo = arg;
591 char *spath = cinfo->spath;
592 char *dpath = cinfo->dpath;
593 int r;
595 r = pthread_detach(pthread_self());
596 assert(r == 0);
597 pthread_cond_init(&cinfo->cond, NULL);
598 pthread_mutex_lock(&MasterMutex);
599 cinfo->r += DoCopy(cinfo, 0);
600 /* cinfo arguments invalid on return */
601 --cinfo->parent->children;
602 --CurParallel;
603 pthread_cond_signal(&cinfo->parent->cond);
604 free(spath);
605 if (dpath)
606 free(dpath);
607 pthread_cond_destroy(&cinfo->cond);
608 free(cinfo);
609 hcc_free_trans(&SrcHost);
610 hcc_free_trans(&DstHost);
611 pthread_mutex_unlock(&MasterMutex);
612 return(NULL);
615 #endif
618 DoCopy(copy_info_t info, int depth)
620 const char *spath = info->spath;
621 const char *dpath = info->dpath;
622 dev_t sdevNo = info->sdevNo;
623 dev_t ddevNo = info->ddevNo;
624 struct stat st1;
625 struct stat st2;
626 int r, mres, fres, st2Valid;
627 struct hlink *hln;
628 List *list = malloc(sizeof(List));
629 u_int64_t size;
631 InitList(list);
632 r = mres = fres = st2Valid = 0;
633 size = 0;
634 hln = NULL;
636 if (hc_lstat(&SrcHost, spath, &st1) != 0) {
637 r = 0;
638 goto done;
640 st2.st_mode = 0; /* in case lstat fails */
641 st2.st_flags = 0; /* in case lstat fails */
642 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
643 st2Valid = 1;
645 if (S_ISREG(st1.st_mode)) {
646 size = st1.st_size;
650 * Handle hardlinks
653 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
654 if ((hln = hltlookup(&st1)) != NULL) {
655 hln->nlinked++;
657 if (st2Valid) {
658 if (st2.st_ino == hln->dino) {
660 * hard link is already correct, nothing to do
662 if (VerboseOpt >= 3)
663 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
664 if (hln->nlinked == st1.st_nlink) {
665 hltdelete(hln);
666 hln = NULL;
668 CountSourceItems++;
669 r = 0;
670 goto done;
671 } else {
673 * hard link is not correct, attempt to unlink it
675 if (hc_remove(&DstHost, dpath) < 0) {
676 logerr("%-32s hardlink: unable to unlink: %s\n",
677 ((dpath) ? dpath : spath), strerror(errno));
678 hltdelete(hln);
679 hln = NULL;
680 ++r;
681 goto done;
686 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
687 int tryrelink = (errno == EMLINK);
688 logerr("%-32s hardlink: unable to link to %s: %s\n",
689 (dpath ? dpath : spath), hln->name, strerror(errno)
691 hltdelete(hln);
692 hln = NULL;
693 if (tryrelink) {
694 logerr("%-20s hardlink: will attempt to copy normally\n");
695 goto relink;
697 ++r;
698 } else {
699 if (hln->nlinked == st1.st_nlink) {
700 hltdelete(hln);
701 hln = NULL;
703 if (r == 0) {
704 if (VerboseOpt) {
705 logstd("%-32s hardlink: %s\n",
706 (dpath ? dpath : spath),
707 (st2Valid ? "relinked" : "linked")
710 CountSourceItems++;
711 CountCopiedItems++;
712 r = 0;
713 goto done;
716 } else {
718 * first instance of hardlink must be copied normally
720 relink:
721 hln = hltadd(&st1, dpath);
726 * Do we need to copy the file/dir/link/whatever? Early termination
727 * if we do not. Always redo links. Directories are always traversed
728 * except when the FSMID options are used.
730 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
733 if (
734 st2Valid
735 && st1.st_mode == st2.st_mode
736 #ifdef _ST_FLAGS_PRESENT_
737 && st1.st_flags == st2.st_flags
738 #endif
740 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
742 * If FSMID tracking is turned on we can avoid recursing through
743 * an entire directory subtree if the FSMID matches.
745 #ifdef _ST_FSMID_PRESENT_
746 if (ForceOpt == 0 &&
747 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
749 if (VerboseOpt >= 3) {
750 if (UseFSMIDOpt)
751 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
752 else
753 logstd("%-32s nochange\n", (dpath ? dpath : spath));
755 r = 0;
756 goto done;
758 #endif
759 } else {
760 if (ForceOpt == 0 &&
761 st1.st_size == st2.st_size &&
762 st1.st_uid == st2.st_uid &&
763 st1.st_gid == st2.st_gid &&
764 st1.st_mtime == st2.st_mtime
765 #ifndef NOMD5
766 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
767 (mres = md5_check(spath, dpath)) == 0)
768 #endif
769 #ifdef _ST_FSMID_PRESENT_
770 && (UseFSMIDOpt == 0 ||
771 (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
772 #endif
773 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
774 validate_check(spath, dpath) == 0)
776 if (hln)
777 hltsetdino(hln, st2.st_ino);
778 if (VerboseOpt >= 3) {
779 #ifndef NOMD5
780 if (UseMD5Opt)
781 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
782 else
783 #endif
784 if (UseFSMIDOpt)
785 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
786 else if (ValidateOpt)
787 logstd("%-32s nochange (contents validated)\n", (dpath ? dpath : spath));
788 else
789 logstd("%-32s nochange\n", (dpath ? dpath : spath));
791 CountSourceBytes += size;
792 CountSourceItems++;
793 r = 0;
794 goto done;
798 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
799 if (SafetyOpt) {
800 logerr("%-32s SAFETY - refusing to copy file over directory\n",
801 (dpath ? dpath : spath)
803 ++r; /* XXX */
804 r = 0;
805 goto done; /* continue with the cpdup anyway */
807 if (QuietOpt == 0 || AskConfirmation) {
808 logstd("%-32s WARNING: non-directory source will blow away\n"
809 "%-32s preexisting dest directory, continuing anyway!\n",
810 ((dpath) ? dpath : spath), "");
812 if (dpath)
813 RemoveRecur(dpath, ddevNo);
817 * The various comparisons failed, copy it.
819 if (S_ISDIR(st1.st_mode)) {
820 DIR *dir;
822 if (fres < 0)
823 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
824 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
825 struct dirent *den;
826 int noLoop = 0;
828 if (dpath) {
829 if (S_ISDIR(st2.st_mode) == 0) {
830 hc_remove(&DstHost, dpath);
831 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
832 logerr("%s: mkdir failed: %s\n",
833 (dpath ? dpath : spath), strerror(errno));
834 r = 1;
835 noLoop = 1;
838 * Matt: why don't you check error codes here?
840 hc_lstat(&DstHost, dpath, &st2);
841 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
842 CountCopiedItems++;
843 } else {
845 * Directory must be scanable by root for cpdup to
846 * work. We'll fix it later if the directory isn't
847 * supposed to be readable ( which is why we fixup
848 * st2.st_mode to match what we did ).
850 if ((st2.st_mode & 0700) != 0700) {
851 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
852 st2.st_mode |= 0700;
854 if (VerboseOpt >= 2)
855 logstd("%s\n", dpath ? dpath : spath);
859 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
860 noLoop = 1;
861 } else {
862 sdevNo = st1.st_dev;
865 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
866 noLoop = 1;
867 } else {
868 ddevNo = st2.st_dev;
872 * scan .cpignore file for files/directories
873 * to ignore.
876 if (UseCpFile) {
877 FILE *fi;
878 char *buf = malloc(GETBUFSIZE);
879 char *fpath;
881 if (UseCpFile[0] == '/') {
882 fpath = mprintf("%s", UseCpFile);
883 } else {
884 fpath = mprintf("%s/%s", spath, UseCpFile);
886 AddList(list, strrchr(fpath, '/') + 1, 1);
887 if ((fi = fopen(fpath, "r")) != NULL) {
888 while (fgets(buf, GETBUFSIZE, fi) != NULL) {
889 int l = strlen(buf);
890 CountSourceReadBytes += l;
891 if (l && buf[l-1] == '\n')
892 buf[--l] = 0;
893 if (buf[0] && buf[0] != '#')
894 AddList(list, buf, 1);
896 fclose(fi);
898 free(fpath);
899 free(buf);
903 * Automatically exclude MD5CacheFile that we create on the
904 * source from the copy to the destination.
906 * Automatically exclude a FSMIDCacheFile on the source that
907 * would otherwise overwrite the one we maintain on the target.
909 if (UseMD5Opt)
910 AddList(list, MD5CacheFile, 1);
911 if (UseFSMIDOpt)
912 AddList(list, FSMIDCacheFile, 1);
914 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
916 * ignore . and ..
918 char *nspath;
919 char *ndpath = NULL;
921 if (strcmp(den->d_name, ".") == 0 ||
922 strcmp(den->d_name, "..") == 0
924 continue;
927 * ignore if on .cpignore list
929 if (AddList(list, den->d_name, 0) == 1) {
930 continue;
932 nspath = mprintf("%s/%s", spath, den->d_name);
933 if (dpath)
934 ndpath = mprintf("%s/%s", dpath, den->d_name);
936 #if USE_PTHREADS
937 if (CurParallel < MaxParallel || depth > MAXDEPTH) {
938 copy_info_t cinfo = malloc(sizeof(*cinfo));
939 pthread_t dummy_thr = NULL;
941 bzero(cinfo, sizeof(*cinfo));
942 cinfo->spath = nspath;
943 cinfo->dpath = ndpath;
944 cinfo->sdevNo = sdevNo;
945 cinfo->ddevNo = ddevNo;
946 cinfo->parent = info;
947 ++CurParallel;
948 ++info->children;
949 pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
950 } else
951 #endif
953 info->spath = nspath;
954 info->dpath = ndpath;
955 info->sdevNo = sdevNo;
956 info->ddevNo = ddevNo;
957 if (depth < 0)
958 r += DoCopy(info, depth);
959 else
960 r += DoCopy(info, depth + 1);
961 free(nspath);
962 if (ndpath)
963 free(ndpath);
964 info->spath = NULL;
965 info->dpath = NULL;
969 hc_closedir(&SrcHost, dir);
971 #if USE_PTHREADS
973 * Wait for our children to finish
975 while (info->children) {
976 pthread_cond_wait(&info->cond, &MasterMutex);
978 r += info->r;
979 info->r = 0;
980 #endif
983 * Remove files/directories from destination that do not appear
984 * in the source.
986 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
987 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
989 * ignore . or ..
991 if (strcmp(den->d_name, ".") == 0 ||
992 strcmp(den->d_name, "..") == 0
994 continue;
997 * If object does not exist in source or .cpignore
998 * then recursively remove it.
1000 if (AddList(list, den->d_name, 3) == 3) {
1001 char *ndpath;
1003 ndpath = mprintf("%s/%s", dpath, den->d_name);
1004 RemoveRecur(ndpath, ddevNo);
1005 free(ndpath);
1008 hc_closedir(&DstHost, dir);
1011 if (dpath) {
1012 struct timeval tv[2];
1014 if (ForceOpt ||
1015 st2Valid == 0 ||
1016 st1.st_uid != st2.st_uid ||
1017 st1.st_gid != st2.st_gid
1019 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
1021 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
1022 hc_chmod(&DstHost, dpath, st1.st_mode);
1024 #ifdef _ST_FLAGS_PRESENT_
1025 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
1026 hc_chflags(&DstHost, dpath, st1.st_flags);
1028 #endif
1029 if (ForceOpt ||
1030 st2Valid == 0 ||
1031 st1.st_mtime != st2.st_mtime
1033 bzero(tv, sizeof(tv));
1034 tv[0].tv_sec = st1.st_mtime;
1035 tv[1].tv_sec = st1.st_mtime;
1036 hc_utimes(&DstHost, dpath, tv);
1040 } else if (dpath == NULL) {
1042 * If dpath is NULL, we are just updating the MD5
1044 #ifndef NOMD5
1045 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
1046 mres = md5_check(spath, NULL);
1048 if (VerboseOpt > 1) {
1049 if (mres < 0)
1050 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1051 else
1052 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
1053 } else if (!QuietOpt && mres < 0) {
1054 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1057 #endif
1058 } else if (S_ISREG(st1.st_mode)) {
1059 char *path;
1060 char *hpath;
1061 int fd1;
1062 int fd2;
1064 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1067 * Handle check failure message.
1069 #ifndef NOMD5
1070 if (mres < 0)
1071 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1072 else
1073 #endif
1074 if (fres < 0)
1075 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1078 * Not quite ready to do the copy yet. If UseHLPath is defined,
1079 * see if we can hardlink instead.
1081 * If we can hardlink, and the target exists, we have to remove it
1082 * first or the hardlink will fail. This can occur in a number of
1083 * situations but must typically when the '-f -H' combination is
1084 * used.
1086 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1087 if (st2Valid)
1088 hc_remove(&DstHost, dpath);
1089 if (hc_link(&DstHost, hpath, dpath) == 0) {
1090 ++CountLinkedItems;
1091 if (VerboseOpt) {
1092 logstd("%-32s hardlinked(-H)\n",
1093 (dpath ? dpath : spath));
1095 free(hpath);
1096 goto skip_copy;
1099 * Shucks, we may have hit a filesystem hard linking limit,
1100 * we have to copy instead.
1102 free(hpath);
1105 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1106 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1108 * There could be a .tmp file from a previously interrupted
1109 * run, delete and retry. Fail if we still can't get at it.
1111 #ifdef _ST_FLAGS_PRESENT_
1112 hc_chflags(&DstHost, path, 0);
1113 #endif
1114 hc_remove(&DstHost, path);
1115 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1117 if (fd2 >= 0) {
1118 const char *op;
1119 char *iobuf1 = malloc(GETIOSIZE);
1120 int n;
1123 * Matt: What about holes?
1125 op = "read";
1126 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1127 op = "write";
1128 if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1129 break;
1130 op = "read";
1132 hc_close(&DstHost, fd2);
1133 if (n == 0) {
1134 struct timeval tv[2];
1136 bzero(tv, sizeof(tv));
1137 tv[0].tv_sec = st1.st_mtime;
1138 tv[1].tv_sec = st1.st_mtime;
1140 hc_utimes(&DstHost, path, tv);
1141 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1142 hc_chmod(&DstHost, path, st1.st_mode);
1143 if (xrename(path, dpath, st2.st_flags) != 0) {
1144 logerr("%-32s rename-after-copy failed: %s\n",
1145 (dpath ? dpath : spath), strerror(errno)
1147 ++r;
1148 } else {
1149 if (VerboseOpt)
1150 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1151 #ifdef _ST_FLAGS_PRESENT_
1152 if (st1.st_flags)
1153 hc_chflags(&DstHost, dpath, st1.st_flags);
1154 #endif
1156 CountSourceReadBytes += size;
1157 CountWriteBytes += size;
1158 CountSourceBytes += size;
1159 CountSourceItems++;
1160 CountCopiedItems++;
1161 } else {
1162 logerr("%-32s %s failed: %s\n",
1163 (dpath ? dpath : spath), op, strerror(errno)
1165 hc_remove(&DstHost, path);
1166 ++r;
1168 free(iobuf1);
1169 } else {
1170 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1171 (dpath ? dpath : spath), getuid(), geteuid(),
1172 strerror(errno)
1174 ++r;
1176 hc_close(&SrcHost, fd1);
1177 } else {
1178 logerr("%-32s copy: open failed: %s\n",
1179 (dpath ? dpath : spath),
1180 strerror(errno)
1182 ++r;
1184 skip_copy:
1185 free(path);
1187 if (hln) {
1188 if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1189 hltsetdino(hln, st2.st_ino);
1190 } else {
1191 hltdelete(hln);
1192 hln = NULL;
1195 } else if (S_ISLNK(st1.st_mode)) {
1196 char *link1 = malloc(GETLINKSIZE);
1197 char *link2 = malloc(GETLINKSIZE);
1198 char *path = malloc(GETPATHSIZE);
1199 int n1;
1200 int n2;
1202 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1203 n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1204 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1205 if (n1 >= 0) {
1206 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1207 hc_umask(&DstHost, ~st1.st_mode);
1208 hc_remove(&DstHost, path);
1209 link1[n1] = 0;
1210 if (hc_symlink(&DstHost, link1, path) < 0) {
1211 logerr("%-32s symlink (%s->%s) failed: %s\n",
1212 (dpath ? dpath : spath), link1, path,
1213 strerror(errno)
1215 ++r;
1216 } else {
1217 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1219 * there is no lchmod() or lchflags(), we
1220 * cannot chmod or chflags a softlink.
1222 if (xrename(path, dpath, st2.st_flags) != 0) {
1223 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1224 (dpath ? dpath : spath),
1225 path, dpath, strerror(errno));
1226 } else if (VerboseOpt) {
1227 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1229 hc_umask(&DstHost, 000);
1230 CountWriteBytes += n1;
1231 CountCopiedItems++;
1233 } else {
1234 if (VerboseOpt >= 3)
1235 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1237 CountSourceBytes += n1;
1238 CountSourceReadBytes += n1;
1239 if (n2 > 0)
1240 CountTargetReadBytes += n2;
1241 CountSourceItems++;
1242 } else {
1243 r = 1;
1244 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1246 free(link1);
1247 free(link2);
1248 free(path);
1249 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1250 char *path = malloc(GETPATHSIZE);
1252 if (ForceOpt ||
1253 st2Valid == 0 ||
1254 st1.st_mode != st2.st_mode ||
1255 st1.st_rdev != st2.st_rdev ||
1256 st1.st_uid != st2.st_uid ||
1257 st1.st_gid != st2.st_gid
1259 snprintf(path, GETPATHSIZE, "%s.tmp%d", dpath, (int)getpid());
1261 hc_remove(&DstHost, path);
1262 if (hc_mknod(&DstHost, path, st1.st_mode, st1.st_rdev) == 0) {
1263 hc_chmod(&DstHost, path, st1.st_mode);
1264 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1265 hc_remove(&DstHost, dpath);
1266 if (xrename(path, dpath, st2.st_flags) != 0) {
1267 logerr("%-32s dev-rename-after-create failed: %s\n",
1268 (dpath ? dpath : spath),
1269 strerror(errno)
1271 } else if (VerboseOpt) {
1272 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1274 CountCopiedItems++;
1275 } else {
1276 r = 1;
1277 logerr("%-32s dev failed: %s\n",
1278 (dpath ? dpath : spath), strerror(errno)
1281 } else {
1282 if (VerboseOpt >= 3)
1283 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1285 free(path);
1286 CountSourceItems++;
1288 done:
1289 if (hln) {
1290 if (hln->dino == (ino_t)-1) {
1291 hltdelete(hln);
1292 /*hln = NULL; unneeded */
1293 } else {
1294 hltrels(hln);
1297 ResetList(list);
1298 free(list);
1299 return (r);
1303 * RemoveRecur()
1306 void
1307 RemoveRecur(const char *dpath, dev_t devNo)
1309 struct stat st;
1311 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1312 if ((int)devNo < 0)
1313 devNo = st.st_dev;
1314 if (st.st_dev == devNo) {
1315 if (S_ISDIR(st.st_mode)) {
1316 DIR *dir;
1318 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1319 struct dirent *den;
1320 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1321 char *ndpath;
1323 if (strcmp(den->d_name, ".") == 0)
1324 continue;
1325 if (strcmp(den->d_name, "..") == 0)
1326 continue;
1327 ndpath = mprintf("%s/%s", dpath, den->d_name);
1328 RemoveRecur(ndpath, devNo);
1329 free(ndpath);
1331 hc_closedir(&DstHost, dir);
1333 if (AskConfirmation && NoRemoveOpt == 0) {
1334 if (YesNo(dpath)) {
1335 if (hc_rmdir(&DstHost, dpath) < 0) {
1336 logerr("%-32s rmdir failed: %s\n",
1337 dpath, strerror(errno)
1340 CountRemovedItems++;
1342 } else {
1343 if (NoRemoveOpt) {
1344 if (VerboseOpt)
1345 logstd("%-32s not-removed\n", dpath);
1346 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1347 if (VerboseOpt)
1348 logstd("%-32s rmdir-ok\n", dpath);
1349 CountRemovedItems++;
1350 } else {
1351 logerr("%-32s rmdir failed: %s\n",
1352 dpath, strerror(errno)
1356 } else {
1357 if (AskConfirmation && NoRemoveOpt == 0) {
1358 if (YesNo(dpath)) {
1359 if (hc_remove(&DstHost, dpath) < 0) {
1360 logerr("%-32s remove failed: %s\n",
1361 dpath, strerror(errno)
1364 CountRemovedItems++;
1366 } else {
1367 if (NoRemoveOpt) {
1368 if (VerboseOpt)
1369 logstd("%-32s not-removed\n", dpath);
1370 } else if (hc_remove(&DstHost, dpath) == 0) {
1371 if (VerboseOpt)
1372 logstd("%-32s remove-ok\n", dpath);
1373 CountRemovedItems++;
1374 } else {
1375 logerr("%-32s remove failed: %s\n",
1376 dpath, strerror(errno)
1385 void
1386 InitList(List *list)
1388 bzero(list, sizeof(List));
1389 list->li_Node.no_Next = &list->li_Node;
1392 void
1393 ResetList(List *list)
1395 Node *node;
1397 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1398 list->li_Node.no_Next = node->no_Next;
1399 free(node);
1401 InitList(list);
1405 AddList(List *list, const char *name, int n)
1407 Node *node;
1408 int hv;
1410 hv = shash(name);
1413 * Scan against wildcards. Only a node value of 1 can be a wildcard
1414 * ( usually scanned from .cpignore )
1417 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1418 if (strcmp(name, node->no_Name) == 0 ||
1419 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1421 return(node->no_Value);
1426 * Look for exact match
1429 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1430 if (strcmp(name, node->no_Name) == 0) {
1431 return(node->no_Value);
1434 node = malloc(sizeof(Node) + strlen(name) + 1);
1435 if (node == NULL) {
1436 fprintf(stderr, "out of memory\n");
1437 exit(EXIT_FAILURE);
1440 node->no_Next = list->li_Node.no_Next;
1441 list->li_Node.no_Next = node;
1443 node->no_HNext = list->li_Hash[hv];
1444 list->li_Hash[hv] = node;
1446 strcpy(node->no_Name, name);
1447 node->no_Value = n;
1449 return(n);
1452 static int
1453 shash(const char *s)
1455 int hv;
1457 hv = 0xA4FB3255;
1459 while (*s) {
1460 if (*s == '*' || *s == '?' ||
1461 *s == '{' || *s == '}' ||
1462 *s == '[' || *s == ']' ||
1463 *s == '|'
1465 return(0);
1467 hv = (hv << 5) ^ *s ^ (hv >> 23);
1468 ++s;
1470 return(((hv >> 16) ^ hv) & HMASK);
1474 * WildCmp() - compare wild string to sane string
1476 * Return 0 on success, -1 on failure.
1480 WildCmp(const char *w, const char *s)
1483 * skip fixed portion
1486 for (;;) {
1487 switch(*w) {
1488 case '*':
1489 if (w[1] == 0) /* optimize wild* case */
1490 return(0);
1492 int i;
1493 int l = strlen(s);
1495 for (i = 0; i <= l; ++i) {
1496 if (WildCmp(w + 1, s + i) == 0)
1497 return(0);
1500 return(-1);
1501 case '?':
1502 if (*s == 0)
1503 return(-1);
1504 ++w;
1505 ++s;
1506 break;
1507 default:
1508 if (*w != *s)
1509 return(-1);
1510 if (*w == 0) /* terminator */
1511 return(0);
1512 ++w;
1513 ++s;
1514 break;
1517 /* not reached */
1518 return(-1);
1522 YesNo(const char *path)
1524 int ch, first;
1526 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1527 fflush(stderr);
1529 first = ch = getchar();
1530 while (ch != '\n' && ch != EOF)
1531 ch = getchar();
1532 return ((first == 'y' || first == 'Y'));
1536 * xrename() - rename with override
1538 * If the rename fails, attempt to override st_flags on the
1539 * destination and rename again. If that fails too, try to
1540 * set the flags back the way they were and give up.
1543 static int
1544 xrename(const char *src, const char *dst, u_long flags)
1546 int r;
1548 r = 0;
1550 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1551 #ifdef _ST_FLAGS_PRESENT_
1552 hc_chflags(&DstHost, dst, 0);
1553 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1554 hc_chflags(&DstHost, dst, flags);
1555 #endif
1557 return(r);
1560 static int
1561 xlink(const char *src, const char *dst, u_long flags)
1563 int r;
1564 #ifdef _ST_FLAGS_PRESENT_
1565 int e;
1566 #endif
1568 r = 0;
1570 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1571 #ifdef _ST_FLAGS_PRESENT_
1572 hc_chflags(&DstHost, src, 0);
1573 r = hc_link(&DstHost, src, dst);
1574 e = errno;
1575 hc_chflags(&DstHost, src, flags);
1576 errno = e;
1577 #endif
1579 if (r == 0)
1580 ++CountLinkedItems;
1581 return(r);