Add parallel transaction support for remote source or target specifications.
[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.20 2008/04/10 22:09:08 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 #ifndef _ST_FLAGS_PRESENT_
68 #define st_flags        st_mode
69 #endif
70
71 typedef struct Node {
72     struct Node *no_Next;
73     struct Node *no_HNext;
74     int  no_Value;
75     char no_Name[4];
76 } Node;
77
78 typedef struct List {
79     Node        li_Node;
80     Node        *li_Hash[HSIZE];
81 } List;
82
83 struct hlink {
84     ino_t ino;
85     ino_t dino;
86     struct hlink *next;
87     struct hlink *prev;
88     nlink_t nlinked;
89     char name[0];
90 };
91
92 typedef struct copy_info {
93         char *spath;
94         char *dpath;
95         dev_t sdevNo;
96         dev_t ddevNo;
97 #ifdef USE_PTHREADS
98         struct copy_info *parent;
99         pthread_cond_t cond;
100         int children;
101         int r;
102 #endif
103 } *copy_info_t;
104
105 struct hlink *hltable[HLSIZE];
106
107 void RemoveRecur(const char *dpath, dev_t devNo);
108 void InitList(List *list);
109 void ResetList(List *list);
110 int AddList(List *list, const char *name, int n);
111 static struct hlink *hltlookup(struct stat *);
112 static struct hlink *hltadd(struct stat *, const char *);
113 static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
114 static int validate_check(const char *spath, const char *dpath);
115 static int shash(const char *s);
116 static void hltdelete(struct hlink *);
117 int YesNo(const char *path);
118 static int xrename(const char *src, const char *dst, u_long flags);
119 static int xlink(const char *src, const char *dst, u_long flags);
120 int WildCmp(const char *s1, const char *s2);
121 static int DoCopy(copy_info_t info);
122
123 int AskConfirmation = 1;
124 int SafetyOpt = 1;
125 int ForceOpt;
126 int DeviceOpt = 1;
127 int VerboseOpt;
128 int QuietOpt;
129 int NoRemoveOpt;
130 int UseMD5Opt;
131 int UseFSMIDOpt;
132 int SummaryOpt;
133 int SlaveOpt;
134 int EnableDirectoryRetries;
135 int DstBaseLen;
136 int ValidateOpt;
137 int CurParallel;
138 int MaxParallel = -1;
139 char IOBuf1[65536];
140 char IOBuf2[65536];
141 const char *UseCpFile;
142 const char *UseHLPath;
143 const char *MD5CacheFile;
144 const char *FSMIDCacheFile;
145
146 int64_t CountSourceBytes;
147 int64_t CountSourceItems;
148 int64_t CountCopiedItems;
149 int64_t CountSourceReadBytes;
150 int64_t CountTargetReadBytes;
151 int64_t CountWriteBytes;
152 int64_t CountRemovedItems;
153 int64_t CountLinkedItems;
154
155 struct HostConf SrcHost;
156 struct HostConf DstHost;
157
158 #if USE_PTHREADS
159 pthread_mutex_t MasterMutex;
160 #endif
161
162 int
163 main(int ac, char **av)
164 {
165     int i;
166     char *src = NULL;
167     char *dst = NULL;
168     char *ptr;
169     struct timeval start;
170     struct copy_info info;
171
172     signal(SIGPIPE, SIG_IGN);
173
174 #if USE_PTHREADS
175     pthread_mutex_init(&SrcHost.read_mutex, NULL);
176     pthread_mutex_init(&DstHost.read_mutex, NULL);
177     pthread_mutex_init(&MasterMutex, NULL);
178     pthread_mutex_lock(&MasterMutex);
179 #endif
180
181     gettimeofday(&start, NULL);
182     for (i = 1; i < ac; ++i) {
183         int v = 1;
184
185         ptr = av[i];
186         if (*ptr != '-') { 
187             if (src == NULL) {
188                 src = ptr;
189             } else if (dst == NULL) {
190                 dst = ptr;
191             } else {
192                 fatal("too many arguments");
193                 /* not reached */
194             }
195             continue;
196         }
197         ptr += 2;
198
199         if (*ptr)
200             v = strtol(ptr, NULL, 0);
201
202         switch(ptr[-1]) {
203         case 'v':
204             VerboseOpt = 1;
205             while (*ptr == 'v') {
206                 ++VerboseOpt;
207                 ++ptr;
208             }
209             if (*ptr >= '0' && *ptr <= '9')
210                 VerboseOpt = strtol(ptr, NULL, 0);
211             break;
212         case 'l':
213             setlinebuf(stdout);
214             setlinebuf(stderr);
215             break;
216         case 'V':
217             ValidateOpt = v;
218             break;
219         case 'I':
220             SummaryOpt = v;
221             break;
222         case 'o':
223             NoRemoveOpt = v;
224             break;
225         case 'x':
226             UseCpFile = ".cpignore";
227             break;
228         case 'X':
229             UseCpFile = (*ptr) ? ptr : av[++i];
230             break;
231         case 'H':
232             UseHLPath = (*ptr) ? ptr : av[++i];
233             break;
234         case 'S':
235             SlaveOpt = v;
236             break;
237         case 'f':
238             ForceOpt = v;
239             break;
240         case 'i':
241             AskConfirmation = v;
242             break;
243         case 'j':
244             DeviceOpt = v;
245             break;
246         case 'p':
247             MaxParallel = v;
248             break;
249         case 's':
250             SafetyOpt = v;
251             break;
252         case 'q':
253             QuietOpt = v;
254             break;
255         case 'k':
256             UseFSMIDOpt = v;
257             FSMIDCacheFile = ".FSMID.CHECK";
258             break;
259         case 'K':
260             UseFSMIDOpt = v;
261             FSMIDCacheFile = av[++i];
262             break;
263         case 'M':
264             UseMD5Opt = v;
265             MD5CacheFile = av[++i];
266             break;
267         case 'm':
268             UseMD5Opt = v;
269             MD5CacheFile = ".MD5.CHECKSUMS";
270             break;
271         case 'u':
272             setvbuf(stdout, NULL, _IOLBF, 0);
273             break;
274         default:
275             fatal("illegal option: %s\n", ptr - 2);
276             /* not reached */
277             break;
278         }
279     }
280
281     /*
282      * If we are told to go into slave mode, run the HC protocol
283      */
284     if (SlaveOpt) {
285         hc_slave(0, 1);
286         exit(0);
287     }
288
289     /*
290      * Extract the source and/or/neither target [user@]host and
291      * make any required connections.
292      */
293     if (src && (ptr = strchr(src, ':')) != NULL) {
294         asprintf(&SrcHost.host, "%*.*s", ptr - src, ptr - src, src);
295         src = ptr + 1;
296         if (UseCpFile) {
297             fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
298             exit(1);
299         }
300         if (UseMD5Opt) {
301             fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
302             exit(1);
303         }
304         if (hc_connect(&SrcHost) < 0)
305             fprintf(stderr, "Unable to connect to %s\n", SrcHost.host);
306     }
307     if (dst && (ptr = strchr(dst, ':')) != NULL) {
308         asprintf(&DstHost.host, "%*.*s", ptr - dst, ptr - dst, dst);
309         dst = ptr + 1;
310         if (UseFSMIDOpt) {
311             fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
312             exit(1);
313         }
314         if (hc_connect(&DstHost) < 0)
315             fprintf(stderr, "Unable to connect to %s\n", DstHost.host);
316     }
317
318     /*
319      * dst may be NULL only if -m option is specified,
320      * which forces an update of the MD5 checksums
321      */
322     if (dst == NULL && UseMD5Opt == 0) {
323         fatal(NULL);
324         /* not reached */
325     }
326 #if USE_PTHREADS
327     info.r = 0;
328     info.children = 0;
329     pthread_cond_init(&info.cond, NULL);
330 #endif
331     if (dst) {
332         DstBaseLen = strlen(dst);
333         info.spath = src;
334         info.dpath = dst;
335         info.sdevNo = (dev_t)-1;
336         info.ddevNo = (dev_t)-1;
337         i = DoCopy(&info);
338     } else {
339         info.spath = src;
340         info.dpath = NULL;
341         info.sdevNo = (dev_t)-1;
342         info.ddevNo = (dev_t)-1;
343         i = DoCopy(&info);
344     }
345 #if USE_PTHREADS
346     pthread_cond_destroy(&info.cond);
347 #endif
348 #ifndef NOMD5
349     md5_flush();
350 #endif
351     fsmid_flush();
352
353     if (SummaryOpt && i == 0) {
354         long duration;
355         struct timeval end;
356
357         gettimeofday(&end, NULL);
358 #if 0
359         /* don't count stat's in our byte statistics */
360         CountSourceBytes += sizeof(struct stat) * CountSourceItems;
361         CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
362         CountWriteBytes +=  sizeof(struct stat) * CountCopiedItems;
363         CountWriteBytes +=  sizeof(struct stat) * CountRemovedItems;
364 #endif
365
366         duration = end.tv_sec - start.tv_sec;
367         duration *= 1000000;
368         duration += end.tv_usec - start.tv_usec;
369         if (duration == 0) duration = 1;
370         logstd("cpdup completed successfully\n");
371         logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
372                "%lld bytes written (%.1fX speedup)\n",
373             (long long)CountSourceBytes,
374             (long long)CountSourceReadBytes,
375             (long long)CountTargetReadBytes,
376             (long long)CountWriteBytes,
377             ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
378         logstd("%lld source items, %lld items copied, %lld items linked, "
379                "%lld things deleted\n",
380             (long long)CountSourceItems,
381             (long long)CountCopiedItems,
382             (long long)CountLinkedItems,
383             (long long)CountRemovedItems);
384         logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
385             (float)duration / (float)1000000,
386             (long)((long)1000000 * (CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration  / 1024.0),
387             (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
388     }
389     exit((i == 0) ? 0 : 1);
390 }
391
392 static struct hlink *
393 hltlookup(struct stat *stp)
394 {
395     struct hlink *hl;
396     int n;
397
398     n = stp->st_ino & HLMASK;
399
400     for (hl = hltable[n]; hl; hl = hl->next)
401         if (hl->ino == stp->st_ino)
402               return hl;
403
404     return NULL;
405 }
406
407 static struct hlink *
408 hltadd(struct stat *stp, const char *path)
409 {
410     struct hlink *new;
411     int plen = strlen(path);
412     int n;
413
414     new = malloc(offsetof(struct hlink, name[plen + 1]));
415     if (new == NULL) {
416         fprintf(stderr, "out of memory\n");
417         exit(EXIT_FAILURE);
418     }
419
420     /* initialize and link the new element into the table */
421     new->ino = stp->st_ino;
422     new->dino = 0;
423     bcopy(path, new->name, plen + 1);
424     new->nlinked = 1;
425     new->prev = NULL;
426     n = stp->st_ino & HLMASK;
427     new->next = hltable[n];
428     if (hltable[n])
429         hltable[n]->prev = new;
430     hltable[n] = new;
431
432     return new;
433 }
434
435 static void
436 hltdelete(struct hlink *hl)
437 {
438     if (hl->prev) {
439         if (hl->next)
440             hl->next->prev = hl->prev;
441         hl->prev->next = hl->next;
442     } else {
443         if (hl->next)
444             hl->next->prev = NULL;
445
446         hltable[hl->ino & HLMASK] = hl->next;
447     }
448
449     free(hl);
450 }
451
452 /*
453  * If UseHLPath is defined check to see if the file in question is
454  * the same as the source file, and if it is return a pointer to the
455  * -H path based file for hardlinking.  Else return NULL.
456  */
457 static char *
458 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
459 {
460     struct stat sthl;
461     char *hpath;
462     int error;
463
464     asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
465
466     /*
467      * stat info matches ?
468      */
469     if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
470         st1->st_size != sthl.st_size ||
471         st1->st_uid != sthl.st_uid ||
472         st1->st_gid != sthl.st_gid ||
473         st1->st_mtime != sthl.st_mtime
474     ) {
475         free(hpath);
476         return(NULL);
477     }
478
479     /*
480      * If ForceOpt or ValidateOpt is set we have to compare the files
481      */
482     if (ForceOpt || ValidateOpt) {
483         error = validate_check(spath, hpath);
484         if (error) {
485             free(hpath);
486             hpath = NULL;
487         }
488     }
489     return(hpath);
490 }
491
492 /*
493  * Return 0 if the contents of the file <spath> matches the contents of
494  * the file <dpath>.
495  */
496 static int
497 validate_check(const char *spath, const char *dpath)
498 {
499     int error;
500     int fd1;
501     int fd2;
502
503     fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
504     fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
505     error = -1;
506
507     if (fd1 >= 0 && fd2 >= 0) {
508         int n;
509         int x;
510
511         while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
512             CountSourceReadBytes += n;
513             x = hc_read(&DstHost, fd2, IOBuf2, sizeof(IOBuf2));
514             if (x > 0)
515                     CountTargetReadBytes += x;
516             if (x != n)
517                 break;
518             if (bcmp(IOBuf1, IOBuf2, n) != 0)
519                 break;
520         }
521         if (n == 0)
522             error = 0;
523     }
524     if (fd1 >= 0)
525         hc_close(&SrcHost, fd1);
526     if (fd2 >= 0)
527         hc_close(&DstHost, fd2);
528     return (error);
529 }
530 #if USE_PTHREADS
531
532 static void *
533 DoCopyThread(void *arg)
534 {
535     copy_info_t cinfo = arg;
536     char *spath = cinfo->spath;
537     char *dpath = cinfo->dpath;
538  
539     pthread_cond_init(&cinfo->cond, NULL);
540     pthread_mutex_lock(&MasterMutex);
541     cinfo->r += DoCopy(cinfo);
542     /* cinfo arguments invalid on return */
543     --cinfo->parent->children;
544     --CurParallel;
545     pthread_cond_signal(&cinfo->parent->cond);
546     pthread_mutex_unlock(&MasterMutex);
547     free(spath);
548     if (dpath)
549         free(dpath);
550     pthread_cond_destroy(&cinfo->cond);
551     free(cinfo);
552     return(NULL);
553 }
554
555 #endif
556
557 int
558 DoCopy(copy_info_t info)
559 {
560     const char *spath = info->spath;
561     const char *dpath = info->dpath;
562     dev_t sdevNo = info->sdevNo;
563     dev_t ddevNo = info->ddevNo;
564     struct stat st1;
565     struct stat st2;
566     int r, mres, fres, st2Valid;
567     struct hlink *hln;
568     List *list = malloc(sizeof(List));
569     u_int64_t size;
570
571     InitList(list);
572     r = mres = fres = st2Valid = 0;
573     size = 0;
574     hln = NULL;
575
576     if (hc_lstat(&SrcHost, spath, &st1) != 0) {
577         r = 0;
578         goto done;
579     }
580     st2.st_mode = 0;    /* in case lstat fails */
581     st2.st_flags = 0;   /* in case lstat fails */
582     if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0)
583         st2Valid = 1;
584
585     if (S_ISREG(st1.st_mode)) {
586         size = st1.st_size;
587     }
588
589     /*
590      * Handle hardlinks
591      */
592
593     if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
594         if ((hln = hltlookup(&st1)) != NULL) {
595             hln->nlinked++;
596
597             if (st2Valid) {
598                 if (st2.st_ino == hln->dino) {
599                     /*
600                      * hard link is already correct, nothing to do
601                      */
602                     if (VerboseOpt >= 3)
603                         logstd("%-32s nochange\n", (dpath) ? dpath : spath);
604                     if (hln->nlinked == st1.st_nlink)
605                         hltdelete(hln);
606                     CountSourceItems++;
607                     r = 0;
608                     goto done;
609                 } else {
610                     /*
611                      * hard link is not correct, attempt to unlink it
612                      */
613                     if (hc_remove(&DstHost, dpath) < 0) {
614                         logerr("%-32s hardlink: unable to unlink: %s\n", 
615                             ((dpath) ? dpath : spath), strerror(errno));
616                         hltdelete(hln);
617                         return (r + 1);
618                     }
619                 }
620             }
621
622             if (xlink(hln->name, dpath, st1.st_flags) < 0) {
623                 int tryrelink = (errno == EMLINK);
624                 logerr("%-32s hardlink: unable to link to %s: %s\n",
625                     (dpath ? dpath : spath), hln->name, strerror(errno)
626                 );
627                 hltdelete(hln);
628                 hln = NULL;
629                 if (tryrelink) {
630                     logerr("%-20s hardlink: will attempt to copy normally\n");
631                     goto relink;
632                 }
633                 ++r;
634             } else {
635                 if (hln->nlinked == st1.st_nlink) {
636                     hltdelete(hln);
637                     hln = NULL;
638                 }
639                 if (r == 0) {
640                     if (VerboseOpt) {
641                         logstd("%-32s hardlink: %s\n", 
642                             (dpath ? dpath : spath),
643                             (st2Valid ? "relinked" : "linked")
644                         );
645                     }
646                     CountSourceItems++;
647                     CountCopiedItems++;
648                     r = 0;
649                     goto done;
650                 }
651             }
652         } else {
653             /*
654              * first instance of hardlink must be copied normally
655              */
656 relink:
657             hln = hltadd(&st1, dpath);
658         }
659     }
660
661     /*
662      * Do we need to copy the file/dir/link/whatever?  Early termination
663      * if we do not.  Always redo links.  Directories are always traversed
664      * except when the FSMID options are used.
665      *
666      * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
667      */
668
669     if (
670         st2Valid
671         && st1.st_mode == st2.st_mode
672 #ifdef _ST_FLAGS_PRESENT_
673         && st1.st_flags == st2.st_flags
674 #endif
675     ) {
676         if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
677             /*
678              * If FSMID tracking is turned on we can avoid recursing through
679              * an entire directory subtree if the FSMID matches.
680              */
681 #ifdef _ST_FSMID_PRESENT_
682             if (ForceOpt == 0 &&
683                 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
684             ) {
685                 if (VerboseOpt >= 3) {
686                     if (UseFSMIDOpt)
687                         logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
688                     else
689                         logstd("%-32s nochange\n", (dpath ? dpath : spath));
690                 }
691                 r = 0;
692                 goto done;
693             }
694 #endif
695         } else {
696             if (ForceOpt == 0 &&
697                 st1.st_size == st2.st_size &&
698                 st1.st_uid == st2.st_uid &&
699                 st1.st_gid == st2.st_gid &&
700                 st1.st_mtime == st2.st_mtime
701 #ifndef NOMD5
702                 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
703                     (mres = md5_check(spath, dpath)) == 0)
704 #endif
705 #ifdef _ST_FSMID_PRESENT_
706                 && (UseFSMIDOpt == 0 ||
707                     (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
708 #endif
709                 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
710                     validate_check(spath, dpath) == 0)
711             ) {
712                 if (hln)
713                     hln->dino = st2.st_ino;
714                 if (VerboseOpt >= 3) {
715 #ifndef NOMD5
716                     if (UseMD5Opt)
717                         logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
718                     else
719 #endif
720                     if (UseFSMIDOpt)
721                         logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
722                     else if (ValidateOpt)
723                         logstd("%-32s nochange (contents validated)\n", (dpath ? dpath : spath));
724                     else
725                         logstd("%-32s nochange\n", (dpath ? dpath : spath));
726                 }
727                 CountSourceBytes += size;
728                 CountSourceItems++;
729                 r = 0;
730                 goto done;
731             }
732         }
733     }
734     if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
735         if (SafetyOpt) {
736             logerr("%-32s SAFETY - refusing to copy file over directory\n",
737                 (dpath ? dpath : spath)
738             );
739             ++r;                /* XXX */
740             r = 0;
741             goto done;          /* continue with the cpdup anyway */
742         }
743         if (QuietOpt == 0 || AskConfirmation) {
744             logstd("%-32s WARNING: non-directory source will blow away\n"
745                    "%-32s preexisting dest directory, continuing anyway!\n",
746                    ((dpath) ? dpath : spath), "");
747         }
748         if (dpath)
749             RemoveRecur(dpath, ddevNo);
750     }
751
752     /*
753      * The various comparisons failed, copy it.
754      */
755     if (S_ISDIR(st1.st_mode)) {
756         DIR *dir;
757
758         if (fres < 0)
759             logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
760         if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
761             struct dirent *den;
762             int noLoop = 0;
763
764             if (dpath) {
765                 if (S_ISDIR(st2.st_mode) == 0) {
766                     hc_remove(&DstHost, dpath);
767                     if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
768                         logerr("%s: mkdir failed: %s\n", 
769                             (dpath ? dpath : spath), strerror(errno));
770                         r = 1;
771                         noLoop = 1;
772                     }
773                     /*
774                      * Matt: why don't you check error codes here?
775                      */
776                     hc_lstat(&DstHost, dpath, &st2);
777                     hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
778                     CountCopiedItems++;
779                 } else {
780                     /*
781                      * Directory must be scanable by root for cpdup to
782                      * work.  We'll fix it later if the directory isn't
783                      * supposed to be readable ( which is why we fixup
784                      * st2.st_mode to match what we did ).
785                      */
786                     if ((st2.st_mode & 0700) != 0700) {
787                         hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
788                         st2.st_mode |= 0700;
789                     }
790                     if (VerboseOpt >= 2)
791                         logstd("%s\n", dpath ? dpath : spath);
792                 }
793             }
794
795             if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
796                 noLoop = 1;
797             } else {
798                 sdevNo = st1.st_dev;
799             }
800
801             if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
802                 noLoop = 1;
803             } else {
804                 ddevNo = st2.st_dev;
805             }
806
807             /*
808              * scan .cpignore file for files/directories 
809              * to ignore.
810              */
811
812             if (UseCpFile) {
813                 FILE *fi;
814                 char buf[8192];
815                 char *fpath;
816
817                 if (UseCpFile[0] == '/') {
818                     fpath = mprintf("%s", UseCpFile);
819                 } else {
820                     fpath = mprintf("%s/%s", spath, UseCpFile);
821                 }
822                 AddList(list, strrchr(fpath, '/') + 1, 1);
823                 if ((fi = fopen(fpath, "r")) != NULL) {
824                     while (fgets(buf, sizeof(buf), fi) != NULL) {
825                         int l = strlen(buf);
826                         CountSourceReadBytes += l;
827                         if (l && buf[l-1] == '\n')
828                             buf[--l] = 0;
829                         if (buf[0] && buf[0] != '#')
830                             AddList(list, buf, 1);
831                     }
832                     fclose(fi);
833                 }
834                 free(fpath);
835             }
836
837             /*
838              * Automatically exclude MD5CacheFile that we create on the
839              * source from the copy to the destination.
840              *
841              * Automatically exclude a FSMIDCacheFile on the source that
842              * would otherwise overwrite the one we maintain on the target.
843              */
844             if (UseMD5Opt)
845                 AddList(list, MD5CacheFile, 1);
846             if (UseFSMIDOpt)
847                 AddList(list, FSMIDCacheFile, 1);
848
849             while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
850                 /*
851                  * ignore . and ..
852                  */
853                 char *nspath;
854                 char *ndpath = NULL;
855
856                 if (strcmp(den->d_name, ".") == 0 ||
857                     strcmp(den->d_name, "..") == 0
858                 ) {
859                     continue;
860                 }
861                 /*
862                  * ignore if on .cpignore list
863                  */
864                 if (AddList(list, den->d_name, 0) == 1) {
865                     continue;
866                 }
867                 nspath = mprintf("%s/%s", spath, den->d_name);
868                 if (dpath)
869                     ndpath = mprintf("%s/%s", dpath, den->d_name);
870
871 #if USE_PTHREADS
872                 if (CurParallel < MaxParallel) {
873                     copy_info_t cinfo = malloc(sizeof(*cinfo));
874                     pthread_t dummy_thr = NULL;
875
876                     bzero(cinfo, sizeof(*cinfo));
877                     cinfo->spath = nspath;
878                     cinfo->dpath = ndpath;
879                     cinfo->sdevNo = sdevNo;
880                     cinfo->ddevNo = ddevNo;
881                     cinfo->parent = info;
882                     ++CurParallel;
883                     ++info->children;
884                     pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
885                 } else
886 #endif
887                 {
888                     info->spath = nspath;
889                     info->dpath = ndpath;
890                     info->sdevNo = sdevNo;
891                     info->ddevNo = ddevNo;
892                     r += DoCopy(info);
893                     free(nspath);
894                     if (ndpath)
895                         free(ndpath);
896                 }
897             }
898
899             hc_closedir(&SrcHost, dir);
900
901 #if USE_PTHREADS
902             /*
903              * Wait for our children to finish
904              */
905             while (info->children) {
906                 pthread_cond_wait(&info->cond, &MasterMutex);
907             }
908             r += info->r;
909             info->r = 0;
910 #endif
911
912             /*
913              * Remove files/directories from destination that do not appear
914              * in the source.
915              */
916             if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
917                 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
918                     /*
919                      * ignore . or ..
920                      */
921                     if (strcmp(den->d_name, ".") == 0 ||
922                         strcmp(den->d_name, "..") == 0
923                     ) {
924                         continue;
925                     }
926                     /*
927                      * If object does not exist in source or .cpignore
928                      * then recursively remove it.
929                      */
930                     if (AddList(list, den->d_name, 3) == 3) {
931                         char *ndpath;
932
933                         ndpath = mprintf("%s/%s", dpath, den->d_name);
934                         RemoveRecur(ndpath, ddevNo);
935                         free(ndpath);
936                     }
937                 }
938                 hc_closedir(&DstHost, dir);
939             }
940
941             if (dpath) {
942                 struct timeval tv[2];
943
944                 if (ForceOpt ||
945                     st2Valid == 0 || 
946                     st1.st_uid != st2.st_uid ||
947                     st1.st_gid != st2.st_gid
948                 ) {
949                     hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
950                 }
951                 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
952                     hc_chmod(&DstHost, dpath, st1.st_mode);
953                 }
954 #ifdef _ST_FLAGS_PRESENT_
955                 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
956                     hc_chflags(&DstHost, dpath, st1.st_flags);
957                 }
958 #endif
959                 if (ForceOpt ||
960                     st2Valid == 0 ||
961                     st1.st_mtime != st2.st_mtime
962                 ) {
963                     bzero(tv, sizeof(tv));
964                     tv[0].tv_sec = st1.st_mtime;
965                     tv[1].tv_sec = st1.st_mtime;
966                     hc_utimes(&DstHost, dpath, tv);
967                 }
968             }
969         }
970     } else if (dpath == NULL) {
971         /*
972          * If dpath is NULL, we are just updating the MD5
973          */
974 #ifndef NOMD5
975         if (UseMD5Opt && S_ISREG(st1.st_mode)) {
976             mres = md5_check(spath, NULL);
977
978             if (VerboseOpt > 1) {
979                 if (mres < 0)
980                     logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
981                 else
982                     logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
983             } else if (!QuietOpt && mres < 0) {
984                 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
985             }
986         }
987 #endif
988     } else if (S_ISREG(st1.st_mode)) {
989         char *path;
990         char *hpath;
991         int fd1;
992         int fd2;
993
994         path = mprintf("%s.tmp", dpath);
995
996         /*
997          * Handle check failure message.
998          */
999 #ifndef NOMD5
1000         if (mres < 0)
1001             logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1002         else 
1003 #endif
1004         if (fres < 0)
1005             logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1006
1007         /*
1008          * Not quite ready to do the copy yet.  If UseHLPath is defined,
1009          * see if we can hardlink instead.
1010          *
1011          * If we can hardlink, and the target exists, we have to remove it
1012          * first or the hardlink will fail.  This can occur in a number of
1013          * situations but must typically when the '-f -H' combination is 
1014          * used.
1015          */
1016         if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1017                 if (st2Valid)
1018                         hc_remove(&DstHost, dpath);
1019                 if (hc_link(&DstHost, hpath, dpath) == 0) {
1020                         ++CountLinkedItems;
1021                         if (VerboseOpt) {
1022                             logstd("%-32s hardlinked(-H)\n",
1023                                    (dpath ? dpath : spath));
1024                         }
1025                         free(hpath);
1026                         goto skip_copy;
1027                 }
1028                 /*
1029                  * Shucks, we may have hit a filesystem hard linking limit,
1030                  * we have to copy instead.
1031                  */
1032                 free(hpath);
1033         }
1034
1035         if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1036             if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1037                 /*
1038                  * There could be a .tmp file from a previously interrupted
1039                  * run, delete and retry.  Fail if we still can't get at it.
1040                  */
1041 #ifdef _ST_FLAGS_PRESENT_
1042                 hc_chflags(&DstHost, path, 0);
1043 #endif
1044                 hc_remove(&DstHost, path);
1045                 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1046             }
1047             if (fd2 >= 0) {
1048                 const char *op;
1049                 int n;
1050
1051                 /*
1052                  * Matt: What about holes?
1053                  */
1054                 op = "read";
1055                 while ((n = hc_read(&SrcHost, fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
1056                     op = "write";
1057                     if (hc_write(&DstHost, fd2, IOBuf1, n) != n)
1058                         break;
1059                     op = "read";
1060                 }
1061                 hc_close(&DstHost, fd2);
1062                 if (n == 0) {
1063                     struct timeval tv[2];
1064
1065                     bzero(tv, sizeof(tv));
1066                     tv[0].tv_sec = st1.st_mtime;
1067                     tv[1].tv_sec = st1.st_mtime;
1068
1069                     hc_utimes(&DstHost, path, tv);
1070                     hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1071                     hc_chmod(&DstHost, path, st1.st_mode);
1072                     if (xrename(path, dpath, st2.st_flags) != 0) {
1073                         logerr("%-32s rename-after-copy failed: %s\n",
1074                             (dpath ? dpath : spath), strerror(errno)
1075                         );
1076                         ++r;
1077                     } else {
1078                         if (VerboseOpt)
1079                             logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1080 #ifdef _ST_FLAGS_PRESENT_
1081                         if (st1.st_flags)
1082                             hc_chflags(&DstHost, dpath, st1.st_flags);
1083 #endif
1084                     }
1085                     CountSourceReadBytes += size;
1086                     CountWriteBytes += size;
1087                     CountSourceBytes += size;
1088                     CountSourceItems++;
1089                     CountCopiedItems++;
1090                 } else {
1091                     logerr("%-32s %s failed: %s\n",
1092                         (dpath ? dpath : spath), op, strerror(errno)
1093                     );
1094                     hc_remove(&DstHost, path);
1095                     ++r;
1096                 }
1097             } else {
1098                 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1099                     (dpath ? dpath : spath), getuid(), geteuid(),
1100                     strerror(errno)
1101                 );
1102                 ++r;
1103             }
1104             hc_close(&SrcHost, fd1);
1105         } else {
1106             logerr("%-32s copy: open failed: %s\n",
1107                 (dpath ? dpath : spath),
1108                 strerror(errno)
1109             );
1110             ++r;
1111         }
1112 skip_copy:
1113         free(path);
1114
1115         if (hln) {
1116             if (!r && hc_stat(&DstHost, dpath, &st2) == 0)
1117                 hln->dino = st2.st_ino;
1118             else
1119                 hltdelete(hln);
1120         }
1121     } else if (S_ISLNK(st1.st_mode)) {
1122         char link1[1024];
1123         char link2[1024];
1124         char path[2048];
1125         int n1;
1126         int n2;
1127
1128         snprintf(path, sizeof(path), "%s.tmp", dpath);
1129         n1 = hc_readlink(&SrcHost, spath, link1, sizeof(link1) - 1);
1130         n2 = hc_readlink(&DstHost, dpath, link2, sizeof(link2) - 1);
1131         if (n1 >= 0) {
1132             if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1133                 hc_umask(&DstHost, ~st1.st_mode);
1134                 hc_remove(&DstHost, path);
1135                 link1[n1] = 0;
1136                 if (hc_symlink(&DstHost, link1, path) < 0) {
1137                       logerr("%-32s symlink (%s->%s) failed: %s\n",
1138                           (dpath ? dpath : spath), link1, path,
1139                           strerror(errno)
1140                       );
1141                       ++r;
1142                 } else {
1143                     hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1144                     /*
1145                      * there is no lchmod() or lchflags(), we 
1146                      * cannot chmod or chflags a softlink.
1147                      */
1148                     if (xrename(path, dpath, st2.st_flags) != 0) {
1149                         logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1150                             (dpath ? dpath : spath),
1151                             path, dpath, strerror(errno));
1152                     } else if (VerboseOpt) {
1153                         logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1154                     }
1155                     hc_umask(&DstHost, 000);
1156                     CountWriteBytes += n1;
1157                     CountCopiedItems++;
1158                 }
1159             } else {
1160                 if (VerboseOpt >= 3)
1161                     logstd("%-32s nochange\n", (dpath ? dpath : spath));
1162             }
1163             CountSourceBytes += n1;
1164             CountSourceReadBytes += n1;
1165             if (n2 > 0) 
1166                 CountTargetReadBytes += n2;
1167             CountSourceItems++;
1168         } else {
1169             r = 1;
1170             logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1171         }
1172     } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1173         char path[2048];
1174
1175         if (ForceOpt ||
1176             st2Valid == 0 || 
1177             st1.st_mode != st2.st_mode || 
1178             st1.st_rdev != st2.st_rdev ||
1179             st1.st_uid != st2.st_uid ||
1180             st1.st_gid != st2.st_gid
1181         ) {
1182             snprintf(path, sizeof(path), "%s.tmp", dpath);
1183
1184             hc_remove(&DstHost, path);
1185             if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
1186                 hc_chmod(&DstHost, path, st1.st_mode);
1187                 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1188                 hc_remove(&DstHost, dpath);
1189                 if (xrename(path, dpath, st2.st_flags) != 0) {
1190                     logerr("%-32s dev-rename-after-create failed: %s\n",
1191                         (dpath ? dpath : spath),
1192                         strerror(errno)
1193                     );
1194                 } else if (VerboseOpt) {
1195                     logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1196                 }
1197                 CountCopiedItems++;
1198             } else {
1199                 r = 1;
1200                 logerr("%-32s dev failed: %s\n", 
1201                     (dpath ? dpath : spath), strerror(errno)
1202                 );
1203             }
1204         } else {
1205             if (VerboseOpt >= 3)
1206                 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1207         }
1208         CountSourceItems++;
1209     }
1210 done:
1211     ResetList(list);
1212     free(list);
1213     return (r);
1214 }
1215
1216 /*
1217  * RemoveRecur()
1218  */
1219
1220 void
1221 RemoveRecur(const char *dpath, dev_t devNo)
1222 {
1223     struct stat st;
1224
1225     if (hc_lstat(&DstHost, dpath, &st) == 0) {
1226         if ((int)devNo < 0)
1227             devNo = st.st_dev;
1228         if (st.st_dev == devNo) {
1229             if (S_ISDIR(st.st_mode)) {
1230                 DIR *dir;
1231
1232                 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1233                     struct dirent *den;
1234                     while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1235                         char *ndpath;
1236
1237                         if (strcmp(den->d_name, ".") == 0)
1238                             continue;
1239                         if (strcmp(den->d_name, "..") == 0)
1240                             continue;
1241                         ndpath = mprintf("%s/%s", dpath, den->d_name);
1242                         RemoveRecur(ndpath, devNo);
1243                         free(ndpath);
1244                     }
1245                     hc_closedir(&DstHost, dir);
1246                 }
1247                 if (AskConfirmation && NoRemoveOpt == 0) {
1248                     if (YesNo(dpath)) {
1249                         if (hc_rmdir(&DstHost, dpath) < 0) {
1250                             logerr("%-32s rmdir failed: %s\n",
1251                                 dpath, strerror(errno)
1252                             );
1253                         }
1254                         CountRemovedItems++;
1255                     }
1256                 } else {
1257                     if (NoRemoveOpt) {
1258                         if (VerboseOpt)
1259                             logstd("%-32s not-removed\n", dpath);
1260                     } else if (hc_rmdir(&DstHost, dpath) == 0) {
1261                         if (VerboseOpt)
1262                             logstd("%-32s rmdir-ok\n", dpath);
1263                         CountRemovedItems++;
1264                     } else {
1265                         logerr("%-32s rmdir failed: %s\n",
1266                             dpath, strerror(errno)
1267                         );
1268                     }
1269                 }
1270             } else {
1271                 if (AskConfirmation && NoRemoveOpt == 0) {
1272                     if (YesNo(dpath)) {
1273                         if (hc_remove(&DstHost, dpath) < 0) {
1274                             logerr("%-32s remove failed: %s\n",
1275                                 dpath, strerror(errno)
1276                             );
1277                         }
1278                         CountRemovedItems++;
1279                     }
1280                 } else {
1281                     if (NoRemoveOpt) {
1282                         if (VerboseOpt)
1283                             logstd("%-32s not-removed\n", dpath);
1284                     } else if (hc_remove(&DstHost, dpath) == 0) {
1285                         if (VerboseOpt)
1286                             logstd("%-32s remove-ok\n", dpath);
1287                         CountRemovedItems++;
1288                     } else {
1289                         logerr("%-32s remove failed: %s\n",
1290                             dpath, strerror(errno)
1291                         );
1292                     }
1293                 }
1294             }
1295         }
1296     }
1297 }
1298
1299 void
1300 InitList(List *list)
1301 {
1302     bzero(list, sizeof(List));
1303     list->li_Node.no_Next = &list->li_Node;
1304 }
1305
1306 void 
1307 ResetList(List *list)
1308 {
1309     Node *node;
1310
1311     while ((node = list->li_Node.no_Next) != &list->li_Node) {
1312         list->li_Node.no_Next = node->no_Next;
1313         free(node);
1314     }
1315     InitList(list);
1316 }
1317
1318 int
1319 AddList(List *list, const char *name, int n)
1320 {
1321     Node *node;
1322     int hv;
1323
1324     hv = shash(name);
1325
1326     /*
1327      * Scan against wildcards.  Only a node value of 1 can be a wildcard
1328      * ( usually scanned from .cpignore )
1329      */
1330
1331     for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1332         if (strcmp(name, node->no_Name) == 0 ||
1333             (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1334         ) {
1335             return(node->no_Value);
1336         }
1337     }
1338
1339     /*
1340      * Look for exact match
1341      */
1342
1343     for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1344         if (strcmp(name, node->no_Name) == 0) {
1345             return(node->no_Value);
1346         }
1347     }
1348     node = malloc(sizeof(Node) + strlen(name) + 1);
1349     if (node == NULL) {
1350         fprintf(stderr, "out of memory\n");
1351         exit(EXIT_FAILURE);
1352     }
1353
1354     node->no_Next = list->li_Node.no_Next;
1355     list->li_Node.no_Next = node;
1356
1357     node->no_HNext = list->li_Hash[hv];
1358     list->li_Hash[hv] = node;
1359
1360     strcpy(node->no_Name, name);
1361     node->no_Value = n;
1362
1363     return(n);
1364 }
1365
1366 static int
1367 shash(const char *s)
1368 {
1369     int hv;
1370
1371     hv = 0xA4FB3255;
1372
1373     while (*s) {
1374         if (*s == '*' || *s == '?' || 
1375             *s == '{' || *s == '}' || 
1376             *s == '[' || *s == ']' ||
1377             *s == '|'
1378         ) {
1379             return(0);
1380         }
1381         hv = (hv << 5) ^ *s ^ (hv >> 23);
1382         ++s;
1383     }
1384     return(((hv >> 16) ^ hv) & HMASK);
1385 }
1386
1387 /*
1388  * WildCmp() - compare wild string to sane string
1389  *
1390  *      Return 0 on success, -1 on failure.
1391  */
1392
1393 int
1394 WildCmp(const char *w, const char *s)
1395 {
1396     /*
1397      * skip fixed portion
1398      */
1399   
1400     for (;;) {
1401         switch(*w) {
1402         case '*':
1403             if (w[1] == 0)      /* optimize wild* case */
1404                 return(0);
1405             {
1406                 int i;
1407                 int l = strlen(s);
1408
1409                 for (i = 0; i <= l; ++i) {
1410                     if (WildCmp(w + 1, s + i) == 0)
1411                         return(0);
1412                 }
1413             }
1414             return(-1);
1415         case '?':
1416             if (*s == 0)
1417                 return(-1);
1418             ++w;
1419             ++s;
1420             break;
1421         default:
1422             if (*w != *s)
1423                 return(-1);
1424             if (*w == 0)        /* terminator */
1425                 return(0);
1426             ++w;
1427             ++s;
1428             break;
1429         }
1430     }
1431     /* not reached */
1432     return(-1);
1433 }
1434
1435 int
1436 YesNo(const char *path)
1437 {
1438     int ch, first;
1439
1440     fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1441     fflush(stderr);
1442
1443     first = ch = getchar();
1444     while (ch != '\n' && ch != EOF)
1445         ch = getchar();
1446     return ((first == 'y' || first == 'Y'));
1447 }
1448
1449 /*
1450  * xrename() - rename with override
1451  *
1452  *      If the rename fails, attempt to override st_flags on the 
1453  *      destination and rename again.  If that fails too, try to
1454  *      set the flags back the way they were and give up.
1455  */
1456
1457 static int
1458 xrename(const char *src, const char *dst, u_long flags)
1459 {
1460     int r;
1461
1462     r = 0;
1463
1464     if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1465 #ifdef _ST_FLAGS_PRESENT_
1466         hc_chflags(&DstHost, dst, 0);
1467         if ((r = hc_rename(&DstHost, src, dst)) < 0)
1468                 hc_chflags(&DstHost, dst, flags);
1469 #endif
1470     }
1471     return(r);
1472 }
1473
1474 static int
1475 xlink(const char *src, const char *dst, u_long flags)
1476 {
1477     int r;
1478 #ifdef _ST_FLAGS_PRESENT_
1479     int e;
1480 #endif
1481
1482     r = 0;
1483
1484     if ((r = hc_link(&DstHost, src, dst)) < 0) {
1485 #ifdef _ST_FLAGS_PRESENT_
1486         hc_chflags(&DstHost, src, 0);
1487         r = hc_link(&DstHost, src, dst);
1488         e = errno;
1489         hc_chflags(&DstHost, src, flags);
1490         errno = e;
1491 #endif
1492     }
1493     if (r == 0)
1494             ++CountLinkedItems;
1495     return(r);
1496 }
1497