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