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.6 2004/07/22 13:09:02 asmodai 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)
60 const char *MD5CacheFile;
64 struct Node *no_HNext;
83 typedef struct MD5Node {
84 struct MD5Node *md_Next;
91 void RemoveRecur(const char *dpath, dev_t devNo);
92 void InitList(List *list);
93 void ResetList(List *list);
94 int AddList(List *list, const char *name, int n);
95 static struct hlink *hltlookup(struct stat *);
96 static struct hlink *hltadd(struct stat *, const char *);
97 static int shash(const char *s);
98 static void hltdelete(struct hlink *);
99 int YesNo(const char *path);
100 static int xrename(const char *src, const char *dst, u_long flags);
101 static int xlink(const char *src, const char *dst, u_long flags);
102 int WildCmp(const char *s1, const char *s2);
103 static MD5Node *md5_lookup(const char *sfile);
104 static int md5_check(const char *spath, const char *dpath);
105 static void md5_flush(void);
106 static void md5_cache(const char *spath, int sdirlen);
107 static char *fextract(FILE *fi, int n, int *pc, int skip);
108 int DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo);
109 char *doMD5File(const char *filename, char *buf);
111 int AskConfirmation = 1;
119 const char *UseCpFile;
121 int64_t CountSourceBytes = 0;
122 int64_t CountSourceItems = 0;
123 int64_t CountCopiedItems = 0;
124 int64_t CountReadBytes = 0;
125 int64_t CountWriteBytes = 0;
126 int64_t CountRemovedItems = 0;
128 char *MD5SCache; /* cache source directory name */
135 main(int ac, char **av)
140 struct timeval start;
142 gettimeofday(&start, NULL);
143 for (i = 1; i < ac; ++i) {
150 } else if (dst == NULL) {
153 fatal("too many arguments");
161 v = strtol(ptr, NULL, 0);
166 while (*ptr == 'v') {
170 if (*ptr >= '0' && *ptr <= '9')
171 VerboseOpt = strtol(ptr, NULL, 0);
180 UseCpFile = ".cpignore";
183 UseCpFile = (*ptr) ? ptr : av[++i];
199 MD5CacheFile = av[++i];
203 MD5CacheFile = ".MD5.CHECKSUMS";
206 setvbuf(stdout, NULL, _IOLBF, 0);
209 fatal("illegal option: %s\n", ptr - 2);
216 * dst may be NULL only if -m option is specified,
217 * which forces an update of the MD5 checksums
220 if (dst == NULL && UseMD5Opt == 0) {
225 i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
227 i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
231 if (SummaryOpt && i == 0) {
235 gettimeofday(&end, NULL);
236 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
237 CountReadBytes += sizeof(struct stat) * CountSourceItems;
238 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
239 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
241 duration = end.tv_sec - start.tv_sec;
243 duration += end.tv_usec - start.tv_usec;
244 if (duration == 0) duration = 1;
245 logstd("cpdup completed sucessfully\n");
246 logstd("%lld bytes source %lld bytes read %lld bytes written (%.1fX speedup)\n",
247 (long long)CountSourceBytes,
248 (long long)CountReadBytes,
249 (long long)CountWriteBytes,
250 ((double)CountSourceBytes * 2.0) / ((double)(CountReadBytes + CountWriteBytes)));
251 logstd("%lld source items %lld items copied %lld things deleted\n",
252 (long long)CountSourceItems,
253 (long long)CountCopiedItems,
254 (long long)CountRemovedItems);
255 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
256 (float)duration / (float)1000000,
257 (long)((long)1000000 * (CountReadBytes + CountWriteBytes) / duration / 1024.0),
258 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
260 exit((i == 0) ? 0 : 1);
265 struct hlink *hltable[HASHF];
267 static struct hlink *
268 hltlookup(struct stat *stp)
273 n = stp->st_ino % HASHF;
275 for (hl = hltable[n]; hl; hl = hl->next)
276 if (hl->ino == stp->st_ino)
282 static struct hlink *
283 hltadd(struct stat *stp, const char *path)
288 if (!(new = (struct hlink *)malloc(sizeof (struct hlink)))) {
289 fprintf(stderr, "out of memory\n");
293 /* initialize and link the new element into the table */
294 new->ino = stp->st_ino;
296 strncpy(new->name, path, 2048);
299 n = stp->st_ino % HASHF;
300 new->next = hltable[n];
302 hltable[n]->prev = new;
309 hltdelete(struct hlink *hl)
313 hl->next->prev = hl->prev;
314 hl->prev->next = hl->next;
317 hl->next->prev = NULL;
319 hltable[hl->ino % HASHF] = hl->next;
326 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
333 struct hlink *hln = NULL;
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)
948 int hv = shash(name);
951 * Scan against wildcards. Only a node value of 1 can be a wildcard
952 * ( usually scanned from .cpignore )
955 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
956 if (strcmp(name, node->no_Name) == 0 ||
957 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
959 return(node->no_Value);
964 * Look for exact match
967 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
968 if (strcmp(name, node->no_Name) == 0) {
969 return(node->no_Value);
972 node = malloc(sizeof(Node) + strlen(name) + 1);
974 node->no_Next = list->li_Node.no_Next;
975 list->li_Node.no_Next = node;
977 node->no_HNext = list->li_Hash[hv];
978 list->li_Hash[hv] = node;
980 strcpy(node->no_Name, name);
992 if (*s == '*' || *s == '?' ||
993 *s == '{' || *s == '}' ||
994 *s == '[' || *s == ']' ||
999 hv = (hv << 5) ^ *s ^ (hv >> 23);
1002 return(((hv >> 16) ^ hv) & HMASK);
1006 * WildCmp() - compare wild string to sane string
1008 * Return 0 on success, -1 on failure.
1012 WildCmp(const char *w, const char *s)
1015 * skip fixed portion
1021 if (w[1] == 0) /* optimize wild* case */
1027 for (i = 0; i <= l; ++i) {
1028 if (WildCmp(w + 1, s + i) == 0)
1042 if (*w == 0) /* terminator */
1054 YesNo(const char *path)
1058 (void)fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1059 (void)fflush(stderr);
1061 first = ch = getchar();
1062 while (ch != '\n' && ch != EOF)
1064 return ((first == 'y' || first == 'Y'));
1070 if (MD5SCacheDirty && MD5SCache) {
1073 if ((fo = fopen(MD5SCache, "w")) != NULL) {
1076 for (node = MD5Base; node; node = node->md_Next) {
1077 if (node->md_Accessed && node->md_Code) {
1078 fprintf(fo, "%s %d %s\n",
1080 strlen(node->md_Name),
1094 while ((node = MD5Base) != NULL) {
1095 MD5Base = node->md_Next;
1098 free(node->md_Code);
1100 free(node->md_Name);
1109 md5_cache(const char *spath, int sdirlen)
1119 sdirlen == MD5SCacheDirLen &&
1120 strncmp(spath, MD5SCache, sdirlen) == 0
1126 * Different cache, flush old cache
1129 if (MD5SCache != NULL)
1136 MD5SCacheDirLen = sdirlen;
1137 MD5SCache = mprintf("%*.*s%s", sdirlen, sdirlen, spath, MD5CacheFile);
1139 if ((fi = fopen(MD5SCache, "r")) != NULL) {
1140 MD5Node **pnode = &MD5Base;
1145 MD5Node *node = *pnode = malloc(sizeof(MD5Node));
1149 bzero(node, sizeof(MD5Node));
1150 node->md_Code = fextract(fi, -1, &c, ' ');
1151 node->md_Accessed = 1;
1152 if ((s = fextract(fi, -1, &c, ' ')) != NULL) {
1153 nlen = strtol(s, NULL, 0);
1157 * extracting md_Name - name may contain embedded control
1160 CountReadBytes += nlen+1;
1161 node->md_Name = fextract(fi, nlen, &c, EOF);
1163 fprintf(stderr, "Error parsing MD5 Cache: %s (%c)\n", MD5SCache, c);
1164 while (c != EOF && c != '\n')
1169 pnode = &node->md_Next;
1176 * md5_lookup: lookup/create md5 entry
1180 md5_lookup(const char *sfile)
1185 for (pnode = &MD5Base; (node = *pnode) != NULL; pnode = &node->md_Next) {
1186 if (strcmp(sfile, node->md_Name) == 0) {
1191 node = *pnode = malloc(sizeof(MD5Node));
1192 bzero(node, sizeof(MD5Node));
1193 node->md_Name = strdup(sfile);
1195 node->md_Accessed = 1;
1200 * md5_check: check MD5 against file
1202 * Return -1 if check failed
1203 * Return 0 if check succeeded
1205 * dpath can be NULL, in which case we are force-updating
1210 md5_check(const char *spath, const char *dpath)
1218 if ((sfile = strrchr(spath, '/')) != NULL)
1222 sdirlen = sfile - spath;
1224 md5_cache(spath, sdirlen);
1226 node = md5_lookup(sfile);
1229 * If dpath == NULL, we are force-updating the source .MD5* files
1232 if (dpath == NULL) {
1233 char *scode = doMD5File(spath, NULL);
1236 if (node->md_Code == NULL) {
1238 node->md_Code = scode;
1240 } else if (strcmp(scode, node->md_Code) != 0) {
1242 free(node->md_Code);
1243 node->md_Code = scode;
1252 * Otherwise the .MD5* file is used as a cache.
1255 if (node->md_Code == NULL) {
1256 node->md_Code = doMD5File(spath, NULL);
1260 dcode = doMD5File(dpath, NULL);
1262 if (strcmp(node->md_Code, dcode) == 0) {
1265 char *scode = doMD5File(spath, NULL);
1267 if (strcmp(node->md_Code, scode) == 0) {
1270 free(node->md_Code);
1271 node->md_Code = scode;
1273 if (strcmp(node->md_Code, dcode) == 0)
1283 * xrename() - rename with override
1285 * If the rename fails, attempt to override st_flags on the
1286 * destination and rename again. If that fails too, try to
1287 * set the flags back the way they were and give up.
1291 xrename(const char *src, const char *dst, u_long flags)
1295 if ((r = rename(src, dst)) < 0) {
1297 if ((r = rename(src, dst)) < 0)
1298 chflags(dst, flags);
1304 xlink(const char *src, const char *dst, u_long flags)
1309 if ((r = link(src, dst)) < 0) {
1313 chflags(src, flags);
1320 fextract(FILE *fi, int n, int *pc, int skip)
1323 int imax = (n < 0) ? 64 : n + 1;
1324 char *s = malloc(imax);
1328 if (n == 0 || (n < 0 && (c == ' ' || c == '\n')))
1334 s = realloc(s, imax);
1340 if (c == skip && skip != EOF)
1348 doMD5File(const char *filename, char *buf)
1352 if (stat(filename, &st) == 0) {
1353 u_int64_t size = st.st_blocks * 512;
1354 if (st.st_size % 512)
1355 size += st.st_size % 512 - 512;
1356 CountReadBytes += size;
1359 return MD5File(filename, buf);