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.4 2004/06/09 07:40:15 cpressey 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 struct hlink *hltlookup(struct stat *);
96 struct hlink *hltadd(struct stat *, const char *);
97 int shash(const char *s);
98 void hltdelete(struct hlink *);
99 int YesNo(const char *path);
100 int xrename(const char *src, const char *dst, u_long flags);
101 int xlink(const char *src, const char *dst, u_long flags);
102 int WildCmp(const char *s1, const char *s2);
103 MD5Node *md5_lookup(const char *sfile);
104 int md5_check(const char *spath, const char *dpath);
105 void md5_flush(void);
106 void md5_cache(const char *spath, int sdirlen);
107 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;
120 const char *UseCpFile;
122 int64_t CountSourceBytes = 0;
123 int64_t CountSourceItems = 0;
124 int64_t CountCopiedBytes = 0;
125 int64_t CountCopiedItems = 0;
126 int64_t CountReadBytes = 0;
127 int64_t CountWriteBytes = 0;
128 int64_t CountRemovedItems = 0;
130 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 sucessfully\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);
267 struct hlink *hltable[HASHF];
270 hltlookup(struct stat *stp)
275 n = stp->st_ino % HASHF;
277 for (hl = hltable[n]; hl; hl = hl->next)
278 if (hl->ino == stp->st_ino)
285 hltadd(struct stat *stp, const char *path)
290 if (!(new = (struct hlink *)malloc(sizeof (struct hlink)))) {
291 fprintf(stderr, "out of memory\n");
295 /* initialize and link the new element into the table */
296 new->ino = stp->st_ino;
298 strncpy(new->name, path, 2048);
301 n = stp->st_ino % HASHF;
302 new->next = hltable[n];
304 hltable[n]->prev = new;
311 hltdelete(struct hlink *hl)
315 hl->next->prev = hl->prev;
316 hl->prev->next = hl->next;
319 hl->next->prev = NULL;
321 hltable[hl->ino % HASHF] = hl->next;
328 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
335 struct hlink *hln = NULL;
341 if (lstat(spath, &st1) != 0)
343 st2.st_mode = 0; /* in case lstat fails */
344 st2.st_flags = 0; /* in case lstat fails */
345 if (dpath && lstat(dpath, &st2) == 0)
348 if (S_ISREG(st1.st_mode)) {
349 size = st1.st_blocks * 512;
350 if (st1.st_size % 512)
351 size += st1.st_size % 512 - 512;
358 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
359 if ((hln = hltlookup(&st1)) != NULL) {
363 if (st2.st_ino == hln->dino) {
365 * hard link is already correct, nothing to do
368 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
369 if (hln->nlinked == st1.st_nlink)
375 * hard link is not correct, attempt to unlink it
377 if (unlink(dpath) < 0) {
378 logerr("%-32s hardlink: unable to unlink: %s\n",
379 ((dpath) ? dpath : spath), strerror(errno));
386 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
387 logerr("%-32s hardlink: unable to link to %s: %s\n",
388 (dpath ? dpath : spath), hln->name, strerror(errno)
394 if (hln->nlinked == st1.st_nlink) {
400 logstd("%-32s hardlink: %s\n",
401 (dpath ? dpath : spath),
402 (st2Valid ? "relinked" : "linked")
412 * first instance of hardlink must be copied normally
414 hln = hltadd(&st1, dpath);
419 * Do we need to copy the file/dir/link/whatever? Early termination
420 * if we do not. Always traverse directories. Always redo links.
422 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
427 st1.st_mode == st2.st_mode &&
428 st1.st_flags == st2.st_flags
430 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
434 st1.st_size == st2.st_size &&
435 st1.st_uid == st2.st_uid &&
436 st1.st_gid == st2.st_gid &&
437 st1.st_mtime == st2.st_mtime
438 && (UseMD5Opt == 0 || (mres = md5_check(spath, dpath)) == 0)
441 hln->dino = st2.st_ino;
442 if (VerboseOpt >= 3) {
444 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
446 logstd("%-32s nochange\n", (dpath ? dpath : spath));
448 CountSourceBytes += size;
455 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
457 logerr("%-32s SAFETY - refusing to copy file over directory\n",
458 (dpath ? dpath : spath)
461 return(0); /* continue with the cpdup anyway */
463 if (QuietOpt == 0 || AskConfirmation) {
464 logstd("%-32s WARNING: non-directory source will blow away\n"
465 "%-32s preexisting dest directory, continuing anyway!\n",
466 ((dpath) ? dpath : spath), "");
469 RemoveRecur(dpath, ddevNo);
472 if (S_ISDIR(st1.st_mode)) {
475 if ((dir = opendir(spath)) != NULL) {
480 if (S_ISDIR(st2.st_mode) == 0) {
482 if (mkdir(dpath, st1.st_mode | 0700) != 0) {
483 logerr("%s: mkdir failed: %s\n",
484 (dpath ? dpath : spath), strerror(errno));
489 * Matt: why don't you check error codes here?
492 chown(dpath, st1.st_uid, st1.st_gid);
496 * Directory must be scanable by root for cpdup to
497 * work. We'll fix it later if the directory isn't
498 * supposed to be readable ( which is why we fixup
499 * st2.st_mode to match what we did ).
501 if ((st2.st_mode & 0700) != 0700) {
502 chmod(dpath, st2.st_mode | 0700);
506 logstd("%s\n", dpath ? dpath : spath);
510 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
516 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
523 * scan .cpignore file for files/directories
532 if (UseCpFile[0] == '/') {
533 fpath = mprintf("%s", UseCpFile);
535 fpath = mprintf("%s/%s", spath, UseCpFile);
537 AddList(&list, strrchr(fpath, '/') + 1, 1);
538 if ((fi = fopen(fpath, "r")) != NULL) {
539 while (fgets(buf, sizeof(buf), fi) != NULL) {
542 if (l && buf[l-1] == '\n')
544 if (buf[0] && buf[0] != '#')
545 AddList(&list, buf, 1);
553 * Automatically exclude MD5CacheFile that we create on the
554 * source from the copy to the destination.
557 AddList(&list, MD5CacheFile, 1);
559 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
566 if (strcmp(den->d_name, ".") == 0 ||
567 strcmp(den->d_name, "..") == 0
572 * ignore if on .cpignore list
574 if (AddList(&list, den->d_name, 0) == 1) {
577 nspath = mprintf("%s/%s", spath, den->d_name);
579 ndpath = mprintf("%s/%s", dpath, den->d_name);
594 * Remove files/directories from destination that do not appear
597 if (dpath && (dir = opendir(dpath)) != NULL) {
598 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
602 if (strcmp(den->d_name, ".") == 0 ||
603 strcmp(den->d_name, "..") == 0
608 * If object does not exist in source or .cpignore
609 * then recursively remove it.
611 if (AddList(&list, den->d_name, 3) == 3) {
614 ndpath = mprintf("%s/%s", dpath, den->d_name);
615 RemoveRecur(ndpath, ddevNo);
625 st1.st_uid != st2.st_uid ||
626 st1.st_gid != st2.st_gid
628 chown(dpath, st1.st_uid, st1.st_gid);
630 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
631 chmod(dpath, st1.st_mode);
633 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
634 chflags(dpath, st1.st_flags);
638 } else if (dpath == NULL) {
640 * If dpath is NULL, we are just updating the MD5
642 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
643 mres = md5_check(spath, NULL);
645 if (VerboseOpt > 1) {
647 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
649 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
650 } else if (!QuietOpt && mres < 0) {
651 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
654 } else if (S_ISREG(st1.st_mode)) {
659 path = mprintf("%s.tmp", dpath);
662 * Handle check failure message.
665 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
667 if ((fd1 = open(spath, O_RDONLY)) >= 0) {
668 if ((fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
670 * There could be a .tmp file from a previously interrupted
671 * run, delete and retry. Fail if we still can't get at it.
675 fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
679 * Matt: I think 64k would be faster here
686 * Matt: What about holes?
689 while ((n = read(fd1, buf, sizeof(buf))) > 0) {
691 if (write(fd2, buf, n) != n)
697 struct timeval tv[2];
699 bzero(tv, sizeof(tv));
700 tv[0].tv_sec = st1.st_mtime;
701 tv[1].tv_sec = st1.st_mtime;
704 chown(path, st1.st_uid, st1.st_gid);
705 chmod(path, st1.st_mode);
706 if (xrename(path, dpath, st2.st_flags) != 0) {
707 logerr("%-32s rename-after-copy failed: %s\n",
708 (dpath ? dpath : spath), strerror(errno)
713 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
715 chflags(dpath, st1.st_flags);
717 CountReadBytes += size;
718 CountWriteBytes += size;
719 CountSourceBytes += size;
723 logerr("%-32s %s failed: %s\n",
724 (dpath ? dpath : spath), op, strerror(errno)
730 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
731 (dpath ? dpath : spath), getuid(), geteuid(),
738 logerr("%-32s copy: open failed: %s\n",
739 (dpath ? dpath : spath),
747 if (!r && stat(dpath, &st2) == 0)
748 hln->dino = st2.st_ino;
752 } else if (S_ISLNK(st1.st_mode)) {
759 snprintf(path, sizeof(path), "%s.tmp", dpath);
760 n1 = readlink(spath, link1, sizeof(link1) - 1);
761 n2 = readlink(dpath, link2, sizeof(link2) - 1);
763 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
767 if (symlink(link1, path) < 0) {
768 logerr("%-32s symlink (%s->%s) failed: %s\n",
769 (dpath ? dpath : spath), link1, path,
774 lchown(path, st1.st_uid, st1.st_gid);
776 * there is no lchmod() or lchflags(), we
777 * cannot chmod or chflags a softlink.
779 if (xrename(path, dpath, st2.st_flags) != 0) {
780 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
781 (dpath ? dpath : spath),
782 path, dpath, strerror(errno));
783 } else if (VerboseOpt) {
784 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
787 CountWriteBytes += n1;
792 logstd("%-32s nochange\n", (dpath ? dpath : spath));
794 CountSourceBytes += n1;
795 CountReadBytes += n1;
796 if (n2 > 0) CountReadBytes += n2;
800 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
802 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
807 st1.st_mode != st2.st_mode ||
808 st1.st_rdev != st2.st_rdev ||
809 st1.st_uid != st2.st_uid ||
810 st1.st_gid != st2.st_gid
812 snprintf(path, sizeof(path), "%s.tmp", dpath);
815 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
816 chmod(path, st1.st_mode);
817 chown(path, st1.st_uid, st1.st_gid);
819 if (xrename(path, dpath, st2.st_flags) != 0) {
820 logerr("%-32s dev-rename-after-create failed: %s\n",
821 (dpath ? dpath : spath),
824 } else if (VerboseOpt) {
825 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
830 logerr("%-32s dev failed: %s\n",
831 (dpath ? dpath : spath), strerror(errno)
836 logstd("%-32s nochange\n", (dpath ? dpath : spath));
849 RemoveRecur(const char *dpath, dev_t devNo)
853 if (lstat(dpath, &st) == 0) {
856 if (st.st_dev == devNo) {
857 if (S_ISDIR(st.st_mode)) {
860 if ((dir = opendir(dpath)) != NULL) {
862 while ((den = readdir(dir)) != NULL) {
865 if (strcmp(den->d_name, ".") == 0)
867 if (strcmp(den->d_name, "..") == 0)
869 ndpath = mprintf("%s/%s", dpath, den->d_name);
870 RemoveRecur(ndpath, devNo);
875 if (AskConfirmation && NoRemoveOpt == 0) {
877 if (rmdir(dpath) < 0) {
878 logerr("%-32s rmdir failed: %s\n",
879 dpath, strerror(errno)
887 logstd("%-32s not-removed\n", dpath);
888 } else if (rmdir(dpath) == 0) {
890 logstd("%-32s rmdir-ok\n", dpath);
893 logerr("%-32s rmdir failed: %s\n",
894 dpath, strerror(errno)
899 if (AskConfirmation && NoRemoveOpt == 0) {
901 if (remove(dpath) < 0) {
902 logerr("%-32s remove failed: %s\n",
903 dpath, strerror(errno)
911 logstd("%-32s not-removed\n", dpath);
912 } else if (remove(dpath) == 0) {
914 logstd("%-32s remove-ok\n", dpath);
917 logerr("%-32s remove failed: %s\n",
918 dpath, strerror(errno)
930 bzero(list, sizeof(List));
931 list->li_Node.no_Next = &list->li_Node;
935 ResetList(List *list)
939 while ((node = list->li_Node.no_Next) != &list->li_Node) {
940 list->li_Node.no_Next = node->no_Next;
947 AddList(List *list, const char *name, int n)
950 int hv = shash(name);
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 node->no_Next = list->li_Node.no_Next;
977 list->li_Node.no_Next = node;
979 node->no_HNext = list->li_Hash[hv];
980 list->li_Hash[hv] = node;
982 strcpy(node->no_Name, name);
994 if (*s == '*' || *s == '?' ||
995 *s == '{' || *s == '}' ||
996 *s == '[' || *s == ']' ||
1001 hv = (hv << 5) ^ *s ^ (hv >> 23);
1004 return(((hv >> 16) ^ hv) & HMASK);
1008 * WildCmp() - compare wild string to sane string
1010 * Return 0 on success, -1 on failure.
1014 WildCmp(const char *w, const char *s)
1017 * skip fixed portion
1023 if (w[1] == 0) /* optimize wild* case */
1029 for (i = 0; i <= l; ++i) {
1030 if (WildCmp(w + 1, s + i) == 0)
1044 if (*w == 0) /* terminator */
1056 YesNo(const char *path)
1060 (void)fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1061 (void)fflush(stderr);
1063 first = ch = getchar();
1064 while (ch != '\n' && ch != EOF)
1066 return ((first == 'y' || first == 'Y'));
1072 if (MD5SCacheDirty && MD5SCache) {
1075 if ((fo = fopen(MD5SCache, "w")) != NULL) {
1078 for (node = MD5Base; node; node = node->md_Next) {
1079 if (node->md_Accessed && node->md_Code) {
1080 fprintf(fo, "%s %d %s\n",
1082 strlen(node->md_Name),
1096 while ((node = MD5Base) != NULL) {
1097 MD5Base = node->md_Next;
1100 free(node->md_Code);
1102 free(node->md_Name);
1111 md5_cache(const char *spath, int sdirlen)
1121 sdirlen == MD5SCacheDirLen &&
1122 strncmp(spath, MD5SCache, sdirlen) == 0
1128 * Different cache, flush old cache
1131 if (MD5SCache != NULL)
1138 MD5SCacheDirLen = sdirlen;
1139 MD5SCache = mprintf("%*.*s%s", sdirlen, sdirlen, spath, MD5CacheFile);
1141 if ((fi = fopen(MD5SCache, "r")) != NULL) {
1142 MD5Node **pnode = &MD5Base;
1147 MD5Node *node = *pnode = malloc(sizeof(MD5Node));
1151 bzero(node, sizeof(MD5Node));
1152 node->md_Code = fextract(fi, -1, &c, ' ');
1153 node->md_Accessed = 1;
1154 if ((s = fextract(fi, -1, &c, ' ')) != NULL) {
1155 nlen = strtol(s, NULL, 0);
1159 * extracting md_Name - name may contain embedded control
1162 CountReadBytes += nlen+1;
1163 node->md_Name = fextract(fi, nlen, &c, EOF);
1165 fprintf(stderr, "Error parsing MD5 Cache: %s (%c)\n", MD5SCache, c);
1166 while (c != EOF && c != '\n')
1171 pnode = &node->md_Next;
1178 * md5_lookup: lookup/create md5 entry
1182 md5_lookup(const char *sfile)
1187 for (pnode = &MD5Base; (node = *pnode) != NULL; pnode = &node->md_Next) {
1188 if (strcmp(sfile, node->md_Name) == 0) {
1193 node = *pnode = malloc(sizeof(MD5Node));
1194 bzero(node, sizeof(MD5Node));
1195 node->md_Name = strdup(sfile);
1197 node->md_Accessed = 1;
1202 * md5_check: check MD5 against file
1204 * Return -1 if check failed
1205 * Return 0 if check succeeded
1207 * dpath can be NULL, in which case we are force-updating
1212 md5_check(const char *spath, const char *dpath)
1220 if ((sfile = strrchr(spath, '/')) != NULL)
1224 sdirlen = sfile - spath;
1226 md5_cache(spath, sdirlen);
1228 node = md5_lookup(sfile);
1231 * If dpath == NULL, we are force-updating the source .MD5* files
1234 if (dpath == NULL) {
1235 char *scode = doMD5File(spath, NULL);
1238 if (node->md_Code == NULL) {
1240 node->md_Code = scode;
1242 } else if (strcmp(scode, node->md_Code) != 0) {
1244 free(node->md_Code);
1245 node->md_Code = scode;
1254 * Otherwise the .MD5* file is used as a cache.
1257 if (node->md_Code == NULL) {
1258 node->md_Code = doMD5File(spath, NULL);
1262 dcode = doMD5File(dpath, NULL);
1264 if (strcmp(node->md_Code, dcode) == 0) {
1267 char *scode = doMD5File(spath, NULL);
1269 if (strcmp(node->md_Code, scode) == 0) {
1272 free(node->md_Code);
1273 node->md_Code = scode;
1275 if (strcmp(node->md_Code, dcode) == 0)
1285 * xrename() - rename with override
1287 * If the rename fails, attempt to override st_flags on the
1288 * destination and rename again. If that fails too, try to
1289 * set the flags back the way they were and give up.
1293 xrename(const char *src, const char *dst, u_long flags)
1297 if ((r = rename(src, dst)) < 0) {
1299 if ((r = rename(src, dst)) < 0)
1300 chflags(dst, flags);
1306 xlink(const char *src, const char *dst, u_long flags)
1311 if ((r = link(src, dst)) < 0) {
1315 chflags(src, flags);
1322 fextract(FILE *fi, int n, int *pc, int skip)
1325 int imax = (n < 0) ? 64 : n + 1;
1326 char *s = malloc(imax);
1330 if (n == 0 || (n < 0 && (c == ' ' || c == '\n')))
1336 s = realloc(s, imax);
1342 if (c == skip && skip != EOF)
1350 doMD5File(const char *filename, char *buf)
1354 if (stat(filename, &st) == 0) {
1355 u_int64_t size = st.st_blocks * 512;
1356 if (st.st_size % 512)
1357 size += st.st_size % 512 - 512;
1358 CountReadBytes += size;
1361 return MD5File(filename, buf);