xref: /dflybsd-src/bin/cpdup/cpdup.c (revision 52347f71f500fe4113e37cca4ea6edc6150698e7)
1 /*-
2  * CPDUP.C
3  *
4  * CPDUP <options> source destination
5  *
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.
9  *
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.
17  *
18  * Safety features:
19  *
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
25  *
26  * Copying features:
27  *
28  *	- does not copy file if mtime, flags, perms, and size match unless
29  *	  forced
30  *
31  *	- copies to temporary and renames-over the original, allowing
32  *	  you to update live systems
33  *
34  *	- copies uid, gid, mtime, perms, flags, softlinks, devices, hardlinks,
35  *	  and recurses through directories.
36  *
37  *	- accesses a per-directory exclusion file, .cpignore, containing
38  *	  standard wildcarded ( ? / * style, NOT regex) exclusions.
39  *
40  *	- tries to play permissions and flags smart in regards to overwriting
41  *	  schg files and doing related stuff.
42  *
43  *	- Can do MD5 consistancy checks
44  *
45  *	- Is able to do incremental mirroring/backups via hardlinks from
46  *	  the 'previous' version (supplied with -H path).
47  *
48  * $DragonFly: src/bin/cpdup/cpdup.c,v 1.19 2008/03/22 18:09:16 dillon Exp $
49  */
50 
51 /*-
52  * Example: cc -O cpdup.c -o cpdup -lmd
53  *
54  * ".MD5.CHECKSUMS" contains md5 checksumms for the current directory.
55  * This file is stored on the source.
56  */
57 
58 #include "cpdup.h"
59 #include "hclink.h"
60 #include "hcproto.h"
61 
62 #define HSIZE	16384
63 #define HMASK	(HSIZE-1)
64 #define HLSIZE	8192
65 #define HLMASK	(HLSIZE - 1)
66 
67 #ifndef _ST_FLAGS_PRESENT_
68 #define st_flags	st_mode
69 #endif
70 
71 typedef struct Node {
72     struct Node *no_Next;
73     struct Node *no_HNext;
74     int  no_Value;
75     char no_Name[4];
76 } Node;
77 
78 typedef struct List {
79     Node	li_Node;
80     Node	*li_Hash[HSIZE];
81 } List;
82 
83 struct hlink {
84     ino_t ino;
85     ino_t dino;
86     struct hlink *next;
87     struct hlink *prev;
88     nlink_t nlinked;
89     char name[0];
90 };
91 
92 struct hlink *hltable[HLSIZE];
93 
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);
109 
110 int AskConfirmation = 1;
111 int SafetyOpt = 1;
112 int ForceOpt;
113 int DeviceOpt = 1;
114 int VerboseOpt;
115 int QuietOpt;
116 int NoRemoveOpt;
117 int UseMD5Opt;
118 int UseFSMIDOpt;
119 int SummaryOpt;
120 int SlaveOpt;
121 int EnableDirectoryRetries;
122 int DstBaseLen;
123 int ValidateOpt;
124 char IOBuf1[65536];
125 char IOBuf2[65536];
126 const char *UseCpFile;
127 const char *UseHLPath;
128 const char *MD5CacheFile;
129 const char *FSMIDCacheFile;
130 
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;
139 
140 struct HostConf SrcHost;
141 struct HostConf DstHost;
142 
143 int
144 main(int ac, char **av)
145 {
146     int i;
147     char *src = NULL;
148     char *dst = NULL;
149     char *ptr;
150     struct timeval start;
151 
152     signal(SIGPIPE, SIG_IGN);
153 
154     gettimeofday(&start, NULL);
155     for (i = 1; i < ac; ++i) {
156 	int v = 1;
157 
158 	ptr = av[i];
159 	if (*ptr != '-') {
160 	    if (src == NULL) {
161 		src = ptr;
162 	    } else if (dst == NULL) {
163 		dst = ptr;
164 	    } else {
165 		fatal("too many arguments");
166 		/* not reached */
167 	    }
168 	    continue;
169 	}
170 	ptr += 2;
171 
172 	if (*ptr)
173 	    v = strtol(ptr, NULL, 0);
174 
175 	switch(ptr[-1]) {
176 	case 'v':
177 	    VerboseOpt = 1;
178 	    while (*ptr == 'v') {
179 		++VerboseOpt;
180 		++ptr;
181 	    }
182 	    if (*ptr >= '0' && *ptr <= '9')
183 		VerboseOpt = strtol(ptr, NULL, 0);
184 	    break;
185 	case 'V':
186 	    ValidateOpt = v;
187 	    break;
188 	case 'I':
189 	    SummaryOpt = v;
190 	    break;
191 	case 'o':
192 	    NoRemoveOpt = v;
193 	    break;
194 	case 'x':
195 	    UseCpFile = ".cpignore";
196 	    break;
197 	case 'X':
198 	    UseCpFile = (*ptr) ? ptr : av[++i];
199 	    break;
200 	case 'H':
201 	    UseHLPath = (*ptr) ? ptr : av[++i];
202 	    break;
203 	case 'S':
204 	    SlaveOpt = v;
205 	    break;
206 	case 'f':
207 	    ForceOpt = v;
208 	    break;
209 	case 'i':
210 	    AskConfirmation = v;
211 	    break;
212 	case 'j':
213 	    DeviceOpt = v;
214 	    break;
215 	case 's':
216 	    SafetyOpt = v;
217 	    break;
218 	case 'q':
219 	    QuietOpt = v;
220 	    break;
221 	case 'k':
222 	    UseFSMIDOpt = v;
223 	    FSMIDCacheFile = ".FSMID.CHECK";
224 	    break;
225 	case 'K':
226 	    UseFSMIDOpt = v;
227 	    FSMIDCacheFile = av[++i];
228 	    break;
229 	case 'M':
230 	    UseMD5Opt = v;
231 	    MD5CacheFile = av[++i];
232 	    break;
233 	case 'm':
234 	    UseMD5Opt = v;
235 	    MD5CacheFile = ".MD5.CHECKSUMS";
236 	    break;
237 	case 'u':
238 	    setvbuf(stdout, NULL, _IOLBF, 0);
239 	    break;
240 	default:
241 	    fatal("illegal option: %s\n", ptr - 2);
242 	    /* not reached */
243 	    break;
244 	}
245     }
246 
247     /*
248      * If we are told to go into slave mode, run the HC protocol
249      */
250     if (SlaveOpt) {
251 	hc_slave(0, 1);
252 	exit(0);
253     }
254 
255     /*
256      * Extract the source and/or/neither target [user@]host and
257      * make any required connections.
258      */
259     if (src && (ptr = strchr(src, ':')) != NULL) {
260 	asprintf(&SrcHost.host, "%*.*s", ptr - src, ptr - src, src);
261 	src = ptr + 1;
262 	if (UseCpFile) {
263 	    fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
264 	    exit(1);
265 	}
266 	if (UseMD5Opt) {
267 	    fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
268 	    exit(1);
269 	}
270 	if (hc_connect(&SrcHost) < 0)
271 	    fprintf(stderr, "Unable to connect to %s\n", SrcHost.host);
272     }
273     if (dst && (ptr = strchr(dst, ':')) != NULL) {
274 	asprintf(&DstHost.host, "%*.*s", ptr - dst, ptr - dst, dst);
275 	dst = ptr + 1;
276 	if (UseFSMIDOpt) {
277 	    fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
278 	    exit(1);
279 	}
280 	if (hc_connect(&DstHost) < 0)
281 	    fprintf(stderr, "Unable to connect to %s\n", DstHost.host);
282     }
283 
284     /*
285      * dst may be NULL only if -m option is specified,
286      * which forces an update of the MD5 checksums
287      */
288     if (dst == NULL && UseMD5Opt == 0) {
289 	fatal(NULL);
290 	/* not reached */
291     }
292     if (dst) {
293 	DstBaseLen = strlen(dst);
294 	i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
295     } else {
296 	i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
297     }
298 #ifndef NOMD5
299     md5_flush();
300 #endif
301     fsmid_flush();
302 
303     if (SummaryOpt && i == 0) {
304 	long duration;
305 	struct timeval end;
306 
307 	gettimeofday(&end, NULL);
308 #if 0
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;
314 #endif
315 
316 	duration = end.tv_sec - start.tv_sec;
317 	duration *= 1000000;
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));
338     }
339     exit((i == 0) ? 0 : 1);
340 }
341 
342 static struct hlink *
343 hltlookup(struct stat *stp)
344 {
345     struct hlink *hl;
346     int n;
347 
348     n = stp->st_ino & HLMASK;
349 
350     for (hl = hltable[n]; hl; hl = hl->next)
351         if (hl->ino == stp->st_ino)
352               return hl;
353 
354     return NULL;
355 }
356 
357 static struct hlink *
358 hltadd(struct stat *stp, const char *path)
359 {
360     struct hlink *new;
361     int plen = strlen(path);
362     int n;
363 
364     new = malloc(offsetof(struct hlink, name[plen + 1]));
365     if (new == NULL) {
366         fprintf(stderr, "out of memory\n");
367         exit(EXIT_FAILURE);
368     }
369 
370     /* initialize and link the new element into the table */
371     new->ino = stp->st_ino;
372     new->dino = 0;
373     bcopy(path, new->name, plen + 1);
374     new->nlinked = 1;
375     new->prev = NULL;
376     n = stp->st_ino & HLMASK;
377     new->next = hltable[n];
378     if (hltable[n])
379         hltable[n]->prev = new;
380     hltable[n] = new;
381 
382     return new;
383 }
384 
385 static void
386 hltdelete(struct hlink *hl)
387 {
388     if (hl->prev) {
389         if (hl->next)
390             hl->next->prev = hl->prev;
391         hl->prev->next = hl->next;
392     } else {
393         if (hl->next)
394             hl->next->prev = NULL;
395 
396         hltable[hl->ino & HLMASK] = hl->next;
397     }
398 
399     free(hl);
400 }
401 
402 /*
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.
406  */
407 static char *
408 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
409 {
410     struct stat sthl;
411     char *hpath;
412     int error;
413 
414     asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
415 
416     /*
417      * stat info matches ?
418      */
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
424     ) {
425 	free(hpath);
426 	return(NULL);
427     }
428 
429     /*
430      * If ForceOpt or ValidateOpt is set we have to compare the files
431      */
432     if (ForceOpt || ValidateOpt) {
433 	error = validate_check(spath, hpath);
434 	if (error) {
435 	    free(hpath);
436 	    hpath = NULL;
437 	}
438     }
439     return(hpath);
440 }
441 
442 /*
443  * Return 0 if the contents of the file <spath> matches the contents of
444  * the file <dpath>.
445  */
446 static int
447 validate_check(const char *spath, const char *dpath)
448 {
449     int error;
450     int fd1;
451     int fd2;
452 
453     fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
454     fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
455     error = -1;
456 
457     if (fd1 >= 0 && fd2 >= 0) {
458 	int n;
459 	int x;
460 
461 	while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
462 	    CountSourceReadBytes += n;
463 	    x = hc_read(&DstHost, fd2, IOBuf2, sizeof(IOBuf2));
464 	    if (x > 0)
465 		    CountTargetReadBytes += x;
466 	    if (x != n)
467 		break;
468 	    if (bcmp(IOBuf1, IOBuf2, n) != 0)
469 		break;
470 	}
471 	if (n == 0)
472 	    error = 0;
473     }
474     if (fd1 >= 0)
475 	hc_close(&SrcHost, fd1);
476     if (fd2 >= 0)
477 	hc_close(&DstHost, fd2);
478     return (error);
479 }
480 
481 int
482 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
483 {
484     struct stat st1;
485     struct stat st2;
486     int r, mres, fres, st2Valid;
487     struct hlink *hln;
488     List list;
489     u_int64_t size;
490 
491     InitList(&list);
492     r = mres = fres = st2Valid = 0;
493     size = 0;
494     hln = NULL;
495 
496     if (hc_lstat(&SrcHost, spath, &st1) != 0)
497 	return(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)
501 	st2Valid = 1;
502 
503     if (S_ISREG(st1.st_mode)) {
504 	size = st1.st_size;
505     }
506 
507     /*
508      * Handle hardlinks
509      */
510 
511     if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
512         if ((hln = hltlookup(&st1)) != NULL) {
513             hln->nlinked++;
514 
515             if (st2Valid) {
516                 if (st2.st_ino == hln->dino) {
517 		    /*
518 		     * hard link is already correct, nothing to do
519 		     */
520 		    if (VerboseOpt >= 3)
521 			logstd("%-32s nochange\n", (dpath) ? dpath : spath);
522                     if (hln->nlinked == st1.st_nlink)
523                         hltdelete(hln);
524 		    CountSourceItems++;
525                     return 0;
526                 } else {
527 		    /*
528 		     * hard link is not correct, attempt to unlink it
529 		     */
530                     if (hc_remove(&DstHost, dpath) < 0) {
531 			logerr("%-32s hardlink: unable to unlink: %s\n",
532 			    ((dpath) ? dpath : spath), strerror(errno));
533                         hltdelete(hln);
534 			return (r + 1);
535 		    }
536                 }
537             }
538 
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)
543 		);
544                 hltdelete(hln);
545                 hln = NULL;
546 		if (tryrelink) {
547 		    logerr("%-20s hardlink: will attempt to copy normally\n");
548 		    goto relink;
549 		}
550 		++r;
551             } else {
552                 if (hln->nlinked == st1.st_nlink) {
553                     hltdelete(hln);
554 		    hln = NULL;
555 		}
556                 if (r == 0) {
557 		    if (VerboseOpt) {
558 			logstd("%-32s hardlink: %s\n",
559 			    (dpath ? dpath : spath),
560 			    (st2Valid ? "relinked" : "linked")
561 			);
562 		    }
563 		    CountSourceItems++;
564 		    CountCopiedItems++;
565                     return 0;
566 		}
567             }
568         } else {
569 	    /*
570 	     * first instance of hardlink must be copied normally
571 	     */
572 relink:
573             hln = hltadd(&st1, dpath);
574 	}
575     }
576 
577     /*
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.
581      *
582      * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
583      */
584 
585     if (
586 	st2Valid
587 	&& st1.st_mode == st2.st_mode
588 #ifdef _ST_FLAGS_PRESENT_
589 	&& st1.st_flags == st2.st_flags
590 #endif
591     ) {
592 	if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
593 	    /*
594 	     * If FSMID tracking is turned on we can avoid recursing through
595 	     * an entire directory subtree if the FSMID matches.
596 	     */
597 #ifdef _ST_FSMID_PRESENT_
598 	    if (ForceOpt == 0 &&
599 		(UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
600 	    ) {
601 		if (VerboseOpt >= 3) {
602 		    if (UseFSMIDOpt)
603 			logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
604 		    else
605 			logstd("%-32s nochange\n", (dpath ? dpath : spath));
606 		}
607 		return(0);
608 	    }
609 #endif
610 	} else {
611 	    if (ForceOpt == 0 &&
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
616 #ifndef NOMD5
617 		&& (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
618 		    (mres = md5_check(spath, dpath)) == 0)
619 #endif
620 #ifdef _ST_FSMID_PRESENT_
621 		&& (UseFSMIDOpt == 0 ||
622 		    (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
623 #endif
624 		&& (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
625 		    validate_check(spath, dpath) == 0)
626 	    ) {
627                 if (hln)
628                     hln->dino = st2.st_ino;
629 		if (VerboseOpt >= 3) {
630 #ifndef NOMD5
631 		    if (UseMD5Opt)
632 			logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
633 		    else
634 #endif
635 		    if (UseFSMIDOpt)
636 			logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
637 		    else if (ValidateOpt)
638 			logstd("%-32s nochange (contents validated)\n", (dpath ? dpath : spath));
639 		    else
640 			logstd("%-32s nochange\n", (dpath ? dpath : spath));
641 		}
642 		CountSourceBytes += size;
643 		CountSourceItems++;
644 
645 		return(0);
646 	    }
647 	}
648     }
649     if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
650 	if (SafetyOpt) {
651 	    logerr("%-32s SAFETY - refusing to copy file over directory\n",
652 		(dpath ? dpath : spath)
653 	    );
654 	    ++r;		/* XXX */
655 	    return(0);	/* continue with the cpdup anyway */
656 	}
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), "");
661 	}
662 	if (dpath)
663 	    RemoveRecur(dpath, ddevNo);
664     }
665 
666     /*
667      * The various comparisons failed, copy it.
668      */
669     if (S_ISDIR(st1.st_mode)) {
670 	DIR *dir;
671 
672 	if (fres < 0)
673 	    logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
674 	if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
675 	    struct dirent *den;
676 	    int noLoop = 0;
677 
678 	    if (dpath) {
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));
684 			r = 1;
685 			noLoop = 1;
686 		    }
687 		    /*
688 		     * Matt: why don't you check error codes here?
689 		     */
690 		    hc_lstat(&DstHost, dpath, &st2);
691 		    hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
692 		    CountCopiedItems++;
693 		} else {
694 		    /*
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 ).
699 		     */
700 		    if ((st2.st_mode & 0700) != 0700) {
701 			hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
702 			st2.st_mode |= 0700;
703 		    }
704 		    if (VerboseOpt >= 2)
705 			logstd("%s\n", dpath ? dpath : spath);
706 		}
707 	    }
708 
709 	    if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
710 		noLoop = 1;
711 	    } else {
712 		sdevNo = st1.st_dev;
713 	    }
714 
715 	    if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
716 		noLoop = 1;
717 	    } else {
718 		ddevNo = st2.st_dev;
719 	    }
720 
721 	    /*
722 	     * scan .cpignore file for files/directories
723 	     * to ignore.
724 	     */
725 
726 	    if (UseCpFile) {
727 		FILE *fi;
728 		char buf[8192];
729 		char *fpath;
730 
731 		if (UseCpFile[0] == '/') {
732 		    fpath = mprintf("%s", UseCpFile);
733 		} else {
734 		    fpath = mprintf("%s/%s", spath, UseCpFile);
735 		}
736 		AddList(&list, strrchr(fpath, '/') + 1, 1);
737 		if ((fi = fopen(fpath, "r")) != NULL) {
738 		    while (fgets(buf, sizeof(buf), fi) != NULL) {
739 			int l = strlen(buf);
740 			CountSourceReadBytes += l;
741 			if (l && buf[l-1] == '\n')
742 			    buf[--l] = 0;
743 			if (buf[0] && buf[0] != '#')
744 			    AddList(&list, buf, 1);
745 		    }
746 		    fclose(fi);
747 		}
748 		free(fpath);
749 	    }
750 
751 	    /*
752 	     * Automatically exclude MD5CacheFile that we create on the
753 	     * source from the copy to the destination.
754 	     *
755 	     * Automatically exclude a FSMIDCacheFile on the source that
756 	     * would otherwise overwrite the one we maintain on the target.
757 	     */
758 	    if (UseMD5Opt)
759 		AddList(&list, MD5CacheFile, 1);
760 	    if (UseFSMIDOpt)
761 		AddList(&list, FSMIDCacheFile, 1);
762 
763 	    while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
764 		/*
765 		 * ignore . and ..
766 		 */
767 		char *nspath;
768 		char *ndpath = NULL;
769 
770 		if (strcmp(den->d_name, ".") == 0 ||
771 		    strcmp(den->d_name, "..") == 0
772 		) {
773 		    continue;
774 		}
775 		/*
776 		 * ignore if on .cpignore list
777 		 */
778 		if (AddList(&list, den->d_name, 0) == 1) {
779 		    continue;
780 		}
781 		nspath = mprintf("%s/%s", spath, den->d_name);
782 		if (dpath)
783 		    ndpath = mprintf("%s/%s", dpath, den->d_name);
784 		r += DoCopy(
785 		    nspath,
786 		    ndpath,
787 		    sdevNo,
788 		    ddevNo
789 		);
790 		free(nspath);
791 		if (ndpath)
792 		    free(ndpath);
793 	    }
794 
795 	    hc_closedir(&SrcHost, dir);
796 
797 	    /*
798 	     * Remove files/directories from destination that do not appear
799 	     * in the source.
800 	     */
801 	    if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
802 		while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
803 		    /*
804 		     * ignore . or ..
805 		     */
806 		    if (strcmp(den->d_name, ".") == 0 ||
807 			strcmp(den->d_name, "..") == 0
808 		    ) {
809 			continue;
810 		    }
811 		    /*
812 		     * If object does not exist in source or .cpignore
813 		     * then recursively remove it.
814 		     */
815 		    if (AddList(&list, den->d_name, 3) == 3) {
816 			char *ndpath;
817 
818 			ndpath = mprintf("%s/%s", dpath, den->d_name);
819 			RemoveRecur(ndpath, ddevNo);
820 			free(ndpath);
821 		    }
822 		}
823 		hc_closedir(&DstHost, dir);
824 	    }
825 
826 	    if (dpath) {
827 		struct timeval tv[2];
828 
829 		if (ForceOpt ||
830 		    st2Valid == 0 ||
831 		    st1.st_uid != st2.st_uid ||
832 		    st1.st_gid != st2.st_gid
833 		) {
834 		    hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
835 		}
836 		if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
837 		    hc_chmod(&DstHost, dpath, st1.st_mode);
838 		}
839 #ifdef _ST_FLAGS_PRESENT_
840 		if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
841 		    hc_chflags(&DstHost, dpath, st1.st_flags);
842 		}
843 #endif
844 		if (ForceOpt ||
845 		    st2Valid == 0 ||
846 		    st1.st_mtime != st2.st_mtime
847 		) {
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);
852 		}
853 	    }
854 	}
855     } else if (dpath == NULL) {
856 	/*
857 	 * If dpath is NULL, we are just updating the MD5
858 	 */
859 #ifndef NOMD5
860 	if (UseMD5Opt && S_ISREG(st1.st_mode)) {
861 	    mres = md5_check(spath, NULL);
862 
863 	    if (VerboseOpt > 1) {
864 		if (mres < 0)
865 		    logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
866 		else
867 		    logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
868 	    } else if (!QuietOpt && mres < 0) {
869 		logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
870 	    }
871 	}
872 #endif
873     } else if (S_ISREG(st1.st_mode)) {
874 	char *path;
875 	char *hpath;
876 	int fd1;
877 	int fd2;
878 
879 	path = mprintf("%s.tmp", dpath);
880 
881 	/*
882 	 * Handle check failure message.
883 	 */
884 #ifndef NOMD5
885 	if (mres < 0)
886 	    logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
887 	else
888 #endif
889 	if (fres < 0)
890 	    logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
891 
892 	/*
893 	 * Not quite ready to do the copy yet.  If UseHLPath is defined,
894 	 * see if we can hardlink instead.
895 	 *
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
899 	 * used.
900 	 */
901 	if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
902 		if (st2Valid)
903 			hc_remove(&DstHost, dpath);
904 		if (hc_link(&DstHost, hpath, dpath) == 0) {
905 			++CountLinkedItems;
906 			if (VerboseOpt) {
907 			    logstd("%-32s hardlinked(-H)\n",
908 				   (dpath ? dpath : spath));
909 			}
910 			free(hpath);
911 			goto skip_copy;
912 		}
913 		/*
914 		 * Shucks, we may have hit a filesystem hard linking limit,
915 		 * we have to copy instead.
916 		 */
917 		free(hpath);
918 	}
919 
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) {
922 		/*
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.
925 		 */
926 #ifdef _ST_FLAGS_PRESENT_
927 		hc_chflags(&DstHost, path, 0);
928 #endif
929 		hc_remove(&DstHost, path);
930 		fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
931 	    }
932 	    if (fd2 >= 0) {
933 		const char *op;
934 		int n;
935 
936 		/*
937 		 * Matt: What about holes?
938 		 */
939 		op = "read";
940 		while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
941 		    op = "write";
942 		    if (hc_write(&DstHost, fd2, IOBuf1, n) != n)
943 			break;
944 		    op = "read";
945 		}
946 		hc_close(&DstHost, fd2);
947 		if (n == 0) {
948 		    struct timeval tv[2];
949 
950 		    bzero(tv, sizeof(tv));
951 		    tv[0].tv_sec = st1.st_mtime;
952 		    tv[1].tv_sec = st1.st_mtime;
953 
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)
960 			);
961 			++r;
962 		    } else {
963 			if (VerboseOpt)
964 			    logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
965 #ifdef _ST_FLAGS_PRESENT_
966 			if (st1.st_flags)
967 			    hc_chflags(&DstHost, dpath, st1.st_flags);
968 #endif
969 		    }
970 		    CountSourceReadBytes += size;
971 		    CountWriteBytes += size;
972 		    CountSourceBytes += size;
973 		    CountSourceItems++;
974 		    CountCopiedItems++;
975 		} else {
976 		    logerr("%-32s %s failed: %s\n",
977 			(dpath ? dpath : spath), op, strerror(errno)
978 		    );
979 		    hc_remove(&DstHost, path);
980 		    ++r;
981 		}
982 	    } else {
983 		logerr("%-32s create (uid %d, euid %d) failed: %s\n",
984 		    (dpath ? dpath : spath), getuid(), geteuid(),
985 		    strerror(errno)
986 		);
987 		++r;
988 	    }
989 	    hc_close(&SrcHost, fd1);
990 	} else {
991 	    logerr("%-32s copy: open failed: %s\n",
992 		(dpath ? dpath : spath),
993 		strerror(errno)
994 	    );
995 	    ++r;
996 	}
997 skip_copy:
998 	free(path);
999 
1000         if (hln) {
1001             if (!r && hc_stat(&DstHost, dpath, &st2) == 0)
1002                 hln->dino = st2.st_ino;
1003             else
1004                 hltdelete(hln);
1005         }
1006     } else if (S_ISLNK(st1.st_mode)) {
1007 	char link1[1024];
1008 	char link2[1024];
1009 	char path[2048];
1010 	int n1;
1011 	int n2;
1012 
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);
1016 	if (n1 >= 0) {
1017 	    if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1018 		hc_umask(&DstHost, ~st1.st_mode);
1019 		hc_remove(&DstHost, path);
1020 		link1[n1] = 0;
1021 		if (hc_symlink(&DstHost, link1, path) < 0) {
1022                       logerr("%-32s symlink (%s->%s) failed: %s\n",
1023 			  (dpath ? dpath : spath), link1, path,
1024 			  strerror(errno)
1025 		      );
1026 		      ++r;
1027 		} else {
1028 		    hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1029 		    /*
1030 		     * there is no lchmod() or lchflags(), we
1031 		     * cannot chmod or chflags a softlink.
1032 		     */
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));
1039 		    }
1040 		    hc_umask(&DstHost, 000);
1041 		    CountWriteBytes += n1;
1042 		    CountCopiedItems++;
1043 	  	}
1044 	    } else {
1045 		if (VerboseOpt >= 3)
1046 		    logstd("%-32s nochange\n", (dpath ? dpath : spath));
1047 	    }
1048 	    CountSourceBytes += n1;
1049 	    CountSourceReadBytes += n1;
1050 	    if (n2 > 0)
1051 		CountTargetReadBytes += n2;
1052 	    CountSourceItems++;
1053 	} else {
1054 	    r = 1;
1055 	    logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1056 	}
1057     } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1058 	char path[2048];
1059 
1060 	if (ForceOpt ||
1061 	    st2Valid == 0 ||
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
1066 	) {
1067 	    snprintf(path, sizeof(path), "%s.tmp", dpath);
1068 
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),
1077 			strerror(errno)
1078 		    );
1079 		} else if (VerboseOpt) {
1080 		    logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1081 		}
1082 		CountCopiedItems++;
1083 	    } else {
1084 		r = 1;
1085 		logerr("%-32s dev failed: %s\n",
1086 		    (dpath ? dpath : spath), strerror(errno)
1087 		);
1088 	    }
1089 	} else {
1090 	    if (VerboseOpt >= 3)
1091 		logstd("%-32s nochange\n", (dpath ? dpath : spath));
1092 	}
1093 	CountSourceItems++;
1094     }
1095     ResetList(&list);
1096     return (r);
1097 }
1098 
1099 /*
1100  * RemoveRecur()
1101  */
1102 
1103 void
1104 RemoveRecur(const char *dpath, dev_t devNo)
1105 {
1106     struct stat st;
1107 
1108     if (hc_lstat(&DstHost, dpath, &st) == 0) {
1109 	if ((int)devNo < 0)
1110 	    devNo = st.st_dev;
1111 	if (st.st_dev == devNo) {
1112 	    if (S_ISDIR(st.st_mode)) {
1113 		DIR *dir;
1114 
1115 		if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1116 		    struct dirent *den;
1117 		    while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1118 			char *ndpath;
1119 
1120 			if (strcmp(den->d_name, ".") == 0)
1121 			    continue;
1122 			if (strcmp(den->d_name, "..") == 0)
1123 			    continue;
1124 			ndpath = mprintf("%s/%s", dpath, den->d_name);
1125 			RemoveRecur(ndpath, devNo);
1126 			free(ndpath);
1127 		    }
1128 		    hc_closedir(&DstHost, dir);
1129 		}
1130 		if (AskConfirmation && NoRemoveOpt == 0) {
1131 		    if (YesNo(dpath)) {
1132 			if (hc_rmdir(&DstHost, dpath) < 0) {
1133 			    logerr("%-32s rmdir failed: %s\n",
1134 				dpath, strerror(errno)
1135 			    );
1136 			}
1137 			CountRemovedItems++;
1138 		    }
1139 		} else {
1140 		    if (NoRemoveOpt) {
1141 			if (VerboseOpt)
1142 			    logstd("%-32s not-removed\n", dpath);
1143 		    } else if (hc_rmdir(&DstHost, dpath) == 0) {
1144 			if (VerboseOpt)
1145 			    logstd("%-32s rmdir-ok\n", dpath);
1146 			CountRemovedItems++;
1147 		    } else {
1148 			logerr("%-32s rmdir failed: %s\n",
1149 			    dpath, strerror(errno)
1150 			);
1151 		    }
1152 		}
1153 	    } else {
1154 		if (AskConfirmation && NoRemoveOpt == 0) {
1155 		    if (YesNo(dpath)) {
1156 			if (hc_remove(&DstHost, dpath) < 0) {
1157 			    logerr("%-32s remove failed: %s\n",
1158 				dpath, strerror(errno)
1159 			    );
1160 			}
1161 			CountRemovedItems++;
1162 		    }
1163 		} else {
1164 		    if (NoRemoveOpt) {
1165 			if (VerboseOpt)
1166 			    logstd("%-32s not-removed\n", dpath);
1167 		    } else if (hc_remove(&DstHost, dpath) == 0) {
1168 			if (VerboseOpt)
1169 			    logstd("%-32s remove-ok\n", dpath);
1170 			CountRemovedItems++;
1171 		    } else {
1172 			logerr("%-32s remove failed: %s\n",
1173 			    dpath, strerror(errno)
1174 			);
1175 		    }
1176 		}
1177 	    }
1178 	}
1179     }
1180 }
1181 
1182 void
1183 InitList(List *list)
1184 {
1185     bzero(list, sizeof(List));
1186     list->li_Node.no_Next = &list->li_Node;
1187 }
1188 
1189 void
1190 ResetList(List *list)
1191 {
1192     Node *node;
1193 
1194     while ((node = list->li_Node.no_Next) != &list->li_Node) {
1195 	list->li_Node.no_Next = node->no_Next;
1196 	free(node);
1197     }
1198     InitList(list);
1199 }
1200 
1201 int
1202 AddList(List *list, const char *name, int n)
1203 {
1204     Node *node;
1205     int hv;
1206 
1207     hv = shash(name);
1208 
1209     /*
1210      * Scan against wildcards.  Only a node value of 1 can be a wildcard
1211      * ( usually scanned from .cpignore )
1212      */
1213 
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)
1217 	) {
1218 	    return(node->no_Value);
1219 	}
1220     }
1221 
1222     /*
1223      * Look for exact match
1224      */
1225 
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);
1229 	}
1230     }
1231     node = malloc(sizeof(Node) + strlen(name) + 1);
1232     if (node == NULL) {
1233         fprintf(stderr, "out of memory\n");
1234         exit(EXIT_FAILURE);
1235     }
1236 
1237     node->no_Next = list->li_Node.no_Next;
1238     list->li_Node.no_Next = node;
1239 
1240     node->no_HNext = list->li_Hash[hv];
1241     list->li_Hash[hv] = node;
1242 
1243     strcpy(node->no_Name, name);
1244     node->no_Value = n;
1245 
1246     return(n);
1247 }
1248 
1249 static int
1250 shash(const char *s)
1251 {
1252     int hv;
1253 
1254     hv = 0xA4FB3255;
1255 
1256     while (*s) {
1257 	if (*s == '*' || *s == '?' ||
1258 	    *s == '{' || *s == '}' ||
1259 	    *s == '[' || *s == ']' ||
1260 	    *s == '|'
1261 	) {
1262 	    return(0);
1263 	}
1264 	hv = (hv << 5) ^ *s ^ (hv >> 23);
1265 	++s;
1266     }
1267     return(((hv >> 16) ^ hv) & HMASK);
1268 }
1269 
1270 /*
1271  * WildCmp() - compare wild string to sane string
1272  *
1273  *	Return 0 on success, -1 on failure.
1274  */
1275 
1276 int
1277 WildCmp(const char *w, const char *s)
1278 {
1279     /*
1280      * skip fixed portion
1281      */
1282 
1283     for (;;) {
1284 	switch(*w) {
1285 	case '*':
1286 	    if (w[1] == 0)	/* optimize wild* case */
1287 		return(0);
1288 	    {
1289 		int i;
1290 		int l = strlen(s);
1291 
1292 		for (i = 0; i <= l; ++i) {
1293 		    if (WildCmp(w + 1, s + i) == 0)
1294 			return(0);
1295 		}
1296 	    }
1297 	    return(-1);
1298 	case '?':
1299 	    if (*s == 0)
1300 		return(-1);
1301 	    ++w;
1302 	    ++s;
1303 	    break;
1304 	default:
1305 	    if (*w != *s)
1306 		return(-1);
1307 	    if (*w == 0)	/* terminator */
1308 		return(0);
1309 	    ++w;
1310 	    ++s;
1311 	    break;
1312 	}
1313     }
1314     /* not reached */
1315     return(-1);
1316 }
1317 
1318 int
1319 YesNo(const char *path)
1320 {
1321     int ch, first;
1322 
1323     fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1324     fflush(stderr);
1325 
1326     first = ch = getchar();
1327     while (ch != '\n' && ch != EOF)
1328 	ch = getchar();
1329     return ((first == 'y' || first == 'Y'));
1330 }
1331 
1332 /*
1333  * xrename() - rename with override
1334  *
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.
1338  */
1339 
1340 static int
1341 xrename(const char *src, const char *dst, u_long flags)
1342 {
1343     int r;
1344 
1345     r = 0;
1346 
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);
1352 #endif
1353     }
1354     return(r);
1355 }
1356 
1357 static int
1358 xlink(const char *src, const char *dst, u_long flags)
1359 {
1360     int r;
1361 #ifdef _ST_FLAGS_PRESENT_
1362     int e;
1363 #endif
1364 
1365     r = 0;
1366 
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);
1371 	e = errno;
1372 	hc_chflags(&DstHost, src, flags);
1373 	errno = e;
1374 #endif
1375     }
1376     if (r == 0)
1377 	    ++CountLinkedItems;
1378     return(r);
1379 }
1380 
1381