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;
151 const char *ssh_argv
[16];
154 const char *UseCpFile
;
155 const char *UseHLPath
;
156 const char *MD5CacheFile
;
157 const char *FSMIDCacheFile
;
159 int64_t CountSourceBytes
;
160 int64_t CountSourceItems
;
161 int64_t CountCopiedItems
;
162 int64_t CountSourceReadBytes
;
163 int64_t CountTargetReadBytes
;
164 int64_t CountWriteBytes
;
165 int64_t CountRemovedItems
;
166 int64_t CountLinkedItems
;
168 struct HostConf SrcHost
;
169 struct HostConf DstHost
;
172 pthread_mutex_t MasterMutex
;
176 main(int ac
, char **av
)
182 struct timeval start
;
183 struct copy_info info
;
185 signal(SIGPIPE
, SIG_IGN
);
187 RunningAsUser
= (geteuid() != 0);
188 RunningAsRoot
= !RunningAsUser
;
191 for (i
= 0; i
< HCTHASH_SIZE
; ++i
) {
192 pthread_mutex_init(&SrcHost
.hct_mutex
[i
], NULL
);
193 pthread_mutex_init(&DstHost
.hct_mutex
[i
], NULL
);
195 pthread_mutex_init(&MasterMutex
, NULL
);
196 pthread_mutex_lock(&MasterMutex
);
199 gettimeofday(&start
, NULL
);
200 for (i
= 1; i
< ac
; ++i
) {
207 } else if (dst
== NULL
) {
210 fatal("too many arguments");
218 v
= strtol(ptr
, NULL
, 0);
226 while (*ptr
== 'v') {
230 if (*ptr
>= '0' && *ptr
<= '9')
231 VerboseOpt
= strtol(ptr
, NULL
, 0);
247 UseCpFile
= ".cpignore";
250 UseCpFile
= (*ptr
) ? ptr
: av
[++i
];
253 UseHLPath
= (*ptr
) ? ptr
: av
[++i
];
257 fatal("too many -F options");
258 ssh_argv
[ssh_argc
++] = (*ptr
) ? ptr
: av
[++i
];
283 FSMIDCacheFile
= ".FSMID.CHECK";
287 FSMIDCacheFile
= av
[++i
];
291 MD5CacheFile
= av
[++i
];
295 MD5CacheFile
= ".MD5.CHECKSUMS";
298 setvbuf(stdout
, NULL
, _IOLBF
, 0);
301 fatal("illegal option: %s\n", ptr
- 2);
308 * If we are told to go into slave mode, run the HC protocol
316 * Extract the source and/or/neither target [user@]host and
317 * make any required connections.
319 if (src
&& (ptr
= strchr(src
, ':')) != NULL
) {
320 asprintf(&SrcHost
.host
, "%*.*s", (int)(ptr
- src
), (int)(ptr
- src
), src
);
323 fprintf(stderr
, "The cpignore options are not currently supported for remote sources\n");
327 fprintf(stderr
, "The MD5 options are not currently supported for remote sources\n");
330 if (hc_connect(&SrcHost
) < 0)
333 if (dst
&& (ptr
= strchr(dst
, ':')) != NULL
) {
334 asprintf(&DstHost
.host
, "%*.*s", (int)(ptr
- dst
), (int)(ptr
- dst
), dst
);
337 fprintf(stderr
, "The FSMID options are not currently supported for remote targets\n");
340 if (hc_connect(&DstHost
) < 0)
345 * dst may be NULL only if -m option is specified,
346 * which forces an update of the MD5 checksums
348 if (dst
== NULL
&& UseMD5Opt
== 0) {
352 bzero(&info
, sizeof(info
));
356 pthread_cond_init(&info
.cond
, NULL
);
359 DstBaseLen
= strlen(dst
);
362 info
.sdevNo
= (dev_t
)-1;
363 info
.ddevNo
= (dev_t
)-1;
364 i
= DoCopy(&info
, -1);
368 info
.sdevNo
= (dev_t
)-1;
369 info
.ddevNo
= (dev_t
)-1;
370 i
= DoCopy(&info
, -1);
373 pthread_cond_destroy(&info
.cond
);
380 if (SummaryOpt
&& i
== 0) {
384 gettimeofday(&end
, NULL
);
386 /* don't count stat's in our byte statistics */
387 CountSourceBytes
+= sizeof(struct stat
) * CountSourceItems
;
388 CountSourceReadBytes
+= sizeof(struct stat
) * CountSourceItems
;
389 CountWriteBytes
+= sizeof(struct stat
) * CountCopiedItems
;
390 CountWriteBytes
+= sizeof(struct stat
) * CountRemovedItems
;
393 duration
= (end
.tv_sec
- start
.tv_sec
);
394 duration
+= (double)(end
.tv_usec
- start
.tv_usec
) / 1000000.0;
397 logstd("cpdup completed successfully\n");
398 logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
399 "%lld bytes written (%.1fX speedup)\n",
400 (long long)CountSourceBytes
,
401 (long long)CountSourceReadBytes
,
402 (long long)CountTargetReadBytes
,
403 (long long)CountWriteBytes
,
404 ((double)CountSourceBytes
* 2.0) / ((double)(CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
)));
405 logstd("%lld source items, %lld items copied, %lld items linked, "
406 "%lld things deleted\n",
407 (long long)CountSourceItems
,
408 (long long)CountCopiedItems
,
409 (long long)CountLinkedItems
,
410 (long long)CountRemovedItems
);
411 logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
413 (int)((CountSourceReadBytes
+ CountTargetReadBytes
+ CountWriteBytes
) / duration
/ 1024.0),
414 (int)(CountSourceBytes
/ duration
/ 1024.0));
416 exit((i
== 0) ? 0 : 1);
419 static struct hlink
*
420 hltlookup(struct stat
*stp
)
423 struct timespec ts
= { 0, 100000 };
428 n
= stp
->st_ino
& HLMASK
;
433 for (hl
= hltable
[n
]; hl
; hl
= hl
->next
) {
434 if (hl
->ino
== stp
->st_ino
) {
437 * If the hl entry is still in the process of being created
438 * by another thread we have to wait until it has either been
439 * deleted or completed.
442 pthread_mutex_unlock(&MasterMutex
);
443 nanosleep(&ts
, NULL
);
444 pthread_mutex_lock(&MasterMutex
);
456 static struct hlink
*
457 hltadd(struct stat
*stp
, const char *path
)
460 int plen
= strlen(path
);
463 new = malloc(offsetof(struct hlink
, name
[plen
+ 1]));
465 fprintf(stderr
, "out of memory\n");
470 /* initialize and link the new element into the table */
471 new->ino
= stp
->st_ino
;
472 new->dino
= (ino_t
)-1;
474 bcopy(path
, new->name
, plen
+ 1);
477 n
= stp
->st_ino
& HLMASK
;
478 new->next
= hltable
[n
];
480 hltable
[n
]->prev
= new;
487 hltsetdino(struct hlink
*hl
, ino_t inum
)
493 hltdelete(struct hlink
*hl
)
495 assert(hl
->refs
== 1);
499 hl
->next
->prev
= hl
->prev
;
500 hl
->prev
->next
= hl
->next
;
503 hl
->next
->prev
= NULL
;
505 hltable
[hl
->ino
& HLMASK
] = hl
->next
;
512 hltrels(struct hlink
*hl
)
514 assert(hl
->refs
== 1);
519 * If UseHLPath is defined check to see if the file in question is
520 * the same as the source file, and if it is return a pointer to the
521 * -H path based file for hardlinking. Else return NULL.
524 checkHLPath(struct stat
*st1
, const char *spath
, const char *dpath
)
530 asprintf(&hpath
, "%s%s", UseHLPath
, dpath
+ DstBaseLen
);
533 * stat info matches ?
535 if (hc_stat(&DstHost
, hpath
, &sthl
) < 0 ||
536 st1
->st_size
!= sthl
.st_size
||
537 st1
->st_mtime
!= sthl
.st_mtime
||
538 (RunningAsRoot
&& (st1
->st_uid
!= sthl
.st_uid
||
539 st1
->st_gid
!= sthl
.st_gid
))
546 * If ForceOpt or ValidateOpt is set we have to compare the files
548 if (ForceOpt
|| ValidateOpt
) {
549 error
= validate_check(spath
, hpath
);
559 * Return 0 if the contents of the file <spath> matches the contents of
563 validate_check(const char *spath
, const char *dpath
)
569 fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0);
570 fd2
= hc_open(&DstHost
, dpath
, O_RDONLY
, 0);
573 if (fd1
>= 0 && fd2
>= 0) {
576 char *iobuf1
= malloc(GETIOSIZE
);
577 char *iobuf2
= malloc(GETIOSIZE
);
579 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
580 CountSourceReadBytes
+= n
;
581 x
= hc_read(&DstHost
, fd2
, iobuf2
, GETIOSIZE
);
583 CountTargetReadBytes
+= x
;
586 if (bcmp(iobuf1
, iobuf2
, n
) != 0)
595 hc_close(&SrcHost
, fd1
);
597 hc_close(&DstHost
, fd2
);
603 DoCopyThread(void *arg
)
605 copy_info_t cinfo
= arg
;
606 char *spath
= cinfo
->spath
;
607 char *dpath
= cinfo
->dpath
;
610 r
= pthread_detach(pthread_self());
612 pthread_cond_init(&cinfo
->cond
, NULL
);
613 pthread_mutex_lock(&MasterMutex
);
614 cinfo
->r
+= DoCopy(cinfo
, 0);
615 /* cinfo arguments invalid on return */
616 --cinfo
->parent
->children
;
618 pthread_cond_signal(&cinfo
->parent
->cond
);
622 pthread_cond_destroy(&cinfo
->cond
);
624 hcc_free_trans(&SrcHost
);
625 hcc_free_trans(&DstHost
);
626 pthread_mutex_unlock(&MasterMutex
);
633 DoCopy(copy_info_t info
, int depth
)
635 const char *spath
= info
->spath
;
636 const char *dpath
= info
->dpath
;
637 dev_t sdevNo
= info
->sdevNo
;
638 dev_t ddevNo
= info
->ddevNo
;
641 unsigned long st2_flags
;
642 int r
, mres
, fres
, st2Valid
;
644 List
*list
= malloc(sizeof(List
));
648 r
= mres
= fres
= st2Valid
= 0;
653 if (hc_lstat(&SrcHost
, spath
, &st1
) != 0) {
658 /* skip snapshot files because they're sparse and _huge_ */
659 if (st1
.st_flags
& SF_SNAPSHOT
)
662 st2
.st_mode
= 0; /* in case lstat fails */
663 st2
.st_flags
= 0; /* in case lstat fails */
664 if (dpath
&& hc_lstat(&DstHost
, dpath
, &st2
) == 0) {
666 #ifdef _ST_FLAGS_PRESENT_
667 st2_flags
= st2
.st_flags
;
671 if (S_ISREG(st1
.st_mode
)) {
679 if (S_ISREG(st1
.st_mode
) && st1
.st_nlink
> 1 && dpath
) {
680 if ((hln
= hltlookup(&st1
)) != NULL
) {
684 if (st2
.st_ino
== hln
->dino
) {
686 * hard link is already correct, nothing to do
689 logstd("%-32s nochange\n", (dpath
) ? dpath
: spath
);
690 if (hln
->nlinked
== st1
.st_nlink
) {
699 * hard link is not correct, attempt to unlink it
701 if (xremove(&DstHost
, dpath
) < 0) {
702 logerr("%-32s hardlink: unable to unlink: %s\n",
703 ((dpath
) ? dpath
: spath
), strerror(errno
));
712 if (xlink(hln
->name
, dpath
, st1
.st_flags
) < 0) {
713 int tryrelink
= (errno
== EMLINK
);
714 logerr("%-32s hardlink: unable to link to %s: %s\n",
715 (dpath
? dpath
: spath
), hln
->name
, strerror(errno
)
720 logerr("%-20s hardlink: will attempt to copy normally\n");
725 if (hln
->nlinked
== st1
.st_nlink
) {
731 logstd("%-32s hardlink: %s\n",
732 (dpath
? dpath
: spath
),
733 (st2Valid
? "relinked" : "linked")
744 * first instance of hardlink must be copied normally
747 hln
= hltadd(&st1
, dpath
);
752 * Do we need to copy the file/dir/link/whatever? Early termination
753 * if we do not. Always redo links. Directories are always traversed
754 * except when the FSMID options are used.
756 * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
761 && st1
.st_mode
== st2
.st_mode
762 #ifdef _ST_FLAGS_PRESENT_
763 && (RunningAsUser
|| st1
.st_flags
== st2
.st_flags
)
766 if (S_ISLNK(st1
.st_mode
) || S_ISDIR(st1
.st_mode
)) {
768 * If FSMID tracking is turned on we can avoid recursing through
769 * an entire directory subtree if the FSMID matches.
771 #ifdef _ST_FSMID_PRESENT_
773 (UseFSMIDOpt
&& (fres
= fsmid_check(st1
.st_fsmid
, dpath
)) == 0)
775 if (VerboseOpt
>= 3) {
777 logstd("%-32s fsmid-nochange\n", (dpath
? dpath
: spath
));
779 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
787 st1
.st_size
== st2
.st_size
&&
788 st1
.st_mtime
== st2
.st_mtime
&&
789 (RunningAsUser
|| (st1
.st_uid
== st2
.st_uid
&&
790 st1
.st_gid
== st2
.st_gid
))
792 && (UseMD5Opt
== 0 || !S_ISREG(st1
.st_mode
) ||
793 (mres
= md5_check(spath
, dpath
)) == 0)
795 #ifdef _ST_FSMID_PRESENT_
796 && (UseFSMIDOpt
== 0 ||
797 (fres
= fsmid_check(st1
.st_fsmid
, dpath
)) == 0)
799 && (ValidateOpt
== 0 || !S_ISREG(st1
.st_mode
) ||
800 validate_check(spath
, dpath
) == 0)
803 * The files are identical, but if we are not running as
804 * root we might need to adjust ownership/group/flags.
807 int changedflags
= 0;
809 hltsetdino(hln
, st2
.st_ino
);
811 if (RunningAsUser
&& (st1
.st_uid
!= st2
.st_uid
||
812 st1
.st_gid
!= st2
.st_gid
)) {
813 hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
);
816 #ifdef _ST_FLAGS_PRESENT_
817 if (RunningAsUser
&& st1
.st_flags
!= st2
.st_flags
) {
818 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
822 if (VerboseOpt
>= 3) {
825 logstd("%-32s md5-nochange",
826 (dpath
? dpath
: spath
));
830 logstd("%-32s fsmid-nochange",
831 (dpath
? dpath
: spath
));
832 } else if (ValidateOpt
) {
833 logstd("%-32s nochange (contents validated)",
834 (dpath
? dpath
: spath
));
836 logstd("%-32s nochange", (dpath
? dpath
: spath
));
839 logstd(" (uid/gid differ)");
841 logstd(" (flags differ)");
844 CountSourceBytes
+= size
;
851 if (st2Valid
&& !S_ISDIR(st1
.st_mode
) && S_ISDIR(st2
.st_mode
)) {
853 logerr("%-32s SAFETY - refusing to copy file over directory\n",
854 (dpath
? dpath
: spath
)
858 goto done
; /* continue with the cpdup anyway */
860 if (QuietOpt
== 0 || AskConfirmation
) {
861 logstd("%-32s WARNING: non-directory source will blow away\n"
862 "%-32s preexisting dest directory, continuing anyway!\n",
863 ((dpath
) ? dpath
: spath
), "");
866 RemoveRecur(dpath
, ddevNo
);
871 * The various comparisons failed, copy it.
873 if (S_ISDIR(st1
.st_mode
)) {
877 logerr("%-32s/ fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
878 if ((dir
= hc_opendir(&SrcHost
, spath
)) != NULL
) {
883 if (S_ISDIR(st2
.st_mode
) == 0) {
884 xremove(&DstHost
, dpath
);
885 if (hc_mkdir(&DstHost
, dpath
, st1
.st_mode
| 0700) != 0) {
886 logerr("%s: mkdir failed: %s\n",
887 (dpath
? dpath
: spath
), strerror(errno
));
893 * Matt: why don't you check error codes here?
894 * (Because I'm an idiot... checks added!)
896 if (hc_lstat(&DstHost
, dpath
, &st2
) != 0) {
897 logerr("%s: lstat of newly made dir failed: %s\n",
898 (dpath
? dpath
: spath
), strerror(errno
));
902 if (hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
) != 0){
903 logerr("%s: chown of newly made dir failed: %s\n",
904 (dpath
? dpath
: spath
), strerror(errno
));
911 * Directory must be scanable by root for cpdup to
912 * work. We'll fix it later if the directory isn't
913 * supposed to be readable ( which is why we fixup
914 * st2.st_mode to match what we did ).
916 if ((st2
.st_mode
& 0700) != 0700) {
917 hc_chmod(&DstHost
, dpath
, st2
.st_mode
| 0700);
921 logstd("%s\n", dpath
? dpath
: spath
);
926 * When copying a directory, stop if the source crosses a mount
929 if (sdevNo
!= (dev_t
)-1 && st1
.st_dev
!= sdevNo
) {
936 * When copying a directory, stop if the destination crosses
939 * The target directory will have been created and stat'd
940 * for st2 if it did not previously exist. st2Valid is left
941 * as a flag. If the stat failed st2 will still only have its
942 * default initialization.
944 * So we simply assume here that the directory is within the
945 * current target mount if we had to create it (aka st2Valid is 0)
946 * and we leave ddevNo alone.
949 if (ddevNo
!= (dev_t
)-1 && st2
.st_dev
!= ddevNo
) {
957 * scan .cpignore file for files/directories
963 char *buf
= malloc(GETBUFSIZE
);
966 if (UseCpFile
[0] == '/') {
967 fpath
= mprintf("%s", UseCpFile
);
969 fpath
= mprintf("%s/%s", spath
, UseCpFile
);
971 AddList(list
, strrchr(fpath
, '/') + 1, 1);
972 if ((fi
= fopen(fpath
, "r")) != NULL
) {
973 while (fgets(buf
, GETBUFSIZE
, fi
) != NULL
) {
975 CountSourceReadBytes
+= l
;
976 if (l
&& buf
[l
-1] == '\n')
979 AddList(list
, buf
, 1);
988 * Automatically exclude MD5CacheFile that we create on the
989 * source from the copy to the destination.
991 * Automatically exclude a FSMIDCacheFile on the source that
992 * would otherwise overwrite the one we maintain on the target.
995 AddList(list
, MD5CacheFile
, 1);
997 AddList(list
, FSMIDCacheFile
, 1);
999 while (noLoop
== 0 && (den
= hc_readdir(&SrcHost
, dir
)) != NULL
) {
1004 char *ndpath
= NULL
;
1006 if (strcmp(den
->d_name
, ".") == 0 ||
1007 strcmp(den
->d_name
, "..") == 0
1012 * ignore if on .cpignore list
1014 if (AddList(list
, den
->d_name
, 0) == 1) {
1017 nspath
= mprintf("%s/%s", spath
, den
->d_name
);
1019 ndpath
= mprintf("%s/%s", dpath
, den
->d_name
);
1022 if (CurParallel
< MaxParallel
|| depth
> MAXDEPTH
) {
1023 copy_info_t cinfo
= malloc(sizeof(*cinfo
));
1024 pthread_t dummy_thr
;
1026 bzero(cinfo
, sizeof(*cinfo
));
1027 cinfo
->spath
= nspath
;
1028 cinfo
->dpath
= ndpath
;
1029 cinfo
->sdevNo
= sdevNo
;
1030 cinfo
->ddevNo
= ddevNo
;
1031 cinfo
->parent
= info
;
1034 pthread_create(&dummy_thr
, NULL
, DoCopyThread
, cinfo
);
1038 info
->spath
= nspath
;
1039 info
->dpath
= ndpath
;
1040 info
->sdevNo
= sdevNo
;
1041 info
->ddevNo
= ddevNo
;
1043 r
+= DoCopy(info
, depth
);
1045 r
+= DoCopy(info
, depth
+ 1);
1054 hc_closedir(&SrcHost
, dir
);
1058 * Wait for our children to finish
1060 while (info
->children
) {
1061 pthread_cond_wait(&info
->cond
, &MasterMutex
);
1068 * Remove files/directories from destination that do not appear
1071 if (dpath
&& (dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1072 while (noLoop
== 0 && (den
= hc_readdir(&DstHost
, dir
)) != NULL
) {
1076 if (strcmp(den
->d_name
, ".") == 0 ||
1077 strcmp(den
->d_name
, "..") == 0
1082 * If object does not exist in source or .cpignore
1083 * then recursively remove it.
1085 if (AddList(list
, den
->d_name
, 3) == 3) {
1088 ndpath
= mprintf("%s/%s", dpath
, den
->d_name
);
1089 RemoveRecur(ndpath
, ddevNo
);
1093 hc_closedir(&DstHost
, dir
);
1097 struct timeval tv
[2];
1101 st1
.st_uid
!= st2
.st_uid
||
1102 st1
.st_gid
!= st2
.st_gid
1104 hc_chown(&DstHost
, dpath
, st1
.st_uid
, st1
.st_gid
);
1106 if (st2Valid
== 0 || st1
.st_mode
!= st2
.st_mode
) {
1107 hc_chmod(&DstHost
, dpath
, st1
.st_mode
);
1109 #ifdef _ST_FLAGS_PRESENT_
1110 if (st2Valid
== 0 || st1
.st_flags
!= st2
.st_flags
) {
1111 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
1116 st1
.st_mtime
!= st2
.st_mtime
1118 bzero(tv
, sizeof(tv
));
1119 tv
[0].tv_sec
= st1
.st_mtime
;
1120 tv
[1].tv_sec
= st1
.st_mtime
;
1121 hc_utimes(&DstHost
, dpath
, tv
);
1125 } else if (dpath
== NULL
) {
1127 * If dpath is NULL, we are just updating the MD5
1130 if (UseMD5Opt
&& S_ISREG(st1
.st_mode
)) {
1131 mres
= md5_check(spath
, NULL
);
1133 if (VerboseOpt
> 1) {
1135 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1137 logstd("%-32s md5-ok\n", (dpath
) ? dpath
: spath
);
1138 } else if (!QuietOpt
&& mres
< 0) {
1139 logstd("%-32s md5-update\n", (dpath
) ? dpath
: spath
);
1143 } else if (S_ISREG(st1
.st_mode
)) {
1150 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1152 path
= mprintf("%s", dpath
);
1155 * Handle check failure message.
1159 logerr("%-32s md5-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1163 logerr("%-32s fsmid-CHECK-FAILED\n", (dpath
) ? dpath
: spath
);
1166 * Not quite ready to do the copy yet. If UseHLPath is defined,
1167 * see if we can hardlink instead.
1169 * If we can hardlink, and the target exists, we have to remove it
1170 * first or the hardlink will fail. This can occur in a number of
1171 * situations but must typically when the '-f -H' combination is
1174 if (UseHLPath
&& (hpath
= checkHLPath(&st1
, spath
, dpath
)) != NULL
) {
1176 xremove(&DstHost
, dpath
);
1177 if (hc_link(&DstHost
, hpath
, dpath
) == 0) {
1180 logstd("%-32s hardlinked(-H)\n",
1181 (dpath
? dpath
: spath
));
1187 * Shucks, we may have hit a filesystem hard linking limit,
1188 * we have to copy instead.
1193 if ((fd1
= hc_open(&SrcHost
, spath
, O_RDONLY
, 0)) >= 0) {
1194 if ((fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
, 0600)) < 0) {
1196 * There could be a .tmp file from a previously interrupted
1197 * run, delete and retry. Fail if we still can't get at it.
1199 #ifdef _ST_FLAGS_PRESENT_
1200 hc_chflags(&DstHost
, path
, 0);
1202 hc_remove(&DstHost
, path
);
1203 fd2
= hc_open(&DstHost
, path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_TRUNC
, 0600);
1207 char *iobuf1
= malloc(GETIOSIZE
);
1211 * Matt: What about holes?
1214 while ((n
= hc_read(&SrcHost
, fd1
, iobuf1
, GETIOSIZE
)) > 0) {
1216 if (hc_write(&DstHost
, fd2
, iobuf1
, n
) != n
)
1220 hc_close(&DstHost
, fd2
);
1222 struct timeval tv
[2];
1224 bzero(tv
, sizeof(tv
));
1225 tv
[0].tv_sec
= st1
.st_mtime
;
1226 tv
[1].tv_sec
= st1
.st_mtime
;
1228 hc_chown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1229 hc_chmod(&DstHost
, path
, st1
.st_mode
);
1230 #ifdef _ST_FLAGS_PRESENT_
1231 if (st1
.st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
1232 hc_utimes(&DstHost
, path
, tv
);
1234 hc_utimes(&DstHost
, path
, tv
);
1236 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1237 logerr("%-32s rename-after-copy failed: %s\n",
1238 (dpath
? dpath
: spath
), strerror(errno
)
1243 logstd("%-32s copy-ok\n", (dpath
? dpath
: spath
));
1244 #ifdef _ST_FLAGS_PRESENT_
1246 hc_chflags(&DstHost
, dpath
, st1
.st_flags
);
1249 #ifdef _ST_FLAGS_PRESENT_
1250 if ((st1
.st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
)) == 0)
1251 hc_utimes(&DstHost
, dpath
, tv
);
1253 CountSourceReadBytes
+= size
;
1254 CountWriteBytes
+= size
;
1255 CountSourceBytes
+= size
;
1259 logerr("%-32s %s failed: %s\n",
1260 (dpath
? dpath
: spath
), op
, strerror(errno
)
1262 hc_remove(&DstHost
, path
);
1267 logerr("%-32s create (uid %d, euid %d) failed: %s\n",
1268 (dpath
? dpath
: spath
), getuid(), geteuid(),
1273 hc_close(&SrcHost
, fd1
);
1275 logerr("%-32s copy: open failed: %s\n",
1276 (dpath
? dpath
: spath
),
1285 if (!r
&& hc_stat(&DstHost
, dpath
, &st2
) == 0) {
1286 hltsetdino(hln
, st2
.st_ino
);
1292 } else if (S_ISLNK(st1
.st_mode
)) {
1293 char *link1
= malloc(GETLINKSIZE
);
1294 char *link2
= malloc(GETLINKSIZE
);
1299 n1
= hc_readlink(&SrcHost
, spath
, link1
, GETLINKSIZE
- 1);
1301 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1302 n2
= hc_readlink(&DstHost
, dpath
, link2
, GETLINKSIZE
- 1);
1304 path
= mprintf("%s", dpath
);
1308 if (ForceOpt
|| n1
!= n2
|| bcmp(link1
, link2
, n1
) != 0) {
1309 hc_umask(&DstHost
, ~st1
.st_mode
);
1310 xremove(&DstHost
, path
);
1312 if (hc_symlink(&DstHost
, link1
, path
) < 0) {
1313 logerr("%-32s symlink (%s->%s) failed: %s\n",
1314 (dpath
? dpath
: spath
), link1
, path
,
1319 hc_lchown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1321 * there is no lchmod() or lchflags(), we
1322 * cannot chmod or chflags a softlink.
1324 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1325 logerr("%-32s rename softlink (%s->%s) failed: %s\n",
1326 (dpath
? dpath
: spath
),
1327 path
, dpath
, strerror(errno
));
1328 } else if (VerboseOpt
) {
1329 logstd("%-32s softlink-ok\n", (dpath
? dpath
: spath
));
1331 hc_umask(&DstHost
, 000);
1332 CountWriteBytes
+= n1
;
1336 if (VerboseOpt
>= 3)
1337 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1339 CountSourceBytes
+= n1
;
1340 CountSourceReadBytes
+= n1
;
1342 CountTargetReadBytes
+= n2
;
1346 logerr("%-32s softlink-failed\n", (dpath
? dpath
: spath
));
1351 } else if ((S_ISCHR(st1
.st_mode
) || S_ISBLK(st1
.st_mode
)) && DeviceOpt
) {
1356 st1
.st_mode
!= st2
.st_mode
||
1357 st1
.st_rdev
!= st2
.st_rdev
||
1358 st1
.st_uid
!= st2
.st_uid
||
1359 st1
.st_gid
!= st2
.st_gid
1362 path
= mprintf("%s.tmp%d", dpath
, (int)getpid());
1363 xremove(&DstHost
, path
);
1365 path
= mprintf("%s", dpath
);
1368 if (hc_mknod(&DstHost
, path
, st1
.st_mode
, st1
.st_rdev
) == 0) {
1369 hc_chmod(&DstHost
, path
, st1
.st_mode
);
1370 hc_chown(&DstHost
, path
, st1
.st_uid
, st1
.st_gid
);
1372 xremove(&DstHost
, dpath
);
1373 if (st2Valid
&& xrename(path
, dpath
, st2_flags
) != 0) {
1374 logerr("%-32s dev-rename-after-create failed: %s\n",
1375 (dpath
? dpath
: spath
),
1378 } else if (VerboseOpt
) {
1379 logstd("%-32s dev-ok\n", (dpath
? dpath
: spath
));
1384 logerr("%-32s dev failed: %s\n",
1385 (dpath
? dpath
: spath
), strerror(errno
)
1389 if (VerboseOpt
>= 3)
1390 logstd("%-32s nochange\n", (dpath
? dpath
: spath
));
1398 if (hln
->dino
== (ino_t
)-1) {
1400 /*hln = NULL; unneeded */
1415 RemoveRecur(const char *dpath
, dev_t devNo
)
1419 if (hc_lstat(&DstHost
, dpath
, &st
) == 0) {
1420 if (devNo
== (dev_t
)-1)
1422 if (st
.st_dev
== devNo
) {
1423 if (S_ISDIR(st
.st_mode
)) {
1426 if ((dir
= hc_opendir(&DstHost
, dpath
)) != NULL
) {
1428 while ((den
= hc_readdir(&DstHost
, dir
)) != NULL
) {
1431 if (strcmp(den
->d_name
, ".") == 0)
1433 if (strcmp(den
->d_name
, "..") == 0)
1435 ndpath
= mprintf("%s/%s", dpath
, den
->d_name
);
1436 RemoveRecur(ndpath
, devNo
);
1439 hc_closedir(&DstHost
, dir
);
1441 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1443 if (hc_rmdir(&DstHost
, dpath
) < 0) {
1444 logerr("%-32s rmdir failed: %s\n",
1445 dpath
, strerror(errno
)
1448 CountRemovedItems
++;
1453 logstd("%-32s not-removed\n", dpath
);
1454 } else if (hc_rmdir(&DstHost
, dpath
) == 0) {
1456 logstd("%-32s rmdir-ok\n", dpath
);
1457 CountRemovedItems
++;
1459 logerr("%-32s rmdir failed: %s\n",
1460 dpath
, strerror(errno
)
1465 if (AskConfirmation
&& NoRemoveOpt
== 0) {
1467 if (xremove(&DstHost
, dpath
) < 0) {
1468 logerr("%-32s remove failed: %s\n",
1469 dpath
, strerror(errno
)
1472 CountRemovedItems
++;
1477 logstd("%-32s not-removed\n", dpath
);
1478 } else if (xremove(&DstHost
, dpath
) == 0) {
1480 logstd("%-32s remove-ok\n", dpath
);
1481 CountRemovedItems
++;
1483 logerr("%-32s remove failed: %s\n",
1484 dpath
, strerror(errno
)
1494 InitList(List
*list
)
1496 bzero(list
, sizeof(List
));
1497 list
->li_Node
.no_Next
= &list
->li_Node
;
1501 ResetList(List
*list
)
1505 while ((node
= list
->li_Node
.no_Next
) != &list
->li_Node
) {
1506 list
->li_Node
.no_Next
= node
->no_Next
;
1513 AddList(List
*list
, const char *name
, int n
)
1521 * Scan against wildcards. Only a node value of 1 can be a wildcard
1522 * ( usually scanned from .cpignore )
1525 for (node
= list
->li_Hash
[0]; node
; node
= node
->no_HNext
) {
1526 if (strcmp(name
, node
->no_Name
) == 0 ||
1527 (n
!= 1 && node
->no_Value
== 1 && WildCmp(node
->no_Name
, name
) == 0)
1529 return(node
->no_Value
);
1534 * Look for exact match
1537 for (node
= list
->li_Hash
[hv
]; node
; node
= node
->no_HNext
) {
1538 if (strcmp(name
, node
->no_Name
) == 0) {
1539 return(node
->no_Value
);
1542 node
= malloc(sizeof(Node
) + strlen(name
) + 1);
1544 fprintf(stderr
, "out of memory\n");
1548 node
->no_Next
= list
->li_Node
.no_Next
;
1549 list
->li_Node
.no_Next
= node
;
1551 node
->no_HNext
= list
->li_Hash
[hv
];
1552 list
->li_Hash
[hv
] = node
;
1554 strcpy(node
->no_Name
, name
);
1561 shash(const char *s
)
1568 if (*s
== '*' || *s
== '?' ||
1569 *s
== '{' || *s
== '}' ||
1570 *s
== '[' || *s
== ']' ||
1575 hv
= (hv
<< 5) ^ *s
^ (hv
>> 23);
1578 return(((hv
>> 16) ^ hv
) & HMASK
);
1582 * WildCmp() - compare wild string to sane string
1584 * Return 0 on success, -1 on failure.
1588 WildCmp(const char *w
, const char *s
)
1591 * skip fixed portion
1597 if (w
[1] == 0) /* optimize wild* case */
1603 for (i
= 0; i
<= l
; ++i
) {
1604 if (WildCmp(w
+ 1, s
+ i
) == 0)
1618 if (*w
== 0) /* terminator */
1630 YesNo(const char *path
)
1634 fprintf(stderr
, "remove %s (Yes/No) [No]? ", path
);
1637 first
= ch
= getchar();
1638 while (ch
!= '\n' && ch
!= EOF
)
1640 return ((first
== 'y' || first
== 'Y'));
1644 * xrename() - rename with override
1646 * If the rename fails, attempt to override st_flags on the
1647 * destination and rename again. If that fails too, try to
1648 * set the flags back the way they were and give up.
1652 xrename(const char *src
, const char *dst
, u_long flags
)
1656 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0) {
1657 #ifdef _ST_FLAGS_PRESENT_
1658 hc_chflags(&DstHost
, dst
, 0);
1659 if ((r
= hc_rename(&DstHost
, src
, dst
)) < 0)
1660 hc_chflags(&DstHost
, dst
, flags
);
1667 xlink(const char *src
, const char *dst
, u_long flags
)
1670 #ifdef _ST_FLAGS_PRESENT_
1674 if ((r
= hc_link(&DstHost
, src
, dst
)) < 0) {
1675 #ifdef _ST_FLAGS_PRESENT_
1676 hc_chflags(&DstHost
, src
, 0);
1677 r
= hc_link(&DstHost
, src
, dst
);
1679 hc_chflags(&DstHost
, src
, flags
);
1689 xremove(struct HostConf
*host
, const char *path
)
1693 res
= hc_remove(host
, path
);
1694 #ifdef _ST_FLAGS_PRESENT_
1695 if (res
== -EPERM
) {
1696 hc_chflags(host
, path
, 0);
1697 res
= hc_remove(host
, path
);