xref: /dflybsd-src/bin/cpdup/cpdup.c (revision 5ca0a96d6c3bf50926197b4bb92af7969ed3528a)
13a42736dSMatthew Dillon /*-
23a42736dSMatthew Dillon  * CPDUP.C
33a42736dSMatthew Dillon  *
43a42736dSMatthew Dillon  * CPDUP <options> source destination
53a42736dSMatthew Dillon  *
63a42736dSMatthew Dillon  * (c) Copyright 1997-1999 by Matthew Dillon and Dima Ruban.  Permission to
73a42736dSMatthew Dillon  *     use and distribute based on the FreeBSD copyright.  Supplied as-is,
83a42736dSMatthew Dillon  *     USE WITH EXTREME CAUTION.
93a42736dSMatthew Dillon  *
103a42736dSMatthew Dillon  * This program attempts to duplicate the source onto the destination as
113a42736dSMatthew Dillon  * exactly as possible, retaining modify times, flags, perms, uid, and gid.
123a42736dSMatthew Dillon  * It can duplicate devices, files (including hardlinks), softlinks,
133a42736dSMatthew Dillon  * directories, and so forth.  It is recursive by default!  The duplication
143a42736dSMatthew Dillon  * is inclusive of removal of files/directories on the destination that do
153a42736dSMatthew Dillon  * not exist on the source.  This program supports a per-directory exception
163a42736dSMatthew Dillon  * file called .cpignore, or a user-specified exception file.
173a42736dSMatthew Dillon  *
183a42736dSMatthew Dillon  * Safety features:
193a42736dSMatthew Dillon  *
203a42736dSMatthew Dillon  *	- does not cross partition boundries on source
213a42736dSMatthew Dillon  *	- asks for confirmation on deletions unless -i0 is specified
223a42736dSMatthew Dillon  *	- refuses to replace a destination directory with a source file
233a42736dSMatthew Dillon  *	  unless -s0 is specified.
243a42736dSMatthew Dillon  *	- terminates on error
253a42736dSMatthew Dillon  *
263a42736dSMatthew Dillon  * Copying features:
273a42736dSMatthew Dillon  *
283a42736dSMatthew Dillon  *	- does not copy file if mtime, flags, perms, and size match unless
293a42736dSMatthew Dillon  *	  forced
303a42736dSMatthew Dillon  *
313a42736dSMatthew Dillon  *	- copies to temporary and renames-over the original, allowing
323a42736dSMatthew Dillon  *	  you to update live systems
333a42736dSMatthew Dillon  *
343a42736dSMatthew Dillon  *	- copies uid, gid, mtime, perms, flags, softlinks, devices, hardlinks,
353a42736dSMatthew Dillon  *	  and recurses through directories.
363a42736dSMatthew Dillon  *
373a42736dSMatthew Dillon  *	- accesses a per-directory exclusion file, .cpignore, containing
383a42736dSMatthew Dillon  *	  standard wildcarded ( ? / * style, NOT regex) exclusions.
393a42736dSMatthew Dillon  *
403a42736dSMatthew Dillon  *	- tries to play permissions and flags smart in regards to overwriting
413a42736dSMatthew Dillon  *	  schg files and doing related stuff.
423a42736dSMatthew Dillon  *
433a42736dSMatthew Dillon  *	- Can do MD5 consistancy checks
443a42736dSMatthew Dillon  *
454e316ad5SMatthew Dillon  *	- Is able to do incremental mirroring/backups via hardlinks from
464e316ad5SMatthew Dillon  *	  the 'previous' version (supplied with -H path).
473a42736dSMatthew Dillon  */
483a42736dSMatthew Dillon 
493a42736dSMatthew Dillon /*-
50de78d61cSzrj  * Example: cc -O cpdup.c -o cpdup -lcrypto
513a42736dSMatthew Dillon  *
523a42736dSMatthew Dillon  * ".MD5.CHECKSUMS" contains md5 checksumms for the current directory.
533a42736dSMatthew Dillon  * This file is stored on the source.
543a42736dSMatthew Dillon  */
553a42736dSMatthew Dillon 
563a42736dSMatthew Dillon #include "cpdup.h"
574d858d58SMatthew Dillon #include "hclink.h"
584d858d58SMatthew Dillon #include "hcproto.h"
593a42736dSMatthew Dillon 
60a2dc574cSMatthew Dillon #define HSIZE	8192
613a42736dSMatthew Dillon #define HMASK	(HSIZE-1)
624e316ad5SMatthew Dillon #define HLSIZE	8192
634e316ad5SMatthew Dillon #define HLMASK	(HLSIZE - 1)
643a42736dSMatthew Dillon 
65975200d7SMatthew Dillon #define GETBUFSIZE	8192
66975200d7SMatthew Dillon #define GETPATHSIZE	2048
67975200d7SMatthew Dillon #define GETLINKSIZE	1024
68975200d7SMatthew Dillon #define GETIOSIZE	65536
69975200d7SMatthew Dillon 
704d858d58SMatthew Dillon #ifndef _ST_FLAGS_PRESENT_
714d858d58SMatthew Dillon #define st_flags	st_mode
724d858d58SMatthew Dillon #endif
734d858d58SMatthew Dillon 
743a42736dSMatthew Dillon typedef struct Node {
753a42736dSMatthew Dillon     struct Node *no_Next;
763a42736dSMatthew Dillon     struct Node *no_HNext;
77c0538630SMatthew Dillon     struct stat *no_Stat;
783a42736dSMatthew Dillon     int  no_Value;
793a42736dSMatthew Dillon     char no_Name[4];
803a42736dSMatthew Dillon } Node;
813a42736dSMatthew Dillon 
823a42736dSMatthew Dillon typedef struct List {
833a42736dSMatthew Dillon     Node	li_Node;
843a42736dSMatthew Dillon     Node	*li_Hash[HSIZE];
853a42736dSMatthew Dillon } List;
863a42736dSMatthew Dillon 
873a42736dSMatthew Dillon struct hlink {
883a42736dSMatthew Dillon     ino_t ino;
893a42736dSMatthew Dillon     ino_t dino;
90975200d7SMatthew Dillon     int	refs;
913a42736dSMatthew Dillon     struct hlink *next;
923a42736dSMatthew Dillon     struct hlink *prev;
9302141299SSascha Wildner     nlink_t nlinked;
94c0538630SMatthew Dillon     char name[];
953a42736dSMatthew Dillon };
963a42736dSMatthew Dillon 
97a2dc574cSMatthew Dillon typedef struct copy_info {
98a2dc574cSMatthew Dillon 	char *spath;
99a2dc574cSMatthew Dillon 	char *dpath;
100a2dc574cSMatthew Dillon 	dev_t sdevNo;
101a2dc574cSMatthew Dillon 	dev_t ddevNo;
102a2dc574cSMatthew Dillon } *copy_info_t;
103a2dc574cSMatthew Dillon 
10475bd842aSMatthew Dillon static struct hlink *hltable[HLSIZE];
10558860d7dSMatthew Dillon 
10675bd842aSMatthew Dillon static void RemoveRecur(const char *dpath, dev_t devNo, struct stat *dstat);
10775bd842aSMatthew Dillon static void InitList(List *list);
10875bd842aSMatthew Dillon static void ResetList(List *list);
10975bd842aSMatthew Dillon static Node *IterateList(List *list, Node *node, int n);
11075bd842aSMatthew Dillon static int AddList(List *list, const char *name, int n, struct stat *st);
111560e4370SMatthew Dillon static int CheckList(List *list, const char *path, const char *name);
112293141b7SMatthew Dillon static int getbool(const char *str);
113d72200edSMatthew Dillon static char *SplitRemote(char **pathp);
114293141b7SMatthew Dillon static int ChgrpAllowed(gid_t g);
115293141b7SMatthew Dillon static int OwnerMatch(struct stat *st1, struct stat *st2);
116293141b7SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
117293141b7SMatthew Dillon static int FlagsMatch(struct stat *st1, struct stat *st2);
118293141b7SMatthew Dillon #else
119293141b7SMatthew Dillon #define FlagsMatch(st1, st2)	1
120293141b7SMatthew Dillon #endif
121fce2564bSJeroen Ruigrok/asmodai static struct hlink *hltlookup(struct stat *);
122fce2564bSJeroen Ruigrok/asmodai static struct hlink *hltadd(struct stat *, const char *);
1234e316ad5SMatthew Dillon static char *checkHLPath(struct stat *st, const char *spath, const char *dpath);
124d5fdcd00SMatthew Dillon static int validate_check(const char *spath, const char *dpath);
125fce2564bSJeroen Ruigrok/asmodai static int shash(const char *s);
126fce2564bSJeroen Ruigrok/asmodai static void hltdelete(struct hlink *);
12744dd1628SMatthew Dillon static void hltsetdino(struct hlink *, ino_t);
12875bd842aSMatthew Dillon static int YesNo(const char *path);
129fce2564bSJeroen Ruigrok/asmodai static int xrename(const char *src, const char *dst, u_long flags);
130fce2564bSJeroen Ruigrok/asmodai static int xlink(const char *src, const char *dst, u_long flags);
13177133d96SMatthew Dillon static int xremove(struct HostConf *host, const char *path);
1329d626b29SSascha Wildner static int xrmdir(struct HostConf *host, const char *path);
133c0538630SMatthew Dillon static int DoCopy(copy_info_t info, struct stat *stat1, int depth);
134293141b7SMatthew Dillon static int ScanDir(List *list, struct HostConf *host, const char *path,
135293141b7SMatthew Dillon 	int64_t *CountReadBytes, int n);
1360212bfceSMatthew Dillon static int mtimecmp(struct stat *st1, struct stat *st2);
1372b7dbe20SMatthew Dillon static int symlink_mfo_test(struct HostConf *hc, struct stat *st1,
1382b7dbe20SMatthew Dillon 	struct stat *st2);
1393a42736dSMatthew Dillon 
1403a42736dSMatthew Dillon int AskConfirmation = 1;
1413a42736dSMatthew Dillon int SafetyOpt = 1;
142577109eaSMatthew Dillon int ForceOpt;
143ac482a7eSMatthew Dillon int DeviceOpt = 1;
144577109eaSMatthew Dillon int VerboseOpt;
145293141b7SMatthew Dillon int DirShowOpt;
146698d0b11SMatthew Dillon int NotForRealOpt;
147577109eaSMatthew Dillon int QuietOpt;
148577109eaSMatthew Dillon int NoRemoveOpt;
149577109eaSMatthew Dillon int UseMD5Opt;
150577109eaSMatthew Dillon int SummaryOpt;
15144dd1628SMatthew Dillon int CompressOpt;
1524d858d58SMatthew Dillon int SlaveOpt;
153c0538630SMatthew Dillon int ReadOnlyOpt;
154d5fdcd00SMatthew Dillon int ValidateOpt;
1558f0e7bc1SMatthew Dillon int ssh_argc;
1568f0e7bc1SMatthew Dillon const char *ssh_argv[16];
157293141b7SMatthew Dillon int DstRootPrivs;
15875bd842aSMatthew Dillon 
1591c755102SChris Pressey const char *UseCpFile;
160577109eaSMatthew Dillon const char *MD5CacheFile;
16175bd842aSMatthew Dillon const char *UseHLPath;
16275bd842aSMatthew Dillon 
16375bd842aSMatthew Dillon static int DstBaseLen;
16475bd842aSMatthew Dillon static int HardLinkCount;
16575bd842aSMatthew Dillon static int GroupCount;
16675bd842aSMatthew Dillon static gid_t *GroupList;
1673a42736dSMatthew Dillon 
168577109eaSMatthew Dillon int64_t CountSourceBytes;
169577109eaSMatthew Dillon int64_t CountSourceItems;
170577109eaSMatthew Dillon int64_t CountCopiedItems;
171d5fdcd00SMatthew Dillon int64_t CountSourceReadBytes;
172d5fdcd00SMatthew Dillon int64_t CountTargetReadBytes;
173577109eaSMatthew Dillon int64_t CountWriteBytes;
174577109eaSMatthew Dillon int64_t CountRemovedItems;
175d5fdcd00SMatthew Dillon int64_t CountLinkedItems;
1761c755102SChris Pressey 
17775bd842aSMatthew Dillon static struct HostConf SrcHost;
17875bd842aSMatthew Dillon static struct HostConf DstHost;
1794d858d58SMatthew Dillon 
1803a42736dSMatthew Dillon int
main(int ac,char ** av)1813a42736dSMatthew Dillon main(int ac, char **av)
1823a42736dSMatthew Dillon {
1833a42736dSMatthew Dillon     int i;
184293141b7SMatthew Dillon     int opt;
1853a42736dSMatthew Dillon     char *src = NULL;
1863a42736dSMatthew Dillon     char *dst = NULL;
1874d858d58SMatthew Dillon     char *ptr;
1883a42736dSMatthew Dillon     struct timeval start;
189a2dc574cSMatthew Dillon     struct copy_info info;
1903a42736dSMatthew Dillon 
191d8bce52dSMatthew Dillon     signal(SIGPIPE, SIG_IGN);
192d8bce52dSMatthew Dillon 
1933a42736dSMatthew Dillon     gettimeofday(&start, NULL);
194293141b7SMatthew Dillon     opterr = 0;
195*5ca0a96dSSascha Wildner     while ((opt = getopt(ac, av, ":CdF:fH:hIi:j:lM:mnoqRSs:uVvX:x")) != -1) {
196293141b7SMatthew Dillon 	switch (opt) {
19744dd1628SMatthew Dillon 	case 'C':
19844dd1628SMatthew Dillon 	    CompressOpt = 1;
19944dd1628SMatthew Dillon 	    break;
200293141b7SMatthew Dillon 	case 'd':
201293141b7SMatthew Dillon 	    DirShowOpt = 1;
2023a42736dSMatthew Dillon 	    break;
2038f0e7bc1SMatthew Dillon 	case 'F':
2048f0e7bc1SMatthew Dillon 	    if (ssh_argc >= 16)
2058f0e7bc1SMatthew Dillon 		fatal("too many -F options");
206293141b7SMatthew Dillon 	    ssh_argv[ssh_argc++] = optarg;
2078f0e7bc1SMatthew Dillon 	    break;
2083a42736dSMatthew Dillon 	case 'f':
209293141b7SMatthew Dillon 	    ForceOpt = 1;
2103a42736dSMatthew Dillon 	    break;
2117651dbc3SAaron LI 	case 'H':
2127651dbc3SAaron LI 	    UseHLPath = optarg;
2137651dbc3SAaron LI 	    break;
2147651dbc3SAaron LI 	case 'h':
2157651dbc3SAaron LI 	    fatal(NULL);
2167651dbc3SAaron LI 	    /* not reached */
2177651dbc3SAaron LI 	    break;
2187651dbc3SAaron LI 	case 'I':
2197651dbc3SAaron LI 	    SummaryOpt = 1;
2207651dbc3SAaron LI 	    break;
2213a42736dSMatthew Dillon 	case 'i':
222293141b7SMatthew Dillon 	    AskConfirmation = getbool(optarg);
2233a42736dSMatthew Dillon 	    break;
224ac482a7eSMatthew Dillon 	case 'j':
225293141b7SMatthew Dillon 	    DeviceOpt = getbool(optarg);
226a2dc574cSMatthew Dillon 	    break;
2277651dbc3SAaron LI 	case 'l':
2287651dbc3SAaron LI 	    setlinebuf(stdout);
2297651dbc3SAaron LI 	    setlinebuf(stderr);
230577109eaSMatthew Dillon 	    break;
2313a42736dSMatthew Dillon 	case 'M':
232293141b7SMatthew Dillon 	    UseMD5Opt = 1;
233293141b7SMatthew Dillon 	    MD5CacheFile = optarg;
2343a42736dSMatthew Dillon 	    break;
2353a42736dSMatthew Dillon 	case 'm':
236293141b7SMatthew Dillon 	    UseMD5Opt = 1;
2373a42736dSMatthew Dillon 	    MD5CacheFile = ".MD5.CHECKSUMS";
2383a42736dSMatthew Dillon 	    break;
2397651dbc3SAaron LI 	case 'n':
2407651dbc3SAaron LI 	    NotForRealOpt = 1;
2417651dbc3SAaron LI 	    break;
2427651dbc3SAaron LI 	case 'o':
2437651dbc3SAaron LI 	    NoRemoveOpt = 1;
2447651dbc3SAaron LI 	    break;
2457651dbc3SAaron LI 	case 'q':
2467651dbc3SAaron LI 	    QuietOpt = 1;
2477651dbc3SAaron LI 	    break;
2487651dbc3SAaron LI 	case 'R':
2497651dbc3SAaron LI 	    ReadOnlyOpt = 1;
2507651dbc3SAaron LI 	    break;
2517651dbc3SAaron LI 	case 'S':
2527651dbc3SAaron LI 	    SlaveOpt = 1;
2537651dbc3SAaron LI 	    break;
2547651dbc3SAaron LI 	case 's':
2557651dbc3SAaron LI 	    SafetyOpt = getbool(optarg);
2567651dbc3SAaron LI 	    break;
2574d2076d3SChris Pressey 	case 'u':
2584d2076d3SChris Pressey 	    setvbuf(stdout, NULL, _IOLBF, 0);
2594d2076d3SChris Pressey 	    break;
2607651dbc3SAaron LI 	case 'V':
2617651dbc3SAaron LI 	    ++ValidateOpt;
2627651dbc3SAaron LI 	    break;
2637651dbc3SAaron LI 	case 'v':
2647651dbc3SAaron LI 	    ++VerboseOpt;
2657651dbc3SAaron LI 	    break;
2667651dbc3SAaron LI 	case 'X':
2677651dbc3SAaron LI 	    UseCpFile = optarg;
2687651dbc3SAaron LI 	    break;
2697651dbc3SAaron LI 	case 'x':
2707651dbc3SAaron LI 	    UseCpFile = ".cpignore";
2717651dbc3SAaron LI 	    break;
272293141b7SMatthew Dillon 	case ':':
273293141b7SMatthew Dillon 	    fatal("missing argument for option: -%c\n", optopt);
274293141b7SMatthew Dillon 	    /* not reached */
275293141b7SMatthew Dillon 	    break;
276293141b7SMatthew Dillon 	case '?':
277293141b7SMatthew Dillon 	    fatal("illegal option: -%c\n", optopt);
278293141b7SMatthew Dillon 	    /* not reached */
279293141b7SMatthew Dillon 	    break;
2803a42736dSMatthew Dillon 	default:
281293141b7SMatthew Dillon 	    fatal(NULL);
2823a42736dSMatthew Dillon 	    /* not reached */
2833a42736dSMatthew Dillon 	    break;
2843a42736dSMatthew Dillon 	}
2853a42736dSMatthew Dillon     }
286293141b7SMatthew Dillon     ac -= optind;
287293141b7SMatthew Dillon     av += optind;
288293141b7SMatthew Dillon     if (ac > 0)
289293141b7SMatthew Dillon 	src = av[0];
290293141b7SMatthew Dillon     if (ac > 1)
291293141b7SMatthew Dillon 	dst = av[1];
292293141b7SMatthew Dillon     if (ac > 2)
293293141b7SMatthew Dillon 	fatal("too many arguments");
2943a42736dSMatthew Dillon 
2953a42736dSMatthew Dillon     /*
2964d858d58SMatthew Dillon      * If we are told to go into slave mode, run the HC protocol
2974d858d58SMatthew Dillon      */
2984d858d58SMatthew Dillon     if (SlaveOpt) {
299293141b7SMatthew Dillon 	DstRootPrivs = (geteuid() == 0);
3004d858d58SMatthew Dillon 	hc_slave(0, 1);
3014d858d58SMatthew Dillon 	exit(0);
3024d858d58SMatthew Dillon     }
3034d858d58SMatthew Dillon 
3044d858d58SMatthew Dillon     /*
3054d858d58SMatthew Dillon      * Extract the source and/or/neither target [user@]host and
3064d858d58SMatthew Dillon      * make any required connections.
3074d858d58SMatthew Dillon      */
308d72200edSMatthew Dillon     if (src && (ptr = SplitRemote(&src)) != NULL) {
309293141b7SMatthew Dillon 	SrcHost.host = src;
310293141b7SMatthew Dillon 	src = ptr;
311c0538630SMatthew Dillon 	if (UseMD5Opt)
312c0538630SMatthew Dillon 	    fatal("The MD5 options are not currently supported for remote sources");
313c0538630SMatthew Dillon 	if (hc_connect(&SrcHost, ReadOnlyOpt) < 0)
3144d858d58SMatthew Dillon 	    exit(1);
3152b7dbe20SMatthew Dillon     } else {
3162b7dbe20SMatthew Dillon 	SrcHost.version = HCPROTO_VERSION;
3172b7dbe20SMatthew Dillon 	if (ReadOnlyOpt)
318c0538630SMatthew Dillon 	    fatal("The -R option is only supported for remote sources");
3192b7dbe20SMatthew Dillon     }
320c0538630SMatthew Dillon 
321d72200edSMatthew Dillon     if (dst && (ptr = SplitRemote(&dst)) != NULL) {
322293141b7SMatthew Dillon 	DstHost.host = dst;
323293141b7SMatthew Dillon 	dst = ptr;
324c0538630SMatthew Dillon 	if (hc_connect(&DstHost, 0) < 0)
325e7624993SMatthew Dillon 	    exit(1);
3262b7dbe20SMatthew Dillon     } else {
3272b7dbe20SMatthew Dillon 	DstHost.version = HCPROTO_VERSION;
3284d858d58SMatthew Dillon     }
3294d858d58SMatthew Dillon 
3304d858d58SMatthew Dillon     /*
3313a42736dSMatthew Dillon      * dst may be NULL only if -m option is specified,
3323a42736dSMatthew Dillon      * which forces an update of the MD5 checksums
3333a42736dSMatthew Dillon      */
3343a42736dSMatthew Dillon     if (dst == NULL && UseMD5Opt == 0) {
3353a42736dSMatthew Dillon 	fatal(NULL);
3363a42736dSMatthew Dillon 	/* not reached */
3373a42736dSMatthew Dillon     }
338293141b7SMatthew Dillon 
339293141b7SMatthew Dillon     if (dst) {
340293141b7SMatthew Dillon 	DstRootPrivs = (hc_geteuid(&DstHost) == 0);
341293141b7SMatthew Dillon 	if (!DstRootPrivs)
342293141b7SMatthew Dillon 	    GroupCount = hc_getgroups(&DstHost, &GroupList);
343293141b7SMatthew Dillon     }
344293141b7SMatthew Dillon #if 0
345293141b7SMatthew Dillon     /* XXXX DEBUG */
346293141b7SMatthew Dillon     fprintf(stderr, "DstRootPrivs == %s\n", DstRootPrivs ? "true" : "false");
347293141b7SMatthew Dillon     fprintf(stderr, "GroupCount == %d\n", GroupCount);
348293141b7SMatthew Dillon     for (i = 0; i < GroupCount; i++)
349293141b7SMatthew Dillon 	fprintf(stderr, "Group[%d] == %d\n", i, GroupList[i]);
350a2dc574cSMatthew Dillon #endif
351293141b7SMatthew Dillon 
352293141b7SMatthew Dillon     bzero(&info, sizeof(info));
3533a42736dSMatthew Dillon     if (dst) {
3544e316ad5SMatthew Dillon 	DstBaseLen = strlen(dst);
355a2dc574cSMatthew Dillon 	info.spath = src;
356a2dc574cSMatthew Dillon 	info.dpath = dst;
357a2dc574cSMatthew Dillon 	info.sdevNo = (dev_t)-1;
358a2dc574cSMatthew Dillon 	info.ddevNo = (dev_t)-1;
359c0538630SMatthew Dillon 	i = DoCopy(&info, NULL, -1);
3603a42736dSMatthew Dillon     } else {
361a2dc574cSMatthew Dillon 	info.spath = src;
362a2dc574cSMatthew Dillon 	info.dpath = NULL;
363a2dc574cSMatthew Dillon 	info.sdevNo = (dev_t)-1;
364a2dc574cSMatthew Dillon 	info.ddevNo = (dev_t)-1;
365c0538630SMatthew Dillon 	i = DoCopy(&info, NULL, -1);
3663a42736dSMatthew Dillon     }
3674d858d58SMatthew Dillon #ifndef NOMD5
3683a42736dSMatthew Dillon     md5_flush();
3694d858d58SMatthew Dillon #endif
3703a42736dSMatthew Dillon 
3713a42736dSMatthew Dillon     if (SummaryOpt && i == 0) {
3724a159c4cSMatthew Dillon 	double duration;
3733a42736dSMatthew Dillon 	struct timeval end;
3743a42736dSMatthew Dillon 
3753a42736dSMatthew Dillon 	gettimeofday(&end, NULL);
376d5fdcd00SMatthew Dillon #if 0
377d5fdcd00SMatthew Dillon 	/* don't count stat's in our byte statistics */
3783a42736dSMatthew Dillon 	CountSourceBytes += sizeof(struct stat) * CountSourceItems;
379d5fdcd00SMatthew Dillon 	CountSourceReadBytes += sizeof(struct stat) * CountSourceItems;
3803a42736dSMatthew Dillon 	CountWriteBytes +=  sizeof(struct stat) * CountCopiedItems;
3813a42736dSMatthew Dillon 	CountWriteBytes +=  sizeof(struct stat) * CountRemovedItems;
382d5fdcd00SMatthew Dillon #endif
3833a42736dSMatthew Dillon 
3844a159c4cSMatthew Dillon 	duration = (end.tv_sec - start.tv_sec);
3854a159c4cSMatthew Dillon 	duration += (double)(end.tv_usec - start.tv_usec) / 1000000.0;
3864a159c4cSMatthew Dillon 	if (duration == 0.0)
3874a159c4cSMatthew Dillon 		duration = 1.0;
388d0d91865SMatthew Dillon 	logstd("cpdup completed successfully\n");
389d5fdcd00SMatthew Dillon 	logstd("%lld bytes source, %lld src bytes read, %lld tgt bytes read\n"
390d5fdcd00SMatthew Dillon 	       "%lld bytes written (%.1fX speedup)\n",
3913a42736dSMatthew Dillon 	    (long long)CountSourceBytes,
392d5fdcd00SMatthew Dillon 	    (long long)CountSourceReadBytes,
393d5fdcd00SMatthew Dillon 	    (long long)CountTargetReadBytes,
3943a42736dSMatthew Dillon 	    (long long)CountWriteBytes,
395d5fdcd00SMatthew Dillon 	    ((double)CountSourceBytes * 2.0) / ((double)(CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes)));
396d5fdcd00SMatthew Dillon  	logstd("%lld source items, %lld items copied, %lld items linked, "
397d5fdcd00SMatthew Dillon 	       "%lld things deleted\n",
3983a42736dSMatthew Dillon 	    (long long)CountSourceItems,
3993a42736dSMatthew Dillon 	    (long long)CountCopiedItems,
400d5fdcd00SMatthew Dillon 	    (long long)CountLinkedItems,
4013a42736dSMatthew Dillon 	    (long long)CountRemovedItems);
4023a42736dSMatthew Dillon 	logstd("%.1f seconds %5d Kbytes/sec synced %5d Kbytes/sec scanned\n",
4034a159c4cSMatthew Dillon 	    duration,
4044a159c4cSMatthew Dillon 	    (int)((CountSourceReadBytes + CountTargetReadBytes + CountWriteBytes) / duration  / 1024.0),
4054a159c4cSMatthew Dillon 	    (int)(CountSourceBytes / duration / 1024.0));
4063a42736dSMatthew Dillon     }
4073a42736dSMatthew Dillon     exit((i == 0) ? 0 : 1);
4083a42736dSMatthew Dillon }
4093a42736dSMatthew Dillon 
410293141b7SMatthew Dillon static int
getbool(const char * str)411293141b7SMatthew Dillon getbool(const char *str)
412293141b7SMatthew Dillon {
413293141b7SMatthew Dillon     if (strcmp(str, "0") == 0)
414293141b7SMatthew Dillon 	return (0);
415293141b7SMatthew Dillon     if (strcmp(str, "1") == 0)
416293141b7SMatthew Dillon 	return (1);
417293141b7SMatthew Dillon     fatal("option requires boolean argument (0 or 1): -%c\n", optopt);
418293141b7SMatthew Dillon     /* not reached */
419293141b7SMatthew Dillon     return (0);
420293141b7SMatthew Dillon }
421293141b7SMatthew Dillon 
422293141b7SMatthew Dillon /*
423293141b7SMatthew Dillon  * Check if path specifies a remote path, using the same syntax as scp(1),
424293141b7SMatthew Dillon  * i.e. a path is considered remote if the first colon is not preceded by
425293141b7SMatthew Dillon  * a slash, so e.g. "./foo:bar" is considered local.
426293141b7SMatthew Dillon  * If a remote path is detected, the colon is replaced with a null byte,
427293141b7SMatthew Dillon  * and the return value is a pointer to the next character.
428293141b7SMatthew Dillon  * Otherwise NULL is returned.
429d72200edSMatthew Dillon  *
430d72200edSMatthew Dillon  * A path prefix of localhost is the same as a locally specified file or
431d72200edSMatthew Dillon  * directory path, but prevents any further interpretation of the path
432d72200edSMatthew Dillon  * as being a remote hostname (for paths that have colons in them).
433293141b7SMatthew Dillon  */
434293141b7SMatthew Dillon static char *
SplitRemote(char ** pathp)435d72200edSMatthew Dillon SplitRemote(char **pathp)
436293141b7SMatthew Dillon {
437293141b7SMatthew Dillon     int cindex;
438d72200edSMatthew Dillon     char *path = *pathp;
439293141b7SMatthew Dillon 
440293141b7SMatthew Dillon     if (path[(cindex = strcspn(path, ":/"))] == ':') {
441293141b7SMatthew Dillon 	path[cindex++] = 0;
442d72200edSMatthew Dillon 	if (strcmp(path, "localhost") != 0)
443293141b7SMatthew Dillon 		return (path + cindex);
444d72200edSMatthew Dillon 	*pathp = path + cindex;
445293141b7SMatthew Dillon     }
446293141b7SMatthew Dillon     return (NULL);
447293141b7SMatthew Dillon }
448293141b7SMatthew Dillon 
449293141b7SMatthew Dillon /*
450293141b7SMatthew Dillon  * Check if group g is in our GroupList.
451293141b7SMatthew Dillon  *
452293141b7SMatthew Dillon  * Typically the number of groups a user belongs to isn't large
453293141b7SMatthew Dillon  * enough to warrant more effort than a simple linear search.
454293141b7SMatthew Dillon  * However, we perform an optimization by moving a group to the
455293141b7SMatthew Dillon  * top of the list when we have a hit.  This assumes that there
456293141b7SMatthew Dillon  * isn't much variance in the gids of files that a non-root user
457293141b7SMatthew Dillon  * copies.  So most of the time the search will terminate on the
458293141b7SMatthew Dillon  * first element of the list.
459293141b7SMatthew Dillon  */
460293141b7SMatthew Dillon static int
ChgrpAllowed(gid_t g)461293141b7SMatthew Dillon ChgrpAllowed(gid_t g)
462293141b7SMatthew Dillon {
463293141b7SMatthew Dillon     int i;
464293141b7SMatthew Dillon 
465293141b7SMatthew Dillon     for (i = 0; i < GroupCount; i++)
466293141b7SMatthew Dillon 	if (GroupList[i] == g) {
467293141b7SMatthew Dillon 	    if (i > 0) {
468293141b7SMatthew Dillon 		/* Optimize: Move g to the front of the list. */
469293141b7SMatthew Dillon 		for (; i > 0; i--)
470293141b7SMatthew Dillon 		    GroupList[i] = GroupList[i - 1];
471293141b7SMatthew Dillon 		GroupList[0] = g;
472293141b7SMatthew Dillon 	    }
473293141b7SMatthew Dillon 	    return (1);
474293141b7SMatthew Dillon 	}
475293141b7SMatthew Dillon     return (0);
476293141b7SMatthew Dillon }
477293141b7SMatthew Dillon 
478293141b7SMatthew Dillon /*
479293141b7SMatthew Dillon  * The following two functions return true if the ownership (UID + GID)
480293141b7SMatthew Dillon  * or the flags of two files match, respectively.
481293141b7SMatthew Dillon  *
482293141b7SMatthew Dillon  * Only perform weak checking if we don't have sufficient privileges on
483293141b7SMatthew Dillon  * the target machine, so we don't waste transfers with things that are
484293141b7SMatthew Dillon  * bound to fail anyway.
485293141b7SMatthew Dillon  */
486293141b7SMatthew Dillon static int
OwnerMatch(struct stat * st1,struct stat * st2)487293141b7SMatthew Dillon OwnerMatch(struct stat *st1, struct stat *st2)
488293141b7SMatthew Dillon {
489293141b7SMatthew Dillon     if (DstRootPrivs)
490293141b7SMatthew Dillon 	/* Both UID and GID must match. */
491293141b7SMatthew Dillon 	return (st1->st_uid == st2->st_uid && st1->st_gid == st2->st_gid);
492293141b7SMatthew Dillon     else
493293141b7SMatthew Dillon 	/* Ignore UID, and also ignore GID if we can't chgrp to that group. */
494293141b7SMatthew Dillon 	return (st1->st_gid == st2->st_gid || !ChgrpAllowed(st1->st_gid));
495293141b7SMatthew Dillon }
496293141b7SMatthew Dillon 
497293141b7SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
498293141b7SMatthew Dillon static int
FlagsMatch(struct stat * st1,struct stat * st2)499293141b7SMatthew Dillon FlagsMatch(struct stat *st1, struct stat *st2)
500293141b7SMatthew Dillon {
5019d626b29SSascha Wildner /*
5029d626b29SSascha Wildner  * Ignore UF_ARCHIVE.  It gets set automatically by the filesystem, for
5039d626b29SSascha Wildner  * filesystems that support it.  If the destination filesystem supports it, but
5049d626b29SSascha Wildner  * it's cleared on the source file, then multiple invocations of cpdup would
5059d626b29SSascha Wildner  * all try to copy the file because the flags wouldn't match.
5069d626b29SSascha Wildner  *
5079d626b29SSascha Wildner  * When unpriveleged, ignore flags we can't set
5089d626b29SSascha Wildner  */
5099d626b29SSascha Wildner     u_long ignored = DstRootPrivs ? 0 : SF_SETTABLE;
5109d626b29SSascha Wildner 
5119d626b29SSascha Wildner #ifdef UF_ARCHIVE
5129d626b29SSascha Wildner     ignored |= UF_ARCHIVE;
5139d626b29SSascha Wildner #endif
5149d626b29SSascha Wildner     return (((st1->st_flags ^ st2->st_flags) & ~ignored) == 0);
515293141b7SMatthew Dillon }
516293141b7SMatthew Dillon #endif
517293141b7SMatthew Dillon 
518293141b7SMatthew Dillon 
519fce2564bSJeroen Ruigrok/asmodai static struct hlink *
hltlookup(struct stat * stp)5203a42736dSMatthew Dillon hltlookup(struct stat *stp)
5213a42736dSMatthew Dillon {
5223a42736dSMatthew Dillon     struct hlink *hl;
5233a42736dSMatthew Dillon     int n;
5243a42736dSMatthew Dillon 
5254e316ad5SMatthew Dillon     n = stp->st_ino & HLMASK;
5263a42736dSMatthew Dillon 
52744dd1628SMatthew Dillon     for (hl = hltable[n]; hl; hl = hl->next) {
52844dd1628SMatthew Dillon         if (hl->ino == stp->st_ino) {
529975200d7SMatthew Dillon 	    ++hl->refs;
5303a42736dSMatthew Dillon 	    return hl;
53144dd1628SMatthew Dillon 	}
53244dd1628SMatthew Dillon     }
5333a42736dSMatthew Dillon 
5343a42736dSMatthew Dillon     return NULL;
5353a42736dSMatthew Dillon }
5363a42736dSMatthew Dillon 
537fce2564bSJeroen Ruigrok/asmodai static struct hlink *
hltadd(struct stat * stp,const char * path)5383a42736dSMatthew Dillon hltadd(struct stat *stp, const char *path)
5393a42736dSMatthew Dillon {
5403a42736dSMatthew Dillon     struct hlink *new;
54171de6efcSMatthew Dillon     int plen = strlen(path);
5423a42736dSMatthew Dillon     int n;
5433a42736dSMatthew Dillon 
54471de6efcSMatthew Dillon     new = malloc(offsetof(struct hlink, name[plen + 1]));
545c0538630SMatthew Dillon     if (new == NULL)
546c0538630SMatthew Dillon         fatal("out of memory");
547975200d7SMatthew Dillon     ++HardLinkCount;
5483a42736dSMatthew Dillon 
5493a42736dSMatthew Dillon     /* initialize and link the new element into the table */
5503a42736dSMatthew Dillon     new->ino = stp->st_ino;
55144dd1628SMatthew Dillon     new->dino = (ino_t)-1;
552975200d7SMatthew Dillon     new->refs = 1;
55371de6efcSMatthew Dillon     bcopy(path, new->name, plen + 1);
5543a42736dSMatthew Dillon     new->nlinked = 1;
5553a42736dSMatthew Dillon     new->prev = NULL;
5564e316ad5SMatthew Dillon     n = stp->st_ino & HLMASK;
5573a42736dSMatthew Dillon     new->next = hltable[n];
5583a42736dSMatthew Dillon     if (hltable[n])
5593a42736dSMatthew Dillon         hltable[n]->prev = new;
5603a42736dSMatthew Dillon     hltable[n] = new;
5613a42736dSMatthew Dillon 
5623a42736dSMatthew Dillon     return new;
5633a42736dSMatthew Dillon }
5643a42736dSMatthew Dillon 
565fce2564bSJeroen Ruigrok/asmodai static void
hltsetdino(struct hlink * hl,ino_t inum)56644dd1628SMatthew Dillon hltsetdino(struct hlink *hl, ino_t inum)
56744dd1628SMatthew Dillon {
56844dd1628SMatthew Dillon     hl->dino = inum;
56944dd1628SMatthew Dillon }
57044dd1628SMatthew Dillon 
57144dd1628SMatthew Dillon static void
hltdelete(struct hlink * hl)5723a42736dSMatthew Dillon hltdelete(struct hlink *hl)
5733a42736dSMatthew Dillon {
574975200d7SMatthew Dillon     assert(hl->refs == 1);
575975200d7SMatthew Dillon     --hl->refs;
5763a42736dSMatthew Dillon     if (hl->prev) {
5773a42736dSMatthew Dillon         if (hl->next)
5783a42736dSMatthew Dillon             hl->next->prev = hl->prev;
5793a42736dSMatthew Dillon         hl->prev->next = hl->next;
5803a42736dSMatthew Dillon     } else {
5813a42736dSMatthew Dillon         if (hl->next)
5823a42736dSMatthew Dillon             hl->next->prev = NULL;
5833a42736dSMatthew Dillon 
5844e316ad5SMatthew Dillon         hltable[hl->ino & HLMASK] = hl->next;
5853a42736dSMatthew Dillon     }
586975200d7SMatthew Dillon     --HardLinkCount;
5873a42736dSMatthew Dillon     free(hl);
5883a42736dSMatthew Dillon }
5893a42736dSMatthew Dillon 
590975200d7SMatthew Dillon static void
hltrels(struct hlink * hl)591975200d7SMatthew Dillon hltrels(struct hlink *hl)
592975200d7SMatthew Dillon {
593975200d7SMatthew Dillon     assert(hl->refs == 1);
594975200d7SMatthew Dillon     --hl->refs;
595975200d7SMatthew Dillon }
596975200d7SMatthew Dillon 
5974e316ad5SMatthew Dillon /*
5984e316ad5SMatthew Dillon  * If UseHLPath is defined check to see if the file in question is
5994e316ad5SMatthew Dillon  * the same as the source file, and if it is return a pointer to the
6004e316ad5SMatthew Dillon  * -H path based file for hardlinking.  Else return NULL.
6014e316ad5SMatthew Dillon  */
6024e316ad5SMatthew Dillon static char *
checkHLPath(struct stat * st1,const char * spath,const char * dpath)6034e316ad5SMatthew Dillon checkHLPath(struct stat *st1, const char *spath, const char *dpath)
6044e316ad5SMatthew Dillon {
6054e316ad5SMatthew Dillon     struct stat sthl;
6064e316ad5SMatthew Dillon     char *hpath;
607d5fdcd00SMatthew Dillon     int error;
6084e316ad5SMatthew Dillon 
60960374ee7SAaron LI     if (asprintf(&hpath, "%s%s", UseHLPath, dpath + DstBaseLen) < 0)
61060374ee7SAaron LI 	fatal("out of memory");
6114e316ad5SMatthew Dillon 
6124e316ad5SMatthew Dillon     /*
6134e316ad5SMatthew Dillon      * stat info matches ?
6144e316ad5SMatthew Dillon      */
6154d858d58SMatthew Dillon     if (hc_stat(&DstHost, hpath, &sthl) < 0 ||
6164e316ad5SMatthew Dillon 	st1->st_size != sthl.st_size ||
6170212bfceSMatthew Dillon 	mtimecmp(st1, &sthl) != 0 ||
618293141b7SMatthew Dillon 	!OwnerMatch(st1, &sthl) ||
619293141b7SMatthew Dillon 	!FlagsMatch(st1, &sthl)
6204e316ad5SMatthew Dillon     ) {
6214e316ad5SMatthew Dillon 	free(hpath);
6224e316ad5SMatthew Dillon 	return(NULL);
6234e316ad5SMatthew Dillon     }
6244e316ad5SMatthew Dillon 
6254e316ad5SMatthew Dillon     /*
626d5fdcd00SMatthew Dillon      * If ForceOpt or ValidateOpt is set we have to compare the files
6274e316ad5SMatthew Dillon      */
628d5fdcd00SMatthew Dillon     if (ForceOpt || ValidateOpt) {
629d5fdcd00SMatthew Dillon 	error = validate_check(spath, hpath);
630d5fdcd00SMatthew Dillon 	if (error) {
6314e316ad5SMatthew Dillon 	    free(hpath);
6324e316ad5SMatthew Dillon 	    hpath = NULL;
6334e316ad5SMatthew Dillon 	}
6344e316ad5SMatthew Dillon     }
6354e316ad5SMatthew Dillon     return(hpath);
6364e316ad5SMatthew Dillon }
6374e316ad5SMatthew Dillon 
638d5fdcd00SMatthew Dillon /*
639d5fdcd00SMatthew Dillon  * Return 0 if the contents of the file <spath> matches the contents of
640d5fdcd00SMatthew Dillon  * the file <dpath>.
641d5fdcd00SMatthew Dillon  */
642d5fdcd00SMatthew Dillon static int
validate_check(const char * spath,const char * dpath)643d5fdcd00SMatthew Dillon validate_check(const char *spath, const char *dpath)
644d5fdcd00SMatthew Dillon {
645d5fdcd00SMatthew Dillon     int error;
646d5fdcd00SMatthew Dillon     int fd1;
647d5fdcd00SMatthew Dillon     int fd2;
648d5fdcd00SMatthew Dillon 
649d5fdcd00SMatthew Dillon     fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0);
650d5fdcd00SMatthew Dillon     fd2 = hc_open(&DstHost, dpath, O_RDONLY, 0);
651d5fdcd00SMatthew Dillon     error = -1;
652d5fdcd00SMatthew Dillon 
653d5fdcd00SMatthew Dillon     if (fd1 >= 0 && fd2 >= 0) {
654d5fdcd00SMatthew Dillon 	int n;
655d5fdcd00SMatthew Dillon 	int x;
656975200d7SMatthew Dillon 	char *iobuf1 = malloc(GETIOSIZE);
657975200d7SMatthew Dillon 	char *iobuf2 = malloc(GETIOSIZE);
658d5fdcd00SMatthew Dillon 
659975200d7SMatthew Dillon 	while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
660d5fdcd00SMatthew Dillon 	    CountSourceReadBytes += n;
661975200d7SMatthew Dillon 	    x = hc_read(&DstHost, fd2, iobuf2, GETIOSIZE);
662d5fdcd00SMatthew Dillon 	    if (x > 0)
663d5fdcd00SMatthew Dillon 		    CountTargetReadBytes += x;
664d5fdcd00SMatthew Dillon 	    if (x != n)
665d5fdcd00SMatthew Dillon 		break;
666975200d7SMatthew Dillon 	    if (bcmp(iobuf1, iobuf2, n) != 0)
667d5fdcd00SMatthew Dillon 		break;
668d5fdcd00SMatthew Dillon 	}
669975200d7SMatthew Dillon 	free(iobuf1);
670975200d7SMatthew Dillon 	free(iobuf2);
671d5fdcd00SMatthew Dillon 	if (n == 0)
672d5fdcd00SMatthew Dillon 	    error = 0;
673d5fdcd00SMatthew Dillon     }
674d5fdcd00SMatthew Dillon     if (fd1 >= 0)
675d5fdcd00SMatthew Dillon 	hc_close(&SrcHost, fd1);
676d5fdcd00SMatthew Dillon     if (fd2 >= 0)
677d5fdcd00SMatthew Dillon 	hc_close(&DstHost, fd2);
678d5fdcd00SMatthew Dillon     return (error);
679d5fdcd00SMatthew Dillon }
680d5fdcd00SMatthew Dillon 
6813a42736dSMatthew Dillon int
DoCopy(copy_info_t info,struct stat * stat1,int depth)682c0538630SMatthew Dillon DoCopy(copy_info_t info, struct stat *stat1, int depth)
6833a42736dSMatthew Dillon {
684a2dc574cSMatthew Dillon     const char *spath = info->spath;
685a2dc574cSMatthew Dillon     const char *dpath = info->dpath;
686a2dc574cSMatthew Dillon     dev_t sdevNo = info->sdevNo;
687a2dc574cSMatthew Dillon     dev_t ddevNo = info->ddevNo;
6883a42736dSMatthew Dillon     struct stat st1;
6893a42736dSMatthew Dillon     struct stat st2;
69017e9c4ccSMatthew Dillon     unsigned long st2_flags;
691577109eaSMatthew Dillon     int r, mres, fres, st2Valid;
69258860d7dSMatthew Dillon     struct hlink *hln;
693c0538630SMatthew Dillon     uint64_t size;
6943a42736dSMatthew Dillon 
695577109eaSMatthew Dillon     r = mres = fres = st2Valid = 0;
69617e9c4ccSMatthew Dillon     st2_flags = 0;
69758860d7dSMatthew Dillon     size = 0;
69858860d7dSMatthew Dillon     hln = NULL;
69958860d7dSMatthew Dillon 
700c0538630SMatthew Dillon     if (stat1 == NULL) {
701a2dc574cSMatthew Dillon 	if (hc_lstat(&SrcHost, spath, &st1) != 0) {
702293141b7SMatthew Dillon 	    r = 1;
703a2dc574cSMatthew Dillon 	    goto done;
704a2dc574cSMatthew Dillon 	}
705c0538630SMatthew Dillon 	stat1 = &st1;
706c0538630SMatthew Dillon     }
7078f0e7bc1SMatthew Dillon #ifdef SF_SNAPSHOT
7088f0e7bc1SMatthew Dillon     /* skip snapshot files because they're sparse and _huge_ */
709c0538630SMatthew Dillon     if (stat1->st_flags & SF_SNAPSHOT)
7108f0e7bc1SMatthew Dillon        return(0);
7118f0e7bc1SMatthew Dillon #endif
7123a42736dSMatthew Dillon     st2.st_mode = 0;	/* in case lstat fails */
7133a42736dSMatthew Dillon     st2.st_flags = 0;	/* in case lstat fails */
71417e9c4ccSMatthew Dillon     if (dpath && hc_lstat(&DstHost, dpath, &st2) == 0) {
7153a42736dSMatthew Dillon 	st2Valid = 1;
71617e9c4ccSMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
71717e9c4ccSMatthew Dillon 	st2_flags = st2.st_flags;
71817e9c4ccSMatthew Dillon #endif
71917e9c4ccSMatthew Dillon     }
7203a42736dSMatthew Dillon 
721c0538630SMatthew Dillon     if (S_ISREG(stat1->st_mode))
722c0538630SMatthew Dillon 	size = stat1->st_size;
7233a42736dSMatthew Dillon 
7243a42736dSMatthew Dillon     /*
7253a42736dSMatthew Dillon      * Handle hardlinks
7263a42736dSMatthew Dillon      */
7273a42736dSMatthew Dillon 
728c0538630SMatthew Dillon     if (S_ISREG(stat1->st_mode) && stat1->st_nlink > 1 && dpath) {
729c0538630SMatthew Dillon         if ((hln = hltlookup(stat1)) != NULL) {
7303a42736dSMatthew Dillon             hln->nlinked++;
7313a42736dSMatthew Dillon 
7323a42736dSMatthew Dillon             if (st2Valid) {
7333a42736dSMatthew Dillon                 if (st2.st_ino == hln->dino) {
7343a42736dSMatthew Dillon 		    /*
7353a42736dSMatthew Dillon 		     * hard link is already correct, nothing to do
7363a42736dSMatthew Dillon 		     */
7373a42736dSMatthew Dillon 		    if (VerboseOpt >= 3)
7383a42736dSMatthew Dillon 			logstd("%-32s nochange\n", (dpath) ? dpath : spath);
739c0538630SMatthew Dillon                     if (hln->nlinked == stat1->st_nlink) {
7403a42736dSMatthew Dillon                         hltdelete(hln);
741975200d7SMatthew Dillon 			hln = NULL;
742975200d7SMatthew Dillon 		    }
7433a42736dSMatthew Dillon 		    CountSourceItems++;
744a2dc574cSMatthew Dillon 		    r = 0;
745a2dc574cSMatthew Dillon 		    goto done;
7463a42736dSMatthew Dillon                 } else {
7473a42736dSMatthew Dillon 		    /*
7483a42736dSMatthew Dillon 		     * hard link is not correct, attempt to unlink it
7493a42736dSMatthew Dillon 		     */
75077133d96SMatthew Dillon                     if (xremove(&DstHost, dpath) < 0) {
7513a42736dSMatthew Dillon 			logerr("%-32s hardlink: unable to unlink: %s\n",
7523a42736dSMatthew Dillon 			    ((dpath) ? dpath : spath), strerror(errno));
7533a42736dSMatthew Dillon                         hltdelete(hln);
754975200d7SMatthew Dillon 			hln = NULL;
755975200d7SMatthew Dillon 			++r;
756975200d7SMatthew Dillon 			goto done;
7573a42736dSMatthew Dillon 		    }
7583a42736dSMatthew Dillon                 }
7593a42736dSMatthew Dillon             }
7603a42736dSMatthew Dillon 
761c0538630SMatthew Dillon             if (xlink(hln->name, dpath, stat1->st_flags) < 0) {
7629b2d9484SMatthew Dillon 		int tryrelink = (errno == EMLINK);
7633a42736dSMatthew Dillon 		logerr("%-32s hardlink: unable to link to %s: %s\n",
7643a42736dSMatthew Dillon 		    (dpath ? dpath : spath), hln->name, strerror(errno)
7653a42736dSMatthew Dillon 		);
7663a42736dSMatthew Dillon                 hltdelete(hln);
7673a42736dSMatthew Dillon                 hln = NULL;
7689b2d9484SMatthew Dillon 		if (tryrelink) {
769b58f1e66SSascha Wildner 		    logerr("%-20s hardlink: will attempt to copy normally\n",
770b58f1e66SSascha Wildner 			(dpath ? dpath : spath));
7719b2d9484SMatthew Dillon 		    goto relink;
7729b2d9484SMatthew Dillon 		}
7733a42736dSMatthew Dillon 		++r;
7743a42736dSMatthew Dillon             } else {
775c0538630SMatthew Dillon                 if (hln->nlinked == stat1->st_nlink) {
7763a42736dSMatthew Dillon                     hltdelete(hln);
7773a42736dSMatthew Dillon 		    hln = NULL;
7783a42736dSMatthew Dillon 		}
7793a42736dSMatthew Dillon                 if (r == 0) {
7803a42736dSMatthew Dillon 		    if (VerboseOpt) {
7813a42736dSMatthew Dillon 			logstd("%-32s hardlink: %s\n",
7823a42736dSMatthew Dillon 			    (dpath ? dpath : spath),
7833a42736dSMatthew Dillon 			    (st2Valid ? "relinked" : "linked")
7843a42736dSMatthew Dillon 			);
7853a42736dSMatthew Dillon 		    }
7863a42736dSMatthew Dillon 		    CountSourceItems++;
7873a42736dSMatthew Dillon 		    CountCopiedItems++;
788a2dc574cSMatthew Dillon 		    r = 0;
789a2dc574cSMatthew Dillon 		    goto done;
7903a42736dSMatthew Dillon 		}
7913a42736dSMatthew Dillon             }
7923a42736dSMatthew Dillon         } else {
7933a42736dSMatthew Dillon 	    /*
7943a42736dSMatthew Dillon 	     * first instance of hardlink must be copied normally
7953a42736dSMatthew Dillon 	     */
7969b2d9484SMatthew Dillon relink:
797c0538630SMatthew Dillon             hln = hltadd(stat1, dpath);
7983a42736dSMatthew Dillon 	}
7993a42736dSMatthew Dillon     }
8003a42736dSMatthew Dillon 
8013a42736dSMatthew Dillon     /*
8023a42736dSMatthew Dillon      * Do we need to copy the file/dir/link/whatever?  Early termination
803*5ca0a96dSSascha Wildner      * if we do not.  Always traverse directories.  Always redo links.
8043a42736dSMatthew Dillon      *
8053a42736dSMatthew Dillon      * NOTE: st2Valid is true only if dpath != NULL *and* dpath stats good.
8063a42736dSMatthew Dillon      */
8073a42736dSMatthew Dillon 
8083a42736dSMatthew Dillon     if (
8094d858d58SMatthew Dillon 	st2Valid
810c0538630SMatthew Dillon 	&& stat1->st_mode == st2.st_mode
811c0538630SMatthew Dillon 	&& FlagsMatch(stat1, &st2)
8123a42736dSMatthew Dillon     ) {
813c0538630SMatthew Dillon 	if (S_ISLNK(stat1->st_mode) || S_ISDIR(stat1->st_mode)) {
814*5ca0a96dSSascha Wildner 	    ;
8153a42736dSMatthew Dillon 	} else {
8163a42736dSMatthew Dillon 	    if (ForceOpt == 0 &&
817c0538630SMatthew Dillon 		stat1->st_size == st2.st_size &&
8180212bfceSMatthew Dillon 		(ValidateOpt == 2 || mtimecmp(stat1, &st2) == 0) &&
819c0538630SMatthew Dillon 		OwnerMatch(stat1, &st2)
8204d858d58SMatthew Dillon #ifndef NOMD5
821c0538630SMatthew Dillon 		&& (UseMD5Opt == 0 || !S_ISREG(stat1->st_mode) ||
822d5fdcd00SMatthew Dillon 		    (mres = md5_check(spath, dpath)) == 0)
8234d858d58SMatthew Dillon #endif
824c0538630SMatthew Dillon 		&& (ValidateOpt == 0 || !S_ISREG(stat1->st_mode) ||
825d5fdcd00SMatthew Dillon 		    validate_check(spath, dpath) == 0)
8263a42736dSMatthew Dillon 	    ) {
82717e9c4ccSMatthew Dillon 		/*
828a9fa3eadSMatthew Dillon 		 * The files are identical, but if we are running as
82917e9c4ccSMatthew Dillon 		 * root we might need to adjust ownership/group/flags.
83017e9c4ccSMatthew Dillon 		 */
83117e9c4ccSMatthew Dillon 		int changedown = 0;
83217e9c4ccSMatthew Dillon 		int changedflags = 0;
833a9fa3eadSMatthew Dillon 
8343a42736dSMatthew Dillon                 if (hln)
83544dd1628SMatthew Dillon 		    hltsetdino(hln, st2.st_ino);
83617e9c4ccSMatthew Dillon 
837c0538630SMatthew Dillon 		if (!OwnerMatch(stat1, &st2)) {
838c0538630SMatthew Dillon 		    hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
83917e9c4ccSMatthew Dillon 		    changedown = 1;
84017e9c4ccSMatthew Dillon 		}
84117e9c4ccSMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
842c0538630SMatthew Dillon 		if (!FlagsMatch(stat1, &st2)) {
843c0538630SMatthew Dillon 		    hc_chflags(&DstHost, dpath, stat1->st_flags);
84417e9c4ccSMatthew Dillon 		    changedflags = 1;
84517e9c4ccSMatthew Dillon 		}
84617e9c4ccSMatthew Dillon #endif
8473a42736dSMatthew Dillon 		if (VerboseOpt >= 3) {
8484d858d58SMatthew Dillon #ifndef NOMD5
84917e9c4ccSMatthew Dillon 		    if (UseMD5Opt) {
85017e9c4ccSMatthew Dillon 			logstd("%-32s md5-nochange",
85117e9c4ccSMatthew Dillon 				(dpath ? dpath : spath));
85217e9c4ccSMatthew Dillon 		    } else
8534d858d58SMatthew Dillon #endif
854*5ca0a96dSSascha Wildner 		    if (ValidateOpt) {
85517e9c4ccSMatthew Dillon 			logstd("%-32s nochange (contents validated)",
85617e9c4ccSMatthew Dillon 				(dpath ? dpath : spath));
85717e9c4ccSMatthew Dillon 		    } else {
85817e9c4ccSMatthew Dillon 			logstd("%-32s nochange", (dpath ? dpath : spath));
85917e9c4ccSMatthew Dillon 		    }
86017e9c4ccSMatthew Dillon 		    if (changedown)
86117e9c4ccSMatthew Dillon 			logstd(" (uid/gid differ)");
86217e9c4ccSMatthew Dillon 		    if (changedflags)
86317e9c4ccSMatthew Dillon 			logstd(" (flags differ)");
86417e9c4ccSMatthew Dillon 		    logstd("\n");
8653a42736dSMatthew Dillon 		}
8663a42736dSMatthew Dillon 		CountSourceBytes += size;
8673a42736dSMatthew Dillon 		CountSourceItems++;
868a2dc574cSMatthew Dillon 		r = 0;
869a2dc574cSMatthew Dillon 		goto done;
8703a42736dSMatthew Dillon 	    }
8713a42736dSMatthew Dillon 	}
8723a42736dSMatthew Dillon     }
873c0538630SMatthew Dillon     if (st2Valid && !S_ISDIR(stat1->st_mode) && S_ISDIR(st2.st_mode)) {
8743a42736dSMatthew Dillon 	if (SafetyOpt) {
8753a42736dSMatthew Dillon 	    logerr("%-32s SAFETY - refusing to copy file over directory\n",
8763a42736dSMatthew Dillon 		(dpath ? dpath : spath)
8773a42736dSMatthew Dillon 	    );
8783a42736dSMatthew Dillon 	    ++r;		/* XXX */
879a2dc574cSMatthew Dillon 	    r = 0;
880a2dc574cSMatthew Dillon 	    goto done; 		/* continue with the cpdup anyway */
8813a42736dSMatthew Dillon 	}
8823a42736dSMatthew Dillon 	if (QuietOpt == 0 || AskConfirmation) {
8833a42736dSMatthew Dillon 	    logstd("%-32s WARNING: non-directory source will blow away\n"
8843a42736dSMatthew Dillon 		   "%-32s preexisting dest directory, continuing anyway!\n",
8853a42736dSMatthew Dillon 		   ((dpath) ? dpath : spath), "");
8863a42736dSMatthew Dillon 	}
8873a42736dSMatthew Dillon 	if (dpath)
888c0538630SMatthew Dillon 	    RemoveRecur(dpath, ddevNo, &st2);
8898f0e7bc1SMatthew Dillon 	st2Valid = 0;
8903a42736dSMatthew Dillon     }
8913a42736dSMatthew Dillon 
892577109eaSMatthew Dillon     /*
893577109eaSMatthew Dillon      * The various comparisons failed, copy it.
894577109eaSMatthew Dillon      */
895c0538630SMatthew Dillon     if (S_ISDIR(stat1->st_mode)) {
896293141b7SMatthew Dillon 	int skipdir = 0;
8973a42736dSMatthew Dillon 
8983a42736dSMatthew Dillon 	if (dpath) {
899293141b7SMatthew Dillon 	    if (!st2Valid || S_ISDIR(st2.st_mode) == 0) {
900293141b7SMatthew Dillon 		if (st2Valid)
90177133d96SMatthew Dillon 		    xremove(&DstHost, dpath);
902c0538630SMatthew Dillon 		if (hc_mkdir(&DstHost, dpath, stat1->st_mode | 0700) != 0) {
9033a42736dSMatthew Dillon 		    logerr("%s: mkdir failed: %s\n",
9043a42736dSMatthew Dillon 			(dpath ? dpath : spath), strerror(errno));
9053a42736dSMatthew Dillon 		    r = 1;
906293141b7SMatthew Dillon 		    skipdir = 1;
9073a42736dSMatthew Dillon 		}
90874fa57e3SMatthew Dillon 		if (hc_lstat(&DstHost, dpath, &st2) != 0) {
909698d0b11SMatthew Dillon 		    if (NotForRealOpt == 0)
91074fa57e3SMatthew Dillon 			    logerr("%s: lstat of newly made dir failed: %s\n",
91174fa57e3SMatthew Dillon 				   (dpath ? dpath : spath), strerror(errno));
912293141b7SMatthew Dillon 		    st2Valid = 0;
91374fa57e3SMatthew Dillon 		    r = 1;
914293141b7SMatthew Dillon 		    skipdir = 1;
91574fa57e3SMatthew Dillon 		}
916293141b7SMatthew Dillon 		else {
917293141b7SMatthew Dillon 		    st2Valid = 1;
918c0538630SMatthew Dillon 		    if (!OwnerMatch(stat1, &st2) &&
919c0538630SMatthew Dillon 			hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid) != 0
920293141b7SMatthew Dillon 		    ) {
92174fa57e3SMatthew Dillon 			logerr("%s: chown of newly made dir failed: %s\n",
92274fa57e3SMatthew Dillon 			    (dpath ? dpath : spath), strerror(errno));
92374fa57e3SMatthew Dillon 			r = 1;
924293141b7SMatthew Dillon 			/* Note that we should not set skipdir = 1 here. */
925293141b7SMatthew Dillon 		    }
92674fa57e3SMatthew Dillon 		}
927ae24b5e0SSascha Wildner 		if (VerboseOpt)
928ae24b5e0SSascha Wildner 		    logstd("%-32s mkdir-ok\n", (dpath ? dpath : spath));
9293a42736dSMatthew Dillon 		CountCopiedItems++;
9303a42736dSMatthew Dillon 	    } else {
9313a42736dSMatthew Dillon 		/*
9323a42736dSMatthew Dillon 		 * Directory must be scanable by root for cpdup to
9333a42736dSMatthew Dillon 		 * work.  We'll fix it later if the directory isn't
9343a42736dSMatthew Dillon 		 * supposed to be readable ( which is why we fixup
9353a42736dSMatthew Dillon 		 * st2.st_mode to match what we did ).
9363a42736dSMatthew Dillon 		 */
9373a42736dSMatthew Dillon 		if ((st2.st_mode & 0700) != 0700) {
9384d858d58SMatthew Dillon 		    hc_chmod(&DstHost, dpath, st2.st_mode | 0700);
9393a42736dSMatthew Dillon 		    st2.st_mode |= 0700;
9403a42736dSMatthew Dillon 		}
9413a42736dSMatthew Dillon 		if (VerboseOpt >= 2)
9423a42736dSMatthew Dillon 		    logstd("%s\n", dpath ? dpath : spath);
9433a42736dSMatthew Dillon 	    }
9443a42736dSMatthew Dillon 	}
9453a42736dSMatthew Dillon 
9468f0e7bc1SMatthew Dillon 	/*
9478f0e7bc1SMatthew Dillon 	 * When copying a directory, stop if the source crosses a mount
9488f0e7bc1SMatthew Dillon 	 * point.
9498f0e7bc1SMatthew Dillon 	 */
950c0538630SMatthew Dillon 	if (sdevNo != (dev_t)-1 && stat1->st_dev != sdevNo)
951293141b7SMatthew Dillon 	    skipdir = 1;
952293141b7SMatthew Dillon 	else
953c0538630SMatthew Dillon 	    sdevNo = stat1->st_dev;
9543a42736dSMatthew Dillon 
9558f0e7bc1SMatthew Dillon 	/*
9568f0e7bc1SMatthew Dillon 	 * When copying a directory, stop if the destination crosses
9578f0e7bc1SMatthew Dillon 	 * a mount point.
9588f0e7bc1SMatthew Dillon 	 *
9598f0e7bc1SMatthew Dillon 	 * The target directory will have been created and stat'd
9608f0e7bc1SMatthew Dillon 	 * for st2 if it did not previously exist.   st2Valid is left
9618f0e7bc1SMatthew Dillon 	 * as a flag.  If the stat failed st2 will still only have its
9628f0e7bc1SMatthew Dillon 	 * default initialization.
9638f0e7bc1SMatthew Dillon 	 *
9648f0e7bc1SMatthew Dillon 	 * So we simply assume here that the directory is within the
9658f0e7bc1SMatthew Dillon 	 * current target mount if we had to create it (aka st2Valid is 0)
9668f0e7bc1SMatthew Dillon 	 * and we leave ddevNo alone.
9678f0e7bc1SMatthew Dillon 	 */
9688f0e7bc1SMatthew Dillon 	if (st2Valid) {
969293141b7SMatthew Dillon 	    if (ddevNo != (dev_t)-1 && st2.st_dev != ddevNo)
970293141b7SMatthew Dillon 		skipdir = 1;
971293141b7SMatthew Dillon 	    else
9723a42736dSMatthew Dillon 		ddevNo = st2.st_dev;
9733a42736dSMatthew Dillon 	}
9743a42736dSMatthew Dillon 
975293141b7SMatthew Dillon 	if (!skipdir) {
976293141b7SMatthew Dillon 	    List *list = malloc(sizeof(List));
9778ac50aa3SMatthew Dillon 	    Node *node;
9783a42736dSMatthew Dillon 
979293141b7SMatthew Dillon 	    if (DirShowOpt)
980293141b7SMatthew Dillon 		logstd("Scanning %s ...\n", spath);
981293141b7SMatthew Dillon 	    InitList(list);
982293141b7SMatthew Dillon 	    if (ScanDir(list, &SrcHost, spath, &CountSourceReadBytes, 0) == 0) {
983c0538630SMatthew Dillon 		node = NULL;
984c0538630SMatthew Dillon 		while ((node = IterateList(list, node, 0)) != NULL) {
9853a42736dSMatthew Dillon 		    char *nspath;
9863a42736dSMatthew Dillon 		    char *ndpath = NULL;
9873a42736dSMatthew Dillon 
988c0538630SMatthew Dillon 		    nspath = mprintf("%s/%s", spath, node->no_Name);
9893a42736dSMatthew Dillon 		    if (dpath)
990c0538630SMatthew Dillon 			ndpath = mprintf("%s/%s", dpath, node->no_Name);
991a2dc574cSMatthew Dillon 
992a2dc574cSMatthew Dillon 		    info->spath = nspath;
993a2dc574cSMatthew Dillon 		    info->dpath = ndpath;
994a2dc574cSMatthew Dillon 		    info->sdevNo = sdevNo;
995a2dc574cSMatthew Dillon 		    info->ddevNo = ddevNo;
996975200d7SMatthew Dillon 		    if (depth < 0)
997c0538630SMatthew Dillon 			r += DoCopy(info, node->no_Stat, depth);
998975200d7SMatthew Dillon 		    else
999c0538630SMatthew Dillon 			r += DoCopy(info, node->no_Stat, depth + 1);
10003a42736dSMatthew Dillon 		    free(nspath);
10013a42736dSMatthew Dillon 		    if (ndpath)
10023a42736dSMatthew Dillon 			free(ndpath);
1003975200d7SMatthew Dillon 		    info->spath = NULL;
1004975200d7SMatthew Dillon 		    info->dpath = NULL;
10053a42736dSMatthew Dillon 		}
1006a2dc574cSMatthew Dillon 
10073a42736dSMatthew Dillon 		/*
10083a42736dSMatthew Dillon 		 * Remove files/directories from destination that do not appear
10093a42736dSMatthew Dillon 		 * in the source.
10103a42736dSMatthew Dillon 		 */
1011293141b7SMatthew Dillon 		if (dpath && ScanDir(list, &DstHost, dpath,
1012293141b7SMatthew Dillon 				     &CountTargetReadBytes, 3) == 0) {
10138ac50aa3SMatthew Dillon 		    node = NULL;
1014c0538630SMatthew Dillon 		    while ((node = IterateList(list, node, 3)) != NULL) {
10153a42736dSMatthew Dillon 			/*
10163a42736dSMatthew Dillon 			 * If object does not exist in source or .cpignore
10173a42736dSMatthew Dillon 			 * then recursively remove it.
10183a42736dSMatthew Dillon 			 */
10193a42736dSMatthew Dillon 			char *ndpath;
10203a42736dSMatthew Dillon 
1021c0538630SMatthew Dillon 			ndpath = mprintf("%s/%s", dpath, node->no_Name);
1022c0538630SMatthew Dillon 			RemoveRecur(ndpath, ddevNo, node->no_Stat);
10233a42736dSMatthew Dillon 			free(ndpath);
10243a42736dSMatthew Dillon 		    }
10253a42736dSMatthew Dillon 		}
1026293141b7SMatthew Dillon 	    }
1027293141b7SMatthew Dillon 	    ResetList(list);
1028293141b7SMatthew Dillon 	    free(list);
10293a42736dSMatthew Dillon 	}
10303a42736dSMatthew Dillon 
1031293141b7SMatthew Dillon 	if (dpath && st2Valid) {
10328c2e6bdcSMatthew Dillon 	    struct timeval tv[2];
10338c2e6bdcSMatthew Dillon 
1034c0538630SMatthew Dillon 	    if (ForceOpt || !OwnerMatch(stat1, &st2))
1035c0538630SMatthew Dillon 		hc_chown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
1036c0538630SMatthew Dillon 	    if (stat1->st_mode != st2.st_mode)
1037c0538630SMatthew Dillon 		hc_chmod(&DstHost, dpath, stat1->st_mode);
10384d858d58SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
1039c0538630SMatthew Dillon 	    if (!FlagsMatch(stat1, &st2))
1040c0538630SMatthew Dillon 		hc_chflags(&DstHost, dpath, stat1->st_flags);
10414d858d58SMatthew Dillon #endif
10420212bfceSMatthew Dillon 	    if (ForceOpt || mtimecmp(stat1, &st2) != 0) {
10438c2e6bdcSMatthew Dillon 		bzero(tv, sizeof(tv));
1044c0538630SMatthew Dillon 		tv[0].tv_sec = stat1->st_mtime;
1045c0538630SMatthew Dillon 		tv[1].tv_sec = stat1->st_mtime;
10461a05b9d1SAaron LI #if defined(st_mtime)  /* A macro, so very likely on modern POSIX */
10471a05b9d1SAaron LI 		tv[0].tv_usec = stat1->st_mtim.tv_nsec / 1000;
10481a05b9d1SAaron LI 		tv[1].tv_usec = stat1->st_mtim.tv_nsec / 1000;
10490212bfceSMatthew Dillon #endif
10508c2e6bdcSMatthew Dillon 		hc_utimes(&DstHost, dpath, tv);
10518c2e6bdcSMatthew Dillon 	    }
10523a42736dSMatthew Dillon 	}
10533a42736dSMatthew Dillon     } else if (dpath == NULL) {
10543a42736dSMatthew Dillon 	/*
10553a42736dSMatthew Dillon 	 * If dpath is NULL, we are just updating the MD5
10563a42736dSMatthew Dillon 	 */
10574d858d58SMatthew Dillon #ifndef NOMD5
1058c0538630SMatthew Dillon 	if (UseMD5Opt && S_ISREG(stat1->st_mode)) {
10593a42736dSMatthew Dillon 	    mres = md5_check(spath, NULL);
10603a42736dSMatthew Dillon 
10613a42736dSMatthew Dillon 	    if (VerboseOpt > 1) {
10623a42736dSMatthew Dillon 		if (mres < 0)
10633a42736dSMatthew Dillon 		    logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
10643a42736dSMatthew Dillon 		else
10653a42736dSMatthew Dillon 		    logstd("%-32s md5-ok\n", (dpath) ? dpath : spath);
10663a42736dSMatthew Dillon 	    } else if (!QuietOpt && mres < 0) {
10673a42736dSMatthew Dillon 		logstd("%-32s md5-update\n", (dpath) ? dpath : spath);
10683a42736dSMatthew Dillon 	    }
10693a42736dSMatthew Dillon 	}
10704d858d58SMatthew Dillon #endif
1071c0538630SMatthew Dillon     } else if (S_ISREG(stat1->st_mode)) {
10723a42736dSMatthew Dillon 	char *path;
10734e316ad5SMatthew Dillon 	char *hpath;
10743a42736dSMatthew Dillon 	int fd1;
10753a42736dSMatthew Dillon 	int fd2;
10763a42736dSMatthew Dillon 
10770e9fad5eSMatthew Dillon 	if (st2Valid)
1078975200d7SMatthew Dillon 		path = mprintf("%s.tmp%d", dpath, (int)getpid());
10790e9fad5eSMatthew Dillon 	else
10800e9fad5eSMatthew Dillon 		path = mprintf("%s", dpath);
10813a42736dSMatthew Dillon 
10823a42736dSMatthew Dillon 	/*
10833a42736dSMatthew Dillon 	 * Handle check failure message.
10843a42736dSMatthew Dillon 	 */
10854d858d58SMatthew Dillon #ifndef NOMD5
10863a42736dSMatthew Dillon 	if (mres < 0)
10873a42736dSMatthew Dillon 	    logerr("%-32s md5-CHECK-FAILED\n", (dpath) ? dpath : spath);
10884d858d58SMatthew Dillon #endif
10893a42736dSMatthew Dillon 
10904e316ad5SMatthew Dillon 	/*
10914e316ad5SMatthew Dillon 	 * Not quite ready to do the copy yet.  If UseHLPath is defined,
10924e316ad5SMatthew Dillon 	 * see if we can hardlink instead.
10936db8f15dSMatthew Dillon 	 *
10946db8f15dSMatthew Dillon 	 * If we can hardlink, and the target exists, we have to remove it
10956db8f15dSMatthew Dillon 	 * first or the hardlink will fail.  This can occur in a number of
1096293141b7SMatthew Dillon 	 * situations but most typically when the '-f -H' combination is
10976db8f15dSMatthew Dillon 	 * used.
10984e316ad5SMatthew Dillon 	 */
1099c0538630SMatthew Dillon 	if (UseHLPath && (hpath = checkHLPath(stat1, spath, dpath)) != NULL) {
11006db8f15dSMatthew Dillon 		if (st2Valid)
110177133d96SMatthew Dillon 			xremove(&DstHost, dpath);
11024d858d58SMatthew Dillon 		if (hc_link(&DstHost, hpath, dpath) == 0) {
1103d5fdcd00SMatthew Dillon 			++CountLinkedItems;
11044e316ad5SMatthew Dillon 			if (VerboseOpt) {
11054e316ad5SMatthew Dillon 			    logstd("%-32s hardlinked(-H)\n",
11064e316ad5SMatthew Dillon 				   (dpath ? dpath : spath));
11074e316ad5SMatthew Dillon 			}
11084e316ad5SMatthew Dillon 			free(hpath);
11094e316ad5SMatthew Dillon 			goto skip_copy;
11104e316ad5SMatthew Dillon 		}
11114e316ad5SMatthew Dillon 		/*
11124e316ad5SMatthew Dillon 		 * Shucks, we may have hit a filesystem hard linking limit,
11134e316ad5SMatthew Dillon 		 * we have to copy instead.
11144e316ad5SMatthew Dillon 		 */
11154e316ad5SMatthew Dillon 		free(hpath);
11164e316ad5SMatthew Dillon 	}
11174e316ad5SMatthew Dillon 
11184d858d58SMatthew Dillon 	if ((fd1 = hc_open(&SrcHost, spath, O_RDONLY, 0)) >= 0) {
11194d858d58SMatthew Dillon 	    if ((fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) {
11203a42736dSMatthew Dillon 		/*
11213a42736dSMatthew Dillon 		 * There could be a .tmp file from a previously interrupted
11223a42736dSMatthew Dillon 		 * run, delete and retry.  Fail if we still can't get at it.
11233a42736dSMatthew Dillon 		 */
11244d858d58SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
11254d858d58SMatthew Dillon 		hc_chflags(&DstHost, path, 0);
11264d858d58SMatthew Dillon #endif
11274d858d58SMatthew Dillon 		hc_remove(&DstHost, path);
11284d858d58SMatthew Dillon 		fd2 = hc_open(&DstHost, path, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
11293a42736dSMatthew Dillon 	    }
11303a42736dSMatthew Dillon 	    if (fd2 >= 0) {
11311c755102SChris Pressey 		const char *op;
1132975200d7SMatthew Dillon 		char *iobuf1 = malloc(GETIOSIZE);
11333a42736dSMatthew Dillon 		int n;
11343a42736dSMatthew Dillon 
11353a42736dSMatthew Dillon 		/*
11363a42736dSMatthew Dillon 		 * Matt: What about holes?
11373a42736dSMatthew Dillon 		 */
11383a42736dSMatthew Dillon 		op = "read";
1139975200d7SMatthew Dillon 		while ((n = hc_read(&SrcHost, fd1, iobuf1, GETIOSIZE)) > 0) {
11403a42736dSMatthew Dillon 		    op = "write";
1141975200d7SMatthew Dillon 		    if (hc_write(&DstHost, fd2, iobuf1, n) != n)
11423a42736dSMatthew Dillon 			break;
11433a42736dSMatthew Dillon 		    op = "read";
11443a42736dSMatthew Dillon 		}
11454d858d58SMatthew Dillon 		hc_close(&DstHost, fd2);
11463a42736dSMatthew Dillon 		if (n == 0) {
11473a42736dSMatthew Dillon 		    struct timeval tv[2];
11483a42736dSMatthew Dillon 
11493a42736dSMatthew Dillon 		    bzero(tv, sizeof(tv));
1150c0538630SMatthew Dillon 		    tv[0].tv_sec = stat1->st_mtime;
1151c0538630SMatthew Dillon 		    tv[1].tv_sec = stat1->st_mtime;
11521a05b9d1SAaron LI #if defined(st_mtime)
11531a05b9d1SAaron LI 		    tv[0].tv_usec = stat1->st_mtim.tv_nsec / 1000;
11541a05b9d1SAaron LI 		    tv[1].tv_usec = stat1->st_mtim.tv_nsec / 1000;
11550212bfceSMatthew Dillon #endif
11563a42736dSMatthew Dillon 
1157c0538630SMatthew Dillon 		    if (DstRootPrivs || ChgrpAllowed(stat1->st_gid))
1158c0538630SMatthew Dillon 			hc_chown(&DstHost, path, stat1->st_uid, stat1->st_gid);
1159c0538630SMatthew Dillon 		    hc_chmod(&DstHost, path, stat1->st_mode);
116017e9c4ccSMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
1161c0538630SMatthew Dillon 		    if (stat1->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
116217e9c4ccSMatthew Dillon 			hc_utimes(&DstHost, path, tv);
116317e9c4ccSMatthew Dillon #else
116417e9c4ccSMatthew Dillon 		    hc_utimes(&DstHost, path, tv);
116517e9c4ccSMatthew Dillon #endif
11660e9fad5eSMatthew Dillon 		    if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
11673a42736dSMatthew Dillon 			logerr("%-32s rename-after-copy failed: %s\n",
11683a42736dSMatthew Dillon 			    (dpath ? dpath : spath), strerror(errno)
11693a42736dSMatthew Dillon 			);
11702b7dbe20SMatthew Dillon 			xremove(&DstHost, path);
11713a42736dSMatthew Dillon 			++r;
11723a42736dSMatthew Dillon 		    } else {
11733a42736dSMatthew Dillon 			if (VerboseOpt)
11743a42736dSMatthew Dillon 			    logstd("%-32s copy-ok\n", (dpath ? dpath : spath));
11754d858d58SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
1176c0538630SMatthew Dillon 			if (DstRootPrivs ? stat1->st_flags : stat1->st_flags & UF_SETTABLE)
1177c0538630SMatthew Dillon 			    hc_chflags(&DstHost, dpath, stat1->st_flags);
11784d858d58SMatthew Dillon #endif
11793a42736dSMatthew Dillon 		    }
118017e9c4ccSMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
1181c0538630SMatthew Dillon 		    if ((stat1->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE)) == 0)
118217e9c4ccSMatthew Dillon 			hc_utimes(&DstHost, dpath, tv);
118317e9c4ccSMatthew Dillon #endif
1184d5fdcd00SMatthew Dillon 		    CountSourceReadBytes += size;
11853a42736dSMatthew Dillon 		    CountWriteBytes += size;
11863a42736dSMatthew Dillon 		    CountSourceBytes += size;
11873a42736dSMatthew Dillon 		    CountSourceItems++;
11883a42736dSMatthew Dillon 		    CountCopiedItems++;
11893a42736dSMatthew Dillon 		} else {
11903a42736dSMatthew Dillon 		    logerr("%-32s %s failed: %s\n",
11913a42736dSMatthew Dillon 			(dpath ? dpath : spath), op, strerror(errno)
11923a42736dSMatthew Dillon 		    );
11934d858d58SMatthew Dillon 		    hc_remove(&DstHost, path);
11943a42736dSMatthew Dillon 		    ++r;
11953a42736dSMatthew Dillon 		}
1196975200d7SMatthew Dillon 		free(iobuf1);
11973a42736dSMatthew Dillon 	    } else {
11983a42736dSMatthew Dillon 		logerr("%-32s create (uid %d, euid %d) failed: %s\n",
11993a42736dSMatthew Dillon 		    (dpath ? dpath : spath), getuid(), geteuid(),
12003a42736dSMatthew Dillon 		    strerror(errno)
12013a42736dSMatthew Dillon 		);
12023a42736dSMatthew Dillon 		++r;
12033a42736dSMatthew Dillon 	    }
12044d858d58SMatthew Dillon 	    hc_close(&SrcHost, fd1);
12053a42736dSMatthew Dillon 	} else {
12063a42736dSMatthew Dillon 	    logerr("%-32s copy: open failed: %s\n",
12073a42736dSMatthew Dillon 		(dpath ? dpath : spath),
12083a42736dSMatthew Dillon 		strerror(errno)
12093a42736dSMatthew Dillon 	    );
12103a42736dSMatthew Dillon 	    ++r;
12113a42736dSMatthew Dillon 	}
12124e316ad5SMatthew Dillon skip_copy:
12133a42736dSMatthew Dillon 	free(path);
12143a42736dSMatthew Dillon 
12153a42736dSMatthew Dillon         if (hln) {
121644dd1628SMatthew Dillon             if (!r && hc_stat(&DstHost, dpath, &st2) == 0) {
121744dd1628SMatthew Dillon 		hltsetdino(hln, st2.st_ino);
121844dd1628SMatthew Dillon 	    } else {
12193a42736dSMatthew Dillon                 hltdelete(hln);
122044dd1628SMatthew Dillon 		hln = NULL;
122144dd1628SMatthew Dillon 	    }
12223a42736dSMatthew Dillon         }
1223c0538630SMatthew Dillon     } else if (S_ISLNK(stat1->st_mode)) {
1224975200d7SMatthew Dillon 	char *link1 = malloc(GETLINKSIZE);
1225975200d7SMatthew Dillon 	char *link2 = malloc(GETLINKSIZE);
12260e9fad5eSMatthew Dillon 	char *path;
12273a42736dSMatthew Dillon 	int n1;
12283a42736dSMatthew Dillon 	int n2;
12293a42736dSMatthew Dillon 
1230975200d7SMatthew Dillon 	n1 = hc_readlink(&SrcHost, spath, link1, GETLINKSIZE - 1);
12310e9fad5eSMatthew Dillon 	if (st2Valid) {
12320e9fad5eSMatthew Dillon 		path = mprintf("%s.tmp%d", dpath, (int)getpid());
1233975200d7SMatthew Dillon 		n2 = hc_readlink(&DstHost, dpath, link2, GETLINKSIZE - 1);
12340e9fad5eSMatthew Dillon 	} else {
12350e9fad5eSMatthew Dillon 		path = mprintf("%s", dpath);
12360e9fad5eSMatthew Dillon 		n2 = -1;
12370e9fad5eSMatthew Dillon 	}
12383a42736dSMatthew Dillon 	if (n1 >= 0) {
12392b7dbe20SMatthew Dillon 	    if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0 ||
12402b7dbe20SMatthew Dillon 		(st2Valid && symlink_mfo_test(&DstHost, stat1, &st2))
12412b7dbe20SMatthew Dillon 	    ) {
12422b7dbe20SMatthew Dillon 		struct timeval tv[2];
12432b7dbe20SMatthew Dillon 
12442b7dbe20SMatthew Dillon 		bzero(tv, sizeof(tv));
12452b7dbe20SMatthew Dillon 		tv[0].tv_sec = stat1->st_mtime;
12462b7dbe20SMatthew Dillon 		tv[1].tv_sec = stat1->st_mtime;
12472b7dbe20SMatthew Dillon #if defined(st_mtime)
12482b7dbe20SMatthew Dillon 		tv[0].tv_usec = stat1->st_mtim.tv_nsec / 1000;
12492b7dbe20SMatthew Dillon 		tv[1].tv_usec = stat1->st_mtim.tv_nsec / 1000;
12502b7dbe20SMatthew Dillon #endif
12512b7dbe20SMatthew Dillon 
1252c0538630SMatthew Dillon 		hc_umask(&DstHost, ~stat1->st_mode);
125377133d96SMatthew Dillon 		xremove(&DstHost, path);
12543a42736dSMatthew Dillon 		link1[n1] = 0;
12554d858d58SMatthew Dillon 		if (hc_symlink(&DstHost, link1, path) < 0) {
12563a42736dSMatthew Dillon                       logerr("%-32s symlink (%s->%s) failed: %s\n",
12573a42736dSMatthew Dillon 			  (dpath ? dpath : spath), link1, path,
12583a42736dSMatthew Dillon 			  strerror(errno)
12593a42736dSMatthew Dillon 		      );
12603a42736dSMatthew Dillon 		      ++r;
12613a42736dSMatthew Dillon 		} else {
1262c0538630SMatthew Dillon 		    if (DstRootPrivs || ChgrpAllowed(stat1->st_gid))
1263c0538630SMatthew Dillon 			hc_lchown(&DstHost, path, stat1->st_uid, stat1->st_gid);
12642b7dbe20SMatthew Dillon 
12653a42736dSMatthew Dillon 		    /*
12662b7dbe20SMatthew Dillon 		     * lutimes, lchmod if supported by destination.
12672b7dbe20SMatthew Dillon 		     */
12682b7dbe20SMatthew Dillon 		    if (DstHost.version >= HCPROTO_VERSION_LUCC) {
12692b7dbe20SMatthew Dillon 			hc_lchmod(&DstHost, path, stat1->st_mode);
12702b7dbe20SMatthew Dillon 			hc_lutimes(&DstHost, path, tv);
12712b7dbe20SMatthew Dillon 		    }
12722b7dbe20SMatthew Dillon 
12732b7dbe20SMatthew Dillon 		    /*
12742b7dbe20SMatthew Dillon 		     * rename (and set flags if supported by destination)
12753a42736dSMatthew Dillon 		     */
12760e9fad5eSMatthew Dillon 		    if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
12773a42736dSMatthew Dillon 			logerr("%-32s rename softlink (%s->%s) failed: %s\n",
12783a42736dSMatthew Dillon 			    (dpath ? dpath : spath),
12793a42736dSMatthew Dillon 			    path, dpath, strerror(errno));
12802b7dbe20SMatthew Dillon 			xremove(&DstHost, path);
12812b7dbe20SMatthew Dillon 		    } else {
12822b7dbe20SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
12832b7dbe20SMatthew Dillon 			if (DstHost.version >= HCPROTO_VERSION_LUCC)
12842b7dbe20SMatthew Dillon 			    hc_lchflags(&DstHost, dpath, stat1->st_flags);
12852b7dbe20SMatthew Dillon #endif
12862b7dbe20SMatthew Dillon 			if (VerboseOpt) {
12872b7dbe20SMatthew Dillon 			    logstd("%-32s softlink-ok\n",
12882b7dbe20SMatthew Dillon 				   (dpath ? dpath : spath));
12892b7dbe20SMatthew Dillon 			}
12903a42736dSMatthew Dillon 		    }
12914d858d58SMatthew Dillon 		    hc_umask(&DstHost, 000);
12923a42736dSMatthew Dillon 		    CountWriteBytes += n1;
12933a42736dSMatthew Dillon 		    CountCopiedItems++;
12943a42736dSMatthew Dillon 		}
12953a42736dSMatthew Dillon 	    } else {
12963a42736dSMatthew Dillon 		if (VerboseOpt >= 3)
1297293141b7SMatthew Dillon 		    logstd("%-32s nochange", (dpath ? dpath : spath));
1298c0538630SMatthew Dillon 		if (!OwnerMatch(stat1, &st2)) {
1299c0538630SMatthew Dillon 		    hc_lchown(&DstHost, dpath, stat1->st_uid, stat1->st_gid);
1300293141b7SMatthew Dillon 		    if (VerboseOpt >= 3)
1301293141b7SMatthew Dillon 			logstd(" (uid/gid differ)");
1302293141b7SMatthew Dillon 		}
1303293141b7SMatthew Dillon 		if (VerboseOpt >= 3)
1304293141b7SMatthew Dillon 		    logstd("\n");
13053a42736dSMatthew Dillon 	    }
13063a42736dSMatthew Dillon 	    CountSourceBytes += n1;
1307d5fdcd00SMatthew Dillon 	    CountSourceReadBytes += n1;
1308d5fdcd00SMatthew Dillon 	    if (n2 > 0)
1309d5fdcd00SMatthew Dillon 		CountTargetReadBytes += n2;
13103a42736dSMatthew Dillon 	    CountSourceItems++;
13113a42736dSMatthew Dillon 	} else {
13123a42736dSMatthew Dillon 	    r = 1;
13133a42736dSMatthew Dillon 	    logerr("%-32s softlink-failed\n", (dpath ? dpath : spath));
13143a42736dSMatthew Dillon 	}
1315975200d7SMatthew Dillon 	free(link1);
1316975200d7SMatthew Dillon 	free(link2);
1317975200d7SMatthew Dillon 	free(path);
1318c0538630SMatthew Dillon     } else if ((S_ISCHR(stat1->st_mode) || S_ISBLK(stat1->st_mode)) && DeviceOpt) {
13190e9fad5eSMatthew Dillon 	char *path = NULL;
13203a42736dSMatthew Dillon 
13213a42736dSMatthew Dillon 	if (ForceOpt ||
13223a42736dSMatthew Dillon 	    st2Valid == 0 ||
1323c0538630SMatthew Dillon 	    stat1->st_mode != st2.st_mode ||
1324c0538630SMatthew Dillon 	    stat1->st_rdev != st2.st_rdev ||
1325c0538630SMatthew Dillon 	    !OwnerMatch(stat1, &st2)
13263a42736dSMatthew Dillon 	) {
13270e9fad5eSMatthew Dillon 	    if (st2Valid) {
13280e9fad5eSMatthew Dillon 		path = mprintf("%s.tmp%d", dpath, (int)getpid());
132977133d96SMatthew Dillon 		xremove(&DstHost, path);
13300e9fad5eSMatthew Dillon 	    } else {
13310e9fad5eSMatthew Dillon 		path = mprintf("%s", dpath);
13320e9fad5eSMatthew Dillon 	    }
13330e9fad5eSMatthew Dillon 
1334c0538630SMatthew Dillon 	    if (hc_mknod(&DstHost, path, stat1->st_mode, stat1->st_rdev) == 0) {
1335c0538630SMatthew Dillon 		hc_chmod(&DstHost, path, stat1->st_mode);
1336c0538630SMatthew Dillon 		hc_chown(&DstHost, path, stat1->st_uid, stat1->st_gid);
13370e9fad5eSMatthew Dillon 		if (st2Valid)
133877133d96SMatthew Dillon 			xremove(&DstHost, dpath);
13390e9fad5eSMatthew Dillon 		if (st2Valid && xrename(path, dpath, st2_flags) != 0) {
13403a42736dSMatthew Dillon 		    logerr("%-32s dev-rename-after-create failed: %s\n",
13413a42736dSMatthew Dillon 			(dpath ? dpath : spath),
13423a42736dSMatthew Dillon 			strerror(errno)
13433a42736dSMatthew Dillon 		    );
13443a42736dSMatthew Dillon 		} else if (VerboseOpt) {
13453a42736dSMatthew Dillon 		    logstd("%-32s dev-ok\n", (dpath ? dpath : spath));
13463a42736dSMatthew Dillon 		}
13473a42736dSMatthew Dillon 		CountCopiedItems++;
13483a42736dSMatthew Dillon 	    } else {
13493a42736dSMatthew Dillon 		r = 1;
13503a42736dSMatthew Dillon 		logerr("%-32s dev failed: %s\n",
13513a42736dSMatthew Dillon 		    (dpath ? dpath : spath), strerror(errno)
13523a42736dSMatthew Dillon 		);
13533a42736dSMatthew Dillon 	    }
13543a42736dSMatthew Dillon 	} else {
13553a42736dSMatthew Dillon 	    if (VerboseOpt >= 3)
13563a42736dSMatthew Dillon 		logstd("%-32s nochange\n", (dpath ? dpath : spath));
13573a42736dSMatthew Dillon 	}
13580e9fad5eSMatthew Dillon 	if (path)
1359975200d7SMatthew Dillon 		free(path);
13603a42736dSMatthew Dillon 	CountSourceItems++;
13613a42736dSMatthew Dillon     }
1362a2dc574cSMatthew Dillon done:
1363975200d7SMatthew Dillon     if (hln) {
1364975200d7SMatthew Dillon 	if (hln->dino == (ino_t)-1) {
136544dd1628SMatthew Dillon 	    hltdelete(hln);
1366975200d7SMatthew Dillon 	    /*hln = NULL; unneeded */
1367975200d7SMatthew Dillon 	} else {
1368975200d7SMatthew Dillon 	    hltrels(hln);
1369975200d7SMatthew Dillon 	}
1370975200d7SMatthew Dillon     }
13713a42736dSMatthew Dillon     return (r);
13723a42736dSMatthew Dillon }
13733a42736dSMatthew Dillon 
1374293141b7SMatthew Dillon int
ScanDir(List * list,struct HostConf * host,const char * path,int64_t * CountReadBytes,int n)1375293141b7SMatthew Dillon ScanDir(List *list, struct HostConf *host, const char *path,
1376293141b7SMatthew Dillon 	int64_t *CountReadBytes, int n)
1377293141b7SMatthew Dillon {
1378293141b7SMatthew Dillon     DIR *dir;
137916802529SMatthew Dillon     struct HostConf *cphost;
1380c0538630SMatthew Dillon     struct HCDirEntry *den;
1381c0538630SMatthew Dillon     struct stat *statptr;
1382293141b7SMatthew Dillon 
1383293141b7SMatthew Dillon     if (n == 0) {
1384293141b7SMatthew Dillon 	/*
1385293141b7SMatthew Dillon 	 * scan .cpignore file for files/directories to ignore
1386293141b7SMatthew Dillon 	 * (only in the source directory, i.e. if n == 0).
1387293141b7SMatthew Dillon 	 */
1388293141b7SMatthew Dillon 	if (UseCpFile) {
1389293141b7SMatthew Dillon 	    int fd;
1390293141b7SMatthew Dillon 	    int nread;
1391293141b7SMatthew Dillon 	    int bufused;
1392293141b7SMatthew Dillon 	    char *buf = malloc(GETBUFSIZE);
1393293141b7SMatthew Dillon 	    char *nl, *next;
1394293141b7SMatthew Dillon 	    char *fpath;
1395293141b7SMatthew Dillon 
1396293141b7SMatthew Dillon 	    if (UseCpFile[0] == '/') {
1397293141b7SMatthew Dillon 		fpath = mprintf("%s", UseCpFile);
139816802529SMatthew Dillon 		cphost = NULL;
1399293141b7SMatthew Dillon 	    } else {
1400293141b7SMatthew Dillon 		fpath = mprintf("%s/%s", path, UseCpFile);
1401c0538630SMatthew Dillon 		AddList(list, strrchr(fpath, '/') + 1, 1, NULL);
140216802529SMatthew Dillon 		cphost = host;
140316802529SMatthew Dillon 	    }
140416802529SMatthew Dillon 	    fd = hc_open(cphost, fpath, O_RDONLY, 0);
140516802529SMatthew Dillon 	    if (fd >= 0) {
1406293141b7SMatthew Dillon 		bufused = 0;
140716802529SMatthew Dillon 		while ((nread = hc_read(cphost, fd, buf + bufused,
1408c0538630SMatthew Dillon 			GETBUFSIZE - bufused - 1)) > 0) {
1409293141b7SMatthew Dillon 		    *CountReadBytes += nread;
1410293141b7SMatthew Dillon 		    bufused += nread;
1411293141b7SMatthew Dillon 		    buf[bufused] = 0;
1412293141b7SMatthew Dillon 		    for (next = buf; (nl = strchr(next, '\n')); next = nl+1) {
1413293141b7SMatthew Dillon 			*nl = 0;
1414c0538630SMatthew Dillon 			AddList(list, next, 1, NULL);
1415293141b7SMatthew Dillon 		    }
1416293141b7SMatthew Dillon 		    bufused = strlen(next);
1417293141b7SMatthew Dillon 		    if (bufused)
1418293141b7SMatthew Dillon 			bcopy(next, buf, bufused);
1419293141b7SMatthew Dillon 		}
1420293141b7SMatthew Dillon 		if (bufused) {
1421293141b7SMatthew Dillon 		    /* last line has no trailing newline */
1422293141b7SMatthew Dillon 		    buf[bufused] = 0;
1423c0538630SMatthew Dillon 		    AddList(list, buf, 1, NULL);
1424293141b7SMatthew Dillon 		}
142516802529SMatthew Dillon 		hc_close(cphost, fd);
1426293141b7SMatthew Dillon 	    }
1427293141b7SMatthew Dillon 	    free(fpath);
1428293141b7SMatthew Dillon 	    free(buf);
1429293141b7SMatthew Dillon 	}
1430293141b7SMatthew Dillon 
1431293141b7SMatthew Dillon 	/*
1432293141b7SMatthew Dillon 	 * Automatically exclude MD5CacheFile that we create on the
1433293141b7SMatthew Dillon 	 * source from the copy to the destination.
1434293141b7SMatthew Dillon 	 */
1435293141b7SMatthew Dillon 	if (UseMD5Opt)
1436c0538630SMatthew Dillon 	    AddList(list, MD5CacheFile, 1, NULL);
1437293141b7SMatthew Dillon     }
1438293141b7SMatthew Dillon 
1439c0538630SMatthew Dillon     if ((dir = hc_opendir(host, path)) == NULL)
1440c0538630SMatthew Dillon 	return (1);
1441c0538630SMatthew Dillon     while ((den = hc_readdir(host, dir, &statptr)) != NULL) {
1442293141b7SMatthew Dillon 	/*
1443293141b7SMatthew Dillon 	 * ignore . and ..
1444293141b7SMatthew Dillon 	 */
1445560e4370SMatthew Dillon 	if (strcmp(den->d_name, ".") != 0 && strcmp(den->d_name, "..") != 0) {
1446560e4370SMatthew Dillon 	     if (UseCpFile && UseCpFile[0] == '/') {
1447560e4370SMatthew Dillon 		 if (CheckList(list, path, den->d_name) == 0)
1448560e4370SMatthew Dillon 			continue;
1449560e4370SMatthew Dillon 	     }
1450c0538630SMatthew Dillon 	     AddList(list, den->d_name, n, statptr);
1451293141b7SMatthew Dillon 	}
1452560e4370SMatthew Dillon     }
1453293141b7SMatthew Dillon     hc_closedir(host, dir);
1454293141b7SMatthew Dillon 
1455293141b7SMatthew Dillon     return (0);
1456293141b7SMatthew Dillon }
1457293141b7SMatthew Dillon 
14583a42736dSMatthew Dillon /*
14593a42736dSMatthew Dillon  * RemoveRecur()
14603a42736dSMatthew Dillon  */
14613a42736dSMatthew Dillon 
146275bd842aSMatthew Dillon static void
RemoveRecur(const char * dpath,dev_t devNo,struct stat * dstat)1463c0538630SMatthew Dillon RemoveRecur(const char *dpath, dev_t devNo, struct stat *dstat)
14643a42736dSMatthew Dillon {
14653a42736dSMatthew Dillon     struct stat st;
14663a42736dSMatthew Dillon 
1467c0538630SMatthew Dillon     if (dstat == NULL) {
1468c0538630SMatthew Dillon 	if (hc_lstat(&DstHost, dpath, &st) == 0)
1469c0538630SMatthew Dillon 	    dstat = &st;
1470c0538630SMatthew Dillon     }
1471c0538630SMatthew Dillon     if (dstat != NULL) {
14727cde0c8bSMatthew Dillon 	if (devNo == (dev_t)-1)
1473c0538630SMatthew Dillon 	    devNo = dstat->st_dev;
1474c0538630SMatthew Dillon 	if (dstat->st_dev == devNo) {
1475c0538630SMatthew Dillon 	    if (S_ISDIR(dstat->st_mode)) {
14763a42736dSMatthew Dillon 		DIR *dir;
14773a42736dSMatthew Dillon 
14784d858d58SMatthew Dillon 		if ((dir = hc_opendir(&DstHost, dpath)) != NULL) {
1479c0538630SMatthew Dillon 		    List *list = malloc(sizeof(List));
1480c0538630SMatthew Dillon 		    Node *node = NULL;
1481c0538630SMatthew Dillon 		    struct HCDirEntry *den;
14823a42736dSMatthew Dillon 
1483c0538630SMatthew Dillon 		    InitList(list);
1484c0538630SMatthew Dillon 		    while ((den = hc_readdir(&DstHost, dir, &dstat)) != NULL) {
14853a42736dSMatthew Dillon 			if (strcmp(den->d_name, ".") == 0)
14863a42736dSMatthew Dillon 			    continue;
14873a42736dSMatthew Dillon 			if (strcmp(den->d_name, "..") == 0)
14883a42736dSMatthew Dillon 			    continue;
1489c0538630SMatthew Dillon 			AddList(list, den->d_name, 3, dstat);
14903a42736dSMatthew Dillon 		    }
14914d858d58SMatthew Dillon 		    hc_closedir(&DstHost, dir);
1492c0538630SMatthew Dillon 		    while ((node = IterateList(list, node, 3)) != NULL) {
1493c0538630SMatthew Dillon 			char *ndpath;
1494c0538630SMatthew Dillon 
1495c0538630SMatthew Dillon 			ndpath = mprintf("%s/%s", dpath, node->no_Name);
1496c0538630SMatthew Dillon 			RemoveRecur(ndpath, devNo, node->no_Stat);
1497c0538630SMatthew Dillon 			free(ndpath);
1498c0538630SMatthew Dillon 		    }
1499c0538630SMatthew Dillon 		    ResetList(list);
1500c0538630SMatthew Dillon 		    free(list);
15013a42736dSMatthew Dillon 		}
1502c0d18d1dSMatthew Dillon 		if (AskConfirmation && NoRemoveOpt == 0) {
15033a42736dSMatthew Dillon 		    if (YesNo(dpath)) {
15049d626b29SSascha Wildner 			if (xrmdir(&DstHost, dpath) < 0) {
15053a42736dSMatthew Dillon 			    logerr("%-32s rmdir failed: %s\n",
15063a42736dSMatthew Dillon 				dpath, strerror(errno)
15073a42736dSMatthew Dillon 			    );
15083a42736dSMatthew Dillon 			}
15093a42736dSMatthew Dillon 			CountRemovedItems++;
15103a42736dSMatthew Dillon 		    }
15113a42736dSMatthew Dillon 		} else {
15123a42736dSMatthew Dillon 		    if (NoRemoveOpt) {
15133a42736dSMatthew Dillon 			if (VerboseOpt)
15143a42736dSMatthew Dillon 			    logstd("%-32s not-removed\n", dpath);
15159d626b29SSascha Wildner 		    } else if (xrmdir(&DstHost, dpath) == 0) {
15163a42736dSMatthew Dillon 			if (VerboseOpt)
15173a42736dSMatthew Dillon 			    logstd("%-32s rmdir-ok\n", dpath);
15183a42736dSMatthew Dillon 			CountRemovedItems++;
15193a42736dSMatthew Dillon 		    } else {
15203a42736dSMatthew Dillon 			logerr("%-32s rmdir failed: %s\n",
15213a42736dSMatthew Dillon 			    dpath, strerror(errno)
15223a42736dSMatthew Dillon 			);
15233a42736dSMatthew Dillon 		    }
15243a42736dSMatthew Dillon 		}
15253a42736dSMatthew Dillon 	    } else {
1526c0d18d1dSMatthew Dillon 		if (AskConfirmation && NoRemoveOpt == 0) {
15273a42736dSMatthew Dillon 		    if (YesNo(dpath)) {
152877133d96SMatthew Dillon 			if (xremove(&DstHost, dpath) < 0) {
15293a42736dSMatthew Dillon 			    logerr("%-32s remove failed: %s\n",
15303a42736dSMatthew Dillon 				dpath, strerror(errno)
15313a42736dSMatthew Dillon 			    );
15323a42736dSMatthew Dillon 			}
15333a42736dSMatthew Dillon 			CountRemovedItems++;
15343a42736dSMatthew Dillon 		    }
15353a42736dSMatthew Dillon 		} else {
15363a42736dSMatthew Dillon 		    if (NoRemoveOpt) {
15373a42736dSMatthew Dillon 			if (VerboseOpt)
15383a42736dSMatthew Dillon 			    logstd("%-32s not-removed\n", dpath);
153977133d96SMatthew Dillon 		    } else if (xremove(&DstHost, dpath) == 0) {
15403a42736dSMatthew Dillon 			if (VerboseOpt)
15413a42736dSMatthew Dillon 			    logstd("%-32s remove-ok\n", dpath);
15423a42736dSMatthew Dillon 			CountRemovedItems++;
15433a42736dSMatthew Dillon 		    } else {
15443a42736dSMatthew Dillon 			logerr("%-32s remove failed: %s\n",
15453a42736dSMatthew Dillon 			    dpath, strerror(errno)
15463a42736dSMatthew Dillon 			);
15473a42736dSMatthew Dillon 		    }
15483a42736dSMatthew Dillon 		}
15493a42736dSMatthew Dillon 	    }
15503a42736dSMatthew Dillon 	}
15513a42736dSMatthew Dillon     }
15523a42736dSMatthew Dillon }
15533a42736dSMatthew Dillon 
155475bd842aSMatthew Dillon static void
InitList(List * list)15553a42736dSMatthew Dillon InitList(List *list)
15563a42736dSMatthew Dillon {
15573a42736dSMatthew Dillon     bzero(list, sizeof(List));
15583a42736dSMatthew Dillon     list->li_Node.no_Next = &list->li_Node;
15593a42736dSMatthew Dillon }
15603a42736dSMatthew Dillon 
156175bd842aSMatthew Dillon static void
ResetList(List * list)15623a42736dSMatthew Dillon ResetList(List *list)
15633a42736dSMatthew Dillon {
15643a42736dSMatthew Dillon     Node *node;
15653a42736dSMatthew Dillon 
15663a42736dSMatthew Dillon     while ((node = list->li_Node.no_Next) != &list->li_Node) {
15673a42736dSMatthew Dillon 	list->li_Node.no_Next = node->no_Next;
1568c0538630SMatthew Dillon 	if (node->no_Stat != NULL)
1569c0538630SMatthew Dillon 	    free(node->no_Stat);
15703a42736dSMatthew Dillon 	free(node);
15713a42736dSMatthew Dillon     }
15723a42736dSMatthew Dillon     InitList(list);
15733a42736dSMatthew Dillon }
15743a42736dSMatthew Dillon 
157575bd842aSMatthew Dillon static Node *
IterateList(List * list,Node * node,int n)1576c0538630SMatthew Dillon IterateList(List *list, Node *node, int n)
1577293141b7SMatthew Dillon {
1578293141b7SMatthew Dillon     if (node == NULL)
1579293141b7SMatthew Dillon 	node = list->li_Node.no_Next;
1580c0538630SMatthew Dillon     else
1581293141b7SMatthew Dillon 	node = node->no_Next;
1582c0538630SMatthew Dillon     while (node->no_Value != n && node != &list->li_Node)
1583c0538630SMatthew Dillon 	node = node->no_Next;
1584c0538630SMatthew Dillon     return (node == &list->li_Node ? NULL : node);
1585293141b7SMatthew Dillon }
1586293141b7SMatthew Dillon 
158775bd842aSMatthew Dillon static int
AddList(List * list,const char * name,int n,struct stat * st)1588c0538630SMatthew Dillon AddList(List *list, const char *name, int n, struct stat *st)
15893a42736dSMatthew Dillon {
15903a42736dSMatthew Dillon     Node *node;
159158860d7dSMatthew Dillon     int hv;
159258860d7dSMatthew Dillon 
15933a42736dSMatthew Dillon     /*
15943a42736dSMatthew Dillon      * Scan against wildcards.  Only a node value of 1 can be a wildcard
15953a42736dSMatthew Dillon      * ( usually scanned from .cpignore )
15963a42736dSMatthew Dillon      */
15973a42736dSMatthew Dillon     for (node = list->li_Hash[0]; node; node = node->no_HNext) {
15983a42736dSMatthew Dillon 	if (strcmp(name, node->no_Name) == 0 ||
1599293141b7SMatthew Dillon 	    (n != 1 && node->no_Value == 1 &&
1600293141b7SMatthew Dillon 	    fnmatch(node->no_Name, name, 0) == 0)
16013a42736dSMatthew Dillon 	) {
16023a42736dSMatthew Dillon 	    return(node->no_Value);
16033a42736dSMatthew Dillon 	}
16043a42736dSMatthew Dillon     }
16053a42736dSMatthew Dillon 
16063a42736dSMatthew Dillon     /*
16073a42736dSMatthew Dillon      * Look for exact match
16083a42736dSMatthew Dillon      */
16093a42736dSMatthew Dillon 
1610c0538630SMatthew Dillon     hv = shash(name);
16113a42736dSMatthew Dillon     for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
16123a42736dSMatthew Dillon 	if (strcmp(name, node->no_Name) == 0) {
16133a42736dSMatthew Dillon 	    return(node->no_Value);
16143a42736dSMatthew Dillon 	}
16153a42736dSMatthew Dillon     }
16163a42736dSMatthew Dillon     node = malloc(sizeof(Node) + strlen(name) + 1);
1617c0538630SMatthew Dillon     if (node == NULL)
1618c0538630SMatthew Dillon 	fatal("out of memory");
16193a42736dSMatthew Dillon 
16203a42736dSMatthew Dillon     node->no_Next = list->li_Node.no_Next;
16213a42736dSMatthew Dillon     list->li_Node.no_Next = node;
16223a42736dSMatthew Dillon 
16233a42736dSMatthew Dillon     node->no_HNext = list->li_Hash[hv];
16243a42736dSMatthew Dillon     list->li_Hash[hv] = node;
16253a42736dSMatthew Dillon 
16263a42736dSMatthew Dillon     strcpy(node->no_Name, name);
16273a42736dSMatthew Dillon     node->no_Value = n;
1628c0538630SMatthew Dillon     node->no_Stat = st;
16293a42736dSMatthew Dillon 
16303a42736dSMatthew Dillon     return(n);
16313a42736dSMatthew Dillon }
16323a42736dSMatthew Dillon 
1633560e4370SMatthew Dillon /*
1634560e4370SMatthew Dillon  * Match against n=1 (cpignore) entries
1635560e4370SMatthew Dillon  *
1636560e4370SMatthew Dillon  * Returns 0 on match, non-zero if no match
1637560e4370SMatthew Dillon  */
1638560e4370SMatthew Dillon static int
CheckList(List * list,const char * path,const char * name)1639560e4370SMatthew Dillon CheckList(List *list, const char *path, const char *name)
1640560e4370SMatthew Dillon {
1641560e4370SMatthew Dillon     char *fpath = NULL;
1642560e4370SMatthew Dillon     Node *node;
1643560e4370SMatthew Dillon     int hv;
1644560e4370SMatthew Dillon 
164560374ee7SAaron LI     if (asprintf(&fpath, "%s/%s", path, name) < 0)
164660374ee7SAaron LI 	fatal("out of memory");
1647560e4370SMatthew Dillon 
1648560e4370SMatthew Dillon     /*
1649560e4370SMatthew Dillon      * Scan against wildcards.  Only a node value of 1 can be a wildcard
1650560e4370SMatthew Dillon      * ( usually scanned from .cpignore )
1651560e4370SMatthew Dillon      */
1652560e4370SMatthew Dillon     for (node = list->li_Hash[0]; node; node = node->no_HNext) {
1653560e4370SMatthew Dillon 	if (node->no_Value != 1)
1654560e4370SMatthew Dillon 		continue;
1655560e4370SMatthew Dillon 	if (fnmatch(node->no_Name, fpath, 0) == 0) {
1656560e4370SMatthew Dillon 		free(fpath);
1657560e4370SMatthew Dillon 		return 0;
1658560e4370SMatthew Dillon 	}
1659560e4370SMatthew Dillon     }
1660560e4370SMatthew Dillon 
1661560e4370SMatthew Dillon     /*
1662560e4370SMatthew Dillon      * Look for exact match
1663560e4370SMatthew Dillon      */
1664560e4370SMatthew Dillon     hv = shash(fpath);
1665560e4370SMatthew Dillon     for (node = list->li_Hash[hv]; node; node = node->no_HNext) {
1666560e4370SMatthew Dillon 	if (node->no_Value != 1)
1667560e4370SMatthew Dillon 		continue;
1668560e4370SMatthew Dillon 	if (strcmp(fpath, node->no_Name) == 0) {
1669560e4370SMatthew Dillon 		free(fpath);
1670560e4370SMatthew Dillon 		return 0;
1671560e4370SMatthew Dillon 	}
1672560e4370SMatthew Dillon     }
1673560e4370SMatthew Dillon 
1674560e4370SMatthew Dillon     free(fpath);
1675560e4370SMatthew Dillon     return 1;
1676560e4370SMatthew Dillon }
1677560e4370SMatthew Dillon 
1678fce2564bSJeroen Ruigrok/asmodai static int
shash(const char * s)16793a42736dSMatthew Dillon shash(const char *s)
16803a42736dSMatthew Dillon {
168158860d7dSMatthew Dillon     int hv;
168258860d7dSMatthew Dillon 
168358860d7dSMatthew Dillon     hv = 0xA4FB3255;
16843a42736dSMatthew Dillon 
16853a42736dSMatthew Dillon     while (*s) {
16863a42736dSMatthew Dillon 	if (*s == '*' || *s == '?' ||
16873a42736dSMatthew Dillon 	    *s == '{' || *s == '}' ||
16883a42736dSMatthew Dillon 	    *s == '[' || *s == ']' ||
16893a42736dSMatthew Dillon 	    *s == '|'
16903a42736dSMatthew Dillon 	) {
16913a42736dSMatthew Dillon 	    return(0);
16923a42736dSMatthew Dillon 	}
16933a42736dSMatthew Dillon 	hv = (hv << 5) ^ *s ^ (hv >> 23);
16943a42736dSMatthew Dillon 	++s;
16953a42736dSMatthew Dillon     }
16963a42736dSMatthew Dillon     return(((hv >> 16) ^ hv) & HMASK);
16973a42736dSMatthew Dillon }
16983a42736dSMatthew Dillon 
169975bd842aSMatthew Dillon static int
YesNo(const char * path)17003a42736dSMatthew Dillon YesNo(const char *path)
17013a42736dSMatthew Dillon {
17023a42736dSMatthew Dillon     int ch, first;
17033a42736dSMatthew Dillon 
170458860d7dSMatthew Dillon     fprintf(stderr, "remove %s (Yes/No) [No]? ", path);
170558860d7dSMatthew Dillon     fflush(stderr);
17063a42736dSMatthew Dillon 
17073a42736dSMatthew Dillon     first = ch = getchar();
17083a42736dSMatthew Dillon     while (ch != '\n' && ch != EOF)
17093a42736dSMatthew Dillon 	ch = getchar();
17103a42736dSMatthew Dillon     return ((first == 'y' || first == 'Y'));
17113a42736dSMatthew Dillon }
17123a42736dSMatthew Dillon 
17133a42736dSMatthew Dillon /*
17143a42736dSMatthew Dillon  * xrename() - rename with override
17153a42736dSMatthew Dillon  *
17163a42736dSMatthew Dillon  *	If the rename fails, attempt to override st_flags on the
17173a42736dSMatthew Dillon  *	destination and rename again.  If that fails too, try to
17183a42736dSMatthew Dillon  *	set the flags back the way they were and give up.
17193a42736dSMatthew Dillon  */
17203a42736dSMatthew Dillon 
1721fce2564bSJeroen Ruigrok/asmodai static int
xrename(const char * src,const char * dst,u_long flags)17223a42736dSMatthew Dillon xrename(const char *src, const char *dst, u_long flags)
17233a42736dSMatthew Dillon {
172458860d7dSMatthew Dillon     int r;
172558860d7dSMatthew Dillon 
17264d858d58SMatthew Dillon     if ((r = hc_rename(&DstHost, src, dst)) < 0) {
17274d858d58SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
17282b7dbe20SMatthew Dillon 	if (DstHost.version >= HCPROTO_VERSION_LUCC)
17292b7dbe20SMatthew Dillon 	    hc_lchflags(&DstHost, dst, 0);
17302b7dbe20SMatthew Dillon 	else
17314d858d58SMatthew Dillon 	    hc_chflags(&DstHost, dst, 0);
17322b7dbe20SMatthew Dillon 
17332b7dbe20SMatthew Dillon 	if ((r = hc_rename(&DstHost, src, dst)) < 0) {
17342b7dbe20SMatthew Dillon 	    if (DstHost.version >= HCPROTO_VERSION_LUCC)
17352b7dbe20SMatthew Dillon 		hc_lchflags(&DstHost, dst, flags);
17362b7dbe20SMatthew Dillon 	    else
17374d858d58SMatthew Dillon 		hc_chflags(&DstHost, dst, flags);
17382b7dbe20SMatthew Dillon 	}
17394d858d58SMatthew Dillon #endif
17403a42736dSMatthew Dillon     }
17413a42736dSMatthew Dillon     return(r);
17423a42736dSMatthew Dillon }
17433a42736dSMatthew Dillon 
1744fce2564bSJeroen Ruigrok/asmodai static int
xlink(const char * src,const char * dst,u_long flags)17453a42736dSMatthew Dillon xlink(const char *src, const char *dst, u_long flags)
17463a42736dSMatthew Dillon {
1747d8bce52dSMatthew Dillon     int r;
1748d8bce52dSMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
1749d8bce52dSMatthew Dillon     int e;
1750d8bce52dSMatthew Dillon #endif
175158860d7dSMatthew Dillon 
17524d858d58SMatthew Dillon     if ((r = hc_link(&DstHost, src, dst)) < 0) {
17534d858d58SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
17542b7dbe20SMatthew Dillon 	if (DstHost.version >= HCPROTO_VERSION_LUCC)
1755c9c5aa9eSMatthew Dillon 	    hc_lchflags(&DstHost, src, 0);
17562b7dbe20SMatthew Dillon 	else
17574d858d58SMatthew Dillon 	    hc_chflags(&DstHost, src, 0);
17584d858d58SMatthew Dillon 	r = hc_link(&DstHost, src, dst);
17593a42736dSMatthew Dillon 	e = errno;
17604d858d58SMatthew Dillon 	hc_chflags(&DstHost, src, flags);
17613a42736dSMatthew Dillon 	errno = e;
17624d858d58SMatthew Dillon #endif
17633a42736dSMatthew Dillon     }
1764d5fdcd00SMatthew Dillon     if (r == 0)
1765d5fdcd00SMatthew Dillon 	    ++CountLinkedItems;
17663a42736dSMatthew Dillon     return(r);
17673a42736dSMatthew Dillon }
17683a42736dSMatthew Dillon 
176977133d96SMatthew Dillon static int
xremove(struct HostConf * host,const char * path)177077133d96SMatthew Dillon xremove(struct HostConf *host, const char *path)
177177133d96SMatthew Dillon {
177277133d96SMatthew Dillon     int res;
177377133d96SMatthew Dillon 
177477133d96SMatthew Dillon     res = hc_remove(host, path);
177577133d96SMatthew Dillon #ifdef _ST_FLAGS_PRESENT_
177677133d96SMatthew Dillon     if (res == -EPERM) {
17772b7dbe20SMatthew Dillon 	if (host->version >= HCPROTO_VERSION_LUCC)
17782b7dbe20SMatthew Dillon 	    hc_lchflags(host, path, 0);
17792b7dbe20SMatthew Dillon 	else
178077133d96SMatthew Dillon 	    hc_chflags(host, path, 0);
178177133d96SMatthew Dillon 	res = hc_remove(host, path);
178277133d96SMatthew Dillon     }
178377133d96SMatthew Dillon #endif
178477133d96SMatthew Dillon     return(res);
178577133d96SMatthew Dillon }
178677133d96SMatthew Dillon 
17879d626b29SSascha Wildner static int
xrmdir(struct HostConf * host,const char * path)17889d626b29SSascha Wildner xrmdir(struct HostConf *host, const char *path)
17899d626b29SSascha Wildner {
17909d626b29SSascha Wildner     int res;
17919d626b29SSascha Wildner 
17929d626b29SSascha Wildner     res = hc_rmdir(host, path);
17939d626b29SSascha Wildner #ifdef _ST_FLAGS_PRESENT_
17949d626b29SSascha Wildner     if (res == -EPERM) {
17959d626b29SSascha Wildner 	hc_chflags(host, path, 0);
17969d626b29SSascha Wildner 	res = hc_rmdir(host, path);
17979d626b29SSascha Wildner     }
17989d626b29SSascha Wildner #endif
17999d626b29SSascha Wildner     return(res);
18009d626b29SSascha Wildner }
18010212bfceSMatthew Dillon 
18020212bfceSMatthew Dillon /*
18030212bfceSMatthew Dillon  * Compare mtimes.  By default cpdup only compares the seconds field
18040212bfceSMatthew Dillon  * because different operating systems and filesystems will store time
18050212bfceSMatthew Dillon  * fields with varying amounts of precision.
18060212bfceSMatthew Dillon  *
18070212bfceSMatthew Dillon  * This subroutine can be adjusted to also compare to microseconds or
18080212bfceSMatthew Dillon  * nanoseconds precision.  However, since cpdup() uses utimes() to
18090212bfceSMatthew Dillon  * set a file's timestamp and utimes() only takes timeval's (usec precision),
18100212bfceSMatthew Dillon  * I strongly recommend only comparing down to usec precision at best.
18110212bfceSMatthew Dillon  */
18120212bfceSMatthew Dillon static int
mtimecmp(struct stat * st1,struct stat * st2)18130212bfceSMatthew Dillon mtimecmp(struct stat *st1, struct stat *st2)
18140212bfceSMatthew Dillon {
18150212bfceSMatthew Dillon     if (st1->st_mtime < st2->st_mtime)
18160212bfceSMatthew Dillon 	return -1;
18170212bfceSMatthew Dillon     if (st1->st_mtime == st2->st_mtime)
18180212bfceSMatthew Dillon 	return 0;
18190212bfceSMatthew Dillon     return 1;
18200212bfceSMatthew Dillon }
18212b7dbe20SMatthew Dillon 
18222b7dbe20SMatthew Dillon /*
18232b7dbe20SMatthew Dillon  * Check to determine if a symlink's mtime, flags, or mode differ.
18242b7dbe20SMatthew Dillon  *
18252b7dbe20SMatthew Dillon  * This is only supported on targets that support lchflags, lutimes,
18262b7dbe20SMatthew Dillon  * and lchmod.
18272b7dbe20SMatthew Dillon  */
18282b7dbe20SMatthew Dillon static int
symlink_mfo_test(struct HostConf * hc,struct stat * st1,struct stat * st2)18292b7dbe20SMatthew Dillon symlink_mfo_test(struct HostConf *hc, struct stat *st1, struct stat *st2)
18302b7dbe20SMatthew Dillon {
18312b7dbe20SMatthew Dillon     int res = 0;
18322b7dbe20SMatthew Dillon 
18332b7dbe20SMatthew Dillon     if (hc->version >= HCPROTO_VERSION_LUCC) {
18342b7dbe20SMatthew Dillon 	if (!FlagsMatch(st1, st2))
18352b7dbe20SMatthew Dillon 	    res = 1;
18362b7dbe20SMatthew Dillon 	if (mtimecmp(st1, st2) != 0)
18372b7dbe20SMatthew Dillon 	    res = 1;
18382b7dbe20SMatthew Dillon 	if (st1->st_mode != st2->st_mode)
18392b7dbe20SMatthew Dillon 	    res = 1;
18402b7dbe20SMatthew Dillon     }
18412b7dbe20SMatthew Dillon     return res;
18422b7dbe20SMatthew Dillon }
1843