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.
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
28 * - does not copy file if mtime, flags, perms, and size match unless
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 * $DragonFly: src/bin/cpdup/cpdup.c,v 1.9 2005/10/15 19:09:17 swildner Exp $
49 * Example: cc -O cpdup.c -o cpdup -lmd
51 * ".MD5.CHECKSUMS" contains md5 checksumms for the current directory.
52 * This file is stored on the source.
58 #define HMASK (HSIZE-1)
61 const char *MD5CacheFile;
65 struct Node *no_HNext;
84 struct hlink *hltable[HASHF];
86 typedef struct MD5Node {
87 struct MD5Node *md_Next;
94 void RemoveRecur(const char *dpath, dev_t devNo);
95 void InitList(List *list);
96 void ResetList(List *list);
97 int AddList(List *list, const char *name, int n);
98 static struct hlink *hltlookup(struct stat *);
99 static struct hlink *hltadd(struct stat *, const char *);
100 static int shash(const char *s);
101 static void hltdelete(struct hlink *);
102 int YesNo(const char *path);
103 static int xrename(const char *src, const char *dst, u_long flags);
104 static int xlink(const char *src, const char *dst, u_long flags);
105 int WildCmp(const char *s1, const char *s2);
106 static MD5Node *md5_lookup(const char *sfile);
107 static int md5_check(const char *spath, const char *dpath);
108 static void md5_flush(void);
109 static void md5_cache(const char *spath, int sdirlen);
110 static char *fextract(FILE *fi, int n, int *pc, int skip);
111 int DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo);
112 char *doMD5File(const char *filename, char *buf);
114 int AskConfirmation = 1;
122 const char *UseCpFile;
124 int64_t CountSourceBytes = 0;
125 int64_t CountSourceItems = 0;
126 int64_t CountCopiedItems = 0;
127 int64_t CountReadBytes = 0;
128 int64_t CountWriteBytes = 0;
129 int64_t CountRemovedItems = 0;
131 char *MD5SCache; /* cache source directory name */
137 main(int ac, char **av)
142 struct timeval start;
144 gettimeofday(&start, NULL);
145 for (i = 1; i < ac; ++i) {
152 } else if (dst == NULL) {
155 fatal("too many arguments");
163 v = strtol(ptr, NULL, 0);
168 while (*ptr == 'v') {
172 if (*ptr >= '0' && *ptr <= '9')
173 VerboseOpt = strtol(ptr, NULL, 0);
182 UseCpFile = ".cpignore";
185 UseCpFile = (*ptr) ? ptr : av[++i];
201 MD5CacheFile = av[++i];
205 MD5CacheFile = ".MD5.CHECKSUMS";
208 setvbuf(stdout, NULL, _IOLBF, 0);
211 fatal("illegal option: %s\n", ptr - 2);
218 * dst may be NULL only if -m option is specified,
219 * which forces an update of the MD5 checksums
222 if (dst == NULL && UseMD5Opt == 0) {
227 i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
229 i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
233 if (SummaryOpt && i == 0) {
237 gettimeofday(&end, NULL);
238 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
239 CountReadBytes += sizeof(struct stat) * CountSourceItems;
240 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
241 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
243 duration = end.tv_sec - start.tv_sec;
245 duration += end.tv_usec - start.tv_usec;
246 if (duration == 0) duration = 1;
247 logstd("cpdup completed successfully\n");
248 logstd("%lld bytes source %lld bytes read %lld bytes written (%.1fX speedup)\n",
249 (long long)CountSourceBytes,
250 (long long)CountReadBytes,
251 (long long)CountWriteBytes,
252 ((double)CountSourceBytes * 2.0) / ((double)(CountReadBytes + CountWriteBytes)));
253 logstd("%lld source items %lld items copied %lld things deleted\n",
254 (long long)CountSourceItems,
255 (long long)CountCopiedItems,
256 (long long)CountRemovedItems);
257 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
258 (float)duration / (float)1000000,
259 (long)((long)1000000 * (CountReadBytes + CountWriteBytes) / duration / 1024.0),
260 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
262 exit((i == 0) ? 0 : 1);
265 static struct hlink *
266 hltlookup(struct stat *stp)
271 n = stp->st_ino % HASHF;
273 for (hl = hltable[n]; hl; hl = hl->next)
274 if (hl->ino == stp->st_ino)
280 static struct hlink *
281 hltadd(struct stat *stp, const char *path)
286 if (!(new = malloc(sizeof (struct hlink)))) {
287 fprintf(stderr, "out of memory\n");
291 /* initialize and link the new element into the table */
292 new->ino = stp->st_ino;
294 strncpy(new->name, path, 2048);
297 n = stp->st_ino % HASHF;
298 new->next = hltable[n];
300 hltable[n]->prev = new;
307 hltdelete(struct hlink *hl)
311 hl->next->prev = hl->prev;
312 hl->prev->next = hl->next;
315 hl->next->prev = NULL;
317 hltable[hl->ino % HASHF] = hl->next;
324 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
328 int r, mres, st2Valid;
335 r = mres = st2Valid = 0;
339 if (lstat(spath, &st1) != 0)
341 st2.st_mode = 0; /* in case lstat fails */
342 st2.st_flags = 0; /* in case lstat fails */
343 if (dpath && lstat(dpath, &st2) == 0)
346 if (S_ISREG(st1.st_mode)) {
347 size = st1.st_blocks * 512;
348 if (st1.st_size % 512)
349 size += st1.st_size % 512 - 512;
356 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
357 if ((hln = hltlookup(&st1)) != NULL) {
361 if (st2.st_ino == hln->dino) {
363 * hard link is already correct, nothing to do
366 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
367 if (hln->nlinked == st1.st_nlink)
373 * hard link is not correct, attempt to unlink it
375 if (unlink(dpath) < 0) {
376 logerr("%-32s hardlink: unable to unlink: %s\n",
377 ((dpath) ? dpath : spath), strerror(errno));
384 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
385 logerr("%-32s hardlink: unable to link to %s: %s\n",
386 (dpath ? dpath : spath), hln->name, strerror(errno)
392 if (hln->nlinked == st1.st_nlink) {
398 logstd("%-32s hardlink: %s\n",
399 (dpath ? dpath : spath),
400 (st2Valid ? "relinked" : "linked")
410 * first instance of hardlink must be copied normally
412 hln = hltadd(&st1, dpath);
417 * Do we need to copy the file/dir/link/whatever? Early termination
418 * if we do not. Always traverse directories. Always redo links.
420 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
425 st1.st_mode == st2.st_mode &&
426 st1.st_flags == st2.st_flags
428 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
432 st1.st_size == st2.st_size &&
433 st1.st_uid == st2.st_uid &&
434 st1.st_gid == st2.st_gid &&
435 st1.st_mtime == st2.st_mtime
436 && (UseMD5Opt == 0 || (mres = md5_check(spath, dpath)) == 0)
439 hln->dino = st2.st_ino;
440 if (VerboseOpt >= 3) {
442 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
444 logstd("%-32s nochange\n", (dpath ? dpath : spath));
446 CountSourceBytes += size;
453 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
455 logerr("%-32s SAFETY - refusing to copy file over directory\n",
456 (dpath ? dpath : spath)
459 return(0); /* continue with the cpdup anyway */
461 if (QuietOpt == 0 || AskConfirmation) {
462 logstd("%-32s WARNING: non-directory source will blow away\n"
463 "%-32s preexisting dest directory, continuing anyway!\n",
464 ((dpath) ? dpath : spath), "");
467 RemoveRecur(dpath, ddevNo);
470 if (S_ISDIR(st1.st_mode)) {
473 if ((dir = opendir(spath)) != NULL) {
478 if (S_ISDIR(st2.st_mode) == 0) {
480 if (mkdir(dpath, st1.st_mode | 0700) != 0) {
481 logerr("%s: mkdir failed: %s\n",
482 (dpath ? dpath : spath), strerror(errno));
487 * Matt: why don't you check error codes here?
490 chown(dpath, st1.st_uid, st1.st_gid);
494 * Directory must be scanable by root for cpdup to
495 * work. We'll fix it later if the directory isn't
496 * supposed to be readable ( which is why we fixup
497 * st2.st_mode to match what we did ).
499 if ((st2.st_mode & 0700) != 0700) {
500 chmod(dpath, st2.st_mode | 0700);
504 logstd("%s\n", dpath ? dpath : spath);
508 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
514 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
521 * scan .cpignore file for files/directories
530 if (UseCpFile[0] == '/') {
531 fpath = mprintf("%s", UseCpFile);
533 fpath = mprintf("%s/%s", spath, UseCpFile);
535 AddList(&list, strrchr(fpath, '/') + 1, 1);
536 if ((fi = fopen(fpath, "r")) != NULL) {
537 while (fgets(buf, sizeof(buf), fi) != NULL) {
540 if (l && buf[l-1] == '\n')
542 if (buf[0] && buf[0] != '#')
543 AddList(&list, buf, 1);
551 * Automatically exclude MD5CacheFile that we create on the
552 * source from the copy to the destination.
555 AddList(&list, MD5CacheFile, 1);
557 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
564 if (strcmp(den->d_name, ".") == 0 ||
565 strcmp(den->d_name, "..") == 0
570 * ignore if on .cpignore list
572 if (AddList(&list, den->d_name, 0) == 1) {
575 nspath = mprintf("%s/%s", spath, den->d_name);
577 ndpath = mprintf("%s/%s", dpath, den->d_name);
592 * Remove files/directories from destination that do not appear
595 if (dpath && (dir = opendir(dpath)) != NULL) {
596 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
600 if (strcmp(den->d_name, ".") == 0 ||
601 strcmp(den->d_name, "..") == 0
606 * If object does not exist in source or .cpignore
607 * then recursively remove it.
609 if (AddList(&list, den->d_name, 3) == 3) {
612 ndpath = mprintf("%s/%s", dpath, den->d_name);
613 RemoveRecur(ndpath, ddevNo);
623 st1.st_uid != st2.st_uid ||
624 st1.st_gid != st2.st_gid
626 chown(dpath, st1.st_uid, st1.st_gid);
628 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
629 chmod(dpath, st1.st_mode);
631 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
632 chflags(dpath, st1.st_flags);
636 } else if (dpath == NULL) {
638 * If dpath is NULL, we are just updating the MD5
640 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
641 mres = md5_check(spath, NULL);
643 if (VerboseOpt > 1) {
645 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
647 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
648 } else if (!QuietOpt && mres < 0) {
649 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
652 } else if (S_ISREG(st1.st_mode)) {
657 path = mprintf("%s.tmp", dpath);
660 * Handle check failure message.
663 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
665 if ((fd1 = open(spath, O_RDONLY)) >= 0) {
666 if ((fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
668 * There could be a .tmp file from a previously interrupted
669 * run, delete and retry. Fail if we still can't get at it.
673 fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
677 * Matt: I think 64k would be faster here
684 * Matt: What about holes?
687 while ((n = read(fd1, buf, sizeof(buf))) > 0) {
689 if (write(fd2, buf, n) != n)
695 struct timeval tv[2];
697 bzero(tv, sizeof(tv));
698 tv[0].tv_sec = st1.st_mtime;
699 tv[1].tv_sec = st1.st_mtime;
702 chown(path, st1.st_uid, st1.st_gid);
703 chmod(path, st1.st_mode);
704 if (xrename(path, dpath, st2.st_flags) != 0) {
705 logerr("%-32s rename-after-copy failed: %s\n",
706 (dpath ? dpath : spath), strerror(errno)
711 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
713 chflags(dpath, st1.st_flags);
715 CountReadBytes += size;
716 CountWriteBytes += size;
717 CountSourceBytes += size;
721 logerr("%-32s %s failed: %s\n",
722 (dpath ? dpath : spath), op, strerror(errno)
728 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
729 (dpath ? dpath : spath), getuid(), geteuid(),
736 logerr("%-32s copy: open failed: %s\n",
737 (dpath ? dpath : spath),
745 if (!r && stat(dpath, &st2) == 0)
746 hln->dino = st2.st_ino;
750 } else if (S_ISLNK(st1.st_mode)) {
757 snprintf(path, sizeof(path), "%s.tmp", dpath);
758 n1 = readlink(spath, link1, sizeof(link1) - 1);
759 n2 = readlink(dpath, link2, sizeof(link2) - 1);
761 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
765 if (symlink(link1, path) < 0) {
766 logerr("%-32s symlink (%s->%s) failed: %s\n",
767 (dpath ? dpath : spath), link1, path,
772 lchown(path, st1.st_uid, st1.st_gid);
774 * there is no lchmod() or lchflags(), we
775 * cannot chmod or chflags a softlink.
777 if (xrename(path, dpath, st2.st_flags) != 0) {
778 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
779 (dpath ? dpath : spath),
780 path, dpath, strerror(errno));
781 } else if (VerboseOpt) {
782 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
785 CountWriteBytes += n1;
790 logstd("%-32s nochange\n", (dpath ? dpath : spath));
792 CountSourceBytes += n1;
793 CountReadBytes += n1;
794 if (n2 > 0) CountReadBytes += n2;
798 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
800 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
805 st1.st_mode != st2.st_mode ||
806 st1.st_rdev != st2.st_rdev ||
807 st1.st_uid != st2.st_uid ||
808 st1.st_gid != st2.st_gid
810 snprintf(path, sizeof(path), "%s.tmp", dpath);
813 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
814 chmod(path, st1.st_mode);
815 chown(path, st1.st_uid, st1.st_gid);
817 if (xrename(path, dpath, st2.st_flags) != 0) {
818 logerr("%-32s dev-rename-after-create failed: %s\n",
819 (dpath ? dpath : spath),
822 } else if (VerboseOpt) {
823 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
828 logerr("%-32s dev failed: %s\n",
829 (dpath ? dpath : spath), strerror(errno)
834 logstd("%-32s nochange\n", (dpath ? dpath : spath));
847 RemoveRecur(const char *dpath, dev_t devNo)
851 if (lstat(dpath, &st) == 0) {
854 if (st.st_dev == devNo) {
855 if (S_ISDIR(st.st_mode)) {
858 if ((dir = opendir(dpath)) != NULL) {
860 while ((den = readdir(dir)) != NULL) {
863 if (strcmp(den->d_name, ".") == 0)
865 if (strcmp(den->d_name, "..") == 0)
867 ndpath = mprintf("%s/%s", dpath, den->d_name);
868 RemoveRecur(ndpath, devNo);
873 if (AskConfirmation && NoRemoveOpt == 0) {
875 if (rmdir(dpath) < 0) {
876 logerr("%-32s rmdir failed: %s\n",
877 dpath, strerror(errno)
885 logstd("%-32s not-removed\n", dpath);
886 } else if (rmdir(dpath) == 0) {
888 logstd("%-32s rmdir-ok\n", dpath);
891 logerr("%-32s rmdir failed: %s\n",
892 dpath, strerror(errno)
897 if (AskConfirmation && NoRemoveOpt == 0) {
899 if (remove(dpath) < 0) {
900 logerr("%-32s remove failed: %s\n",
901 dpath, strerror(errno)
909 logstd("%-32s not-removed\n", dpath);
910 } else if (remove(dpath) == 0) {
912 logstd("%-32s remove-ok\n", dpath);
915 logerr("%-32s remove failed: %s\n",
916 dpath, strerror(errno)
928 bzero(list, sizeof(List));
929 list->li_Node.no_Next = &list->li_Node;
933 ResetList(List *list)
937 while ((node = list->li_Node.no_Next) != &list->li_Node) {
938 list->li_Node.no_Next = node->no_Next;
945 AddList(List *list, const char *name, int n)
953 * Scan against wildcards. Only a node value of 1 can be a wildcard
954 * ( usually scanned from .cpignore )
957 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
958 if (strcmp(name, node->no_Name) == 0 ||
959 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
961 return(node->no_Value);
966 * Look for exact match
969 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
970 if (strcmp(name, node->no_Name) == 0) {
971 return(node->no_Value);
974 node = malloc(sizeof(Node) + strlen(name) + 1);
976 fprintf(stderr, "out of memory\n");
980 node->no_Next = list->li_Node.no_Next;
981 list->li_Node.no_Next = node;
983 node->no_HNext = list->li_Hash[hv];
984 list->li_Hash[hv] = node;
986 strcpy(node->no_Name, name);
1000 if (*s == '*' || *s == '?' ||
1001 *s == '{' || *s == '}' ||
1002 *s == '[' || *s == ']' ||
1007 hv = (hv << 5) ^ *s ^ (hv >> 23);
1010 return(((hv >> 16) ^ hv) & HMASK);
1014 * WildCmp() - compare wild string to sane string
1016 * Return 0 on success, -1 on failure.
1020 WildCmp(const char *w, const char *s)
1023 * skip fixed portion
1029 if (w[1] == 0) /* optimize wild* case */
1035 for (i = 0; i <= l; ++i) {
1036 if (WildCmp(w + 1, s + i) == 0)
1050 if (*w == 0) /* terminator */
1062 YesNo(const char *path)
1066 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1069 first = ch = getchar();
1070 while (ch != '\n' && ch != EOF)
1072 return ((first == 'y' || first == 'Y'));
1078 if (MD5SCacheDirty && MD5SCache) {
1081 if ((fo = fopen(MD5SCache, "w")) != NULL) {
1084 for (node = MD5Base; node; node = node->md_Next) {
1085 if (node->md_Accessed && node->md_Code) {
1086 fprintf(fo, "%s %d %s\n",
1088 strlen(node->md_Name),
1102 while ((node = MD5Base) != NULL) {
1103 MD5Base = node->md_Next;
1106 free(node->md_Code);
1108 free(node->md_Name);
1117 md5_cache(const char *spath, int sdirlen)
1127 sdirlen == MD5SCacheDirLen &&
1128 strncmp(spath, MD5SCache, sdirlen) == 0
1134 * Different cache, flush old cache
1137 if (MD5SCache != NULL)
1144 MD5SCacheDirLen = sdirlen;
1145 MD5SCache = mprintf("%*.*s%s", sdirlen, sdirlen, spath, MD5CacheFile);
1147 if ((fi = fopen(MD5SCache, "r")) != NULL) {
1148 MD5Node **pnode = &MD5Base;
1153 MD5Node *node = *pnode = malloc(sizeof(MD5Node));
1159 if (pnode == NULL || node == NULL) {
1160 fprintf(stderr, "out of memory\n");
1164 bzero(node, sizeof(MD5Node));
1165 node->md_Code = fextract(fi, -1, &c, ' ');
1166 node->md_Accessed = 1;
1167 if ((s = fextract(fi, -1, &c, ' ')) != NULL) {
1168 nlen = strtol(s, NULL, 0);
1172 * extracting md_Name - name may contain embedded control
1175 CountReadBytes += nlen+1;
1176 node->md_Name = fextract(fi, nlen, &c, EOF);
1178 fprintf(stderr, "Error parsing MD5 Cache: %s (%c)\n", MD5SCache, c);
1179 while (c != EOF && c != '\n')
1184 pnode = &node->md_Next;
1191 * md5_lookup: lookup/create md5 entry
1195 md5_lookup(const char *sfile)
1200 for (pnode = &MD5Base; (node = *pnode) != NULL; pnode = &node->md_Next) {
1201 if (strcmp(sfile, node->md_Name) == 0) {
1207 if ((node = *pnode = malloc(sizeof(MD5Node))) == NULL) {
1208 fprintf(stderr,"out of memory\n");
1212 bzero(node, sizeof(MD5Node));
1213 node->md_Name = strdup(sfile);
1215 node->md_Accessed = 1;
1220 * md5_check: check MD5 against file
1222 * Return -1 if check failed
1223 * Return 0 if check succeeded
1225 * dpath can be NULL, in which case we are force-updating
1230 md5_check(const char *spath, const char *dpath)
1240 if ((sfile = strrchr(spath, '/')) != NULL)
1244 sdirlen = sfile - spath;
1246 md5_cache(spath, sdirlen);
1248 node = md5_lookup(sfile);
1251 * If dpath == NULL, we are force-updating the source .MD5* files
1254 if (dpath == NULL) {
1255 char *scode = doMD5File(spath, NULL);
1258 if (node->md_Code == NULL) {
1260 node->md_Code = scode;
1262 } else if (strcmp(scode, node->md_Code) != 0) {
1264 free(node->md_Code);
1265 node->md_Code = scode;
1274 * Otherwise the .MD5* file is used as a cache.
1277 if (node->md_Code == NULL) {
1278 node->md_Code = doMD5File(spath, NULL);
1282 dcode = doMD5File(dpath, NULL);
1284 if (strcmp(node->md_Code, dcode) == 0) {
1287 char *scode = doMD5File(spath, NULL);
1289 if (strcmp(node->md_Code, scode) == 0) {
1292 free(node->md_Code);
1293 node->md_Code = scode;
1295 if (strcmp(node->md_Code, dcode) == 0)
1305 * xrename() - rename with override
1307 * If the rename fails, attempt to override st_flags on the
1308 * destination and rename again. If that fails too, try to
1309 * set the flags back the way they were and give up.
1313 xrename(const char *src, const char *dst, u_long flags)
1319 if ((r = rename(src, dst)) < 0) {
1321 if ((r = rename(src, dst)) < 0)
1322 chflags(dst, flags);
1328 xlink(const char *src, const char *dst, u_long flags)
1334 if ((r = link(src, dst)) < 0) {
1338 chflags(src, flags);
1345 fextract(FILE *fi, int n, int *pc, int skip)
1354 imax = (n < 0) ? 64 : n + 1;
1358 fprintf(stderr, "out of memory\n");
1363 if (n == 0 || (n < 0 && (c == ' ' || c == '\n')))
1369 s = realloc(s, imax);
1371 fprintf(stderr, "out of memory\n");
1379 if (c == skip && skip != EOF)
1387 doMD5File(const char *filename, char *buf)
1391 if (stat(filename, &st) == 0) {
1392 u_int64_t size = st.st_blocks * 512;
1393 if (st.st_size % 512)
1394 size += st.st_size % 512 - 512;
1395 CountReadBytes += size;
1398 return MD5File(filename, buf);