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