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.2 2003/12/01 06:07:16 dillon 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)
64 struct Node *no_HNext;
83 void RemoveRecur(const char *dpath, int devNo);
84 void InitList(List *list);
85 void ResetList(List *list);
86 int AddList(List *list, const char *name, int n);
87 struct hlink *hltlookup(struct stat *);
88 struct hlink *hltadd(struct stat *, const char *);
89 int shash(const char *s);
90 void hltdelete(struct hlink *);
91 int YesNo(const char *path);
92 int xrename(const char *src, const char *dst, u_long flags);
93 int xlink(const char *src, const char *dst, u_long flags);
94 int WildCmp(const char *s1, const char *s2);
95 int md5_check(const char *spath, const char *dpath);
97 void md5_cache(const char *spath, int sdirlen);
98 char *fextract(FILE *fi, int n, int *pc, int skip);
99 int DoCopy(const char *spath, const char *dpath, int sdevNo, int ddevNo);
100 char *doMD5File(const char *filename, char *buf);
102 int AskConfirmation = 1;
113 int64_t CountSourceBytes = 0;
114 int64_t CountSourceItems = 0;
115 int64_t CountCopiedBytes = 0;
116 int64_t CountCopiedItems = 0;
117 int64_t CountReadBytes = 0;
118 int64_t CountWriteBytes = 0;
119 int64_t CountRemovedItems = 0;
123 main(int ac, char **av)
128 struct timeval start;
130 gettimeofday(&start, NULL);
131 for (i = 1; i < ac; ++i) {
138 } else if (dst == NULL) {
141 fatal("too many arguments");
149 v = strtol(ptr, NULL, 0);
154 while (*ptr == 'v') {
158 if (*ptr >= '0' && *ptr <= '9')
159 VerboseOpt = strtol(ptr, NULL, 0);
168 UseCpFile = ".cpignore";
171 UseCpFile = (*ptr) ? ptr : av[++i];
187 MD5CacheFile = av[++i];
191 MD5CacheFile = ".MD5.CHECKSUMS";
194 fatal("illegal option: %s\n", ptr - 2);
201 * dst may be NULL only if -m option is specified,
202 * which forces an update of the MD5 checksums
205 if (dst == NULL && UseMD5Opt == 0) {
210 i = DoCopy(src, dst, -1, -1);
212 i = DoCopy(src, NULL, -1, -1);
216 if (SummaryOpt && i == 0) {
220 gettimeofday(&end, NULL);
221 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
222 CountReadBytes += sizeof(struct stat) * CountSourceItems;
223 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
224 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
226 duration = end.tv_sec - start.tv_sec;
228 duration += end.tv_usec - start.tv_usec;
229 if (duration == 0) duration = 1;
230 logstd("cpdup completed sucessfully\n");
231 logstd("%lld bytes source %lld bytes read %lld bytes written (%.1fX speedup)\n",
232 (long long)CountSourceBytes,
233 (long long)CountReadBytes,
234 (long long)CountWriteBytes,
235 ((double)CountSourceBytes * 2.0) / ((double)(CountReadBytes + CountWriteBytes)));
236 logstd("%lld source items %lld items copied %lld things deleted\n",
237 (long long)CountSourceItems,
238 (long long)CountCopiedItems,
239 (long long)CountRemovedItems);
240 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
241 (float)duration / (float)1000000,
242 (long)((long)1000000 * (CountReadBytes + CountWriteBytes) / duration / 1024.0),
243 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
245 exit((i == 0) ? 0 : 1);
250 struct hlink *hltable[HASHF];
253 hltlookup(struct stat *stp)
258 n = stp->st_ino % HASHF;
260 for (hl = hltable[n]; hl; hl = hl->next)
261 if (hl->ino == stp->st_ino)
268 hltadd(struct stat *stp, const char *path)
273 if (!(new = (struct hlink *)malloc(sizeof (struct hlink)))) {
274 fprintf(stderr, "out of memory\n");
278 /* initialize and link the new element into the table */
279 new->ino = stp->st_ino;
281 strncpy(new->name, path, 2048);
284 n = stp->st_ino % HASHF;
285 new->next = hltable[n];
287 hltable[n]->prev = new;
294 hltdelete(struct hlink *hl)
298 hl->next->prev = hl->prev;
299 hl->prev->next = hl->next;
302 hl->next->prev = NULL;
304 hltable[hl->ino % HASHF] = hl->next;
311 DoCopy(const char *spath, const char *dpath, int sdevNo, int ddevNo)
318 struct hlink *hln = NULL;
324 if (lstat(spath, &st1) != 0)
326 st2.st_mode = 0; /* in case lstat fails */
327 st2.st_flags = 0; /* in case lstat fails */
328 if (dpath && lstat(dpath, &st2) == 0)
331 if (S_ISREG(st1.st_mode)) {
332 size = st1.st_blocks * 512;
333 if (st1.st_size % 512)
334 size += st1.st_size % 512 - 512;
341 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
342 if ((hln = hltlookup(&st1)) != NULL) {
346 if (st2.st_ino == hln->dino) {
348 * hard link is already correct, nothing to do
351 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
352 if (hln->nlinked == st1.st_nlink)
358 * hard link is not correct, attempt to unlink it
360 if (unlink(dpath) < 0) {
361 logerr("%-32s hardlink: unable to unlink: %s\n",
362 ((dpath) ? dpath : spath), strerror(errno));
369 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
370 logerr("%-32s hardlink: unable to link to %s: %s\n",
371 (dpath ? dpath : spath), hln->name, strerror(errno)
377 if (hln->nlinked == st1.st_nlink) {
383 logstd("%-32s hardlink: %s\n",
384 (dpath ? dpath : spath),
385 (st2Valid ? "relinked" : "linked")
395 * first instance of hardlink must be copied normally
397 hln = hltadd(&st1, dpath);
402 * Do we need to copy the file/dir/link/whatever? Early termination
403 * if we do not. Always traverse directories. Always redo links.
405 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
410 st1.st_mode == st2.st_mode &&
411 st1.st_flags == st2.st_flags
413 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
417 st1.st_size == st2.st_size &&
418 st1.st_uid == st2.st_uid &&
419 st1.st_gid == st2.st_gid &&
420 st1.st_mtime == st2.st_mtime
421 && (UseMD5Opt == 0 || (mres = md5_check(spath, dpath)) == 0)
424 hln->dino = st2.st_ino;
425 if (VerboseOpt >= 3) {
427 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
429 logstd("%-32s nochange\n", (dpath ? dpath : spath));
431 CountSourceBytes += size;
438 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
440 logerr("%-32s SAFETY - refusing to copy file over directory\n",
441 (dpath ? dpath : spath)
444 return(0); /* continue with the cpdup anyway */
446 if (QuietOpt == 0 || AskConfirmation) {
447 logstd("%-32s WARNING: non-directory source will blow away\n"
448 "%-32s preexisting dest directory, continuing anyway!\n",
449 ((dpath) ? dpath : spath), "");
452 RemoveRecur(dpath, ddevNo);
455 if (S_ISDIR(st1.st_mode)) {
458 if ((dir = opendir(spath)) != NULL) {
463 if (S_ISDIR(st2.st_mode) == 0) {
465 if (mkdir(dpath, st1.st_mode | 0700) != 0) {
466 logerr("%s: mkdir failed: %s\n",
467 (dpath ? dpath : spath), strerror(errno));
472 * Matt: why don't you check error codes here?
475 chown(dpath, st1.st_uid, st1.st_gid);
479 * Directory must be scanable by root for cpdup to
480 * work. We'll fix it later if the directory isn't
481 * supposed to be readable ( which is why we fixup
482 * st2.st_mode to match what we did ).
484 if ((st2.st_mode & 0700) != 0700) {
485 chmod(dpath, st2.st_mode | 0700);
489 logstd("%s\n", dpath ? dpath : spath);
493 if (sdevNo >= 0 && st1.st_dev != sdevNo) {
499 if (ddevNo >= 0 && st2.st_dev != ddevNo) {
506 * scan .cpignore file for files/directories
515 if (UseCpFile[0] == '/') {
516 fpath = mprintf("%s", UseCpFile);
518 fpath = mprintf("%s/%s", spath, UseCpFile);
520 AddList(&list, strrchr(fpath, '/') + 1, 1);
521 if ((fi = fopen(fpath, "r")) != NULL) {
522 while (fgets(buf, sizeof(buf), fi) != NULL) {
525 if (l && buf[l-1] == '\n')
527 if (buf[0] && buf[0] != '#')
528 AddList(&list, buf, 1);
536 * Automatically exclude MD5CacheFile that we create on the
537 * source from the copy to the destination.
540 AddList(&list, MD5CacheFile, 1);
542 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
549 if (strcmp(den->d_name, ".") == 0 ||
550 strcmp(den->d_name, "..") == 0
555 * ignore if on .cpignore list
557 if (AddList(&list, den->d_name, 0) == 1) {
560 nspath = mprintf("%s/%s", spath, den->d_name);
562 ndpath = mprintf("%s/%s", dpath, den->d_name);
577 * Remove files/directories from destination that do not appear
580 if (dpath && (dir = opendir(dpath)) != NULL) {
581 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
585 if (strcmp(den->d_name, ".") == 0 ||
586 strcmp(den->d_name, "..") == 0
591 * If object does not exist in source or .cpignore
592 * then recursively remove it.
594 if (AddList(&list, den->d_name, 3) == 3) {
597 ndpath = mprintf("%s/%s", dpath, den->d_name);
598 RemoveRecur(ndpath, ddevNo);
608 st1.st_uid != st2.st_uid ||
609 st1.st_gid != st2.st_gid
611 chown(dpath, st1.st_uid, st1.st_gid);
613 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
614 chmod(dpath, st1.st_mode);
616 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
617 chflags(dpath, st1.st_flags);
621 } else if (dpath == NULL) {
623 * If dpath is NULL, we are just updating the MD5
625 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
626 mres = md5_check(spath, NULL);
628 if (VerboseOpt > 1) {
630 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
632 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
633 } else if (!QuietOpt && mres < 0) {
634 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
637 } else if (S_ISREG(st1.st_mode)) {
642 path = mprintf("%s.tmp", dpath);
645 * Handle check failure message.
648 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
650 if ((fd1 = open(spath, O_RDONLY)) >= 0) {
651 if ((fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
653 * There could be a .tmp file from a previously interrupted
654 * run, delete and retry. Fail if we still can't get at it.
658 fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
662 * Matt: I think 64k would be faster here
669 * Matt: What about holes?
672 while ((n = read(fd1, buf, sizeof(buf))) > 0) {
674 if (write(fd2, buf, n) != n)
680 struct timeval tv[2];
682 bzero(tv, sizeof(tv));
683 tv[0].tv_sec = st1.st_mtime;
684 tv[1].tv_sec = st1.st_mtime;
687 chown(path, st1.st_uid, st1.st_gid);
688 chmod(path, st1.st_mode);
689 if (xrename(path, dpath, st2.st_flags) != 0) {
690 logerr("%-32s rename-after-copy failed: %s\n",
691 (dpath ? dpath : spath), strerror(errno)
696 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
698 chflags(dpath, st1.st_flags);
700 CountReadBytes += size;
701 CountWriteBytes += size;
702 CountSourceBytes += size;
706 logerr("%-32s %s failed: %s\n",
707 (dpath ? dpath : spath), op, strerror(errno)
713 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
714 (dpath ? dpath : spath), getuid(), geteuid(),
721 logerr("%-32s copy: open failed: %s\n",
722 (dpath ? dpath : spath),
730 if (!r && stat(dpath, &st2) == 0)
731 hln->dino = st2.st_ino;
735 } else if (S_ISLNK(st1.st_mode)) {
742 snprintf(path, sizeof(path), "%s.tmp", dpath);
743 n1 = readlink(spath, link1, sizeof(link1) - 1);
744 n2 = readlink(dpath, link2, sizeof(link2) - 1);
746 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
750 if (symlink(link1, path) < 0) {
751 logerr("%-32s symlink (%s->%s) failed: %s\n",
752 (dpath ? dpath : spath), link1, path,
757 lchown(path, st1.st_uid, st1.st_gid);
759 * there is no lchmod() or lchflags(), we
760 * cannot chmod or chflags a softlink.
762 if (xrename(path, dpath, st2.st_flags) != 0) {
763 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
764 (dpath ? dpath : spath),
765 path, dpath, strerror(errno));
766 } else if (VerboseOpt) {
767 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
770 CountWriteBytes += n1;
775 logstd("%-32s nochange\n", (dpath ? dpath : spath));
777 CountSourceBytes += n1;
778 CountReadBytes += n1;
779 if (n2 > 0) CountReadBytes += n2;
783 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
785 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
790 st1.st_mode != st2.st_mode ||
791 st1.st_rdev != st2.st_rdev ||
792 st1.st_uid != st2.st_uid ||
793 st1.st_gid != st2.st_gid
795 snprintf(path, sizeof(path), "%s.tmp", dpath);
798 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
799 chmod(path, st1.st_mode);
800 chown(path, st1.st_uid, st1.st_gid);
802 if (xrename(path, dpath, st2.st_flags) != 0) {
803 logerr("%-32s dev-rename-after-create failed: %s\n",
804 (dpath ? dpath : spath),
807 } else if (VerboseOpt) {
808 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
813 logerr("%-32s dev failed: %s\n",
814 (dpath ? dpath : spath), strerror(errno)
819 logstd("%-32s nochange\n", (dpath ? dpath : spath));
832 RemoveRecur(const char *dpath, int devNo)
836 if (lstat(dpath, &st) == 0) {
839 if (st.st_dev == devNo) {
840 if (S_ISDIR(st.st_mode)) {
843 if ((dir = opendir(dpath)) != NULL) {
845 while ((den = readdir(dir)) != NULL) {
848 if (strcmp(den->d_name, ".") == 0)
850 if (strcmp(den->d_name, "..") == 0)
852 ndpath = mprintf("%s/%s", dpath, den->d_name);
853 RemoveRecur(ndpath, devNo);
858 if (AskConfirmation && NoRemoveOpt == 0) {
860 if (rmdir(dpath) < 0) {
861 logerr("%-32s rmdir failed: %s\n",
862 dpath, strerror(errno)
870 logstd("%-32s not-removed\n", dpath);
871 } else if (rmdir(dpath) == 0) {
873 logstd("%-32s rmdir-ok\n", dpath);
876 logerr("%-32s rmdir failed: %s\n",
877 dpath, strerror(errno)
882 if (AskConfirmation && NoRemoveOpt == 0) {
884 if (remove(dpath) < 0) {
885 logerr("%-32s remove failed: %s\n",
886 dpath, strerror(errno)
894 logstd("%-32s not-removed\n", dpath);
895 } else if (remove(dpath) == 0) {
897 logstd("%-32s remove-ok\n", dpath);
900 logerr("%-32s remove failed: %s\n",
901 dpath, strerror(errno)
913 bzero(list, sizeof(List));
914 list->li_Node.no_Next = &list->li_Node;
918 ResetList(List *list)
922 while ((node = list->li_Node.no_Next) != &list->li_Node) {
923 list->li_Node.no_Next = node->no_Next;
930 AddList(List *list, const char *name, int n)
933 int hv = shash(name);
936 * Scan against wildcards. Only a node value of 1 can be a wildcard
937 * ( usually scanned from .cpignore )
940 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
941 if (strcmp(name, node->no_Name) == 0 ||
942 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
944 return(node->no_Value);
949 * Look for exact match
952 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
953 if (strcmp(name, node->no_Name) == 0) {
954 return(node->no_Value);
957 node = malloc(sizeof(Node) + strlen(name) + 1);
959 node->no_Next = list->li_Node.no_Next;
960 list->li_Node.no_Next = node;
962 node->no_HNext = list->li_Hash[hv];
963 list->li_Hash[hv] = node;
965 strcpy(node->no_Name, name);
977 if (*s == '*' || *s == '?' ||
978 *s == '{' || *s == '}' ||
979 *s == '[' || *s == ']' ||
984 hv = (hv << 5) ^ *s ^ (hv >> 23);
987 return(((hv >> 16) ^ hv) & HMASK);
991 * WildCmp() - compare wild string to sane string
993 * Return 0 on success, -1 on failure.
997 WildCmp(const char *w, const char *s)
1000 * skip fixed portion
1006 if (w[1] == 0) /* optimize wild* case */
1012 for (i = 0; i <= l; ++i) {
1013 if (WildCmp(w + 1, s + i) == 0)
1027 if (*w == 0) /* terminator */
1039 YesNo(const char *path)
1043 (void)fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1044 (void)fflush(stderr);
1046 first = ch = getchar();
1047 while (ch != '\n' && ch != EOF)
1049 return ((first == 'y' || first == 'Y'));
1052 typedef struct MD5Node {
1053 struct MD5Node *md_Next;
1059 char *MD5SCache; /* cache source directory name */
1061 int MD5SCacheDirLen;
1067 if (MD5SCacheDirty && MD5SCache) {
1070 if ((fo = fopen(MD5SCache, "w")) != NULL) {
1073 for (node = MD5Base; node; node = node->md_Next) {
1074 if (node->md_Accessed && node->md_Code) {
1075 fprintf(fo, "%s %d %s\n",
1077 strlen(node->md_Name),
1091 while ((node = MD5Base) != NULL) {
1092 MD5Base = node->md_Next;
1095 free(node->md_Code);
1097 free(node->md_Name);
1106 md5_cache(const char *spath, int sdirlen)
1116 sdirlen == MD5SCacheDirLen &&
1117 strncmp(spath, MD5SCache, sdirlen) == 0
1123 * Different cache, flush old cache
1126 if (MD5SCache != NULL)
1133 MD5SCacheDirLen = sdirlen;
1134 MD5SCache = mprintf("%*.*s%s", sdirlen, sdirlen, spath, MD5CacheFile);
1136 if ((fi = fopen(MD5SCache, "r")) != NULL) {
1137 MD5Node **pnode = &MD5Base;
1142 MD5Node *node = *pnode = malloc(sizeof(MD5Node));
1146 bzero(node, sizeof(MD5Node));
1147 node->md_Code = fextract(fi, -1, &c, ' ');
1148 node->md_Accessed = 1;
1149 if ((s = fextract(fi, -1, &c, ' ')) != NULL) {
1150 nlen = strtol(s, NULL, 0);
1154 * extracting md_Name - name may contain embedded control
1157 CountReadBytes += nlen+1;
1158 node->md_Name = fextract(fi, nlen, &c, EOF);
1160 fprintf(stderr, "Error parsing MD5 Cache: %s (%c)\n", MD5SCache, c);
1161 while (c != EOF && c != '\n')
1166 pnode = &node->md_Next;
1173 * md5_lookup: lookup/create md5 entry
1177 md5_lookup(const char *sfile)
1182 for (pnode = &MD5Base; (node = *pnode) != NULL; pnode = &node->md_Next) {
1183 if (strcmp(sfile, node->md_Name) == 0) {
1188 node = *pnode = malloc(sizeof(MD5Node));
1189 bzero(node, sizeof(MD5Node));
1190 node->md_Name = strdup(sfile);
1192 node->md_Accessed = 1;
1197 * md5_check: check MD5 against file
1199 * Return -1 if check failed
1200 * Return 0 if check succeeded
1202 * dpath can be NULL, in which case we are force-updating
1207 md5_check(const char *spath, const char *dpath)
1215 if ((sfile = strrchr(spath, '/')) != NULL)
1219 sdirlen = sfile - spath;
1221 md5_cache(spath, sdirlen);
1223 node = md5_lookup(sfile);
1226 * If dpath == NULL, we are force-updating the source .MD5* files
1229 if (dpath == NULL) {
1230 char *scode = doMD5File(spath, NULL);
1233 if (node->md_Code == NULL) {
1235 node->md_Code = scode;
1237 } else if (strcmp(scode, node->md_Code) != 0) {
1239 free(node->md_Code);
1240 node->md_Code = scode;
1249 * Otherwise the .MD5* file is used as a cache.
1252 if (node->md_Code == NULL) {
1253 node->md_Code = doMD5File(spath, NULL);
1257 dcode = doMD5File(dpath, NULL);
1259 if (strcmp(node->md_Code, dcode) == 0) {
1262 char *scode = doMD5File(spath, NULL);
1264 if (strcmp(node->md_Code, scode) == 0) {
1267 free(node->md_Code);
1268 node->md_Code = scode;
1270 if (strcmp(node->md_Code, dcode) == 0)
1280 * xrename() - rename with override
1282 * If the rename fails, attempt to override st_flags on the
1283 * destination and rename again. If that fails too, try to
1284 * set the flags back the way they were and give up.
1288 xrename(const char *src, const char *dst, u_long flags)
1292 if ((r = rename(src, dst)) < 0) {
1294 if ((r = rename(src, dst)) < 0)
1295 chflags(dst, flags);
1301 xlink(const char *src, const char *dst, u_long flags)
1306 if ((r = link(src, dst)) < 0) {
1310 chflags(src, flags);
1317 fextract(FILE *fi, int n, int *pc, int skip)
1320 int imax = (n < 0) ? 64 : n + 1;
1321 char *s = malloc(imax);
1325 if (n == 0 || (n < 0 && (c == ' ' || c == '\n')))
1331 s = realloc(s, imax);
1337 if (c == skip && skip != EOF)
1345 doMD5File(const char *filename, char *buf)
1349 if (stat(filename, &st) == 0) {
1350 u_int64_t size = st.st_blocks * 512;
1351 if (st.st_size % 512)
1352 size += st.st_size % 512 - 512;
1353 CountReadBytes += size;
1356 return MD5File(filename, buf);