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.32 2008/11/11 04:36:00 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.
63 #define HMASK (HSIZE-1)
65 #define HLMASK (HLSIZE - 1)
67 #define MAXDEPTH 32 /* max copy depth for thread */
68 #define GETBUFSIZE 8192
69 #define GETPATHSIZE 2048
70 #define GETLINKSIZE 1024
71 #define GETIOSIZE 65536
73 #ifndef _ST_FLAGS_PRESENT_
74 #define st_flags st_mode
79 struct Node
*no_HNext
;
99 typedef struct copy_info
{
105 struct copy_info
*parent
;
112 struct hlink
*hltable
[HLSIZE
];
114 void RemoveRecur(const char *dpath
, dev_t devNo
);
115 void InitList(List
*list
);
116 void ResetList(List
*list
);
117 int AddList(List
*list
, const char *name
, int n
);
118 static struct hlink
*hltlookup(struct stat
*);
119 static struct hlink
*hltadd(struct stat
*, const char *);
120 static char *checkHLPath(struct stat
*st
, const char *spath
, const char *dpath
);
121 static int validate_check(const char *spath
, const char *dpath
);
122 static int shash(const char *s
);
123 static void hltdelete(struct hlink
*);
124 static void hltsetdino(struct hlink
*, ino_t
);
125 int YesNo(const char *path
);
126 static int xrename(const char *src
, const char *dst
, u_long flags
);
127 static int xlink(const char *src
, const char *dst
, u_long flags
);
128 static int xremove(struct HostConf
*host
, const char *path
);
129 int WildCmp(const char *s1
, const char *s2
);
130 static int DoCopy(copy_info_t info
, int depth
);
132 int AskConfirmation
= 1;
144 int EnableDirectoryRetries
;
148 int MaxParallel
= -1;
152 const char *UseCpFile
;
153 const char *UseHLPath
;
154 const char *MD5CacheFile
;
155 const char *FSMIDCacheFile
;
157 int64_t CountSourceBytes
;
158 int64_t CountSourceItems
;
159 int64_t CountCopiedItems
;
160 int64_t CountSourceReadBytes
;
161 int64_t CountTargetReadBytes
;
162 int64_t CountWriteBytes
;
163 int64_t CountRemovedItems
;
164 int64_t CountLinkedItems
;
166 struct HostConf SrcHost
;
167 struct HostConf DstHost
;
170 pthread_mutex_t MasterMutex
;
174 main(int ac
, char **av
)
180 struct timeval start
;
181 struct copy_info info
;
183 signal(SIGPIPE
, SIG_IGN
);
185 RunningAsUser
= (geteuid() != 0);
186 RunningAsRoot
= !RunningAsUser
;
189 for (i
= 0; i
< HCTHASH_SIZE
; ++i
) {
190 pthread_mutex_init(&SrcHost
.hct_mutex
[i
], NULL
);
191 pthread_mutex_init(&DstHost
.hct_mutex
[i
], NULL
);
193 pthread_mutex_init(&MasterMutex
, NULL
);
194 pthread_mutex_lock(&MasterMutex
);
197 gettimeofday(&start
, NULL
);
198 for (i
= 1; i
< ac
; ++i
) {
205 } else if (dst
== NULL
) {
208 fatal("too many arguments");
216 v
= strtol(ptr
, NULL
, 0);
224 while (*ptr
== 'v') {
228 if (*ptr
>= '0' && *ptr
<= '9')
229 VerboseOpt
= strtol(ptr
, NULL
, 0);
245 UseCpFile
= ".cpignore";
248 UseCpFile
= (*ptr
) ? ptr
: av
[++i
];
251 UseHLPath
= (*ptr
) ? ptr
: av
[++i
];
276 FSMIDCacheFile
= ".FSMID.CHECK";
280 FSMIDCacheFile
= av
[++i
];
284 MD5CacheFile
= av
[++i
];
288 MD5CacheFile
= ".MD5.CHECKSUMS";
291 setvbuf(stdout
, NULL
, _IOLBF
, 0);
294 fatal("illegal option: %s\n", ptr
- 2);
301 * If we are told to go into slave mode, run the HC protocol
309 * Extract the source and/or/neither target [user@]host and
310 * make any required connections.
312 if (src
&& (ptr
= strchr(src
, ':')) != NULL
) {
313 asprintf(&SrcHost
.host
, "%*.*s", (int)(ptr
- src
), (int)(ptr
- src
), src
);
316 fprintf(stderr
, "The cpignore options are not currently supported for remote sources\n");
320 fprintf(stderr
, "The MD5 options are not currently supported for remote sources\n");
323 if (hc_connect(&SrcHost
) < 0)
326 if (dst
&& (ptr
= strchr(dst
, ':')) != NULL
) {
327 asprintf(&DstHost
.host
, "%*.*s", (int)(ptr
- dst
), (int)(ptr
- dst
), dst
);
330 fprintf(stderr
, "The FSMID options are not currently supported for remote targets\n");
333 if (hc_connect(&DstHost
) < 0)
338 * dst may be NULL only if -m option is specified,
339 * which forces an update of the MD5 checksums
341 if (dst
== NULL
&& UseMD5Opt
== 0) {
345 bzero(&info
, sizeof(info
));
349 pthread_cond_init(&info
.cond
, NULL
);
352 DstBaseLen
= strlen(dst
);
355 info
.sdevNo
= (dev_t
)-1;
356 info
.ddevNo
= (dev_t
)-1;
357 i
= DoCopy(&info
, -1);
361 info
.sdevNo
= (dev_t
)-1;
362 info
.ddevNo
= (dev_t
)-1;
363 i
= DoCopy(&info
, -1);
366 pthread_cond_destroy(&info
.cond
);
373 if (SummaryOpt
&& i
== 0) {
377 gettimeofday(&end
, NULL
);
379 /* don't count stat's in our byte statistics */
380 CountSourceBytes
+= sizeof(struct stat
) * CountSourceItems
;
381 CountSourceReadBytes
+= sizeof(struct stat
) * CountSourceItems
;
382 CountWriteBytes
+= sizeof(struct stat
) * CountCopiedItems
;
383 CountWriteBytes
+= sizeof(struct stat
) * CountRemovedItems
;
386 duration
= (end
.tv_sec
- start
.tv_sec
);
387 duration
+= (double)(end
.tv_usec
- start
.tv_usec
) / 1000000.0;
390 logstd("cpdup completed successfully\n");
391 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
392 "%lld bytes written (%.1fX speedup)\n",
393 (long long)CountSourceBytes
,
394 (long long)CountSourceReadBytes
,
395 (long long)CountTargetReadBytes
,
396 (long long)CountWriteBytes
,
397 ((double)CountSourceBytes
* 2.0) / ((double)(CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
)));
398 logstd("%lld source items, %lld items copied, %lld items linked, "
399 "%lld things deleted\n",
400 (long long)CountSourceItems
,
401 (long long)CountCopiedItems
,
402 (long long)CountLinkedItems
,
403 (long long)CountRemovedItems
);
404 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
406 (int)((CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
) / duration
/ 1024.0),
407 (int)(CountSourceBytes
/ duration
/ 1024.0));
409 exit((i
== 0) ? 0 : 1);
412 static struct hlink
*
413 hltlookup(struct stat
*stp
)
416 struct timespec ts
= { 0, 100000 };
421 n
= stp
->st_ino
& HLMASK
;
426 for (hl
= hltable
[n
]; hl
; hl
= hl
->next
) {
427 if (hl
->ino
== stp
->st_ino
) {
430 * If the hl entry is still in the process of being created
431 * by another thread we have to wait until it has either been
432 * deleted or completed.
435 pthread_mutex_unlock(&MasterMutex
);
436 nanosleep(&ts
, NULL
);
437 pthread_mutex_lock(&MasterMutex
);
449 static struct hlink
*
450 hltadd(struct stat
*stp
, const char *path
)
453 int plen
= strlen(path
);
456 new = malloc(offsetof(struct hlink
, name
[plen
+ 1]));
458 fprintf(stderr
, "out of memory\n");
463 /* initialize and link the new element into the table */
464 new->ino
= stp
->st_ino
;
465 new->dino
= (ino_t
)-1;
467 bcopy(path
, new->name
, plen
+ 1);
470 n
= stp
->st_ino
& HLMASK
;
471 new->next
= hltable
[n
];
473 hltable
[n
]->prev
= new;
480 hltsetdino(struct hlink
*hl
, ino_t inum
)
486 hltdelete(struct hlink
*hl
)
488 assert(hl
->refs
== 1);
492 hl
->next
->prev
= hl
->prev
;
493 hl
->prev
->next
= hl
->next
;
496 hl
->next
->prev
= NULL
;
498 hltable
[hl
->ino
& HLMASK
] = hl
->next
;
505 hltrels(struct hlink
*hl
)
507 assert(hl
->refs
== 1);
512 * If UseHLPath is defined check to see if the file in question is
513 * the same as the source file, and if it is return a pointer to the
514 * -H path based file for hardlinking. Else return NULL.
517 checkHLPath(struct stat
*st1
, const char *spath
, const char *dpath
)
523 asprintf(&hpath
, "%s%s", UseHLPath
, dpath
+ DstBaseLen
);
526 * stat info matches ?
528 if (hc_stat(&DstHost
, hpath
, &sthl
) < 0 ||
529 st1
->st_size
!= sthl
.st_size
||
530 st1
->st_mtime
!= sthl
.st_mtime
||
531 (RunningAsRoot
&& (st1
->st_uid
!= sthl
.st_uid
||
532 st1
->st_gid
!= sthl
.st_gid
))
539 * If ForceOpt or ValidateOpt is set we have to compare the files
541 if (ForceOpt
|| ValidateOpt
) {
542 error
= validate_check(spath
, hpath
);
552 * Return 0 if the contents of the file <spath> matches the contents of
556 validate_check(const char *spath
, const char *dpath
)
562 fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0);
563 fd2
= hc_open(&DstHost
, dpath
, O_RDONLY
, 0);
566 if (fd1
>= 0 && fd2
>= 0) {
569 char *iobuf1
= malloc(GETIOSIZE
);
570 char *iobuf2
= malloc(GETIOSIZE
);
572 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
573 CountSourceReadBytes
+= n
;
574 x
= hc_read(&DstHost
, fd2
, iobuf2
, GETIOSIZE
);
576 CountTargetReadBytes
+= x
;
579 if (bcmp(iobuf1
, iobuf2
, n
) != 0)
588 hc_close(&SrcHost
, fd1
);
590 hc_close(&DstHost
, fd2
);
596 DoCopyThread(void *arg
)
598 copy_info_t cinfo
= arg
;
599 char *spath
= cinfo
->spath
;
600 char *dpath
= cinfo
->dpath
;
603 r
= pthread_detach(pthread_self());
605 pthread_cond_init(&cinfo
->cond
, NULL
);
606 pthread_mutex_lock(&MasterMutex
);
607 cinfo
->r
+= DoCopy(cinfo
, 0);
608 /* cinfo arguments invalid on return */
609 --cinfo
->parent
->children
;
611 pthread_cond_signal(&cinfo
->parent
->cond
);
615 pthread_cond_destroy(&cinfo
->cond
);
617 hcc_free_trans(&SrcHost
);
618 hcc_free_trans(&DstHost
);
619 pthread_mutex_unlock(&MasterMutex
);
626 DoCopy(copy_info_t info
, int depth
)
628 const char *spath
= info
->spath
;
629 const char *dpath
= info
->dpath
;
630 dev_t sdevNo
= info
->sdevNo
;
631 dev_t ddevNo
= info
->ddevNo
;
634 unsigned long st2_flags
;
635 int r
, mres
, fres
, st2Valid
;
637 List
*list
= malloc(sizeof(List
));
641 r
= mres
= fres
= st2Valid
= 0;
646 if (hc_lstat(&SrcHost
, spath
, &st1
) != 0) {
650 st2
.st_mode
= 0; /* in case lstat fails */
651 st2
.st_flags
= 0; /* in case lstat fails */
652 if (dpath
&& hc_lstat(&DstHost
, dpath
, &st2
) == 0) {
654 #ifdef _ST_FLAGS_PRESENT_
655 st2_flags
= st2
.st_flags
;
659 if (S_ISREG(st1
.st_mode
)) {
667 if (S_ISREG(st1
.st_mode
) && st1
.st_nlink
> 1 && dpath
) {
668 if ((hln
= hltlookup(&st1
)) != NULL
) {
672 if (st2
.st_ino
== hln
->dino
) {
674 * hard link is already correct, nothing to do
677 logstd("%-32s nochange\n", (dpath
) ? dpath
: spath
);
678 if (hln
->nlinked
== st1
.st_nlink
) {
687 * hard link is not correct, attempt to unlink it
689 if (xremove(&DstHost
, dpath
) < 0) {
690 logerr("%-32s hardlink: unable to unlink: %s\n",
691 ((dpath
) ? dpath
: spath
), strerror(errno
));
700 if (xlink(hln
->name
, dpath
, st1
.st_flags
) < 0) {
701 int tryrelink
= (errno
== EMLINK
);
702 logerr("%-32s hardlink: unable to link to %s: %s\n",
703 (dpath
? dpath
: spath
), hln
->name
, strerror(errno
)
708 logerr("%-20s hardlink: will attempt to copy normally\n");
713 if (hln
->nlinked
== st1
.st_nlink
) {
719 logstd("%-32s hardlink: %s\n",
720 (dpath
? dpath
: spath
),
721 (st2Valid
? "relinked" : "linked")
732 * first instance of hardlink must be copied normally
735 hln
= hltadd(&st1
, dpath
);
740 * Do we need to copy the file/dir/link/whatever? Early termination
741 * if we do not. Always redo links. Directories are always traversed
742 * except when the FSMID options are used.
744 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
749 && st1
.st_mode
== st2
.st_mode
750 #ifdef _ST_FLAGS_PRESENT_
751 && (RunningAsUser
|| st1
.st_flags
== st2
.st_flags
)
754 if (S_ISLNK(st1
.st_mode
) || S_ISDIR(st1
.st_mode
)) {
756 * If FSMID tracking is turned on we can avoid recursing through
757 * an entire directory subtree if the FSMID matches.
759 #ifdef _ST_FSMID_PRESENT_
761 (UseFSMIDOpt
&& (fres
= fsmid_check(st1
.st_fsmid
, dpath
)) == 0)
763 if (VerboseOpt
>= 3) {
765 logstd("%-32s fsmid-nochange\n", (dpath
? dpath
: spath
));
767 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
775 st1
.st_size
== st2
.st_size
&&
776 st1
.st_mtime
== st2
.st_mtime
&&
777 (RunningAsUser
|| (st1
.st_uid
== st2
.st_uid
&&
778 st1
.st_gid
== st2
.st_gid
))
780 && (UseMD5Opt
== 0 || !S_ISREG(st1
.st_mode
) ||
781 (mres
= md5_check(spath
, dpath
)) == 0)
783 #ifdef _ST_FSMID_PRESENT_
784 && (UseFSMIDOpt
== 0 ||
785 (fres
= fsmid_check(st1
.st_fsmid
, dpath
)) == 0)
787 && (ValidateOpt
== 0 || !S_ISREG(st1
.st_mode
) ||
788 validate_check(spath
, dpath
) == 0)
791 * The files are identical, but if we are not running as
792 * root we might need to adjust ownership/group/flags.
795 int changedflags
= 0;
797 hltsetdino(hln
, st2
.st_ino
);
799 if (RunningAsUser
&& (st1
.st_uid
!= st2
.st_uid
||
800 st1
.st_gid
!= st2
.st_gid
)) {
801 hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
);
804 #ifdef _ST_FLAGS_PRESENT_
805 if (RunningAsUser
&& st1
.st_flags
!= st2
.st_flags
) {
806 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
810 if (VerboseOpt
>= 3) {
813 logstd("%-32s md5-nochange",
814 (dpath
? dpath
: spath
));
818 logstd("%-32s fsmid-nochange",
819 (dpath
? dpath
: spath
));
820 } else if (ValidateOpt
) {
821 logstd("%-32s nochange (contents validated)",
822 (dpath
? dpath
: spath
));
824 logstd("%-32s nochange", (dpath
? dpath
: spath
));
827 logstd(" (uid/gid differ)");
829 logstd(" (flags differ)");
832 CountSourceBytes
+= size
;
839 if (st2Valid
&& !S_ISDIR(st1
.st_mode
) && S_ISDIR(st2
.st_mode
)) {
841 logerr("%-32s SAFETY - refusing to copy file over directory\n",
842 (dpath
? dpath
: spath
)
846 goto done
; /* continue with the cpdup anyway */
848 if (QuietOpt
== 0 || AskConfirmation
) {
849 logstd("%-32s WARNING: non-directory source will blow away\n"
850 "%-32s preexisting dest directory, continuing anyway!\n",
851 ((dpath
) ? dpath
: spath
), "");
854 RemoveRecur(dpath
, ddevNo
);
858 * The various comparisons failed, copy it.
860 if (S_ISDIR(st1
.st_mode
)) {
864 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
865 if ((dir
= hc_opendir(&SrcHost
, spath
)) != NULL
) {
870 if (S_ISDIR(st2
.st_mode
) == 0) {
871 xremove(&DstHost
, dpath
);
872 if (hc_mkdir(&DstHost
, dpath
, st1
.st_mode
| 0700) != 0) {
873 logerr("%s: mkdir failed: %s\n",
874 (dpath
? dpath
: spath
), strerror(errno
));
880 * Matt: why don't you check error codes here?
881 * (Because I'm an idiot... checks added!)
883 if (hc_lstat(&DstHost
, dpath
, &st2
) != 0) {
884 logerr("%s: lstat of newly made dir failed: %s\n",
885 (dpath
? dpath
: spath
), strerror(errno
));
889 if (hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
) != 0){
890 logerr("%s: chown of newly made dir failed: %s\n",
891 (dpath
? dpath
: spath
), strerror(errno
));
898 * Directory must be scanable by root for cpdup to
899 * work. We'll fix it later if the directory isn't
900 * supposed to be readable ( which is why we fixup
901 * st2.st_mode to match what we did ).
903 if ((st2
.st_mode
& 0700) != 0700) {
904 hc_chmod(&DstHost
, dpath
, st2
.st_mode
| 0700);
908 logstd("%s\n", dpath
? dpath
: spath
);
912 if (sdevNo
!= (dev_t
)-1 && st1
.st_dev
!= sdevNo
) {
918 if (ddevNo
!= (dev_t
)-1 && st2
.st_dev
!= ddevNo
) {
925 * scan .cpignore file for files/directories
931 char *buf
= malloc(GETBUFSIZE
);
934 if (UseCpFile
[0] == '/') {
935 fpath
= mprintf("%s", UseCpFile
);
937 fpath
= mprintf("%s/%s", spath
, UseCpFile
);
939 AddList(list
, strrchr(fpath
, '/') + 1, 1);
940 if ((fi
= fopen(fpath
, "r")) != NULL
) {
941 while (fgets(buf
, GETBUFSIZE
, fi
) != NULL
) {
943 CountSourceReadBytes
+= l
;
944 if (l
&& buf
[l
-1] == '\n')
947 AddList(list
, buf
, 1);
956 * Automatically exclude MD5CacheFile that we create on the
957 * source from the copy to the destination.
959 * Automatically exclude a FSMIDCacheFile on the source that
960 * would otherwise overwrite the one we maintain on the target.
963 AddList(list
, MD5CacheFile
, 1);
965 AddList(list
, FSMIDCacheFile
, 1);
967 while (noLoop
== 0 && (den
= hc_readdir(&SrcHost
, dir
)) != NULL
) {
974 if (strcmp(den
->d_name
, ".") == 0 ||
975 strcmp(den
->d_name
, "..") == 0
980 * ignore if on .cpignore list
982 if (AddList(list
, den
->d_name
, 0) == 1) {
985 nspath
= mprintf("%s/%s", spath
, den
->d_name
);
987 ndpath
= mprintf("%s/%s", dpath
, den
->d_name
);
990 if (CurParallel
< MaxParallel
|| depth
> MAXDEPTH
) {
991 copy_info_t cinfo
= malloc(sizeof(*cinfo
));
994 bzero(cinfo
, sizeof(*cinfo
));
995 cinfo
->spath
= nspath
;
996 cinfo
->dpath
= ndpath
;
997 cinfo
->sdevNo
= sdevNo
;
998 cinfo
->ddevNo
= ddevNo
;
999 cinfo
->parent
= info
;
1002 pthread_create(&dummy_thr
, NULL
, DoCopyThread
, cinfo
);
1006 info
->spath
= nspath
;
1007 info
->dpath
= ndpath
;
1008 info
->sdevNo
= sdevNo
;
1009 info
->ddevNo
= ddevNo
;
1011 r
+= DoCopy(info
, depth
);
1013 r
+= DoCopy(info
, depth
+ 1);
1022 hc_closedir(&SrcHost
, dir
);
1026 * Wait for our children to finish
1028 while (info
->children
) {
1029 pthread_cond_wait(&info
->cond
, &MasterMutex
);
1036 * Remove files/directories from destination that do not appear
1039 if (dpath
&& (dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1040 while (noLoop
== 0 && (den
= hc_readdir(&DstHost
, dir
)) != NULL
) {
1044 if (strcmp(den
->d_name
, ".") == 0 ||
1045 strcmp(den
->d_name
, "..") == 0
1050 * If object does not exist in source or .cpignore
1051 * then recursively remove it.
1053 if (AddList(list
, den
->d_name
, 3) == 3) {
1056 ndpath
= mprintf("%s/%s", dpath
, den
->d_name
);
1057 RemoveRecur(ndpath
, ddevNo
);
1061 hc_closedir(&DstHost
, dir
);
1065 struct timeval tv
[2];
1069 st1
.st_uid
!= st2
.st_uid
||
1070 st1
.st_gid
!= st2
.st_gid
1072 hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
);
1074 if (st2Valid
== 0 || st1
.st_mode
!= st2
.st_mode
) {
1075 hc_chmod(&DstHost
, dpath
, st1
.st_mode
);
1077 #ifdef _ST_FLAGS_PRESENT_
1078 if (st2Valid
== 0 || st1
.st_flags
!= st2
.st_flags
) {
1079 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
1084 st1
.st_mtime
!= st2
.st_mtime
1086 bzero(tv
, sizeof(tv
));
1087 tv
[0].tv_sec
= st1
.st_mtime
;
1088 tv
[1].tv_sec
= st1
.st_mtime
;
1089 hc_utimes(&DstHost
, dpath
, tv
);
1093 } else if (dpath
== NULL
) {
1095 * If dpath is NULL, we are just updating the MD5
1098 if (UseMD5Opt
&& S_ISREG(st1
.st_mode
)) {
1099 mres
= md5_check(spath
, NULL
);
1101 if (VerboseOpt
> 1) {
1103 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1105 logstd("%-32s md5-ok\n", (dpath
) ? dpath
: spath
);
1106 } else if (!QuietOpt
&& mres
< 0) {
1107 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1111 } else if (S_ISREG(st1
.st_mode
)) {
1118 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1120 path
= mprintf("%s", dpath
);
1123 * Handle check failure message.
1127 logerr("%-32s md5-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1131 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1134 * Not quite ready to do the copy yet. If UseHLPath is defined,
1135 * see if we can hardlink instead.
1137 * If we can hardlink, and the target exists, we have to remove it
1138 * first or the hardlink will fail. This can occur in a number of
1139 * situations but must typically when the '-f -H' combination is
1142 if (UseHLPath
&& (hpath
= checkHLPath(&st1
, spath
, dpath
)) != NULL
) {
1144 xremove(&DstHost
, dpath
);
1145 if (hc_link(&DstHost
, hpath
, dpath
) == 0) {
1148 logstd("%-32s hardlinked(-H)\n",
1149 (dpath
? dpath
: spath
));
1155 * Shucks, we may have hit a filesystem hard linking limit,
1156 * we have to copy instead.
1161 if ((fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0)) >= 0) {
1162 if ((fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600)) < 0) {
1164 * There could be a .tmp file from a previously interrupted
1165 * run, delete and retry. Fail if we still can't get at it.
1167 #ifdef _ST_FLAGS_PRESENT_
1168 hc_chflags(&DstHost
, path
, 0);
1170 hc_remove(&DstHost
, path
);
1171 fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_TRUNC
, 0600);
1175 char *iobuf1
= malloc(GETIOSIZE
);
1179 * Matt: What about holes?
1182 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
1184 if (hc_write(&DstHost
, fd2
, iobuf1
, n
) != n
)
1188 hc_close(&DstHost
, fd2
);
1190 struct timeval tv
[2];
1192 bzero(tv
, sizeof(tv
));
1193 tv
[0].tv_sec
= st1
.st_mtime
;
1194 tv
[1].tv_sec
= st1
.st_mtime
;
1196 hc_chown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1197 hc_chmod(&DstHost
, path
, st1
.st_mode
);
1198 #ifdef _ST_FLAGS_PRESENT_
1199 if (st1
.st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
1200 hc_utimes(&DstHost
, path
, tv
);
1202 hc_utimes(&DstHost
, path
, tv
);
1204 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1205 logerr("%-32s rename-after-copy failed: %s\n",
1206 (dpath
? dpath
: spath
), strerror(errno
)
1211 logstd("%-32s copy-ok\n", (dpath
? dpath
: spath
));
1212 #ifdef _ST_FLAGS_PRESENT_
1214 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
1217 #ifdef _ST_FLAGS_PRESENT_
1218 if ((st1
.st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
)) == 0)
1219 hc_utimes(&DstHost
, dpath
, tv
);
1221 CountSourceReadBytes
+= size
;
1222 CountWriteBytes
+= size
;
1223 CountSourceBytes
+= size
;
1227 logerr("%-32s %s failed: %s\n",
1228 (dpath
? dpath
: spath
), op
, strerror(errno
)
1230 hc_remove(&DstHost
, path
);
1235 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1236 (dpath
? dpath
: spath
), getuid(), geteuid(),
1241 hc_close(&SrcHost
, fd1
);
1243 logerr("%-32s copy: open failed: %s\n",
1244 (dpath
? dpath
: spath
),
1253 if (!r
&& hc_stat(&DstHost
, dpath
, &st2
) == 0) {
1254 hltsetdino(hln
, st2
.st_ino
);
1260 } else if (S_ISLNK(st1
.st_mode
)) {
1261 char *link1
= malloc(GETLINKSIZE
);
1262 char *link2
= malloc(GETLINKSIZE
);
1267 n1
= hc_readlink(&SrcHost
, spath
, link1
, GETLINKSIZE
- 1);
1269 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1270 n2
= hc_readlink(&DstHost
, dpath
, link2
, GETLINKSIZE
- 1);
1272 path
= mprintf("%s", dpath
);
1276 if (ForceOpt
|| n1
!= n2
|| bcmp(link1
, link2
, n1
) != 0) {
1277 hc_umask(&DstHost
, ~st1
.st_mode
);
1278 xremove(&DstHost
, path
);
1280 if (hc_symlink(&DstHost
, link1
, path
) < 0) {
1281 logerr("%-32s symlink (%s->%s) failed: %s\n",
1282 (dpath
? dpath
: spath
), link1
, path
,
1287 hc_lchown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1289 * there is no lchmod() or lchflags(), we
1290 * cannot chmod or chflags a softlink.
1292 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1293 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1294 (dpath
? dpath
: spath
),
1295 path
, dpath
, strerror(errno
));
1296 } else if (VerboseOpt
) {
1297 logstd("%-32s softlink-ok\n", (dpath
? dpath
: spath
));
1299 hc_umask(&DstHost
, 000);
1300 CountWriteBytes
+= n1
;
1304 if (VerboseOpt
>= 3)
1305 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1307 CountSourceBytes
+= n1
;
1308 CountSourceReadBytes
+= n1
;
1310 CountTargetReadBytes
+= n2
;
1314 logerr("%-32s softlink-failed\n", (dpath
? dpath
: spath
));
1319 } else if ((S_ISCHR(st1
.st_mode
) || S_ISBLK(st1
.st_mode
)) && DeviceOpt
) {
1324 st1
.st_mode
!= st2
.st_mode
||
1325 st1
.st_rdev
!= st2
.st_rdev
||
1326 st1
.st_uid
!= st2
.st_uid
||
1327 st1
.st_gid
!= st2
.st_gid
1330 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1331 xremove(&DstHost
, path
);
1333 path
= mprintf("%s", dpath
);
1336 if (hc_mknod(&DstHost
, path
, st1
.st_mode
, st1
.st_rdev
) == 0) {
1337 hc_chmod(&DstHost
, path
, st1
.st_mode
);
1338 hc_chown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1340 xremove(&DstHost
, dpath
);
1341 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1342 logerr("%-32s dev-rename-after-create failed: %s\n",
1343 (dpath
? dpath
: spath
),
1346 } else if (VerboseOpt
) {
1347 logstd("%-32s dev-ok\n", (dpath
? dpath
: spath
));
1352 logerr("%-32s dev failed: %s\n",
1353 (dpath
? dpath
: spath
), strerror(errno
)
1357 if (VerboseOpt
>= 3)
1358 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1366 if (hln
->dino
== (ino_t
)-1) {
1368 /*hln = NULL; unneeded */
1383 RemoveRecur(const char *dpath
, dev_t devNo
)
1387 if (hc_lstat(&DstHost
, dpath
, &st
) == 0) {
1388 if (devNo
== (dev_t
)-1)
1390 if (st
.st_dev
== devNo
) {
1391 if (S_ISDIR(st
.st_mode
)) {
1394 if ((dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1396 while ((den
= hc_readdir(&DstHost
, dir
)) != NULL
) {
1399 if (strcmp(den
->d_name
, ".") == 0)
1401 if (strcmp(den
->d_name
, "..") == 0)
1403 ndpath
= mprintf("%s/%s", dpath
, den
->d_name
);
1404 RemoveRecur(ndpath
, devNo
);
1407 hc_closedir(&DstHost
, dir
);
1409 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1411 if (hc_rmdir(&DstHost
, dpath
) < 0) {
1412 logerr("%-32s rmdir failed: %s\n",
1413 dpath
, strerror(errno
)
1416 CountRemovedItems
++;
1421 logstd("%-32s not-removed\n", dpath
);
1422 } else if (hc_rmdir(&DstHost
, dpath
) == 0) {
1424 logstd("%-32s rmdir-ok\n", dpath
);
1425 CountRemovedItems
++;
1427 logerr("%-32s rmdir failed: %s\n",
1428 dpath
, strerror(errno
)
1433 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1435 if (xremove(&DstHost
, dpath
) < 0) {
1436 logerr("%-32s remove failed: %s\n",
1437 dpath
, strerror(errno
)
1440 CountRemovedItems
++;
1445 logstd("%-32s not-removed\n", dpath
);
1446 } else if (xremove(&DstHost
, dpath
) == 0) {
1448 logstd("%-32s remove-ok\n", dpath
);
1449 CountRemovedItems
++;
1451 logerr("%-32s remove failed: %s\n",
1452 dpath
, strerror(errno
)
1462 InitList(List
*list
)
1464 bzero(list
, sizeof(List
));
1465 list
->li_Node
.no_Next
= &list
->li_Node
;
1469 ResetList(List
*list
)
1473 while ((node
= list
->li_Node
.no_Next
) != &list
->li_Node
) {
1474 list
->li_Node
.no_Next
= node
->no_Next
;
1481 AddList(List
*list
, const char *name
, int n
)
1489 * Scan against wildcards. Only a node value of 1 can be a wildcard
1490 * ( usually scanned from .cpignore )
1493 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1494 if (strcmp(name
, node
->no_Name
) == 0 ||
1495 (n
!= 1 && node
->no_Value
== 1 && WildCmp(node
->no_Name
, name
) == 0)
1497 return(node
->no_Value
);
1502 * Look for exact match
1505 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1506 if (strcmp(name
, node
->no_Name
) == 0) {
1507 return(node
->no_Value
);
1510 node
= malloc(sizeof(Node
) + strlen(name
) + 1);
1512 fprintf(stderr
, "out of memory\n");
1516 node
->no_Next
= list
->li_Node
.no_Next
;
1517 list
->li_Node
.no_Next
= node
;
1519 node
->no_HNext
= list
->li_Hash
[hv
];
1520 list
->li_Hash
[hv
] = node
;
1522 strcpy(node
->no_Name
, name
);
1529 shash(const char *s
)
1536 if (*s
== '*' || *s
== '?' ||
1537 *s
== '{' || *s
== '}' ||
1538 *s
== '[' || *s
== ']' ||
1543 hv
= (hv
<< 5) ^ *s
^ (hv
>> 23);
1546 return(((hv
>> 16) ^ hv
) & HMASK
);
1550 * WildCmp() - compare wild string to sane string
1552 * Return 0 on success, -1 on failure.
1556 WildCmp(const char *w
, const char *s
)
1559 * skip fixed portion
1565 if (w
[1] == 0) /* optimize wild* case */
1571 for (i
= 0; i
<= l
; ++i
) {
1572 if (WildCmp(w
+ 1, s
+ i
) == 0)
1586 if (*w
== 0) /* terminator */
1598 YesNo(const char *path
)
1602 fprintf(stderr
, "remove %s (Yes/No) [No]? ", path
);
1605 first
= ch
= getchar();
1606 while (ch
!= '\n' && ch
!= EOF
)
1608 return ((first
== 'y' || first
== 'Y'));
1612 * xrename() - rename with override
1614 * If the rename fails, attempt to override st_flags on the
1615 * destination and rename again. If that fails too, try to
1616 * set the flags back the way they were and give up.
1620 xrename(const char *src
, const char *dst
, u_long flags
)
1624 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1625 #ifdef _ST_FLAGS_PRESENT_
1626 hc_chflags(&DstHost
, dst
, 0);
1627 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0)
1628 hc_chflags(&DstHost
, dst
, flags
);
1635 xlink(const char *src
, const char *dst
, u_long flags
)
1638 #ifdef _ST_FLAGS_PRESENT_
1642 if ((r
= hc_link(&DstHost
, src
, dst
)) < 0) {
1643 #ifdef _ST_FLAGS_PRESENT_
1644 hc_chflags(&DstHost
, src
, 0);
1645 r
= hc_link(&DstHost
, src
, dst
);
1647 hc_chflags(&DstHost
, src
, flags
);
1657 xremove(struct HostConf
*host
, const char *path
)
1661 res
= hc_remove(host
, path
);
1662 #ifdef _ST_FLAGS_PRESENT_
1663 if (res
== -EPERM
) {
1664 hc_chflags(host
, path
, 0);
1665 res
= hc_remove(host
, path
);