Merge branch 'vendor/DHCPCD'
[dragonfly.git] / bin / cpdup / cpdup.c
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.32 2008/11/11 04:36:00 dillon Exp $
49  */
50
51 /*-
52  * Example: cc -O cpdup.c -o cpdup -lcrypto
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   8192
63 #define HMASK   (HSIZE-1)
64 #define HLSIZE  8192
65 #define HLMASK  (HLSIZE - 1)
66
67 #define GETBUFSIZE      8192
68 #define GETPATHSIZE     2048
69 #define GETLINKSIZE     1024
70 #define GETIOSIZE       65536
71
72 #ifndef _ST_FLAGS_PRESENT_
73 #define st_flags        st_mode
74 #endif
75
76 typedef struct Node {
77     struct Node *no_Next;
78     struct Node *no_HNext;
79     struct stat *no_Stat;
80     int  no_Value;
81     char no_Name[4];
82 } Node;
83
84 typedef struct List {
85     Node        li_Node;
86     Node        *li_Hash[HSIZE];
87 } List;
88
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[];
97 };
98
99 typedef struct copy_info {
100         char *spath;
101         char *dpath;
102         dev_t sdevNo;
103         dev_t ddevNo;
104 } *copy_info_t;
105
106 static struct hlink *hltable[HLSIZE];
107
108 static void RemoveRecur(const char *dpath, dev_t devNo, struct stat *dstat);
109 static void InitList(List *list);
110 static void ResetList(List *list);
111 static Node *IterateList(List *list, Node *node, int n);
112 static int AddList(List *list, const char *name, int n, struct stat *st);
113 static int CheckList(List *list, const char *path, const char *name);
114 static int getbool(const char *str);
115 static char *SplitRemote(char **pathp);
116 static int ChgrpAllowed(gid_t g);
117 static int OwnerMatch(struct stat *st1, struct stat *st2);
118 #ifdef _ST_FLAGS_PRESENT_
119 static int FlagsMatch(struct stat *st1, struct stat *st2);
120 #else
121 #define FlagsMatch(st1, st2)    1
122 #endif
123 static struct hlink *hltlookup(struct stat *);
124 static struct hlink *hltadd(struct stat *, const char *);
125 static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
126 static int validate_check(const char *spath, const char *dpath);
127 static int shash(const char *s);
128 static void hltdelete(struct hlink *);
129 static void hltsetdino(struct hlink *, ino_t);
130 static int YesNo(const char *path);
131 static int xrename(const char *src, const char *dst, u_long flags);
132 static int xlink(const char *src, const char *dst, u_long flags);
133 static int xremove(struct HostConf *host, const char *path);
134 static int xrmdir(struct HostConf *host, const char *path);
135 static int DoCopy(copy_info_t info, struct stat *stat1, int depth);
136 static int ScanDir(List *list, struct HostConf *host, const char *path,
137         int64_t *CountReadBytes, int n);
138 static int mtimecmp(struct stat *st1, struct stat *st2);
139
140 int AskConfirmation = 1;
141 int SafetyOpt = 1;
142 int ForceOpt;
143 int DeviceOpt = 1;
144 int VerboseOpt;
145 int DirShowOpt;
146 int NotForRealOpt;
147 int QuietOpt;
148 int NoRemoveOpt;
149 int UseMD5Opt;
150 int UseFSMIDOpt;
151 int SummaryOpt;
152 int CompressOpt;
153 int SlaveOpt;
154 int ReadOnlyOpt;
155 int ValidateOpt;
156 int ssh_argc;
157 const char *ssh_argv[16];
158 int DstRootPrivs;
159
160 const char *UseCpFile;
161 const char *MD5CacheFile;
162 const char *FSMIDCacheFile;
163 const char *UseHLPath;
164
165 static int DstBaseLen;
166 static int HardLinkCount;
167 static int GroupCount;
168 static gid_t *GroupList;
169
170 int64_t CountSourceBytes;
171 int64_t CountSourceItems;
172 int64_t CountCopiedItems;
173 int64_t CountSourceReadBytes;
174 int64_t CountTargetReadBytes;
175 int64_t CountWriteBytes;
176 int64_t CountRemovedItems;
177 int64_t CountLinkedItems;
178
179 static struct HostConf SrcHost;
180 static struct HostConf DstHost;
181
182 int
183 main(int ac, char **av)
184 {
185     int i;
186     int opt;
187     char *src = NULL;
188     char *dst = NULL;
189     char *ptr;
190     struct timeval start;
191     struct copy_info info;
192
193     signal(SIGPIPE, SIG_IGN);
194
195     gettimeofday(&start, NULL);
196     opterr = 0;
197     while ((opt = getopt(ac, av, ":CdF:fH:hIi:j:K:klM:mnoqRSs:uVvX:x")) != -1) {
198         switch (opt) {
199         case 'C':
200             CompressOpt = 1;
201             break;
202         case 'd':
203             DirShowOpt = 1;
204             break;
205         case 'F':
206             if (ssh_argc >= 16)
207                 fatal("too many -F options");
208             ssh_argv[ssh_argc++] = optarg;
209             break;
210         case 'f':
211             ForceOpt = 1;
212             break;
213         case 'H':
214             UseHLPath = optarg;
215             break;
216         case 'h':
217             fatal(NULL);
218             /* not reached */
219             break;
220         case 'I':
221             SummaryOpt = 1;
222             break;
223         case 'i':
224             AskConfirmation = getbool(optarg);
225             break;
226         case 'j':
227             DeviceOpt = getbool(optarg);
228             break;
229         case 'K':
230             UseFSMIDOpt = 1;
231             FSMIDCacheFile = optarg;
232             break;
233         case 'k':
234             UseFSMIDOpt = 1;
235             FSMIDCacheFile = ".FSMID.CHECK";
236             break;
237         case 'l':
238             setlinebuf(stdout);
239             setlinebuf(stderr);
240             break;
241         case 'M':
242             UseMD5Opt = 1;
243             MD5CacheFile = optarg;
244             break;
245         case 'm':
246             UseMD5Opt = 1;
247             MD5CacheFile = ".MD5.CHECKSUMS";
248             break;
249         case 'n':
250             NotForRealOpt = 1;
251             break;
252         case 'o':
253             NoRemoveOpt = 1;
254             break;
255         case 'q':
256             QuietOpt = 1;
257             break;
258         case 'R':
259             ReadOnlyOpt = 1;
260             break;
261         case 'S':
262             SlaveOpt = 1;
263             break;
264         case 's':
265             SafetyOpt = getbool(optarg);
266             break;
267         case 'u':
268             setvbuf(stdout, NULL, _IOLBF, 0);
269             break;
270         case 'V':
271             ++ValidateOpt;
272             break;
273         case 'v':
274             ++VerboseOpt;
275             break;
276         case 'X':
277             UseCpFile = optarg;
278             break;
279         case 'x':
280             UseCpFile = ".cpignore";
281             break;
282         case ':':
283             fatal("missing argument for option: -%c\n", optopt);
284             /* not reached */
285             break;
286         case '?':
287             fatal("illegal option: -%c\n", optopt);
288             /* not reached */
289             break;
290         default:
291             fatal(NULL);
292             /* not reached */
293             break;
294         }
295     }
296     ac -= optind;
297     av += optind;
298     if (ac > 0)
299         src = av[0];
300     if (ac > 1)
301         dst = av[1];
302     if (ac > 2)
303         fatal("too many arguments");
304
305     /*
306      * If we are told to go into slave mode, run the HC protocol
307      */
308     if (SlaveOpt) {
309         DstRootPrivs = (geteuid() == 0);
310         hc_slave(0, 1);
311         exit(0);
312     }
313
314     /*
315      * Extract the source and/or/neither target [user@]host and
316      * make any required connections.
317      */
318     if (src && (ptr = SplitRemote(&src)) != NULL) {
319         SrcHost.host = src;
320         src = ptr;
321         if (UseMD5Opt)
322             fatal("The MD5 options are not currently supported for remote sources");
323         if (hc_connect(&SrcHost, ReadOnlyOpt) < 0)
324             exit(1);
325     } else if (ReadOnlyOpt)
326         fatal("The -R option is only supported for remote sources");
327
328     if (dst && (ptr = SplitRemote(&dst)) != NULL) {
329         DstHost.host = dst;
330         dst = ptr;
331         if (UseFSMIDOpt)
332             fatal("The FSMID options are not currently supported for remote targets");
333         if (hc_connect(&DstHost, 0) < 0)
334             exit(1);
335     }
336
337     /*
338      * dst may be NULL only if -m option is specified,
339      * which forces an update of the MD5 checksums
340      */
341     if (dst == NULL && UseMD5Opt == 0) {
342         fatal(NULL);
343         /* not reached */
344     }
345
346     if (dst) {
347         DstRootPrivs = (hc_geteuid(&DstHost) == 0);
348         if (!DstRootPrivs)
349             GroupCount = hc_getgroups(&DstHost, &GroupList);
350     }
351 #if 0
352     /* XXXX DEBUG */
353     fprintf(stderr, "DstRootPrivs == %s\n", DstRootPrivs ? "true" : "false");
354     fprintf(stderr, "GroupCount == %d\n", GroupCount);
355     for (i = 0; i < GroupCount; i++)
356         fprintf(stderr, "Group[%d] == %d\n", i, GroupList[i]);
357 #endif
358
359     bzero(&info, sizeof(info));
360     if (dst) {
361         DstBaseLen = strlen(dst);
362         info.spath = src;
363         info.dpath = dst;
364         info.sdevNo = (dev_t)-1;
365         info.ddevNo = (dev_t)-1;
366         i = DoCopy(&info, NULL, -1);
367     } else {
368         info.spath = src;
369         info.dpath = NULL;
370         info.sdevNo = (dev_t)-1;
371         info.ddevNo = (dev_t)-1;
372         i = DoCopy(&info, NULL, -1);
373     }
374 #ifndef NOMD5
375     md5_flush();
376 #endif
377     fsmid_flush();
378
379     if (SummaryOpt && i == 0) {
380         double duration;
381         struct timeval end;
382
383         gettimeofday(&end, NULL);
384 #if 0
385         /* don't count stat's in our byte statistics */
386         CountSourceBytes += sizeof(struct stat) * CountSourceItems;
387         CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
388         CountWriteBytes +=  sizeof(struct stat) * CountCopiedItems;
389         CountWriteBytes +=  sizeof(struct stat) * CountRemovedItems;
390 #endif
391
392         duration = (end.tv_sec - start.tv_sec);
393         duration += (double)(end.tv_usec - start.tv_usec) / 1000000.0;
394         if (duration == 0.0)
395                 duration = 1.0;
396         logstd("cpdup completed successfully\n");
397         logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
398                "%lld bytes written (%.1fX speedup)\n",
399             (long long)CountSourceBytes,
400             (long long)CountSourceReadBytes,
401             (long long)CountTargetReadBytes,
402             (long long)CountWriteBytes,
403             ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
404         logstd("%lld source items, %lld items copied, %lld items linked, "
405                "%lld things deleted\n",
406             (long long)CountSourceItems,
407             (long long)CountCopiedItems,
408             (long long)CountLinkedItems,
409             (long long)CountRemovedItems);
410         logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
411             duration,
412             (int)((CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration  / 1024.0),
413             (int)(CountSourceBytes / duration / 1024.0));
414     }
415     exit((i == 0) ? 0 : 1);
416 }
417
418 static int
419 getbool(const char *str)
420 {
421     if (strcmp(str, "0") == 0)
422         return (0);
423     if (strcmp(str, "1") == 0)
424         return (1);
425     fatal("option requires boolean argument (0 or 1): -%c\n", optopt);
426     /* not reached */
427     return (0);
428 }
429
430 /*
431  * Check if path specifies a remote path, using the same syntax as scp(1),
432  * i.e. a path is considered remote if the first colon is not preceded by
433  * a slash, so e.g. "./foo:bar" is considered local.
434  * If a remote path is detected, the colon is replaced with a null byte,
435  * and the return value is a pointer to the next character.
436  * Otherwise NULL is returned.
437  *
438  * A path prefix of localhost is the same as a locally specified file or
439  * directory path, but prevents any further interpretation of the path
440  * as being a remote hostname (for paths that have colons in them).
441  */
442 static char *
443 SplitRemote(char **pathp)
444 {
445     int cindex;
446     char *path = *pathp;
447
448     if (path[(cindex = strcspn(path, ":/"))] == ':') {
449         path[cindex++] = 0;
450         if (strcmp(path, "localhost") != 0)
451                 return (path + cindex);
452         *pathp = path + cindex;
453     }
454     return (NULL);
455 }
456
457 /*
458  * Check if group g is in our GroupList.
459  *
460  * Typically the number of groups a user belongs to isn't large
461  * enough to warrant more effort than a simple linear search.
462  * However, we perform an optimization by moving a group to the
463  * top of the list when we have a hit.  This assumes that there
464  * isn't much variance in the gids of files that a non-root user
465  * copies.  So most of the time the search will terminate on the
466  * first element of the list.
467  */
468 static int
469 ChgrpAllowed(gid_t g)
470 {
471     int i;
472
473     for (i = 0; i < GroupCount; i++)
474         if (GroupList[i] == g) {
475             if (i > 0) {
476                 /* Optimize: Move g to the front of the list. */
477                 for (; i > 0; i--)
478                     GroupList[i] = GroupList[i - 1];
479                 GroupList[0] = g;
480             }
481             return (1);
482         }
483     return (0);
484 }
485
486 /*
487  * The following two functions return true if the ownership (UID + GID)
488  * or the flags of two files match, respectively.
489  *
490  * Only perform weak checking if we don't have sufficient privileges on
491  * the target machine, so we don't waste transfers with things that are
492  * bound to fail anyway.
493  */
494 static int
495 OwnerMatch(struct stat *st1, struct stat *st2)
496 {
497     if (DstRootPrivs)
498         /* Both UID and GID must match. */
499         return (st1->st_uid == st2->st_uid && st1->st_gid == st2->st_gid);
500     else
501         /* Ignore UID, and also ignore GID if we can't chgrp to that group. */
502         return (st1->st_gid == st2->st_gid || !ChgrpAllowed(st1->st_gid));
503 }
504
505 #ifdef _ST_FLAGS_PRESENT_
506 static int
507 FlagsMatch(struct stat *st1, struct stat *st2)
508 {
509 /*
510  * Ignore UF_ARCHIVE.  It gets set automatically by the filesystem, for
511  * filesystems that support it.  If the destination filesystem supports it, but
512  * it's cleared on the source file, then multiple invocations of cpdup would
513  * all try to copy the file because the flags wouldn't match.
514  *
515  * When unpriveleged, ignore flags we can't set
516  */
517     u_long ignored = DstRootPrivs ? 0 : SF_SETTABLE;
518
519 #ifdef UF_ARCHIVE
520     ignored |= UF_ARCHIVE;
521 #endif
522     return (((st1->st_flags ^ st2->st_flags) & ~ignored) == 0);
523 }
524 #endif
525
526
527 static struct hlink *
528 hltlookup(struct stat *stp)
529 {
530     struct hlink *hl;
531     int n;
532
533     n = stp->st_ino & HLMASK;
534
535     for (hl = hltable[n]; hl; hl = hl->next) {
536         if (hl->ino == stp->st_ino) {
537             ++hl->refs;
538             return hl;
539         }
540     }
541
542     return NULL;
543 }
544
545 static struct hlink *
546 hltadd(struct stat *stp, const char *path)
547 {
548     struct hlink *new;
549     int plen = strlen(path);
550     int n;
551
552     new = malloc(offsetof(struct hlink, name[plen + 1]));
553     if (new == NULL)
554         fatal("out of memory");
555     ++HardLinkCount;
556
557     /* initialize and link the new element into the table */
558     new->ino = stp->st_ino;
559     new->dino = (ino_t)-1;
560     new->refs = 1;
561     bcopy(path, new->name, plen + 1);
562     new->nlinked = 1;
563     new->prev = NULL;
564     n = stp->st_ino & HLMASK;
565     new->next = hltable[n];
566     if (hltable[n])
567         hltable[n]->prev = new;
568     hltable[n] = new;
569
570     return new;
571 }
572
573 static void
574 hltsetdino(struct hlink *hl, ino_t inum)
575 {
576     hl->dino = inum;
577 }
578
579 static void
580 hltdelete(struct hlink *hl)
581 {
582     assert(hl->refs == 1);
583     --hl->refs;
584     if (hl->prev) {
585         if (hl->next)
586             hl->next->prev = hl->prev;
587         hl->prev->next = hl->next;
588     } else {
589         if (hl->next)
590             hl->next->prev = NULL;
591
592         hltable[hl->ino & HLMASK] = hl->next;
593     }
594     --HardLinkCount;
595     free(hl);
596 }
597
598 static void
599 hltrels(struct hlink *hl)
600 {
601     assert(hl->refs == 1);
602     --hl->refs;
603 }
604
605 /*
606  * If UseHLPath is defined check to see if the file in question is
607  * the same as the source file, and if it is return a pointer to the
608  * -H path based file for hardlinking.  Else return NULL.
609  */
610 static char *
611 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
612 {
613     struct stat sthl;
614     char *hpath;
615     int error;
616
617     if (asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen) < 0)
618         fatal("out of memory");
619
620     /*
621      * stat info matches ?
622      */
623     if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
624         st1->st_size != sthl.st_size ||
625         mtimecmp(st1, &sthl) != 0 ||
626         !OwnerMatch(st1, &sthl) ||
627         !FlagsMatch(st1, &sthl)
628     ) {
629         free(hpath);
630         return(NULL);
631     }
632
633     /*
634      * If ForceOpt or ValidateOpt is set we have to compare the files
635      */
636     if (ForceOpt || ValidateOpt) {
637         error = validate_check(spath, hpath);
638         if (error) {
639             free(hpath);
640             hpath = NULL;
641         }
642     }
643     return(hpath);
644 }
645
646 /*
647  * Return 0 if the contents of the file <spath> matches the contents of
648  * the file <dpath>.
649  */
650 static int
651 validate_check(const char *spath, const char *dpath)
652 {
653     int error;
654     int fd1;
655     int fd2;
656
657     fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
658     fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
659     error = -1;
660
661     if (fd1 >= 0 && fd2 >= 0) {
662         int n;
663         int x;
664         char *iobuf1 = malloc(GETIOSIZE);
665         char *iobuf2 = malloc(GETIOSIZE);
666
667         while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
668             CountSourceReadBytes += n;
669             x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
670             if (x > 0)
671                     CountTargetReadBytes += x;
672             if (x != n)
673                 break;
674             if (bcmp(iobuf1, iobuf2, n) != 0)
675                 break;
676         }
677         free(iobuf1);
678         free(iobuf2);
679         if (n == 0)
680             error = 0;
681     }
682     if (fd1 >= 0)
683         hc_close(&SrcHost, fd1);
684     if (fd2 >= 0)
685         hc_close(&DstHost, fd2);
686     return (error);
687 }
688
689 int
690 DoCopy(copy_info_t info, struct stat *stat1, int depth)
691 {
692     const char *spath = info->spath;
693     const char *dpath = info->dpath;
694     dev_t sdevNo = info->sdevNo;
695     dev_t ddevNo = info->ddevNo;
696     struct stat st1;
697     struct stat st2;
698     unsigned long st2_flags;
699     int r, mres, fres, st2Valid;
700     struct hlink *hln;
701     uint64_t size;
702
703     r = mres = fres = st2Valid = 0;
704     st2_flags = 0;
705     size = 0;
706     hln = NULL;
707
708     if (stat1 == NULL) {
709         if (hc_lstat(&SrcHost, spath, &st1) != 0) {
710             r = 1;
711             goto done;
712         }
713         stat1 = &st1;
714     }
715 #ifdef SF_SNAPSHOT
716     /* skip snapshot files because they're sparse and _huge_ */
717     if (stat1->st_flags & SF_SNAPSHOT)
718        return(0);
719 #endif
720     st2.st_mode = 0;    /* in case lstat fails */
721     st2.st_flags = 0;   /* in case lstat fails */
722     if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0) {
723         st2Valid = 1;
724 #ifdef _ST_FLAGS_PRESENT_
725         st2_flags = st2.st_flags;
726 #endif
727     }
728
729     if (S_ISREG(stat1->st_mode))
730         size = stat1->st_size;
731
732     /*
733      * Handle hardlinks
734      */
735
736     if (S_ISREG(stat1->st_mode) && stat1->st_nlink > 1 && dpath) {
737         if ((hln = hltlookup(stat1)) != NULL) {
738             hln->nlinked++;
739
740             if (st2Valid) {
741                 if (st2.st_ino == hln->dino) {
742                     /*
743                      * hard link is already correct, nothing to do
744                      */
745                     if (VerboseOpt >= 3)
746                         logstd("%-32s nochange\n", (dpath) ? dpath : spath);
747                     if (hln->nlinked == stat1->st_nlink) {
748                         hltdelete(hln);
749                         hln = NULL;
750                     }
751                     CountSourceItems++;
752                     r = 0;
753                     goto done;
754                 } else {
755                     /*
756                      * hard link is not correct, attempt to unlink it
757                      */
758                     if (xremove(&DstHost, dpath) < 0) {
759                         logerr("%-32s hardlink: unable to unlink: %s\n",
760                             ((dpath) ? dpath : spath), strerror(errno));
761                         hltdelete(hln);
762                         hln = NULL;
763                         ++r;
764                         goto done;
765                     }
766                 }
767             }
768
769             if (xlink(hln->name, dpath, stat1->st_flags) < 0) {
770                 int tryrelink = (errno == EMLINK);
771                 logerr("%-32s hardlink: unable to link to %s: %s\n",
772                     (dpath ? dpath : spath), hln->name, strerror(errno)
773                 );
774                 hltdelete(hln);
775                 hln = NULL;
776                 if (tryrelink) {
777                     logerr("%-20s hardlink: will attempt to copy normally\n",
778                         (dpath ? dpath : spath));
779                     goto relink;
780                 }
781                 ++r;
782             } else {
783                 if (hln->nlinked == stat1->st_nlink) {
784                     hltdelete(hln);
785                     hln = NULL;
786                 }
787                 if (r == 0) {
788                     if (VerboseOpt) {
789                         logstd("%-32s hardlink: %s\n",
790                             (dpath ? dpath : spath),
791                             (st2Valid ? "relinked" : "linked")
792                         );
793                     }
794                     CountSourceItems++;
795                     CountCopiedItems++;
796                     r = 0;
797                     goto done;
798                 }
799             }
800         } else {
801             /*
802              * first instance of hardlink must be copied normally
803              */
804 relink:
805             hln = hltadd(stat1, dpath);
806         }
807     }
808
809     /*
810      * Do we need to copy the file/dir/link/whatever?  Early termination
811      * if we do not.  Always redo links.  Directories are always traversed
812      * except when the FSMID options are used.
813      *
814      * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
815      */
816
817     if (
818         st2Valid
819         && stat1->st_mode == st2.st_mode
820         && FlagsMatch(stat1, &st2)
821     ) {
822         if (S_ISLNK(stat1->st_mode) || S_ISDIR(stat1->st_mode)) {
823             /*
824              * If FSMID tracking is turned on we can avoid recursing through
825              * an entire directory subtree if the FSMID matches.
826              */
827 #ifdef _ST_FSMID_PRESENT_
828             if (ForceOpt == 0 &&
829                 (UseFSMIDOpt && (fres = fsmid_check(stat1->st_fsmid, dpath)) == 0)
830             ) {
831                 if (VerboseOpt >= 3) {
832                     if (UseFSMIDOpt) /* always true!?! */
833                         logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
834                     else
835                         logstd("%-32s nochange\n", (dpath ? dpath : spath));
836                 }
837                 r = 0;
838                 goto done;
839             }
840 #endif
841         } else {
842             if (ForceOpt == 0 &&
843                 stat1->st_size == st2.st_size &&
844                 (ValidateOpt == 2 || mtimecmp(stat1, &st2) == 0) &&
845                 OwnerMatch(stat1, &st2)
846 #ifndef NOMD5
847                 && (UseMD5Opt == 0 || !S_ISREG(stat1->st_mode) ||
848                     (mres = md5_check(spath, dpath)) == 0)
849 #endif
850 #ifdef _ST_FSMID_PRESENT_
851                 && (UseFSMIDOpt == 0 ||
852                     (fres = fsmid_check(stat1->st_fsmid, dpath)) == 0)
853 #endif
854                 && (ValidateOpt == 0 || !S_ISREG(stat1->st_mode) ||
855                     validate_check(spath, dpath) == 0)
856             ) {
857                 /*
858                  * The files are identical, but if we are running as
859                  * root we might need to adjust ownership/group/flags.
860                  */
861                 int changedown = 0;
862                 int changedflags = 0;
863
864                 if (hln)
865                     hltsetdino(hln, st2.st_ino);
866
867                 if (!OwnerMatch(stat1, &st2)) {
868                     hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
869                     changedown = 1;
870                 }
871 #ifdef _ST_FLAGS_PRESENT_
872                 if (!FlagsMatch(stat1, &st2)) {
873                     hc_chflags(&DstHost, dpath, stat1->st_flags);
874                     changedflags = 1;
875                 }
876 #endif
877                 if (VerboseOpt >= 3) {
878 #ifndef NOMD5
879                     if (UseMD5Opt) {
880                         logstd("%-32s md5-nochange",
881                                 (dpath ? dpath : spath));
882                     } else
883 #endif
884                     if (UseFSMIDOpt) {
885                         logstd("%-32s fsmid-nochange",
886                                 (dpath ? dpath : spath));
887                     } else if (ValidateOpt) {
888                         logstd("%-32s nochange (contents validated)",
889                                 (dpath ? dpath : spath));
890                     } else {
891                         logstd("%-32s nochange", (dpath ? dpath : spath));
892                     }
893                     if (changedown)
894                         logstd(" (uid/gid differ)");
895                     if (changedflags)
896                         logstd(" (flags differ)");
897                     logstd("\n");
898                 }
899                 CountSourceBytes += size;
900                 CountSourceItems++;
901                 r = 0;
902                 goto done;
903             }
904         }
905     }
906     if (st2Valid && !S_ISDIR(stat1->st_mode) && S_ISDIR(st2.st_mode)) {
907         if (SafetyOpt) {
908             logerr("%-32s SAFETY - refusing to copy file over directory\n",
909                 (dpath ? dpath : spath)
910             );
911             ++r;                /* XXX */
912             r = 0;
913             goto done;          /* continue with the cpdup anyway */
914         }
915         if (QuietOpt == 0 || AskConfirmation) {
916             logstd("%-32s WARNING: non-directory source will blow away\n"
917                    "%-32s preexisting dest directory, continuing anyway!\n",
918                    ((dpath) ? dpath : spath), "");
919         }
920         if (dpath)
921             RemoveRecur(dpath, ddevNo, &st2);
922         st2Valid = 0;
923     }
924
925     /*
926      * The various comparisons failed, copy it.
927      */
928     if (S_ISDIR(stat1->st_mode)) {
929         int skipdir = 0;
930
931         if (fres < 0)
932             logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
933
934         if (dpath) {
935             if (!st2Valid || S_ISDIR(st2.st_mode) == 0) {
936                 if (st2Valid)
937                     xremove(&DstHost, dpath);
938                 if (hc_mkdir(&DstHost, dpath, stat1->st_mode | 0700) != 0) {
939                     logerr("%s: mkdir failed: %s\n",
940                         (dpath ? dpath : spath), strerror(errno));
941                     r = 1;
942                     skipdir = 1;
943                 }
944                 if (hc_lstat(&DstHost, dpath, &st2) != 0) {
945                     if (NotForRealOpt == 0)
946                             logerr("%s: lstat of newly made dir failed: %s\n",
947                                    (dpath ? dpath : spath), strerror(errno));
948                     st2Valid = 0;
949                     r = 1;
950                     skipdir = 1;
951                 }
952                 else {
953                     st2Valid = 1;
954                     if (!OwnerMatch(stat1, &st2) &&
955                         hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid) != 0
956                     ) {
957                         logerr("%s: chown of newly made dir failed: %s\n",
958                             (dpath ? dpath : spath), strerror(errno));
959                         r = 1;
960                         /* Note that we should not set skipdir = 1 here. */
961                     }
962                 }
963                 if (VerboseOpt)
964                     logstd("%-32s mkdir-ok\n", (dpath ? dpath : spath));
965                 CountCopiedItems++;
966             } else {
967                 /*
968                  * Directory must be scanable by root for cpdup to
969                  * work.  We'll fix it later if the directory isn't
970                  * supposed to be readable ( which is why we fixup
971                  * st2.st_mode to match what we did ).
972                  */
973                 if ((st2.st_mode & 0700) != 0700) {
974                     hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
975                     st2.st_mode |= 0700;
976                 }
977                 if (VerboseOpt >= 2)
978                     logstd("%s\n", dpath ? dpath : spath);
979             }
980         }
981
982         /*
983          * When copying a directory, stop if the source crosses a mount
984          * point.
985          */
986         if (sdevNo != (dev_t)-1 && stat1->st_dev != sdevNo)
987             skipdir = 1;
988         else
989             sdevNo = stat1->st_dev;
990
991         /*
992          * When copying a directory, stop if the destination crosses
993          * a mount point.
994          *
995          * The target directory will have been created and stat'd
996          * for st2 if it did not previously exist.   st2Valid is left
997          * as a flag.  If the stat failed st2 will still only have its
998          * default initialization.
999          *
1000          * So we simply assume here that the directory is within the
1001          * current target mount if we had to create it (aka st2Valid is 0)
1002          * and we leave ddevNo alone.
1003          */
1004         if (st2Valid) {
1005             if (ddevNo != (dev_t)-1 && st2.st_dev != ddevNo)
1006                 skipdir = 1;
1007             else
1008                 ddevNo = st2.st_dev;
1009         }
1010
1011         if (!skipdir) {
1012             List *list = malloc(sizeof(List));
1013             Node *node;
1014
1015             if (DirShowOpt)
1016                 logstd("Scanning %s ...\n", spath);
1017             InitList(list);
1018             if (ScanDir(list, &SrcHost, spath, &CountSourceReadBytes, 0) == 0) {
1019                 node = NULL;
1020                 while ((node = IterateList(list, node, 0)) != NULL) {
1021                     char *nspath;
1022                     char *ndpath = NULL;
1023
1024                     nspath = mprintf("%s/%s", spath, node->no_Name);
1025                     if (dpath)
1026                         ndpath = mprintf("%s/%s", dpath, node->no_Name);
1027
1028                     info->spath = nspath;
1029                     info->dpath = ndpath;
1030                     info->sdevNo = sdevNo;
1031                     info->ddevNo = ddevNo;
1032                     if (depth < 0)
1033                         r += DoCopy(info, node->no_Stat, depth);
1034                     else
1035                         r += DoCopy(info, node->no_Stat, depth + 1);
1036                     free(nspath);
1037                     if (ndpath)
1038                         free(ndpath);
1039                     info->spath = NULL;
1040                     info->dpath = NULL;
1041                 }
1042
1043                 /*
1044                  * Remove files/directories from destination that do not appear
1045                  * in the source.
1046                  */
1047                 if (dpath && ScanDir(list, &DstHost, dpath,
1048                                      &CountTargetReadBytes, 3) == 0) {
1049                     node = NULL;
1050                     while ((node = IterateList(list, node, 3)) != NULL) {
1051                         /*
1052                          * If object does not exist in source or .cpignore
1053                          * then recursively remove it.
1054                          */
1055                         char *ndpath;
1056
1057                         ndpath = mprintf("%s/%s", dpath, node->no_Name);
1058                         RemoveRecur(ndpath, ddevNo, node->no_Stat);
1059                         free(ndpath);
1060                     }
1061                 }
1062             }
1063             ResetList(list);
1064             free(list);
1065         }
1066
1067         if (dpath && st2Valid) {
1068             struct timeval tv[2];
1069
1070             if (ForceOpt || !OwnerMatch(stat1, &st2))
1071                 hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
1072             if (stat1->st_mode != st2.st_mode)
1073                 hc_chmod(&DstHost, dpath, stat1->st_mode);
1074 #ifdef _ST_FLAGS_PRESENT_
1075             if (!FlagsMatch(stat1, &st2))
1076                 hc_chflags(&DstHost, dpath, stat1->st_flags);
1077 #endif
1078             if (ForceOpt || mtimecmp(stat1, &st2) != 0) {
1079                 bzero(tv, sizeof(tv));
1080                 tv[0].tv_sec = stat1->st_mtime;
1081                 tv[1].tv_sec = stat1->st_mtime;
1082 #if defined(st_mtime)  /* A macro, so very likely on modern POSIX */
1083                 tv[0].tv_usec = stat1->st_mtim.tv_nsec / 1000;
1084                 tv[1].tv_usec = stat1->st_mtim.tv_nsec / 1000;
1085 #endif
1086                 hc_utimes(&DstHost, dpath, tv);
1087             }
1088         }
1089     } else if (dpath == NULL) {
1090         /*
1091          * If dpath is NULL, we are just updating the MD5
1092          */
1093 #ifndef NOMD5
1094         if (UseMD5Opt && S_ISREG(stat1->st_mode)) {
1095             mres = md5_check(spath, NULL);
1096
1097             if (VerboseOpt > 1) {
1098                 if (mres < 0)
1099                     logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1100                 else
1101                     logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
1102             } else if (!QuietOpt && mres < 0) {
1103                 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1104             }
1105         }
1106 #endif
1107     } else if (S_ISREG(stat1->st_mode)) {
1108         char *path;
1109         char *hpath;
1110         int fd1;
1111         int fd2;
1112
1113         if (st2Valid)
1114                 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1115         else
1116                 path = mprintf("%s", dpath);
1117
1118         /*
1119          * Handle check failure message.
1120          */
1121 #ifndef NOMD5
1122         if (mres < 0)
1123             logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1124         else
1125 #endif
1126         if (fres < 0)
1127             logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1128
1129         /*
1130          * Not quite ready to do the copy yet.  If UseHLPath is defined,
1131          * see if we can hardlink instead.
1132          *
1133          * If we can hardlink, and the target exists, we have to remove it
1134          * first or the hardlink will fail.  This can occur in a number of
1135          * situations but most typically when the '-f -H' combination is
1136          * used.
1137          */
1138         if (UseHLPath && (hpath = checkHLPath(stat1, spath, dpath)) != NULL) {
1139                 if (st2Valid)
1140                         xremove(&DstHost, dpath);
1141                 if (hc_link(&DstHost, hpath, dpath) == 0) {
1142                         ++CountLinkedItems;
1143                         if (VerboseOpt) {
1144                             logstd("%-32s hardlinked(-H)\n",
1145                                    (dpath ? dpath : spath));
1146                         }
1147                         free(hpath);
1148                         goto skip_copy;
1149                 }
1150                 /*
1151                  * Shucks, we may have hit a filesystem hard linking limit,
1152                  * we have to copy instead.
1153                  */
1154                 free(hpath);
1155         }
1156
1157         if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1158             if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1159                 /*
1160                  * There could be a .tmp file from a previously interrupted
1161                  * run, delete and retry.  Fail if we still can't get at it.
1162                  */
1163 #ifdef _ST_FLAGS_PRESENT_
1164                 hc_chflags(&DstHost, path, 0);
1165 #endif
1166                 hc_remove(&DstHost, path);
1167                 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1168             }
1169             if (fd2 >= 0) {
1170                 const char *op;
1171                 char *iobuf1 = malloc(GETIOSIZE);
1172                 int n;
1173
1174                 /*
1175                  * Matt: What about holes?
1176                  */
1177                 op = "read";
1178                 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1179                     op = "write";
1180                     if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1181                         break;
1182                     op = "read";
1183                 }
1184                 hc_close(&DstHost, fd2);
1185                 if (n == 0) {
1186                     struct timeval tv[2];
1187
1188                     bzero(tv, sizeof(tv));
1189                     tv[0].tv_sec = stat1->st_mtime;
1190                     tv[1].tv_sec = stat1->st_mtime;
1191 #if defined(st_mtime)
1192                     tv[0].tv_usec = stat1->st_mtim.tv_nsec / 1000;
1193                     tv[1].tv_usec = stat1->st_mtim.tv_nsec / 1000;
1194 #endif
1195
1196                     if (DstRootPrivs || ChgrpAllowed(stat1->st_gid))
1197                         hc_chown(&DstHost, path, stat1->st_uid, stat1->st_gid);
1198                     hc_chmod(&DstHost, path, stat1->st_mode);
1199 #ifdef _ST_FLAGS_PRESENT_
1200                     if (stat1->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
1201                         hc_utimes(&DstHost, path, tv);
1202 #else
1203                     hc_utimes(&DstHost, path, tv);
1204 #endif
1205                     if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1206                         logerr("%-32s rename-after-copy failed: %s\n",
1207                             (dpath ? dpath : spath), strerror(errno)
1208                         );
1209                         ++r;
1210                     } else {
1211                         if (VerboseOpt)
1212                             logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1213 #ifdef _ST_FLAGS_PRESENT_
1214                         if (DstRootPrivs ? stat1->st_flags : stat1->st_flags & UF_SETTABLE)
1215                             hc_chflags(&DstHost, dpath, stat1->st_flags);
1216 #endif
1217                     }
1218 #ifdef _ST_FLAGS_PRESENT_
1219                     if ((stat1->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE)) == 0)
1220                         hc_utimes(&DstHost, dpath, tv);
1221 #endif
1222                     CountSourceReadBytes += size;
1223                     CountWriteBytes += size;
1224                     CountSourceBytes += size;
1225                     CountSourceItems++;
1226                     CountCopiedItems++;
1227                 } else {
1228                     logerr("%-32s %s failed: %s\n",
1229                         (dpath ? dpath : spath), op, strerror(errno)
1230                     );
1231                     hc_remove(&DstHost, path);
1232                     ++r;
1233                 }
1234                 free(iobuf1);
1235             } else {
1236                 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1237                     (dpath ? dpath : spath), getuid(), geteuid(),
1238                     strerror(errno)
1239                 );
1240                 ++r;
1241             }
1242             hc_close(&SrcHost, fd1);
1243         } else {
1244             logerr("%-32s copy: open failed: %s\n",
1245                 (dpath ? dpath : spath),
1246                 strerror(errno)
1247             );
1248             ++r;
1249         }
1250 skip_copy:
1251         free(path);
1252
1253         if (hln) {
1254             if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1255                 hltsetdino(hln, st2.st_ino);
1256             } else {
1257                 hltdelete(hln);
1258                 hln = NULL;
1259             }
1260         }
1261     } else if (S_ISLNK(stat1->st_mode)) {
1262         char *link1 = malloc(GETLINKSIZE);
1263         char *link2 = malloc(GETLINKSIZE);
1264         char *path;
1265         int n1;
1266         int n2;
1267
1268         n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1269         if (st2Valid) {
1270                 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1271                 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1272         } else {
1273                 path = mprintf("%s", dpath);
1274                 n2 = -1;
1275         }
1276         if (n1 >= 0) {
1277             if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1278                 hc_umask(&DstHost, ~stat1->st_mode);
1279                 xremove(&DstHost, path);
1280                 link1[n1] = 0;
1281                 if (hc_symlink(&DstHost, link1, path) < 0) {
1282                       logerr("%-32s symlink (%s->%s) failed: %s\n",
1283                           (dpath ? dpath : spath), link1, path,
1284                           strerror(errno)
1285                       );
1286                       ++r;
1287                 } else {
1288                     if (DstRootPrivs || ChgrpAllowed(stat1->st_gid))
1289                         hc_lchown(&DstHost, path, stat1->st_uid, stat1->st_gid);
1290                     /*
1291                      * there is no lchmod() or lchflags(), we
1292                      * cannot chmod or chflags a softlink.
1293                      */
1294                     if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1295                         logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1296                             (dpath ? dpath : spath),
1297                             path, dpath, strerror(errno));
1298                     } else if (VerboseOpt) {
1299                         logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1300                     }
1301                     hc_umask(&DstHost, 000);
1302                     CountWriteBytes += n1;
1303                     CountCopiedItems++;
1304                 }
1305             } else {
1306                 if (VerboseOpt >= 3)
1307                     logstd("%-32s nochange", (dpath ? dpath : spath));
1308                 if (!OwnerMatch(stat1, &st2)) {
1309                     hc_lchown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
1310                     if (VerboseOpt >= 3)
1311                         logstd(" (uid/gid differ)");
1312                 }
1313                 if (VerboseOpt >= 3)
1314                     logstd("\n");
1315             }
1316             CountSourceBytes += n1;
1317             CountSourceReadBytes += n1;
1318             if (n2 > 0)
1319                 CountTargetReadBytes += n2;
1320             CountSourceItems++;
1321         } else {
1322             r = 1;
1323             logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1324         }
1325         free(link1);
1326         free(link2);
1327         free(path);
1328     } else if ((S_ISCHR(stat1->st_mode) || S_ISBLK(stat1->st_mode)) && DeviceOpt) {
1329         char *path = NULL;
1330
1331         if (ForceOpt ||
1332             st2Valid == 0 ||
1333             stat1->st_mode != st2.st_mode ||
1334             stat1->st_rdev != st2.st_rdev ||
1335             !OwnerMatch(stat1, &st2)
1336         ) {
1337             if (st2Valid) {
1338                 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1339                 xremove(&DstHost, path);
1340             } else {
1341                 path = mprintf("%s", dpath);
1342             }
1343
1344             if (hc_mknod(&DstHost, path, stat1->st_mode, stat1->st_rdev) == 0) {
1345                 hc_chmod(&DstHost, path, stat1->st_mode);
1346                 hc_chown(&DstHost, path, stat1->st_uid, stat1->st_gid);
1347                 if (st2Valid)
1348                         xremove(&DstHost, dpath);
1349                 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1350                     logerr("%-32s dev-rename-after-create failed: %s\n",
1351                         (dpath ? dpath : spath),
1352                         strerror(errno)
1353                     );
1354                 } else if (VerboseOpt) {
1355                     logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1356                 }
1357                 CountCopiedItems++;
1358             } else {
1359                 r = 1;
1360                 logerr("%-32s dev failed: %s\n",
1361                     (dpath ? dpath : spath), strerror(errno)
1362                 );
1363             }
1364         } else {
1365             if (VerboseOpt >= 3)
1366                 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1367         }
1368         if (path)
1369                 free(path);
1370         CountSourceItems++;
1371     }
1372 done:
1373     if (hln) {
1374         if (hln->dino == (ino_t)-1) {
1375             hltdelete(hln);
1376             /*hln = NULL; unneeded */
1377         } else {
1378             hltrels(hln);
1379         }
1380     }
1381     return (r);
1382 }
1383
1384 int
1385 ScanDir(List *list, struct HostConf *host, const char *path,
1386         int64_t *CountReadBytes, int n)
1387 {
1388     DIR *dir;
1389     struct HostConf *cphost;
1390     struct HCDirEntry *den;
1391     struct stat *statptr;
1392
1393     if (n == 0) {
1394         /*
1395          * scan .cpignore file for files/directories to ignore
1396          * (only in the source directory, i.e. if n == 0).
1397          */
1398         if (UseCpFile) {
1399             int fd;
1400             int nread;
1401             int bufused;
1402             char *buf = malloc(GETBUFSIZE);
1403             char *nl, *next;
1404             char *fpath;
1405
1406             if (UseCpFile[0] == '/') {
1407                 fpath = mprintf("%s", UseCpFile);
1408                 cphost = NULL;
1409             } else {
1410                 fpath = mprintf("%s/%s", path, UseCpFile);
1411                 AddList(list, strrchr(fpath, '/') + 1, 1, NULL);
1412                 cphost = host;
1413             }
1414             fd = hc_open(cphost, fpath, O_RDONLY, 0);
1415             if (fd >= 0) {
1416                 bufused = 0;
1417                 while ((nread = hc_read(cphost, fd, buf + bufused,
1418                         GETBUFSIZE - bufused - 1)) > 0) {
1419                     *CountReadBytes += nread;
1420                     bufused += nread;
1421                     buf[bufused] = 0;
1422                     for (next = buf; (nl = strchr(next, '\n')); next = nl+1) {
1423                         *nl = 0;
1424                         AddList(list, next, 1, NULL);
1425                     }
1426                     bufused = strlen(next);
1427                     if (bufused)
1428                         bcopy(next, buf, bufused);
1429                 }
1430                 if (bufused) {
1431                     /* last line has no trailing newline */
1432                     buf[bufused] = 0;
1433                     AddList(list, buf, 1, NULL);
1434                 }
1435                 hc_close(cphost, fd);
1436             }
1437             free(fpath);
1438             free(buf);
1439         }
1440
1441         /*
1442          * Automatically exclude MD5CacheFile that we create on the
1443          * source from the copy to the destination.
1444          *
1445          * Automatically exclude a FSMIDCacheFile on the source that
1446          * would otherwise overwrite the one we maintain on the target.
1447          */
1448         if (UseMD5Opt)
1449             AddList(list, MD5CacheFile, 1, NULL);
1450         if (UseFSMIDOpt)
1451             AddList(list, FSMIDCacheFile, 1, NULL);
1452     }
1453
1454     if ((dir = hc_opendir(host, path)) == NULL)
1455         return (1);
1456     while ((den = hc_readdir(host, dir, &statptr)) != NULL) {
1457         /*
1458          * ignore . and ..
1459          */
1460         if (strcmp(den->d_name, ".") != 0 && strcmp(den->d_name, "..") != 0) {
1461              if (UseCpFile && UseCpFile[0] == '/') {
1462                  if (CheckList(list, path, den->d_name) == 0)
1463                         continue;
1464              }
1465              AddList(list, den->d_name, n, statptr);
1466         }
1467     }
1468     hc_closedir(host, dir);
1469
1470     return (0);
1471 }
1472
1473 /*
1474  * RemoveRecur()
1475  */
1476
1477 static void
1478 RemoveRecur(const char *dpath, dev_t devNo, struct stat *dstat)
1479 {
1480     struct stat st;
1481
1482     if (dstat == NULL) {
1483         if (hc_lstat(&DstHost, dpath, &st) == 0)
1484             dstat = &st;
1485     }
1486     if (dstat != NULL) {
1487         if (devNo == (dev_t)-1)
1488             devNo = dstat->st_dev;
1489         if (dstat->st_dev == devNo) {
1490             if (S_ISDIR(dstat->st_mode)) {
1491                 DIR *dir;
1492
1493                 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1494                     List *list = malloc(sizeof(List));
1495                     Node *node = NULL;
1496                     struct HCDirEntry *den;
1497
1498                     InitList(list);
1499                     while ((den = hc_readdir(&DstHost, dir, &dstat)) != NULL) {
1500                         if (strcmp(den->d_name, ".") == 0)
1501                             continue;
1502                         if (strcmp(den->d_name, "..") == 0)
1503                             continue;
1504                         AddList(list, den->d_name, 3, dstat);
1505                     }
1506                     hc_closedir(&DstHost, dir);
1507                     while ((node = IterateList(list, node, 3)) != NULL) {
1508                         char *ndpath;
1509
1510                         ndpath = mprintf("%s/%s", dpath, node->no_Name);
1511                         RemoveRecur(ndpath, devNo, node->no_Stat);
1512                         free(ndpath);
1513                     }
1514                     ResetList(list);
1515                     free(list);
1516                 }
1517                 if (AskConfirmation && NoRemoveOpt == 0) {
1518                     if (YesNo(dpath)) {
1519                         if (xrmdir(&DstHost, dpath) < 0) {
1520                             logerr("%-32s rmdir failed: %s\n",
1521                                 dpath, strerror(errno)
1522                             );
1523                         }
1524                         CountRemovedItems++;
1525                     }
1526                 } else {
1527                     if (NoRemoveOpt) {
1528                         if (VerboseOpt)
1529                             logstd("%-32s not-removed\n", dpath);
1530                     } else if (xrmdir(&DstHost, dpath) == 0) {
1531                         if (VerboseOpt)
1532                             logstd("%-32s rmdir-ok\n", dpath);
1533                         CountRemovedItems++;
1534                     } else {
1535                         logerr("%-32s rmdir failed: %s\n",
1536                             dpath, strerror(errno)
1537                         );
1538                     }
1539                 }
1540             } else {
1541                 if (AskConfirmation && NoRemoveOpt == 0) {
1542                     if (YesNo(dpath)) {
1543                         if (xremove(&DstHost, dpath) < 0) {
1544                             logerr("%-32s remove failed: %s\n",
1545                                 dpath, strerror(errno)
1546                             );
1547                         }
1548                         CountRemovedItems++;
1549                     }
1550                 } else {
1551                     if (NoRemoveOpt) {
1552                         if (VerboseOpt)
1553                             logstd("%-32s not-removed\n", dpath);
1554                     } else if (xremove(&DstHost, dpath) == 0) {
1555                         if (VerboseOpt)
1556                             logstd("%-32s remove-ok\n", dpath);
1557                         CountRemovedItems++;
1558                     } else {
1559                         logerr("%-32s remove failed: %s\n",
1560                             dpath, strerror(errno)
1561                         );
1562                     }
1563                 }
1564             }
1565         }
1566     }
1567 }
1568
1569 static void
1570 InitList(List *list)
1571 {
1572     bzero(list, sizeof(List));
1573     list->li_Node.no_Next = &list->li_Node;
1574 }
1575
1576 static void
1577 ResetList(List *list)
1578 {
1579     Node *node;
1580
1581     while ((node = list->li_Node.no_Next) != &list->li_Node) {
1582         list->li_Node.no_Next = node->no_Next;
1583         if (node->no_Stat != NULL)
1584             free(node->no_Stat);
1585         free(node);
1586     }
1587     InitList(list);
1588 }
1589
1590 static Node *
1591 IterateList(List *list, Node *node, int n)
1592 {
1593     if (node == NULL)
1594         node = list->li_Node.no_Next;
1595     else
1596         node = node->no_Next;
1597     while (node->no_Value != n && node != &list->li_Node)
1598         node = node->no_Next;
1599     return (node == &list->li_Node ? NULL : node);
1600 }
1601
1602 static int
1603 AddList(List *list, const char *name, int n, struct stat *st)
1604 {
1605     Node *node;
1606     int hv;
1607
1608     /*
1609      * Scan against wildcards.  Only a node value of 1 can be a wildcard
1610      * ( usually scanned from .cpignore )
1611      */
1612     for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1613         if (strcmp(name, node->no_Name) == 0 ||
1614             (n != 1 && node->no_Value == 1 &&
1615             fnmatch(node->no_Name, name, 0) == 0)
1616         ) {
1617             return(node->no_Value);
1618         }
1619     }
1620
1621     /*
1622      * Look for exact match
1623      */
1624
1625     hv = shash(name);
1626     for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1627         if (strcmp(name, node->no_Name) == 0) {
1628             return(node->no_Value);
1629         }
1630     }
1631     node = malloc(sizeof(Node) + strlen(name) + 1);
1632     if (node == NULL)
1633         fatal("out of memory");
1634
1635     node->no_Next = list->li_Node.no_Next;
1636     list->li_Node.no_Next = node;
1637
1638     node->no_HNext = list->li_Hash[hv];
1639     list->li_Hash[hv] = node;
1640
1641     strcpy(node->no_Name, name);
1642     node->no_Value = n;
1643     node->no_Stat = st;
1644
1645     return(n);
1646 }
1647
1648 /*
1649  * Match against n=1 (cpignore) entries
1650  *
1651  * Returns 0 on match, non-zero if no match
1652  */
1653 static int
1654 CheckList(List *list, const char *path, const char *name)
1655 {
1656     char *fpath = NULL;
1657     Node *node;
1658     int hv;
1659
1660     if (asprintf(&fpath, "%s/%s", path, name) < 0)
1661         fatal("out of memory");
1662
1663     /*
1664      * Scan against wildcards.  Only a node value of 1 can be a wildcard
1665      * ( usually scanned from .cpignore )
1666      */
1667     for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1668         if (node->no_Value != 1)
1669                 continue;
1670         if (fnmatch(node->no_Name, fpath, 0) == 0) {
1671                 free(fpath);
1672                 return 0;
1673         }
1674     }
1675
1676     /*
1677      * Look for exact match
1678      */
1679     hv = shash(fpath);
1680     for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1681         if (node->no_Value != 1)
1682                 continue;
1683         if (strcmp(fpath, node->no_Name) == 0) {
1684                 free(fpath);
1685                 return 0;
1686         }
1687     }
1688
1689     free(fpath);
1690     return 1;
1691 }
1692
1693 static int
1694 shash(const char *s)
1695 {
1696     int hv;
1697
1698     hv = 0xA4FB3255;
1699
1700     while (*s) {
1701         if (*s == '*' || *s == '?' ||
1702             *s == '{' || *s == '}' ||
1703             *s == '[' || *s == ']' ||
1704             *s == '|'
1705         ) {
1706             return(0);
1707         }
1708         hv = (hv << 5) ^ *s ^ (hv >> 23);
1709         ++s;
1710     }
1711     return(((hv >> 16) ^ hv) & HMASK);
1712 }
1713
1714 static int
1715 YesNo(const char *path)
1716 {
1717     int ch, first;
1718
1719     fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1720     fflush(stderr);
1721
1722     first = ch = getchar();
1723     while (ch != '\n' && ch != EOF)
1724         ch = getchar();
1725     return ((first == 'y' || first == 'Y'));
1726 }
1727
1728 /*
1729  * xrename() - rename with override
1730  *
1731  *      If the rename fails, attempt to override st_flags on the
1732  *      destination and rename again.  If that fails too, try to
1733  *      set the flags back the way they were and give up.
1734  */
1735
1736 static int
1737 xrename(const char *src, const char *dst, u_long flags)
1738 {
1739     int r;
1740
1741     if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1742 #ifdef _ST_FLAGS_PRESENT_
1743         hc_chflags(&DstHost, dst, 0);
1744         if ((r = hc_rename(&DstHost, src, dst)) < 0)
1745                 hc_chflags(&DstHost, dst, flags);
1746 #endif
1747     }
1748     return(r);
1749 }
1750
1751 static int
1752 xlink(const char *src, const char *dst, u_long flags)
1753 {
1754     int r;
1755 #ifdef _ST_FLAGS_PRESENT_
1756     int e;
1757 #endif
1758
1759     if ((r = hc_link(&DstHost, src, dst)) < 0) {
1760 #ifdef _ST_FLAGS_PRESENT_
1761         hc_chflags(&DstHost, src, 0);
1762         r = hc_link(&DstHost, src, dst);
1763         e = errno;
1764         hc_chflags(&DstHost, src, flags);
1765         errno = e;
1766 #endif
1767     }
1768     if (r == 0)
1769             ++CountLinkedItems;
1770     return(r);
1771 }
1772
1773 static int
1774 xremove(struct HostConf *host, const char *path)
1775 {
1776     int res;
1777
1778     res = hc_remove(host, path);
1779 #ifdef _ST_FLAGS_PRESENT_
1780     if (res == -EPERM) {
1781         hc_chflags(host, path, 0);
1782         res = hc_remove(host, path);
1783     }
1784 #endif
1785     return(res);
1786 }
1787
1788 static int
1789 xrmdir(struct HostConf *host, const char *path)
1790 {
1791     int res;
1792
1793     res = hc_rmdir(host, path);
1794 #ifdef _ST_FLAGS_PRESENT_
1795     if (res == -EPERM) {
1796         hc_chflags(host, path, 0);
1797         res = hc_rmdir(host, path);
1798     }
1799 #endif
1800     return(res);
1801 }
1802
1803 /*
1804  * Compare mtimes.  By default cpdup only compares the seconds field
1805  * because different operating systems and filesystems will store time
1806  * fields with varying amounts of precision.
1807  *
1808  * This subroutine can be adjusted to also compare to microseconds or
1809  * nanoseconds precision.  However, since cpdup() uses utimes() to
1810  * set a file's timestamp and utimes() only takes timeval's (usec precision),
1811  * I strongly recommend only comparing down to usec precision at best.
1812  */
1813 static int
1814 mtimecmp(struct stat *st1, struct stat *st2)
1815 {
1816         if (st1->st_mtime < st2->st_mtime)
1817                 return -1;
1818         if (st1->st_mtime == st2->st_mtime)
1819                 return 0;
1820         return 1;
1821 }