cpdup - Add -F<ssh-opt>, add code to remove need for FreeBSD ports patch
[dragonfly.git] / bin / cpdup / cpdup.c
blob144856b7d03379eb3f14fbceda246c6bd3564762
1 /*-
2 * CPDUP.C
4 * CPDUP <options> source destination
6 * (c) Copyright 1997-1999 by Matthew Dillon and Dima Ruban. Permission to
7 * use and distribute based on the FreeBSD copyright. Supplied as-is,
8 * USE WITH EXTREME CAUTION.
10 * This program attempts to duplicate the source onto the destination as
11 * exactly as possible, retaining modify times, flags, perms, uid, and gid.
12 * It can duplicate devices, files (including hardlinks), softlinks,
13 * directories, and so forth. It is recursive by default! The duplication
14 * is inclusive of removal of files/directories on the destination that do
15 * not exist on the source. This program supports a per-directory exception
16 * file called .cpignore, or a user-specified exception file.
18 * Safety features:
20 * - does not cross partition boundries on source
21 * - asks for confirmation on deletions unless -i0 is specified
22 * - refuses to replace a destination directory with a source file
23 * unless -s0 is specified.
24 * - terminates on error
26 * Copying features:
28 * - does not copy file if mtime, flags, perms, and size match unless
29 * forced
31 * - copies to temporary and renames-over the original, allowing
32 * you to update live systems
34 * - copies uid, gid, mtime, perms, flags, softlinks, devices, hardlinks,
35 * and recurses through directories.
37 * - accesses a per-directory exclusion file, .cpignore, containing
38 * standard wildcarded ( ? / * style, NOT regex) exclusions.
40 * - tries to play permissions and flags smart in regards to overwriting
41 * schg files and doing related stuff.
43 * - Can do MD5 consistancy checks
45 * - Is able to do incremental mirroring/backups via hardlinks from
46 * the 'previous' version (supplied with -H path).
48 * $DragonFly: src/bin/cpdup/cpdup.c,v 1.32 2008/11/11 04:36:00 dillon Exp $
51 /*-
52 * Example: cc -O cpdup.c -o cpdup -lmd
54 * ".MD5.CHECKSUMS" contains md5 checksumms for the current directory.
55 * This file is stored on the source.
58 #include "cpdup.h"
59 #include "hclink.h"
60 #include "hcproto.h"
62 #define HSIZE 8192
63 #define HMASK (HSIZE-1)
64 #define HLSIZE 8192
65 #define HLMASK (HLSIZE - 1)
67 #define MAXDEPTH 32 /* max copy depth for thread */
68 #define GETBUFSIZE 8192
69 #define GETPATHSIZE 2048
70 #define GETLINKSIZE 1024
71 #define GETIOSIZE 65536
73 #ifndef _ST_FLAGS_PRESENT_
74 #define st_flags st_mode
75 #endif
77 typedef struct Node {
78 struct Node *no_Next;
79 struct Node *no_HNext;
80 int no_Value;
81 char no_Name[4];
82 } Node;
84 typedef struct List {
85 Node li_Node;
86 Node *li_Hash[HSIZE];
87 } List;
89 struct hlink {
90 ino_t ino;
91 ino_t dino;
92 int refs;
93 struct hlink *next;
94 struct hlink *prev;
95 nlink_t nlinked;
96 char name[0];
99 typedef struct copy_info {
100 char *spath;
101 char *dpath;
102 dev_t sdevNo;
103 dev_t ddevNo;
104 #ifdef USE_PTHREADS
105 struct copy_info *parent;
106 pthread_cond_t cond;
107 int children;
108 int r;
109 #endif
110 } *copy_info_t;
112 struct hlink *hltable[HLSIZE];
114 void RemoveRecur(const char *dpath, dev_t devNo);
115 void InitList(List *list);
116 void ResetList(List *list);
117 int AddList(List *list, const char *name, int n);
118 static struct hlink *hltlookup(struct stat *);
119 static struct hlink *hltadd(struct stat *, const char *);
120 static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
121 static int validate_check(const char *spath, const char *dpath);
122 static int shash(const char *s);
123 static void hltdelete(struct hlink *);
124 static void hltsetdino(struct hlink *, ino_t);
125 int YesNo(const char *path);
126 static int xrename(const char *src, const char *dst, u_long flags);
127 static int xlink(const char *src, const char *dst, u_long flags);
128 static int xremove(struct HostConf *host, const char *path);
129 int WildCmp(const char *s1, const char *s2);
130 static int DoCopy(copy_info_t info, int depth);
132 int AskConfirmation = 1;
133 int SafetyOpt = 1;
134 int ForceOpt;
135 int DeviceOpt = 1;
136 int VerboseOpt;
137 int QuietOpt;
138 int NoRemoveOpt;
139 int UseMD5Opt;
140 int UseFSMIDOpt;
141 int SummaryOpt;
142 int CompressOpt;
143 int SlaveOpt;
144 int EnableDirectoryRetries;
145 int DstBaseLen;
146 int ValidateOpt;
147 int CurParallel;
148 int MaxParallel = -1;
149 int HardLinkCount;
150 int ssh_argc;
151 const char *ssh_argv[16];
152 int RunningAsUser;
153 int RunningAsRoot;
154 const char *UseCpFile;
155 const char *UseHLPath;
156 const char *MD5CacheFile;
157 const char *FSMIDCacheFile;
159 int64_t CountSourceBytes;
160 int64_t CountSourceItems;
161 int64_t CountCopiedItems;
162 int64_t CountSourceReadBytes;
163 int64_t CountTargetReadBytes;
164 int64_t CountWriteBytes;
165 int64_t CountRemovedItems;
166 int64_t CountLinkedItems;
168 struct HostConf SrcHost;
169 struct HostConf DstHost;
171 #if USE_PTHREADS
172 pthread_mutex_t MasterMutex;
173 #endif
176 main(int ac, char **av)
178 int i;
179 char *src = NULL;
180 char *dst = NULL;
181 char *ptr;
182 struct timeval start;
183 struct copy_info info;
185 signal(SIGPIPE, SIG_IGN);
187 RunningAsUser = (geteuid() != 0);
188 RunningAsRoot = !RunningAsUser;
190 #if USE_PTHREADS
191 for (i = 0; i < HCTHASH_SIZE; ++i) {
192 pthread_mutex_init(&SrcHost.hct_mutex[i], NULL);
193 pthread_mutex_init(&DstHost.hct_mutex[i], NULL);
195 pthread_mutex_init(&MasterMutex, NULL);
196 pthread_mutex_lock(&MasterMutex);
197 #endif
199 gettimeofday(&start, NULL);
200 for (i = 1; i < ac; ++i) {
201 int v = 1;
203 ptr = av[i];
204 if (*ptr != '-') {
205 if (src == NULL) {
206 src = ptr;
207 } else if (dst == NULL) {
208 dst = ptr;
209 } else {
210 fatal("too many arguments");
211 /* not reached */
213 continue;
215 ptr += 2;
217 if (*ptr)
218 v = strtol(ptr, NULL, 0);
220 switch(ptr[-1]) {
221 case 'C':
222 CompressOpt = 1;
223 break;
224 case 'v':
225 VerboseOpt = 1;
226 while (*ptr == 'v') {
227 ++VerboseOpt;
228 ++ptr;
230 if (*ptr >= '0' && *ptr <= '9')
231 VerboseOpt = strtol(ptr, NULL, 0);
232 break;
233 case 'l':
234 setlinebuf(stdout);
235 setlinebuf(stderr);
236 break;
237 case 'V':
238 ValidateOpt = v;
239 break;
240 case 'I':
241 SummaryOpt = v;
242 break;
243 case 'o':
244 NoRemoveOpt = v;
245 break;
246 case 'x':
247 UseCpFile = ".cpignore";
248 break;
249 case 'X':
250 UseCpFile = (*ptr) ? ptr : av[++i];
251 break;
252 case 'H':
253 UseHLPath = (*ptr) ? ptr : av[++i];
254 break;
255 case 'F':
256 if (ssh_argc >= 16)
257 fatal("too many -F options");
258 ssh_argv[ssh_argc++] = (*ptr) ? ptr : av[++i];
259 break;
260 case 'S':
261 SlaveOpt = v;
262 break;
263 case 'f':
264 ForceOpt = v;
265 break;
266 case 'i':
267 AskConfirmation = v;
268 break;
269 case 'j':
270 DeviceOpt = v;
271 break;
272 case 'p':
273 MaxParallel = v;
274 break;
275 case 's':
276 SafetyOpt = v;
277 break;
278 case 'q':
279 QuietOpt = v;
280 break;
281 case 'k':
282 UseFSMIDOpt = v;
283 FSMIDCacheFile = ".FSMID.CHECK";
284 break;
285 case 'K':
286 UseFSMIDOpt = v;
287 FSMIDCacheFile = av[++i];
288 break;
289 case 'M':
290 UseMD5Opt = v;
291 MD5CacheFile = av[++i];
292 break;
293 case 'm':
294 UseMD5Opt = v;
295 MD5CacheFile = ".MD5.CHECKSUMS";
296 break;
297 case 'u':
298 setvbuf(stdout, NULL, _IOLBF, 0);
299 break;
300 default:
301 fatal("illegal option: %s\n", ptr - 2);
302 /* not reached */
303 break;
308 * If we are told to go into slave mode, run the HC protocol
310 if (SlaveOpt) {
311 hc_slave(0, 1);
312 exit(0);
316 * Extract the source and/or/neither target [user@]host and
317 * make any required connections.
319 if (src && (ptr = strchr(src, ':')) != NULL) {
320 asprintf(&SrcHost.host, "%*.*s", (int)(ptr - src), (int)(ptr - src), src);
321 src = ptr + 1;
322 if (UseCpFile) {
323 fprintf(stderr, "The cpignore options are not currently supported for remote sources\n");
324 exit(1);
326 if (UseMD5Opt) {
327 fprintf(stderr, "The MD5 options are not currently supported for remote sources\n");
328 exit(1);
330 if (hc_connect(&SrcHost) < 0)
331 exit(1);
333 if (dst && (ptr = strchr(dst, ':')) != NULL) {
334 asprintf(&DstHost.host, "%*.*s", (int)(ptr - dst), (int)(ptr - dst), dst);
335 dst = ptr + 1;
336 if (UseFSMIDOpt) {
337 fprintf(stderr, "The FSMID options are not currently supported for remote targets\n");
338 exit(1);
340 if (hc_connect(&DstHost) < 0)
341 exit(1);
345 * dst may be NULL only if -m option is specified,
346 * which forces an update of the MD5 checksums
348 if (dst == NULL && UseMD5Opt == 0) {
349 fatal(NULL);
350 /* not reached */
352 bzero(&info, sizeof(info));
353 #if USE_PTHREADS
354 info.r = 0;
355 info.children = 0;
356 pthread_cond_init(&info.cond, NULL);
357 #endif
358 if (dst) {
359 DstBaseLen = strlen(dst);
360 info.spath = src;
361 info.dpath = dst;
362 info.sdevNo = (dev_t)-1;
363 info.ddevNo = (dev_t)-1;
364 i = DoCopy(&info, -1);
365 } else {
366 info.spath = src;
367 info.dpath = NULL;
368 info.sdevNo = (dev_t)-1;
369 info.ddevNo = (dev_t)-1;
370 i = DoCopy(&info, -1);
372 #if USE_PTHREADS
373 pthread_cond_destroy(&info.cond);
374 #endif
375 #ifndef NOMD5
376 md5_flush();
377 #endif
378 fsmid_flush();
380 if (SummaryOpt && i == 0) {
381 double duration;
382 struct timeval end;
384 gettimeofday(&end, NULL);
385 #if 0
386 /* don't count stat's in our byte statistics */
387 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
388 CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
389 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
390 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
391 #endif
393 duration = (end.tv_sec - start.tv_sec);
394 duration += (double)(end.tv_usec - start.tv_usec) / 1000000.0;
395 if (duration == 0.0)
396 duration = 1.0;
397 logstd("cpdup completed successfully\n");
398 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
399 "%lld bytes written (%.1fX speedup)\n",
400 (long long)CountSourceBytes,
401 (long long)CountSourceReadBytes,
402 (long long)CountTargetReadBytes,
403 (long long)CountWriteBytes,
404 ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
405 logstd("%lld source items, %lld items copied, %lld items linked, "
406 "%lld things deleted\n",
407 (long long)CountSourceItems,
408 (long long)CountCopiedItems,
409 (long long)CountLinkedItems,
410 (long long)CountRemovedItems);
411 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
412 duration,
413 (int)((CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration / 1024.0),
414 (int)(CountSourceBytes / duration / 1024.0));
416 exit((i == 0) ? 0 : 1);
419 static struct hlink *
420 hltlookup(struct stat *stp)
422 #if USE_PTHREADS
423 struct timespec ts = { 0, 100000 };
424 #endif
425 struct hlink *hl;
426 int n;
428 n = stp->st_ino & HLMASK;
430 #if USE_PTHREADS
431 again:
432 #endif
433 for (hl = hltable[n]; hl; hl = hl->next) {
434 if (hl->ino == stp->st_ino) {
435 #if USE_PTHREADS
437 * If the hl entry is still in the process of being created
438 * by another thread we have to wait until it has either been
439 * deleted or completed.
441 if (hl->refs) {
442 pthread_mutex_unlock(&MasterMutex);
443 nanosleep(&ts, NULL);
444 pthread_mutex_lock(&MasterMutex);
445 goto again;
447 #endif
448 ++hl->refs;
449 return hl;
453 return NULL;
456 static struct hlink *
457 hltadd(struct stat *stp, const char *path)
459 struct hlink *new;
460 int plen = strlen(path);
461 int n;
463 new = malloc(offsetof(struct hlink, name[plen + 1]));
464 if (new == NULL) {
465 fprintf(stderr, "out of memory\n");
466 exit(EXIT_FAILURE);
468 ++HardLinkCount;
470 /* initialize and link the new element into the table */
471 new->ino = stp->st_ino;
472 new->dino = (ino_t)-1;
473 new->refs = 1;
474 bcopy(path, new->name, plen + 1);
475 new->nlinked = 1;
476 new->prev = NULL;
477 n = stp->st_ino & HLMASK;
478 new->next = hltable[n];
479 if (hltable[n])
480 hltable[n]->prev = new;
481 hltable[n] = new;
483 return new;
486 static void
487 hltsetdino(struct hlink *hl, ino_t inum)
489 hl->dino = inum;
492 static void
493 hltdelete(struct hlink *hl)
495 assert(hl->refs == 1);
496 --hl->refs;
497 if (hl->prev) {
498 if (hl->next)
499 hl->next->prev = hl->prev;
500 hl->prev->next = hl->next;
501 } else {
502 if (hl->next)
503 hl->next->prev = NULL;
505 hltable[hl->ino & HLMASK] = hl->next;
507 --HardLinkCount;
508 free(hl);
511 static void
512 hltrels(struct hlink *hl)
514 assert(hl->refs == 1);
515 --hl->refs;
519 * If UseHLPath is defined check to see if the file in question is
520 * the same as the source file, and if it is return a pointer to the
521 * -H path based file for hardlinking. Else return NULL.
523 static char *
524 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
526 struct stat sthl;
527 char *hpath;
528 int error;
530 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
533 * stat info matches ?
535 if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
536 st1->st_size != sthl.st_size ||
537 st1->st_mtime != sthl.st_mtime ||
538 (RunningAsRoot && (st1->st_uid != sthl.st_uid ||
539 st1->st_gid != sthl.st_gid))
541 free(hpath);
542 return(NULL);
546 * If ForceOpt or ValidateOpt is set we have to compare the files
548 if (ForceOpt || ValidateOpt) {
549 error = validate_check(spath, hpath);
550 if (error) {
551 free(hpath);
552 hpath = NULL;
555 return(hpath);
559 * Return 0 if the contents of the file <spath> matches the contents of
560 * the file <dpath>.
562 static int
563 validate_check(const char *spath, const char *dpath)
565 int error;
566 int fd1;
567 int fd2;
569 fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
570 fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
571 error = -1;
573 if (fd1 >= 0 && fd2 >= 0) {
574 int n;
575 int x;
576 char *iobuf1 = malloc(GETIOSIZE);
577 char *iobuf2 = malloc(GETIOSIZE);
579 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
580 CountSourceReadBytes += n;
581 x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
582 if (x > 0)
583 CountTargetReadBytes += x;
584 if (x != n)
585 break;
586 if (bcmp(iobuf1, iobuf2, n) != 0)
587 break;
589 free(iobuf1);
590 free(iobuf2);
591 if (n == 0)
592 error = 0;
594 if (fd1 >= 0)
595 hc_close(&SrcHost, fd1);
596 if (fd2 >= 0)
597 hc_close(&DstHost, fd2);
598 return (error);
600 #if USE_PTHREADS
602 static void *
603 DoCopyThread(void *arg)
605 copy_info_t cinfo = arg;
606 char *spath = cinfo->spath;
607 char *dpath = cinfo->dpath;
608 int r;
610 r = pthread_detach(pthread_self());
611 assert(r == 0);
612 pthread_cond_init(&cinfo->cond, NULL);
613 pthread_mutex_lock(&MasterMutex);
614 cinfo->r += DoCopy(cinfo, 0);
615 /* cinfo arguments invalid on return */
616 --cinfo->parent->children;
617 --CurParallel;
618 pthread_cond_signal(&cinfo->parent->cond);
619 free(spath);
620 if (dpath)
621 free(dpath);
622 pthread_cond_destroy(&cinfo->cond);
623 free(cinfo);
624 hcc_free_trans(&SrcHost);
625 hcc_free_trans(&DstHost);
626 pthread_mutex_unlock(&MasterMutex);
627 return(NULL);
630 #endif
633 DoCopy(copy_info_t info, int depth)
635 const char *spath = info->spath;
636 const char *dpath = info->dpath;
637 dev_t sdevNo = info->sdevNo;
638 dev_t ddevNo = info->ddevNo;
639 struct stat st1;
640 struct stat st2;
641 unsigned long st2_flags;
642 int r, mres, fres, st2Valid;
643 struct hlink *hln;
644 List *list = malloc(sizeof(List));
645 u_int64_t size;
647 InitList(list);
648 r = mres = fres = st2Valid = 0;
649 st2_flags = 0;
650 size = 0;
651 hln = NULL;
653 if (hc_lstat(&SrcHost, spath, &st1) != 0) {
654 r = 0;
655 goto done;
657 #ifdef SF_SNAPSHOT
658 /* skip snapshot files because they're sparse and _huge_ */
659 if (st1.st_flags & SF_SNAPSHOT)
660 return(0);
661 #endif
662 st2.st_mode = 0; /* in case lstat fails */
663 st2.st_flags = 0; /* in case lstat fails */
664 if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0) {
665 st2Valid = 1;
666 #ifdef _ST_FLAGS_PRESENT_
667 st2_flags = st2.st_flags;
668 #endif
671 if (S_ISREG(st1.st_mode)) {
672 size = st1.st_size;
676 * Handle hardlinks
679 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
680 if ((hln = hltlookup(&st1)) != NULL) {
681 hln->nlinked++;
683 if (st2Valid) {
684 if (st2.st_ino == hln->dino) {
686 * hard link is already correct, nothing to do
688 if (VerboseOpt >= 3)
689 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
690 if (hln->nlinked == st1.st_nlink) {
691 hltdelete(hln);
692 hln = NULL;
694 CountSourceItems++;
695 r = 0;
696 goto done;
697 } else {
699 * hard link is not correct, attempt to unlink it
701 if (xremove(&DstHost, dpath) < 0) {
702 logerr("%-32s hardlink: unable to unlink: %s\n",
703 ((dpath) ? dpath : spath), strerror(errno));
704 hltdelete(hln);
705 hln = NULL;
706 ++r;
707 goto done;
712 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
713 int tryrelink = (errno == EMLINK);
714 logerr("%-32s hardlink: unable to link to %s: %s\n",
715 (dpath ? dpath : spath), hln->name, strerror(errno)
717 hltdelete(hln);
718 hln = NULL;
719 if (tryrelink) {
720 logerr("%-20s hardlink: will attempt to copy normally\n");
721 goto relink;
723 ++r;
724 } else {
725 if (hln->nlinked == st1.st_nlink) {
726 hltdelete(hln);
727 hln = NULL;
729 if (r == 0) {
730 if (VerboseOpt) {
731 logstd("%-32s hardlink: %s\n",
732 (dpath ? dpath : spath),
733 (st2Valid ? "relinked" : "linked")
736 CountSourceItems++;
737 CountCopiedItems++;
738 r = 0;
739 goto done;
742 } else {
744 * first instance of hardlink must be copied normally
746 relink:
747 hln = hltadd(&st1, dpath);
752 * Do we need to copy the file/dir/link/whatever? Early termination
753 * if we do not. Always redo links. Directories are always traversed
754 * except when the FSMID options are used.
756 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
759 if (
760 st2Valid
761 && st1.st_mode == st2.st_mode
762 #ifdef _ST_FLAGS_PRESENT_
763 && (RunningAsUser || st1.st_flags == st2.st_flags)
764 #endif
766 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
768 * If FSMID tracking is turned on we can avoid recursing through
769 * an entire directory subtree if the FSMID matches.
771 #ifdef _ST_FSMID_PRESENT_
772 if (ForceOpt == 0 &&
773 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
775 if (VerboseOpt >= 3) {
776 if (UseFSMIDOpt)
777 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
778 else
779 logstd("%-32s nochange\n", (dpath ? dpath : spath));
781 r = 0;
782 goto done;
784 #endif
785 } else {
786 if (ForceOpt == 0 &&
787 st1.st_size == st2.st_size &&
788 st1.st_mtime == st2.st_mtime &&
789 (RunningAsUser || (st1.st_uid == st2.st_uid &&
790 st1.st_gid == st2.st_gid))
791 #ifndef NOMD5
792 && (UseMD5Opt == 0 || !S_ISREG(st1.st_mode) ||
793 (mres = md5_check(spath, dpath)) == 0)
794 #endif
795 #ifdef _ST_FSMID_PRESENT_
796 && (UseFSMIDOpt == 0 ||
797 (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
798 #endif
799 && (ValidateOpt == 0 || !S_ISREG(st1.st_mode) ||
800 validate_check(spath, dpath) == 0)
803 * The files are identical, but if we are not running as
804 * root we might need to adjust ownership/group/flags.
806 int changedown = 0;
807 int changedflags = 0;
808 if (hln)
809 hltsetdino(hln, st2.st_ino);
811 if (RunningAsUser && (st1.st_uid != st2.st_uid ||
812 st1.st_gid != st2.st_gid)) {
813 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
814 changedown = 1;
816 #ifdef _ST_FLAGS_PRESENT_
817 if (RunningAsUser && st1.st_flags != st2.st_flags) {
818 hc_chflags(&DstHost, dpath, st1.st_flags);
819 changedflags = 1;
821 #endif
822 if (VerboseOpt >= 3) {
823 #ifndef NOMD5
824 if (UseMD5Opt) {
825 logstd("%-32s md5-nochange",
826 (dpath ? dpath : spath));
827 } else
828 #endif
829 if (UseFSMIDOpt) {
830 logstd("%-32s fsmid-nochange",
831 (dpath ? dpath : spath));
832 } else if (ValidateOpt) {
833 logstd("%-32s nochange (contents validated)",
834 (dpath ? dpath : spath));
835 } else {
836 logstd("%-32s nochange", (dpath ? dpath : spath));
838 if (changedown)
839 logstd(" (uid/gid differ)");
840 if (changedflags)
841 logstd(" (flags differ)");
842 logstd("\n");
844 CountSourceBytes += size;
845 CountSourceItems++;
846 r = 0;
847 goto done;
851 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
852 if (SafetyOpt) {
853 logerr("%-32s SAFETY - refusing to copy file over directory\n",
854 (dpath ? dpath : spath)
856 ++r; /* XXX */
857 r = 0;
858 goto done; /* continue with the cpdup anyway */
860 if (QuietOpt == 0 || AskConfirmation) {
861 logstd("%-32s WARNING: non-directory source will blow away\n"
862 "%-32s preexisting dest directory, continuing anyway!\n",
863 ((dpath) ? dpath : spath), "");
865 if (dpath)
866 RemoveRecur(dpath, ddevNo);
867 st2Valid = 0;
871 * The various comparisons failed, copy it.
873 if (S_ISDIR(st1.st_mode)) {
874 DIR *dir;
876 if (fres < 0)
877 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
878 if ((dir = hc_opendir(&SrcHost, spath)) != NULL) {
879 struct dirent *den;
880 int noLoop = 0;
882 if (dpath) {
883 if (S_ISDIR(st2.st_mode) == 0) {
884 xremove(&DstHost, dpath);
885 if (hc_mkdir(&DstHost, dpath, st1.st_mode | 0700) != 0) {
886 logerr("%s: mkdir failed: %s\n",
887 (dpath ? dpath : spath), strerror(errno));
888 r = 1;
889 noLoop = 1;
893 * Matt: why don't you check error codes here?
894 * (Because I'm an idiot... checks added!)
896 if (hc_lstat(&DstHost, dpath, &st2) != 0) {
897 logerr("%s: lstat of newly made dir failed: %s\n",
898 (dpath ? dpath : spath), strerror(errno));
899 r = 1;
900 noLoop = 1;
902 if (hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid) != 0){
903 logerr("%s: chown of newly made dir failed: %s\n",
904 (dpath ? dpath : spath), strerror(errno));
905 r = 1;
906 noLoop = 1;
908 CountCopiedItems++;
909 } else {
911 * Directory must be scanable by root for cpdup to
912 * work. We'll fix it later if the directory isn't
913 * supposed to be readable ( which is why we fixup
914 * st2.st_mode to match what we did ).
916 if ((st2.st_mode & 0700) != 0700) {
917 hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
918 st2.st_mode |= 0700;
920 if (VerboseOpt >= 2)
921 logstd("%s\n", dpath ? dpath : spath);
926 * When copying a directory, stop if the source crosses a mount
927 * point.
929 if (sdevNo != (dev_t)-1 && st1.st_dev != sdevNo) {
930 noLoop = 1;
931 } else {
932 sdevNo = st1.st_dev;
936 * When copying a directory, stop if the destination crosses
937 * a mount point.
939 * The target directory will have been created and stat'd
940 * for st2 if it did not previously exist. st2Valid is left
941 * as a flag. If the stat failed st2 will still only have its
942 * default initialization.
944 * So we simply assume here that the directory is within the
945 * current target mount if we had to create it (aka st2Valid is 0)
946 * and we leave ddevNo alone.
948 if (st2Valid) {
949 if (ddevNo != (dev_t)-1 && st2.st_dev != ddevNo) {
950 noLoop = 1;
951 } else {
952 ddevNo = st2.st_dev;
957 * scan .cpignore file for files/directories
958 * to ignore.
961 if (UseCpFile) {
962 FILE *fi;
963 char *buf = malloc(GETBUFSIZE);
964 char *fpath;
966 if (UseCpFile[0] == '/') {
967 fpath = mprintf("%s", UseCpFile);
968 } else {
969 fpath = mprintf("%s/%s", spath, UseCpFile);
971 AddList(list, strrchr(fpath, '/') + 1, 1);
972 if ((fi = fopen(fpath, "r")) != NULL) {
973 while (fgets(buf, GETBUFSIZE, fi) != NULL) {
974 int l = strlen(buf);
975 CountSourceReadBytes += l;
976 if (l && buf[l-1] == '\n')
977 buf[--l] = 0;
978 if (buf[0])
979 AddList(list, buf, 1);
981 fclose(fi);
983 free(fpath);
984 free(buf);
988 * Automatically exclude MD5CacheFile that we create on the
989 * source from the copy to the destination.
991 * Automatically exclude a FSMIDCacheFile on the source that
992 * would otherwise overwrite the one we maintain on the target.
994 if (UseMD5Opt)
995 AddList(list, MD5CacheFile, 1);
996 if (UseFSMIDOpt)
997 AddList(list, FSMIDCacheFile, 1);
999 while (noLoop == 0 && (den = hc_readdir(&SrcHost, dir)) != NULL) {
1001 * ignore . and ..
1003 char *nspath;
1004 char *ndpath = NULL;
1006 if (strcmp(den->d_name, ".") == 0 ||
1007 strcmp(den->d_name, "..") == 0
1009 continue;
1012 * ignore if on .cpignore list
1014 if (AddList(list, den->d_name, 0) == 1) {
1015 continue;
1017 nspath = mprintf("%s/%s", spath, den->d_name);
1018 if (dpath)
1019 ndpath = mprintf("%s/%s", dpath, den->d_name);
1021 #if USE_PTHREADS
1022 if (CurParallel < MaxParallel || depth > MAXDEPTH) {
1023 copy_info_t cinfo = malloc(sizeof(*cinfo));
1024 pthread_t dummy_thr;
1026 bzero(cinfo, sizeof(*cinfo));
1027 cinfo->spath = nspath;
1028 cinfo->dpath = ndpath;
1029 cinfo->sdevNo = sdevNo;
1030 cinfo->ddevNo = ddevNo;
1031 cinfo->parent = info;
1032 ++CurParallel;
1033 ++info->children;
1034 pthread_create(&dummy_thr, NULL, DoCopyThread, cinfo);
1035 } else
1036 #endif
1038 info->spath = nspath;
1039 info->dpath = ndpath;
1040 info->sdevNo = sdevNo;
1041 info->ddevNo = ddevNo;
1042 if (depth < 0)
1043 r += DoCopy(info, depth);
1044 else
1045 r += DoCopy(info, depth + 1);
1046 free(nspath);
1047 if (ndpath)
1048 free(ndpath);
1049 info->spath = NULL;
1050 info->dpath = NULL;
1054 hc_closedir(&SrcHost, dir);
1056 #if USE_PTHREADS
1058 * Wait for our children to finish
1060 while (info->children) {
1061 pthread_cond_wait(&info->cond, &MasterMutex);
1063 r += info->r;
1064 info->r = 0;
1065 #endif
1068 * Remove files/directories from destination that do not appear
1069 * in the source.
1071 if (dpath && (dir = hc_opendir(&DstHost, dpath)) != NULL) {
1072 while (noLoop == 0 && (den = hc_readdir(&DstHost, dir)) != NULL) {
1074 * ignore . or ..
1076 if (strcmp(den->d_name, ".") == 0 ||
1077 strcmp(den->d_name, "..") == 0
1079 continue;
1082 * If object does not exist in source or .cpignore
1083 * then recursively remove it.
1085 if (AddList(list, den->d_name, 3) == 3) {
1086 char *ndpath;
1088 ndpath = mprintf("%s/%s", dpath, den->d_name);
1089 RemoveRecur(ndpath, ddevNo);
1090 free(ndpath);
1093 hc_closedir(&DstHost, dir);
1096 if (dpath) {
1097 struct timeval tv[2];
1099 if (ForceOpt ||
1100 st2Valid == 0 ||
1101 st1.st_uid != st2.st_uid ||
1102 st1.st_gid != st2.st_gid
1104 hc_chown(&DstHost, dpath, st1.st_uid, st1.st_gid);
1106 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
1107 hc_chmod(&DstHost, dpath, st1.st_mode);
1109 #ifdef _ST_FLAGS_PRESENT_
1110 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
1111 hc_chflags(&DstHost, dpath, st1.st_flags);
1113 #endif
1114 if (ForceOpt ||
1115 st2Valid == 0 ||
1116 st1.st_mtime != st2.st_mtime
1118 bzero(tv, sizeof(tv));
1119 tv[0].tv_sec = st1.st_mtime;
1120 tv[1].tv_sec = st1.st_mtime;
1121 hc_utimes(&DstHost, dpath, tv);
1125 } else if (dpath == NULL) {
1127 * If dpath is NULL, we are just updating the MD5
1129 #ifndef NOMD5
1130 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
1131 mres = md5_check(spath, NULL);
1133 if (VerboseOpt > 1) {
1134 if (mres < 0)
1135 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1136 else
1137 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
1138 } else if (!QuietOpt && mres < 0) {
1139 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
1142 #endif
1143 } else if (S_ISREG(st1.st_mode)) {
1144 char *path;
1145 char *hpath;
1146 int fd1;
1147 int fd2;
1149 if (st2Valid)
1150 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1151 else
1152 path = mprintf("%s", dpath);
1155 * Handle check failure message.
1157 #ifndef NOMD5
1158 if (mres < 0)
1159 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
1160 else
1161 #endif
1162 if (fres < 0)
1163 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
1166 * Not quite ready to do the copy yet. If UseHLPath is defined,
1167 * see if we can hardlink instead.
1169 * If we can hardlink, and the target exists, we have to remove it
1170 * first or the hardlink will fail. This can occur in a number of
1171 * situations but must typically when the '-f -H' combination is
1172 * used.
1174 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
1175 if (st2Valid)
1176 xremove(&DstHost, dpath);
1177 if (hc_link(&DstHost, hpath, dpath) == 0) {
1178 ++CountLinkedItems;
1179 if (VerboseOpt) {
1180 logstd("%-32s hardlinked(-H)\n",
1181 (dpath ? dpath : spath));
1183 free(hpath);
1184 goto skip_copy;
1187 * Shucks, we may have hit a filesystem hard linking limit,
1188 * we have to copy instead.
1190 free(hpath);
1193 if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
1194 if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
1196 * There could be a .tmp file from a previously interrupted
1197 * run, delete and retry. Fail if we still can't get at it.
1199 #ifdef _ST_FLAGS_PRESENT_
1200 hc_chflags(&DstHost, path, 0);
1201 #endif
1202 hc_remove(&DstHost, path);
1203 fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
1205 if (fd2 >= 0) {
1206 const char *op;
1207 char *iobuf1 = malloc(GETIOSIZE);
1208 int n;
1211 * Matt: What about holes?
1213 op = "read";
1214 while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
1215 op = "write";
1216 if (hc_write(&DstHost, fd2, iobuf1, n) != n)
1217 break;
1218 op = "read";
1220 hc_close(&DstHost, fd2);
1221 if (n == 0) {
1222 struct timeval tv[2];
1224 bzero(tv, sizeof(tv));
1225 tv[0].tv_sec = st1.st_mtime;
1226 tv[1].tv_sec = st1.st_mtime;
1228 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1229 hc_chmod(&DstHost, path, st1.st_mode);
1230 #ifdef _ST_FLAGS_PRESENT_
1231 if (st1.st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
1232 hc_utimes(&DstHost, path, tv);
1233 #else
1234 hc_utimes(&DstHost, path, tv);
1235 #endif
1236 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1237 logerr("%-32s rename-after-copy failed: %s\n",
1238 (dpath ? dpath : spath), strerror(errno)
1240 ++r;
1241 } else {
1242 if (VerboseOpt)
1243 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
1244 #ifdef _ST_FLAGS_PRESENT_
1245 if (st1.st_flags)
1246 hc_chflags(&DstHost, dpath, st1.st_flags);
1247 #endif
1249 #ifdef _ST_FLAGS_PRESENT_
1250 if ((st1.st_flags & (UF_IMMUTABLE|SF_IMMUTABLE)) == 0)
1251 hc_utimes(&DstHost, dpath, tv);
1252 #endif
1253 CountSourceReadBytes += size;
1254 CountWriteBytes += size;
1255 CountSourceBytes += size;
1256 CountSourceItems++;
1257 CountCopiedItems++;
1258 } else {
1259 logerr("%-32s %s failed: %s\n",
1260 (dpath ? dpath : spath), op, strerror(errno)
1262 hc_remove(&DstHost, path);
1263 ++r;
1265 free(iobuf1);
1266 } else {
1267 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1268 (dpath ? dpath : spath), getuid(), geteuid(),
1269 strerror(errno)
1271 ++r;
1273 hc_close(&SrcHost, fd1);
1274 } else {
1275 logerr("%-32s copy: open failed: %s\n",
1276 (dpath ? dpath : spath),
1277 strerror(errno)
1279 ++r;
1281 skip_copy:
1282 free(path);
1284 if (hln) {
1285 if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
1286 hltsetdino(hln, st2.st_ino);
1287 } else {
1288 hltdelete(hln);
1289 hln = NULL;
1292 } else if (S_ISLNK(st1.st_mode)) {
1293 char *link1 = malloc(GETLINKSIZE);
1294 char *link2 = malloc(GETLINKSIZE);
1295 char *path;
1296 int n1;
1297 int n2;
1299 n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
1300 if (st2Valid) {
1301 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1302 n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
1303 } else {
1304 path = mprintf("%s", dpath);
1305 n2 = -1;
1307 if (n1 >= 0) {
1308 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
1309 hc_umask(&DstHost, ~st1.st_mode);
1310 xremove(&DstHost, path);
1311 link1[n1] = 0;
1312 if (hc_symlink(&DstHost, link1, path) < 0) {
1313 logerr("%-32s symlink (%s->%s) failed: %s\n",
1314 (dpath ? dpath : spath), link1, path,
1315 strerror(errno)
1317 ++r;
1318 } else {
1319 hc_lchown(&DstHost, path, st1.st_uid, st1.st_gid);
1321 * there is no lchmod() or lchflags(), we
1322 * cannot chmod or chflags a softlink.
1324 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1325 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1326 (dpath ? dpath : spath),
1327 path, dpath, strerror(errno));
1328 } else if (VerboseOpt) {
1329 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
1331 hc_umask(&DstHost, 000);
1332 CountWriteBytes += n1;
1333 CountCopiedItems++;
1335 } else {
1336 if (VerboseOpt >= 3)
1337 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1339 CountSourceBytes += n1;
1340 CountSourceReadBytes += n1;
1341 if (n2 > 0)
1342 CountTargetReadBytes += n2;
1343 CountSourceItems++;
1344 } else {
1345 r = 1;
1346 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
1348 free(link1);
1349 free(link2);
1350 free(path);
1351 } else if ((S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) && DeviceOpt) {
1352 char *path = NULL;
1354 if (ForceOpt ||
1355 st2Valid == 0 ||
1356 st1.st_mode != st2.st_mode ||
1357 st1.st_rdev != st2.st_rdev ||
1358 st1.st_uid != st2.st_uid ||
1359 st1.st_gid != st2.st_gid
1361 if (st2Valid) {
1362 path = mprintf("%s.tmp%d", dpath, (int)getpid());
1363 xremove(&DstHost, path);
1364 } else {
1365 path = mprintf("%s", dpath);
1368 if (hc_mknod(&DstHost, path, st1.st_mode, st1.st_rdev) == 0) {
1369 hc_chmod(&DstHost, path, st1.st_mode);
1370 hc_chown(&DstHost, path, st1.st_uid, st1.st_gid);
1371 if (st2Valid)
1372 xremove(&DstHost, dpath);
1373 if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
1374 logerr("%-32s dev-rename-after-create failed: %s\n",
1375 (dpath ? dpath : spath),
1376 strerror(errno)
1378 } else if (VerboseOpt) {
1379 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
1381 CountCopiedItems++;
1382 } else {
1383 r = 1;
1384 logerr("%-32s dev failed: %s\n",
1385 (dpath ? dpath : spath), strerror(errno)
1388 } else {
1389 if (VerboseOpt >= 3)
1390 logstd("%-32s nochange\n", (dpath ? dpath : spath));
1392 if (path)
1393 free(path);
1394 CountSourceItems++;
1396 done:
1397 if (hln) {
1398 if (hln->dino == (ino_t)-1) {
1399 hltdelete(hln);
1400 /*hln = NULL; unneeded */
1401 } else {
1402 hltrels(hln);
1405 ResetList(list);
1406 free(list);
1407 return (r);
1411 * RemoveRecur()
1414 void
1415 RemoveRecur(const char *dpath, dev_t devNo)
1417 struct stat st;
1419 if (hc_lstat(&DstHost, dpath, &st) == 0) {
1420 if (devNo == (dev_t)-1)
1421 devNo = st.st_dev;
1422 if (st.st_dev == devNo) {
1423 if (S_ISDIR(st.st_mode)) {
1424 DIR *dir;
1426 if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1427 struct dirent *den;
1428 while ((den = hc_readdir(&DstHost, dir)) != NULL) {
1429 char *ndpath;
1431 if (strcmp(den->d_name, ".") == 0)
1432 continue;
1433 if (strcmp(den->d_name, "..") == 0)
1434 continue;
1435 ndpath = mprintf("%s/%s", dpath, den->d_name);
1436 RemoveRecur(ndpath, devNo);
1437 free(ndpath);
1439 hc_closedir(&DstHost, dir);
1441 if (AskConfirmation && NoRemoveOpt == 0) {
1442 if (YesNo(dpath)) {
1443 if (hc_rmdir(&DstHost, dpath) < 0) {
1444 logerr("%-32s rmdir failed: %s\n",
1445 dpath, strerror(errno)
1448 CountRemovedItems++;
1450 } else {
1451 if (NoRemoveOpt) {
1452 if (VerboseOpt)
1453 logstd("%-32s not-removed\n", dpath);
1454 } else if (hc_rmdir(&DstHost, dpath) == 0) {
1455 if (VerboseOpt)
1456 logstd("%-32s rmdir-ok\n", dpath);
1457 CountRemovedItems++;
1458 } else {
1459 logerr("%-32s rmdir failed: %s\n",
1460 dpath, strerror(errno)
1464 } else {
1465 if (AskConfirmation && NoRemoveOpt == 0) {
1466 if (YesNo(dpath)) {
1467 if (xremove(&DstHost, dpath) < 0) {
1468 logerr("%-32s remove failed: %s\n",
1469 dpath, strerror(errno)
1472 CountRemovedItems++;
1474 } else {
1475 if (NoRemoveOpt) {
1476 if (VerboseOpt)
1477 logstd("%-32s not-removed\n", dpath);
1478 } else if (xremove(&DstHost, dpath) == 0) {
1479 if (VerboseOpt)
1480 logstd("%-32s remove-ok\n", dpath);
1481 CountRemovedItems++;
1482 } else {
1483 logerr("%-32s remove failed: %s\n",
1484 dpath, strerror(errno)
1493 void
1494 InitList(List *list)
1496 bzero(list, sizeof(List));
1497 list->li_Node.no_Next = &list->li_Node;
1500 void
1501 ResetList(List *list)
1503 Node *node;
1505 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1506 list->li_Node.no_Next = node->no_Next;
1507 free(node);
1509 InitList(list);
1513 AddList(List *list, const char *name, int n)
1515 Node *node;
1516 int hv;
1518 hv = shash(name);
1521 * Scan against wildcards. Only a node value of 1 can be a wildcard
1522 * ( usually scanned from .cpignore )
1525 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1526 if (strcmp(name, node->no_Name) == 0 ||
1527 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1529 return(node->no_Value);
1534 * Look for exact match
1537 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1538 if (strcmp(name, node->no_Name) == 0) {
1539 return(node->no_Value);
1542 node = malloc(sizeof(Node) + strlen(name) + 1);
1543 if (node == NULL) {
1544 fprintf(stderr, "out of memory\n");
1545 exit(EXIT_FAILURE);
1548 node->no_Next = list->li_Node.no_Next;
1549 list->li_Node.no_Next = node;
1551 node->no_HNext = list->li_Hash[hv];
1552 list->li_Hash[hv] = node;
1554 strcpy(node->no_Name, name);
1555 node->no_Value = n;
1557 return(n);
1560 static int
1561 shash(const char *s)
1563 int hv;
1565 hv = 0xA4FB3255;
1567 while (*s) {
1568 if (*s == '*' || *s == '?' ||
1569 *s == '{' || *s == '}' ||
1570 *s == '[' || *s == ']' ||
1571 *s == '|'
1573 return(0);
1575 hv = (hv << 5) ^ *s ^ (hv >> 23);
1576 ++s;
1578 return(((hv >> 16) ^ hv) & HMASK);
1582 * WildCmp() - compare wild string to sane string
1584 * Return 0 on success, -1 on failure.
1588 WildCmp(const char *w, const char *s)
1591 * skip fixed portion
1594 for (;;) {
1595 switch(*w) {
1596 case '*':
1597 if (w[1] == 0) /* optimize wild* case */
1598 return(0);
1600 int i;
1601 int l = strlen(s);
1603 for (i = 0; i <= l; ++i) {
1604 if (WildCmp(w + 1, s + i) == 0)
1605 return(0);
1608 return(-1);
1609 case '?':
1610 if (*s == 0)
1611 return(-1);
1612 ++w;
1613 ++s;
1614 break;
1615 default:
1616 if (*w != *s)
1617 return(-1);
1618 if (*w == 0) /* terminator */
1619 return(0);
1620 ++w;
1621 ++s;
1622 break;
1625 /* not reached */
1626 return(-1);
1630 YesNo(const char *path)
1632 int ch, first;
1634 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1635 fflush(stderr);
1637 first = ch = getchar();
1638 while (ch != '\n' && ch != EOF)
1639 ch = getchar();
1640 return ((first == 'y' || first == 'Y'));
1644 * xrename() - rename with override
1646 * If the rename fails, attempt to override st_flags on the
1647 * destination and rename again. If that fails too, try to
1648 * set the flags back the way they were and give up.
1651 static int
1652 xrename(const char *src, const char *dst, u_long flags)
1654 int r;
1656 if ((r = hc_rename(&DstHost, src, dst)) < 0) {
1657 #ifdef _ST_FLAGS_PRESENT_
1658 hc_chflags(&DstHost, dst, 0);
1659 if ((r = hc_rename(&DstHost, src, dst)) < 0)
1660 hc_chflags(&DstHost, dst, flags);
1661 #endif
1663 return(r);
1666 static int
1667 xlink(const char *src, const char *dst, u_long flags)
1669 int r;
1670 #ifdef _ST_FLAGS_PRESENT_
1671 int e;
1672 #endif
1674 if ((r = hc_link(&DstHost, src, dst)) < 0) {
1675 #ifdef _ST_FLAGS_PRESENT_
1676 hc_chflags(&DstHost, src, 0);
1677 r = hc_link(&DstHost, src, dst);
1678 e = errno;
1679 hc_chflags(&DstHost, src, flags);
1680 errno = e;
1681 #endif
1683 if (r == 0)
1684 ++CountLinkedItems;
1685 return(r);
1688 static int
1689 xremove(struct HostConf *host, const char *path)
1691 int res;
1693 res = hc_remove(host, path);
1694 #ifdef _ST_FLAGS_PRESENT_
1695 if (res == -EPERM) {
1696 hc_chflags(host, path, 0);
1697 res = hc_remove(host, path);
1699 #endif
1700 return(res);