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.3 2004/03/19 17:30:59 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 fatal("illegal option: %s\n", ptr - 2);
215 * dst may be NULL only if -m option is specified,
216 * which forces an update of the MD5 checksums
219 if (dst == NULL && UseMD5Opt == 0) {
224 i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
226 i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
230 if (SummaryOpt && i == 0) {
234 gettimeofday(&end, NULL);
235 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
236 CountReadBytes += sizeof(struct stat) * CountSourceItems;
237 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
238 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
240 duration = end.tv_sec - start.tv_sec;
242 duration += end.tv_usec - start.tv_usec;
243 if (duration == 0) duration = 1;
244 logstd("cpdup completed sucessfully\n");
245 logstd("%lld bytes source %lld bytes read %lld bytes written (%.1fX speedup)\n",
246 (long long)CountSourceBytes,
247 (long long)CountReadBytes,
248 (long long)CountWriteBytes,
249 ((double)CountSourceBytes * 2.0) / ((double)(CountReadBytes + CountWriteBytes)));
250 logstd("%lld source items %lld items copied %lld things deleted\n",
251 (long long)CountSourceItems,
252 (long long)CountCopiedItems,
253 (long long)CountRemovedItems);
254 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
255 (float)duration / (float)1000000,
256 (long)((long)1000000 * (CountReadBytes + CountWriteBytes) / duration / 1024.0),
257 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
259 exit((i == 0) ? 0 : 1);
264 struct hlink *hltable[HASHF];
267 hltlookup(struct stat *stp)
272 n = stp->st_ino % HASHF;
274 for (hl = hltable[n]; hl; hl = hl->next)
275 if (hl->ino == stp->st_ino)
282 hltadd(struct stat *stp, const char *path)
287 if (!(new = (struct hlink *)malloc(sizeof (struct hlink)))) {
288 fprintf(stderr, "out of memory\n");
292 /* initialize and link the new element into the table */
293 new->ino = stp->st_ino;
295 strncpy(new->name, path, 2048);
298 n = stp->st_ino % HASHF;
299 new->next = hltable[n];
301 hltable[n]->prev = new;
308 hltdelete(struct hlink *hl)
312 hl->next->prev = hl->prev;
313 hl->prev->next = hl->next;
316 hl->next->prev = NULL;
318 hltable[hl->ino % HASHF] = hl->next;
325 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
332 struct hlink *hln = NULL;
338 if (lstat(spath, &st1) != 0)
340 st2.st_mode = 0; /* in case lstat fails */
341 st2.st_flags = 0; /* in case lstat fails */
342 if (dpath && lstat(dpath, &st2) == 0)
345 if (S_ISREG(st1.st_mode)) {
346 size = st1.st_blocks * 512;
347 if (st1.st_size % 512)
348 size += st1.st_size % 512 - 512;
355 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
356 if ((hln = hltlookup(&st1)) != NULL) {
360 if (st2.st_ino == hln->dino) {
362 * hard link is already correct, nothing to do
365 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
366 if (hln->nlinked == st1.st_nlink)
372 * hard link is not correct, attempt to unlink it
374 if (unlink(dpath) < 0) {
375 logerr("%-32s hardlink: unable to unlink: %s\n",
376 ((dpath) ? dpath : spath), strerror(errno));
383 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
384 logerr("%-32s hardlink: unable to link to %s: %s\n",
385 (dpath ? dpath : spath), hln->name, strerror(errno)
391 if (hln->nlinked == st1.st_nlink) {
397 logstd("%-32s hardlink: %s\n",
398 (dpath ? dpath : spath),
399 (st2Valid ? "relinked" : "linked")
409 * first instance of hardlink must be copied normally
411 hln = hltadd(&st1, dpath);
416 * Do we need to copy the file/dir/link/whatever? Early termination
417 * if we do not. Always traverse directories. Always redo links.
419 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
424 st1.st_mode == st2.st_mode &&
425 st1.st_flags == st2.st_flags
427 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
431 st1.st_size == st2.st_size &&
432 st1.st_uid == st2.st_uid &&
433 st1.st_gid == st2.st_gid &&
434 st1.st_mtime == st2.st_mtime
435 && (UseMD5Opt == 0 || (mres = md5_check(spath, dpath)) == 0)
438 hln->dino = st2.st_ino;
439 if (VerboseOpt >= 3) {
441 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
443 logstd("%-32s nochange\n", (dpath ? dpath : spath));
445 CountSourceBytes += size;
452 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
454 logerr("%-32s SAFETY - refusing to copy file over directory\n",
455 (dpath ? dpath : spath)
458 return(0); /* continue with the cpdup anyway */
460 if (QuietOpt == 0 || AskConfirmation) {
461 logstd("%-32s WARNING: non-directory source will blow away\n"
462 "%-32s preexisting dest directory, continuing anyway!\n",
463 ((dpath) ? dpath : spath), "");
466 RemoveRecur(dpath, ddevNo);
469 if (S_ISDIR(st1.st_mode)) {
472 if ((dir = opendir(spath)) != NULL) {
477 if (S_ISDIR(st2.st_mode) == 0) {
479 if (mkdir(dpath, st1.st_mode | 0700) != 0) {
480 logerr("%s: mkdir failed: %s\n",
481 (dpath ? dpath : spath), strerror(errno));
486 * Matt: why don't you check error codes here?
489 chown(dpath, st1.st_uid, st1.st_gid);
493 * Directory must be scanable by root for cpdup to
494 * work. We'll fix it later if the directory isn't
495 * supposed to be readable ( which is why we fixup
496 * st2.st_mode to match what we did ).
498 if ((st2.st_mode & 0700) != 0700) {
499 chmod(dpath, st2.st_mode | 0700);
503 logstd("%s\n", dpath ? dpath : spath);
507 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
513 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
520 * scan .cpignore file for files/directories
529 if (UseCpFile[0] == '/') {
530 fpath = mprintf("%s", UseCpFile);
532 fpath = mprintf("%s/%s", spath, UseCpFile);
534 AddList(&list, strrchr(fpath, '/') + 1, 1);
535 if ((fi = fopen(fpath, "r")) != NULL) {
536 while (fgets(buf, sizeof(buf), fi) != NULL) {
539 if (l && buf[l-1] == '\n')
541 if (buf[0] && buf[0] != '#')
542 AddList(&list, buf, 1);
550 * Automatically exclude MD5CacheFile that we create on the
551 * source from the copy to the destination.
554 AddList(&list, MD5CacheFile, 1);
556 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
563 if (strcmp(den->d_name, ".") == 0 ||
564 strcmp(den->d_name, "..") == 0
569 * ignore if on .cpignore list
571 if (AddList(&list, den->d_name, 0) == 1) {
574 nspath = mprintf("%s/%s", spath, den->d_name);
576 ndpath = mprintf("%s/%s", dpath, den->d_name);
591 * Remove files/directories from destination that do not appear
594 if (dpath && (dir = opendir(dpath)) != NULL) {
595 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
599 if (strcmp(den->d_name, ".") == 0 ||
600 strcmp(den->d_name, "..") == 0
605 * If object does not exist in source or .cpignore
606 * then recursively remove it.
608 if (AddList(&list, den->d_name, 3) == 3) {
611 ndpath = mprintf("%s/%s", dpath, den->d_name);
612 RemoveRecur(ndpath, ddevNo);
622 st1.st_uid != st2.st_uid ||
623 st1.st_gid != st2.st_gid
625 chown(dpath, st1.st_uid, st1.st_gid);
627 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
628 chmod(dpath, st1.st_mode);
630 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
631 chflags(dpath, st1.st_flags);
635 } else if (dpath == NULL) {
637 * If dpath is NULL, we are just updating the MD5
639 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
640 mres = md5_check(spath, NULL);
642 if (VerboseOpt > 1) {
644 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
646 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
647 } else if (!QuietOpt && mres < 0) {
648 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
651 } else if (S_ISREG(st1.st_mode)) {
656 path = mprintf("%s.tmp", dpath);
659 * Handle check failure message.
662 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
664 if ((fd1 = open(spath, O_RDONLY)) >= 0) {
665 if ((fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
667 * There could be a .tmp file from a previously interrupted
668 * run, delete and retry. Fail if we still can't get at it.
672 fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
676 * Matt: I think 64k would be faster here
683 * Matt: What about holes?
686 while ((n = read(fd1, buf, sizeof(buf))) > 0) {
688 if (write(fd2, buf, n) != n)
694 struct timeval tv[2];
696 bzero(tv, sizeof(tv));
697 tv[0].tv_sec = st1.st_mtime;
698 tv[1].tv_sec = st1.st_mtime;
701 chown(path, st1.st_uid, st1.st_gid);
702 chmod(path, st1.st_mode);
703 if (xrename(path, dpath, st2.st_flags) != 0) {
704 logerr("%-32s rename-after-copy failed: %s\n",
705 (dpath ? dpath : spath), strerror(errno)
710 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
712 chflags(dpath, st1.st_flags);
714 CountReadBytes += size;
715 CountWriteBytes += size;
716 CountSourceBytes += size;
720 logerr("%-32s %s failed: %s\n",
721 (dpath ? dpath : spath), op, strerror(errno)
727 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
728 (dpath ? dpath : spath), getuid(), geteuid(),
735 logerr("%-32s copy: open failed: %s\n",
736 (dpath ? dpath : spath),
744 if (!r && stat(dpath, &st2) == 0)
745 hln->dino = st2.st_ino;
749 } else if (S_ISLNK(st1.st_mode)) {
756 snprintf(path, sizeof(path), "%s.tmp", dpath);
757 n1 = readlink(spath, link1, sizeof(link1) - 1);
758 n2 = readlink(dpath, link2, sizeof(link2) - 1);
760 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
764 if (symlink(link1, path) < 0) {
765 logerr("%-32s symlink (%s->%s) failed: %s\n",
766 (dpath ? dpath : spath), link1, path,
771 lchown(path, st1.st_uid, st1.st_gid);
773 * there is no lchmod() or lchflags(), we
774 * cannot chmod or chflags a softlink.
776 if (xrename(path, dpath, st2.st_flags) != 0) {
777 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
778 (dpath ? dpath : spath),
779 path, dpath, strerror(errno));
780 } else if (VerboseOpt) {
781 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
784 CountWriteBytes += n1;
789 logstd("%-32s nochange\n", (dpath ? dpath : spath));
791 CountSourceBytes += n1;
792 CountReadBytes += n1;
793 if (n2 > 0) CountReadBytes += n2;
797 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
799 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
804 st1.st_mode != st2.st_mode ||
805 st1.st_rdev != st2.st_rdev ||
806 st1.st_uid != st2.st_uid ||
807 st1.st_gid != st2.st_gid
809 snprintf(path, sizeof(path), "%s.tmp", dpath);
812 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
813 chmod(path, st1.st_mode);
814 chown(path, st1.st_uid, st1.st_gid);
816 if (xrename(path, dpath, st2.st_flags) != 0) {
817 logerr("%-32s dev-rename-after-create failed: %s\n",
818 (dpath ? dpath : spath),
821 } else if (VerboseOpt) {
822 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
827 logerr("%-32s dev failed: %s\n",
828 (dpath ? dpath : spath), strerror(errno)
833 logstd("%-32s nochange\n", (dpath ? dpath : spath));
846 RemoveRecur(const char *dpath, dev_t devNo)
850 if (lstat(dpath, &st) == 0) {
853 if (st.st_dev == devNo) {
854 if (S_ISDIR(st.st_mode)) {
857 if ((dir = opendir(dpath)) != NULL) {
859 while ((den = readdir(dir)) != NULL) {
862 if (strcmp(den->d_name, ".") == 0)
864 if (strcmp(den->d_name, "..") == 0)
866 ndpath = mprintf("%s/%s", dpath, den->d_name);
867 RemoveRecur(ndpath, devNo);
872 if (AskConfirmation && NoRemoveOpt == 0) {
874 if (rmdir(dpath) < 0) {
875 logerr("%-32s rmdir failed: %s\n",
876 dpath, strerror(errno)
884 logstd("%-32s not-removed\n", dpath);
885 } else if (rmdir(dpath) == 0) {
887 logstd("%-32s rmdir-ok\n", dpath);
890 logerr("%-32s rmdir failed: %s\n",
891 dpath, strerror(errno)
896 if (AskConfirmation && NoRemoveOpt == 0) {
898 if (remove(dpath) < 0) {
899 logerr("%-32s remove failed: %s\n",
900 dpath, strerror(errno)
908 logstd("%-32s not-removed\n", dpath);
909 } else if (remove(dpath) == 0) {
911 logstd("%-32s remove-ok\n", dpath);
914 logerr("%-32s remove failed: %s\n",
915 dpath, strerror(errno)
927 bzero(list, sizeof(List));
928 list->li_Node.no_Next = &list->li_Node;
932 ResetList(List *list)
936 while ((node = list->li_Node.no_Next) != &list->li_Node) {
937 list->li_Node.no_Next = node->no_Next;
944 AddList(List *list, const char *name, int n)
947 int hv = shash(name);
950 * Scan against wildcards. Only a node value of 1 can be a wildcard
951 * ( usually scanned from .cpignore )
954 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
955 if (strcmp(name, node->no_Name) == 0 ||
956 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
958 return(node->no_Value);
963 * Look for exact match
966 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
967 if (strcmp(name, node->no_Name) == 0) {
968 return(node->no_Value);
971 node = malloc(sizeof(Node) + strlen(name) + 1);
973 node->no_Next = list->li_Node.no_Next;
974 list->li_Node.no_Next = node;
976 node->no_HNext = list->li_Hash[hv];
977 list->li_Hash[hv] = node;
979 strcpy(node->no_Name, name);
991 if (*s == '*' || *s == '?' ||
992 *s == '{' || *s == '}' ||
993 *s == '[' || *s == ']' ||
998 hv = (hv << 5) ^ *s ^ (hv >> 23);
1001 return(((hv >> 16) ^ hv) & HMASK);
1005 * WildCmp() - compare wild string to sane string
1007 * Return 0 on success, -1 on failure.
1011 WildCmp(const char *w, const char *s)
1014 * skip fixed portion
1020 if (w[1] == 0) /* optimize wild* case */
1026 for (i = 0; i <= l; ++i) {
1027 if (WildCmp(w + 1, s + i) == 0)
1041 if (*w == 0) /* terminator */
1053 YesNo(const char *path)
1057 (void)fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1058 (void)fflush(stderr);
1060 first = ch = getchar();
1061 while (ch != '\n' && ch != EOF)
1063 return ((first == 'y' || first == 'Y'));
1069 if (MD5SCacheDirty && MD5SCache) {
1072 if ((fo = fopen(MD5SCache, "w")) != NULL) {
1075 for (node = MD5Base; node; node = node->md_Next) {
1076 if (node->md_Accessed && node->md_Code) {
1077 fprintf(fo, "%s %d %s\n",
1079 strlen(node->md_Name),
1093 while ((node = MD5Base) != NULL) {
1094 MD5Base = node->md_Next;
1097 free(node->md_Code);
1099 free(node->md_Name);
1108 md5_cache(const char *spath, int sdirlen)
1118 sdirlen == MD5SCacheDirLen &&
1119 strncmp(spath, MD5SCache, sdirlen) == 0
1125 * Different cache, flush old cache
1128 if (MD5SCache != NULL)
1135 MD5SCacheDirLen = sdirlen;
1136 MD5SCache = mprintf("%*.*s%s", sdirlen, sdirlen, spath, MD5CacheFile);
1138 if ((fi = fopen(MD5SCache, "r")) != NULL) {
1139 MD5Node **pnode = &MD5Base;
1144 MD5Node *node = *pnode = malloc(sizeof(MD5Node));
1148 bzero(node, sizeof(MD5Node));
1149 node->md_Code = fextract(fi, -1, &c, ' ');
1150 node->md_Accessed = 1;
1151 if ((s = fextract(fi, -1, &c, ' ')) != NULL) {
1152 nlen = strtol(s, NULL, 0);
1156 * extracting md_Name - name may contain embedded control
1159 CountReadBytes += nlen+1;
1160 node->md_Name = fextract(fi, nlen, &c, EOF);
1162 fprintf(stderr, "Error parsing MD5 Cache: %s (%c)\n", MD5SCache, c);
1163 while (c != EOF && c != '\n')
1168 pnode = &node->md_Next;
1175 * md5_lookup: lookup/create md5 entry
1179 md5_lookup(const char *sfile)
1184 for (pnode = &MD5Base; (node = *pnode) != NULL; pnode = &node->md_Next) {
1185 if (strcmp(sfile, node->md_Name) == 0) {
1190 node = *pnode = malloc(sizeof(MD5Node));
1191 bzero(node, sizeof(MD5Node));
1192 node->md_Name = strdup(sfile);
1194 node->md_Accessed = 1;
1199 * md5_check: check MD5 against file
1201 * Return -1 if check failed
1202 * Return 0 if check succeeded
1204 * dpath can be NULL, in which case we are force-updating
1209 md5_check(const char *spath, const char *dpath)
1217 if ((sfile = strrchr(spath, '/')) != NULL)
1221 sdirlen = sfile - spath;
1223 md5_cache(spath, sdirlen);
1225 node = md5_lookup(sfile);
1228 * If dpath == NULL, we are force-updating the source .MD5* files
1231 if (dpath == NULL) {
1232 char *scode = doMD5File(spath, NULL);
1235 if (node->md_Code == NULL) {
1237 node->md_Code = scode;
1239 } else if (strcmp(scode, node->md_Code) != 0) {
1241 free(node->md_Code);
1242 node->md_Code = scode;
1251 * Otherwise the .MD5* file is used as a cache.
1254 if (node->md_Code == NULL) {
1255 node->md_Code = doMD5File(spath, NULL);
1259 dcode = doMD5File(dpath, NULL);
1261 if (strcmp(node->md_Code, dcode) == 0) {
1264 char *scode = doMD5File(spath, NULL);
1266 if (strcmp(node->md_Code, scode) == 0) {
1269 free(node->md_Code);
1270 node->md_Code = scode;
1272 if (strcmp(node->md_Code, dcode) == 0)
1282 * xrename() - rename with override
1284 * If the rename fails, attempt to override st_flags on the
1285 * destination and rename again. If that fails too, try to
1286 * set the flags back the way they were and give up.
1290 xrename(const char *src, const char *dst, u_long flags)
1294 if ((r = rename(src, dst)) < 0) {
1296 if ((r = rename(src, dst)) < 0)
1297 chflags(dst, flags);
1303 xlink(const char *src, const char *dst, u_long flags)
1308 if ((r = link(src, dst)) < 0) {
1312 chflags(src, flags);
1319 fextract(FILE *fi, int n, int *pc, int skip)
1322 int imax = (n < 0) ? 64 : n + 1;
1323 char *s = malloc(imax);
1327 if (n == 0 || (n < 0 && (c == ' ' || c == '\n')))
1333 s = realloc(s, imax);
1339 if (c == skip && skip != EOF)
1347 doMD5File(const char *filename, char *buf)
1351 if (stat(filename, &st) == 0) {
1352 u_int64_t size = st.st_blocks * 512;
1353 if (st.st_size % 512)
1354 size += st.st_size % 512 - 512;
1355 CountReadBytes += size;
1358 return MD5File(filename, buf);