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 * - Is able to do incremental mirroring/backups via hardlinks from
46 * the 'previous' version (supplied with -H path).
48 * $DragonFly: src/bin/cpdup/cpdup.c,v 1.12 2006/07/05 17:20:37 dillon Exp $
52 * Example: cc -O cpdup.c -o cpdup -lmd
54 * ".MD5.CHECKSUMS" contains md5 checksumms for the current directory.
55 * This file is stored on the source.
61 #define HMASK (HSIZE-1)
63 #define HLMASK (HLSIZE - 1)
67 struct Node *no_HNext;
86 struct hlink *hltable[HLSIZE];
88 void RemoveRecur(const char *dpath, dev_t devNo);
89 void InitList(List *list);
90 void ResetList(List *list);
91 int AddList(List *list, const char *name, int n);
92 static struct hlink *hltlookup(struct stat *);
93 static struct hlink *hltadd(struct stat *, const char *);
94 static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
95 static int shash(const char *s);
96 static void hltdelete(struct hlink *);
97 int YesNo(const char *path);
98 static int xrename(const char *src, const char *dst, u_long flags);
99 static int xlink(const char *src, const char *dst, u_long flags);
100 int WildCmp(const char *s1, const char *s2);
101 int DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo);
103 int AskConfirmation = 1;
112 int EnableDirectoryRetries;
116 const char *UseCpFile;
117 const char *UseHLPath;
118 const char *MD5CacheFile;
119 const char *FSMIDCacheFile;
121 int64_t CountSourceBytes;
122 int64_t CountSourceItems;
123 int64_t CountCopiedItems;
124 int64_t CountReadBytes;
125 int64_t CountWriteBytes;
126 int64_t CountRemovedItems;
129 main(int ac, char **av)
134 struct timeval start;
136 gettimeofday(&start, NULL);
137 for (i = 1; i < ac; ++i) {
144 } else if (dst == NULL) {
147 fatal("too many arguments");
155 v = strtol(ptr, NULL, 0);
160 while (*ptr == 'v') {
164 if (*ptr >= '0' && *ptr <= '9')
165 VerboseOpt = strtol(ptr, NULL, 0);
174 UseCpFile = ".cpignore";
177 UseCpFile = (*ptr) ? ptr : av[++i];
180 UseHLPath = (*ptr) ? ptr : av[++i];
196 FSMIDCacheFile = ".FSMID.CHECK";
200 FSMIDCacheFile = av[++i];
204 MD5CacheFile = av[++i];
208 MD5CacheFile = ".MD5.CHECKSUMS";
211 setvbuf(stdout, NULL, _IOLBF, 0);
214 fatal("illegal option: %s\n", ptr - 2);
221 * dst may be NULL only if -m option is specified,
222 * which forces an update of the MD5 checksums
225 if (dst == NULL && UseMD5Opt == 0) {
230 DstBaseLen = strlen(dst);
231 i = DoCopy(src, dst, (dev_t)-1, (dev_t)-1);
233 i = DoCopy(src, NULL, (dev_t)-1, (dev_t)-1);
238 if (SummaryOpt && i == 0) {
242 gettimeofday(&end, NULL);
243 CountSourceBytes += sizeof(struct stat) * CountSourceItems;
244 CountReadBytes += sizeof(struct stat) * CountSourceItems;
245 CountWriteBytes += sizeof(struct stat) * CountCopiedItems;
246 CountWriteBytes += sizeof(struct stat) * CountRemovedItems;
248 duration = end.tv_sec - start.tv_sec;
250 duration += end.tv_usec - start.tv_usec;
251 if (duration == 0) duration = 1;
252 logstd("cpdup completed successfully\n");
253 logstd("%lld bytes source %lld bytes read %lld bytes written (%.1fX speedup)\n",
254 (long long)CountSourceBytes,
255 (long long)CountReadBytes,
256 (long long)CountWriteBytes,
257 ((double)CountSourceBytes * 2.0) / ((double)(CountReadBytes + CountWriteBytes)));
258 logstd("%lld source items %lld items copied %lld things deleted\n",
259 (long long)CountSourceItems,
260 (long long)CountCopiedItems,
261 (long long)CountRemovedItems);
262 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
263 (float)duration / (float)1000000,
264 (long)((long)1000000 * (CountReadBytes + CountWriteBytes) / duration / 1024.0),
265 (long)((long)1000000 * CountSourceBytes / duration / 1024.0));
267 exit((i == 0) ? 0 : 1);
270 static struct hlink *
271 hltlookup(struct stat *stp)
276 n = stp->st_ino & HLMASK;
278 for (hl = hltable[n]; hl; hl = hl->next)
279 if (hl->ino == stp->st_ino)
285 static struct hlink *
286 hltadd(struct stat *stp, const char *path)
291 if (!(new = malloc(sizeof (struct hlink)))) {
292 fprintf(stderr, "out of memory\n");
296 /* initialize and link the new element into the table */
297 new->ino = stp->st_ino;
299 strncpy(new->name, path, 2048);
302 n = stp->st_ino & HLMASK;
303 new->next = hltable[n];
305 hltable[n]->prev = new;
312 hltdelete(struct hlink *hl)
316 hl->next->prev = hl->prev;
317 hl->prev->next = hl->next;
320 hl->next->prev = NULL;
322 hltable[hl->ino & HLMASK] = hl->next;
329 * If UseHLPath is defined check to see if the file in question is
330 * the same as the source file, and if it is return a pointer to the
331 * -H path based file for hardlinking. Else return NULL.
334 checkHLPath(struct stat *st1, const char *spath, const char *dpath)
342 asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen);
345 * stat info matches ?
347 if (stat(hpath, &sthl) < 0 ||
348 st1->st_size != sthl.st_size ||
349 st1->st_uid != sthl.st_uid ||
350 st1->st_gid != sthl.st_gid ||
351 st1->st_mtime != sthl.st_mtime
358 * If ForceOpt is set we have to compare the files
361 fd1 = open(spath, O_RDONLY);
362 fd2 = open(hpath, O_RDONLY);
365 if (fd1 >= 0 && fd2 >= 0) {
368 while ((n = read(fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
369 if (read(fd2, IOBuf2, sizeof(IOBuf2)) != n)
371 if (bcmp(IOBuf1, IOBuf2, n) != 0)
390 DoCopy(const char *spath, const char *dpath, dev_t sdevNo, dev_t ddevNo)
394 int r, mres, fres, st2Valid;
400 r = mres = fres = st2Valid = 0;
404 if (lstat(spath, &st1) != 0)
406 st2.st_mode = 0; /* in case lstat fails */
407 st2.st_flags = 0; /* in case lstat fails */
408 if (dpath && lstat(dpath, &st2) == 0)
411 if (S_ISREG(st1.st_mode)) {
412 size = st1.st_blocks * 512;
413 if (st1.st_size % 512)
414 size += st1.st_size % 512 - 512;
421 if (S_ISREG(st1.st_mode) && st1.st_nlink > 1 && dpath) {
422 if ((hln = hltlookup(&st1)) != NULL) {
426 if (st2.st_ino == hln->dino) {
428 * hard link is already correct, nothing to do
431 logstd("%-32s nochange\n", (dpath) ? dpath : spath);
432 if (hln->nlinked == st1.st_nlink)
438 * hard link is not correct, attempt to unlink it
440 if (unlink(dpath) < 0) {
441 logerr("%-32s hardlink: unable to unlink: %s\n",
442 ((dpath) ? dpath : spath), strerror(errno));
449 if (xlink(hln->name, dpath, st1.st_flags) < 0) {
450 int tryrelink = (errno == EMLINK);
451 logerr("%-32s hardlink: unable to link to %s: %s\n",
452 (dpath ? dpath : spath), hln->name, strerror(errno)
457 logerr("%-20s hardlink: will attempt to copy normally\n");
462 if (hln->nlinked == st1.st_nlink) {
468 logstd("%-32s hardlink: %s\n",
469 (dpath ? dpath : spath),
470 (st2Valid ? "relinked" : "linked")
480 * first instance of hardlink must be copied normally
483 hln = hltadd(&st1, dpath);
488 * Do we need to copy the file/dir/link/whatever? Early termination
489 * if we do not. Always redo links. Directories are always traversed
490 * except when the FSMID options are used.
492 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
497 st1.st_mode == st2.st_mode &&
498 st1.st_flags == st2.st_flags
500 if (S_ISLNK(st1.st_mode) || S_ISDIR(st1.st_mode)) {
502 * If FSMID tracking is turned on we can avoid recursing through
503 * an entire directory subtree if the FSMID matches.
505 #ifdef _ST_FSMID_PRESENT_
507 (UseFSMIDOpt && (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
509 if (VerboseOpt >= 3) {
511 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
513 logstd("%-32s nochange\n", (dpath ? dpath : spath));
520 st1.st_size == st2.st_size &&
521 st1.st_uid == st2.st_uid &&
522 st1.st_gid == st2.st_gid &&
523 st1.st_mtime == st2.st_mtime
524 && (UseMD5Opt == 0 || (mres = md5_check(spath, dpath)) == 0)
525 #ifdef _ST_FSMID_PRESENT_
526 && (UseFSMIDOpt == 0 || (fres = fsmid_check(st1.st_fsmid, dpath)) == 0)
530 hln->dino = st2.st_ino;
531 if (VerboseOpt >= 3) {
533 logstd("%-32s md5-nochange\n", (dpath ? dpath : spath));
534 else if (UseFSMIDOpt)
535 logstd("%-32s fsmid-nochange\n", (dpath ? dpath : spath));
537 logstd("%-32s nochange\n", (dpath ? dpath : spath));
539 CountSourceBytes += size;
546 if (st2Valid && !S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
548 logerr("%-32s SAFETY - refusing to copy file over directory\n",
549 (dpath ? dpath : spath)
552 return(0); /* continue with the cpdup anyway */
554 if (QuietOpt == 0 || AskConfirmation) {
555 logstd("%-32s WARNING: non-directory source will blow away\n"
556 "%-32s preexisting dest directory, continuing anyway!\n",
557 ((dpath) ? dpath : spath), "");
560 RemoveRecur(dpath, ddevNo);
564 * The various comparisons failed, copy it.
566 if (S_ISDIR(st1.st_mode)) {
570 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
571 if ((dir = opendir(spath)) != NULL) {
576 if (S_ISDIR(st2.st_mode) == 0) {
578 if (mkdir(dpath, st1.st_mode | 0700) != 0) {
579 logerr("%s: mkdir failed: %s\n",
580 (dpath ? dpath : spath), strerror(errno));
585 * Matt: why don't you check error codes here?
588 chown(dpath, st1.st_uid, st1.st_gid);
592 * Directory must be scanable by root for cpdup to
593 * work. We'll fix it later if the directory isn't
594 * supposed to be readable ( which is why we fixup
595 * st2.st_mode to match what we did ).
597 if ((st2.st_mode & 0700) != 0700) {
598 chmod(dpath, st2.st_mode | 0700);
602 logstd("%s\n", dpath ? dpath : spath);
606 if ((int)sdevNo >= 0 && st1.st_dev != sdevNo) {
612 if ((int)ddevNo >= 0 && st2.st_dev != ddevNo) {
619 * scan .cpignore file for files/directories
628 if (UseCpFile[0] == '/') {
629 fpath = mprintf("%s", UseCpFile);
631 fpath = mprintf("%s/%s", spath, UseCpFile);
633 AddList(&list, strrchr(fpath, '/') + 1, 1);
634 if ((fi = fopen(fpath, "r")) != NULL) {
635 while (fgets(buf, sizeof(buf), fi) != NULL) {
638 if (l && buf[l-1] == '\n')
640 if (buf[0] && buf[0] != '#')
641 AddList(&list, buf, 1);
649 * Automatically exclude MD5CacheFile that we create on the
650 * source from the copy to the destination.
652 * Automatically exclude a FSMIDCacheFile on the source that
653 * would otherwise overwrite the one we maintain on the target.
656 AddList(&list, MD5CacheFile, 1);
658 AddList(&list, FSMIDCacheFile, 1);
660 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
667 if (strcmp(den->d_name, ".") == 0 ||
668 strcmp(den->d_name, "..") == 0
673 * ignore if on .cpignore list
675 if (AddList(&list, den->d_name, 0) == 1) {
678 nspath = mprintf("%s/%s", spath, den->d_name);
680 ndpath = mprintf("%s/%s", dpath, den->d_name);
695 * Remove files/directories from destination that do not appear
698 if (dpath && (dir = opendir(dpath)) != NULL) {
699 while (noLoop == 0 && (den = readdir(dir)) != NULL) {
703 if (strcmp(den->d_name, ".") == 0 ||
704 strcmp(den->d_name, "..") == 0
709 * If object does not exist in source or .cpignore
710 * then recursively remove it.
712 if (AddList(&list, den->d_name, 3) == 3) {
715 ndpath = mprintf("%s/%s", dpath, den->d_name);
716 RemoveRecur(ndpath, ddevNo);
726 st1.st_uid != st2.st_uid ||
727 st1.st_gid != st2.st_gid
729 chown(dpath, st1.st_uid, st1.st_gid);
731 if (st2Valid == 0 || st1.st_mode != st2.st_mode) {
732 chmod(dpath, st1.st_mode);
734 if (st2Valid == 0 || st1.st_flags != st2.st_flags) {
735 chflags(dpath, st1.st_flags);
739 } else if (dpath == NULL) {
741 * If dpath is NULL, we are just updating the MD5
743 if (UseMD5Opt && S_ISREG(st1.st_mode)) {
744 mres = md5_check(spath, NULL);
746 if (VerboseOpt > 1) {
748 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
750 logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
751 } else if (!QuietOpt && mres < 0) {
752 logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
755 } else if (S_ISREG(st1.st_mode)) {
761 path = mprintf("%s.tmp", dpath);
764 * Handle check failure message.
767 logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
769 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath) ? dpath : spath);
772 * Not quite ready to do the copy yet. If UseHLPath is defined,
773 * see if we can hardlink instead.
776 if (UseHLPath && (hpath = checkHLPath(&st1, spath, dpath)) != NULL) {
777 if (link(hpath, dpath) == 0) {
779 logstd("%-32s hardlinked(-H)\n",
780 (dpath ? dpath : spath));
786 * Shucks, we may have hit a filesystem hard linking limit,
787 * we have to copy instead.
792 if ((fd1 = open(spath, O_RDONLY)) >= 0) {
793 if ((fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
795 * There could be a .tmp file from a previously interrupted
796 * run, delete and retry. Fail if we still can't get at it.
800 fd2 = open(path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
807 * Matt: What about holes?
810 while ((n = read(fd1, IOBuf1, sizeof(IOBuf1))) > 0) {
812 if (write(fd2, IOBuf1, n) != n)
818 struct timeval tv[2];
820 bzero(tv, sizeof(tv));
821 tv[0].tv_sec = st1.st_mtime;
822 tv[1].tv_sec = st1.st_mtime;
825 chown(path, st1.st_uid, st1.st_gid);
826 chmod(path, st1.st_mode);
827 if (xrename(path, dpath, st2.st_flags) != 0) {
828 logerr("%-32s rename-after-copy failed: %s\n",
829 (dpath ? dpath : spath), strerror(errno)
834 logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
836 chflags(dpath, st1.st_flags);
838 CountReadBytes += size;
839 CountWriteBytes += size;
840 CountSourceBytes += size;
844 logerr("%-32s %s failed: %s\n",
845 (dpath ? dpath : spath), op, strerror(errno)
851 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
852 (dpath ? dpath : spath), getuid(), geteuid(),
859 logerr("%-32s copy: open failed: %s\n",
860 (dpath ? dpath : spath),
869 if (!r && stat(dpath, &st2) == 0)
870 hln->dino = st2.st_ino;
874 } else if (S_ISLNK(st1.st_mode)) {
881 snprintf(path, sizeof(path), "%s.tmp", dpath);
882 n1 = readlink(spath, link1, sizeof(link1) - 1);
883 n2 = readlink(dpath, link2, sizeof(link2) - 1);
885 if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0) {
889 if (symlink(link1, path) < 0) {
890 logerr("%-32s symlink (%s->%s) failed: %s\n",
891 (dpath ? dpath : spath), link1, path,
896 lchown(path, st1.st_uid, st1.st_gid);
898 * there is no lchmod() or lchflags(), we
899 * cannot chmod or chflags a softlink.
901 if (xrename(path, dpath, st2.st_flags) != 0) {
902 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
903 (dpath ? dpath : spath),
904 path, dpath, strerror(errno));
905 } else if (VerboseOpt) {
906 logstd("%-32s softlink-ok\n", (dpath ? dpath : spath));
909 CountWriteBytes += n1;
914 logstd("%-32s nochange\n", (dpath ? dpath : spath));
916 CountSourceBytes += n1;
917 CountReadBytes += n1;
918 if (n2 > 0) CountReadBytes += n2;
922 logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
924 } else if (S_ISCHR(st1.st_mode) || S_ISBLK(st1.st_mode)) {
929 st1.st_mode != st2.st_mode ||
930 st1.st_rdev != st2.st_rdev ||
931 st1.st_uid != st2.st_uid ||
932 st1.st_gid != st2.st_gid
934 snprintf(path, sizeof(path), "%s.tmp", dpath);
937 if (mknod(path, st1.st_mode, st1.st_rdev) == 0) {
938 chmod(path, st1.st_mode);
939 chown(path, st1.st_uid, st1.st_gid);
941 if (xrename(path, dpath, st2.st_flags) != 0) {
942 logerr("%-32s dev-rename-after-create failed: %s\n",
943 (dpath ? dpath : spath),
946 } else if (VerboseOpt) {
947 logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
952 logerr("%-32s dev failed: %s\n",
953 (dpath ? dpath : spath), strerror(errno)
958 logstd("%-32s nochange\n", (dpath ? dpath : spath));
971 RemoveRecur(const char *dpath, dev_t devNo)
975 if (lstat(dpath, &st) == 0) {
978 if (st.st_dev == devNo) {
979 if (S_ISDIR(st.st_mode)) {
982 if ((dir = opendir(dpath)) != NULL) {
984 while ((den = readdir(dir)) != NULL) {
987 if (strcmp(den->d_name, ".") == 0)
989 if (strcmp(den->d_name, "..") == 0)
991 ndpath = mprintf("%s/%s", dpath, den->d_name);
992 RemoveRecur(ndpath, devNo);
997 if (AskConfirmation && NoRemoveOpt == 0) {
999 if (rmdir(dpath) < 0) {
1000 logerr("%-32s rmdir failed: %s\n",
1001 dpath, strerror(errno)
1004 CountRemovedItems++;
1009 logstd("%-32s not-removed\n", dpath);
1010 } else if (rmdir(dpath) == 0) {
1012 logstd("%-32s rmdir-ok\n", dpath);
1013 CountRemovedItems++;
1015 logerr("%-32s rmdir failed: %s\n",
1016 dpath, strerror(errno)
1021 if (AskConfirmation && NoRemoveOpt == 0) {
1023 if (remove(dpath) < 0) {
1024 logerr("%-32s remove failed: %s\n",
1025 dpath, strerror(errno)
1028 CountRemovedItems++;
1033 logstd("%-32s not-removed\n", dpath);
1034 } else if (remove(dpath) == 0) {
1036 logstd("%-32s remove-ok\n", dpath);
1037 CountRemovedItems++;
1039 logerr("%-32s remove failed: %s\n",
1040 dpath, strerror(errno)
1050 InitList(List *list)
1052 bzero(list, sizeof(List));
1053 list->li_Node.no_Next = &list->li_Node;
1057 ResetList(List *list)
1061 while ((node = list->li_Node.no_Next) != &list->li_Node) {
1062 list->li_Node.no_Next = node->no_Next;
1069 AddList(List *list, const char *name, int n)
1077 * Scan against wildcards. Only a node value of 1 can be a wildcard
1078 * ( usually scanned from .cpignore )
1081 for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1082 if (strcmp(name, node->no_Name) == 0 ||
1083 (n != 1 && node->no_Value == 1 && WildCmp(node->no_Name, name) == 0)
1085 return(node->no_Value);
1090 * Look for exact match
1093 for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1094 if (strcmp(name, node->no_Name) == 0) {
1095 return(node->no_Value);
1098 node = malloc(sizeof(Node) + strlen(name) + 1);
1100 fprintf(stderr, "out of memory\n");
1104 node->no_Next = list->li_Node.no_Next;
1105 list->li_Node.no_Next = node;
1107 node->no_HNext = list->li_Hash[hv];
1108 list->li_Hash[hv] = node;
1110 strcpy(node->no_Name, name);
1117 shash(const char *s)
1124 if (*s == '*' || *s == '?' ||
1125 *s == '{' || *s == '}' ||
1126 *s == '[' || *s == ']' ||
1131 hv = (hv << 5) ^ *s ^ (hv >> 23);
1134 return(((hv >> 16) ^ hv) & HMASK);
1138 * WildCmp() - compare wild string to sane string
1140 * Return 0 on success, -1 on failure.
1144 WildCmp(const char *w, const char *s)
1147 * skip fixed portion
1153 if (w[1] == 0) /* optimize wild* case */
1159 for (i = 0; i <= l; ++i) {
1160 if (WildCmp(w + 1, s + i) == 0)
1174 if (*w == 0) /* terminator */
1186 YesNo(const char *path)
1190 fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
1193 first = ch = getchar();
1194 while (ch != '\n' && ch != EOF)
1196 return ((first == 'y' || first == 'Y'));
1200 * xrename() - rename with override
1202 * If the rename fails, attempt to override st_flags on the
1203 * destination and rename again. If that fails too, try to
1204 * set the flags back the way they were and give up.
1208 xrename(const char *src, const char *dst, u_long flags)
1214 if ((r = rename(src, dst)) < 0) {
1216 if ((r = rename(src, dst)) < 0)
1217 chflags(dst, flags);
1223 xlink(const char *src, const char *dst, u_long flags)
1229 if ((r = link(src, dst)) < 0) {
1233 chflags(src, flags);