1 /*
2  * Generate and receive file lists.
3  *
4  * Copyright (C) 1996 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7  * Copyright (C) 2002-2020 Wayne Davison
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, visit the http://fsf.org website.
21  */
22 
23 #include "rsync.h"
24 #include "ifuncs.h"
25 #include "rounding.h"
26 #include "inums.h"
27 #include "io.h"
28 
29 extern int am_root;
30 extern int am_server;
31 extern int am_daemon;
32 extern int am_sender;
33 extern int am_generator;
34 extern int inc_recurse;
35 extern int always_checksum;
36 extern int checksum_type;
37 extern int module_id;
38 extern int ignore_errors;
39 extern int numeric_ids;
40 extern int quiet;
41 extern int recurse;
42 extern int use_qsort;
43 extern int xfer_dirs;
44 extern int filesfrom_fd;
45 extern int one_file_system;
46 extern int copy_dirlinks;
47 extern int preserve_uid;
48 extern int preserve_gid;
49 extern int preserve_acls;
50 extern int preserve_xattrs;
51 extern int preserve_links;
52 extern int preserve_hard_links;
53 extern int preserve_devices;
54 extern int preserve_specials;
55 extern int preserve_fileflags;
56 extern int delete_during;
57 extern int missing_args;
58 extern int eol_nulls;
59 extern int atimes_ndx;
60 extern int crtimes_ndx;
61 extern int relative_paths;
62 extern int implied_dirs;
63 extern int ignore_perishable;
64 extern int non_perishable_cnt;
65 extern int prune_empty_dirs;
66 extern int copy_links;
67 extern int copy_unsafe_links;
68 extern int protocol_version;
69 extern int sanitize_paths;
70 extern int munge_symlinks;
71 extern int use_safe_inc_flist;
72 extern int need_unsorted_flist;
73 extern int sender_symlink_iconv;
74 extern int output_needs_newline;
75 extern int sender_keeps_checksum;
76 extern int unsort_ndx;
77 extern uid_t our_uid;
78 extern struct stats stats;
79 extern char *filesfrom_host;
80 extern char *usermap, *groupmap;
81 
82 extern char curr_dir[MAXPATHLEN];
83 
84 extern struct chmod_mode_struct *chmod_modes;
85 
86 extern filter_rule_list filter_list;
87 extern filter_rule_list daemon_filter_list;
88 
89 #ifdef ICONV_OPTION
90 extern int filesfrom_convert;
91 extern iconv_t ic_send, ic_recv;
92 #endif
93 
94 #define PTR_SIZE (sizeof (struct file_struct *))
95 
96 int io_error;
97 int flist_csum_len;
98 dev_t filesystem_dev; /* used to implement -x */
99 
100 struct file_list *cur_flist, *first_flist, *dir_flist;
101 int send_dir_ndx = -1, send_dir_depth = -1;
102 int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
103 int file_total = 0; /* total of all active items over all file-lists */
104 int file_old_total = 0; /* total of active items that will soon be gone */
105 int flist_eof = 0; /* all the file-lists are now known */
106 int xfer_flags_as_varint = 0;
107 
108 #define NORMAL_NAME 0
109 #define SLASH_ENDING_NAME 1
110 #define DOTDIR_NAME 2
111 #define MISSING_NAME 3
112 
113 /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
114  * internally, even if this platform does not allow files to have 64-bit inums.
115  * The only exception is if we're on a platform with no 64-bit type at all.
116  *
117  * Because we use read_longint() to get these off the wire, if you transfer
118  * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
119  * machine with no 64-bit types then you will get an overflow error.
120  *
121  * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
122  * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
123  * be truncated.  But it's a kind of silly thing to do anyhow. */
124 
125 /* The tmp_* vars are used as a cache area by make_file() to store data
126  * that the sender doesn't need to remember in its file list.  The data
127  * will survive just long enough to be used by send_file_entry(). */
128 static dev_t tmp_rdev;
129 #ifdef SUPPORT_HARD_LINKS
130 static int64 tmp_dev = -1, tmp_ino;
131 #endif
132 static char tmp_sum[MAX_DIGEST_LEN];
133 
134 static char empty_sum[MAX_DIGEST_LEN];
135 static int flist_count_offset; /* for --delete --progress */
136 static int show_filelist_progress;
137 
138 static struct file_list *flist_new(int flags, const char *msg);
139 static void flist_sort_and_clean(struct file_list *flist, int strip_root);
140 static void output_flist(struct file_list *flist);
141 
init_flist(void)142 void init_flist(void)
143 {
144 	if (DEBUG_GTE(FLIST, 4)) {
145 		rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
146 			(int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
147 	}
148 	flist_csum_len = csum_len_for_type(checksum_type, 1);
149 
150 	show_filelist_progress = INFO_GTE(FLIST, 1) && xfer_dirs && !am_server && !inc_recurse;
151 }
152 
start_filelist_progress(char * kind)153 static void start_filelist_progress(char *kind)
154 {
155 	if (quiet)
156 		return;
157 	rprintf(FCLIENT, "%s ... ", kind);
158 	output_needs_newline = 1;
159 	rflush(FINFO);
160 }
161 
emit_filelist_progress(int count)162 static void emit_filelist_progress(int count)
163 {
164 	if (quiet)
165 		return;
166 	if (output_needs_newline == 2) /* avoid a newline in the middle of this filelist-progress output */
167 		output_needs_newline = 0;
168 	rprintf(FCLIENT, " %d files...\r", count);
169 	output_needs_newline = 2;
170 }
171 
maybe_emit_filelist_progress(int count)172 static void maybe_emit_filelist_progress(int count)
173 {
174 	if (INFO_GTE(FLIST, 2) && show_filelist_progress && (count % 100) == 0)
175 		emit_filelist_progress(count);
176 }
177 
finish_filelist_progress(const struct file_list * flist)178 static void finish_filelist_progress(const struct file_list *flist)
179 {
180 	output_needs_newline = 0;
181 	if (INFO_GTE(FLIST, 2)) {
182 		/* This overwrites the progress line */
183 		rprintf(FINFO, "%d file%sto consider\n",
184 			flist->used, flist->used == 1 ? " " : "s ");
185 	} else {
186 		rprintf(FINFO, "done\n");
187 	}
188 }
189 
show_flist_stats(void)190 void show_flist_stats(void)
191 {
192 	/* Nothing yet */
193 }
194 
195 /* Stat either a symlink or its referent, depending on the settings of
196  * copy_links, copy_unsafe_links, etc.  Returns -1 on error, 0 on success.
197  *
198  * If path is the name of a symlink, then the linkbuf buffer (which must hold
199  * MAXPATHLEN chars) will be set to the symlink's target string.
200  *
201  * The stat structure pointed to by stp will contain information about the
202  * link or the referent as appropriate, if they exist. */
readlink_stat(const char * path,STRUCT_STAT * stp,char * linkbuf)203 static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
204 {
205 #ifdef SUPPORT_LINKS
206 	if (link_stat(path, stp, copy_dirlinks) < 0)
207 		return -1;
208 	if (S_ISLNK(stp->st_mode)) {
209 		int llen = do_readlink(path, linkbuf, MAXPATHLEN - 1);
210 		if (llen < 0)
211 			return -1;
212 		linkbuf[llen] = '\0';
213 		if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
214 			if (INFO_GTE(SYMSAFE, 1)) {
215 				rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
216 					path, linkbuf);
217 			}
218 			return x_stat(path, stp, NULL);
219 		}
220 		if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
221 		 && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
222 			memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
223 				llen - SYMLINK_PREFIX_LEN + 1);
224 		}
225 	}
226 	return 0;
227 #else
228 	return x_stat(path, stp, NULL);
229 #endif
230 }
231 
link_stat(const char * path,STRUCT_STAT * stp,int follow_dirlinks)232 int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
233 {
234 #ifdef SUPPORT_LINKS
235 	if (copy_links)
236 		return x_stat(path, stp, NULL);
237 	if (x_lstat(path, stp, NULL) < 0)
238 		return -1;
239 	if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
240 		STRUCT_STAT st;
241 		if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
242 			*stp = st;
243 	}
244 	return 0;
245 #else
246 	return x_stat(path, stp, NULL);
247 #endif
248 }
249 
path_is_daemon_excluded(char * path,int ignore_filename)250 static inline int path_is_daemon_excluded(char *path, int ignore_filename)
251 {
252 	if (daemon_filter_list.head) {
253 		char *slash = path;
254 
255 		while ((slash = strchr(slash+1, '/')) != NULL) {
256 			int ret;
257 			*slash = '\0';
258 			ret = check_filter(&daemon_filter_list, FLOG, path, 1);
259 			*slash = '/';
260 			if (ret < 0) {
261 				errno = ENOENT;
262 				return 1;
263 			}
264 		}
265 
266 		if (!ignore_filename
267 		 && check_filter(&daemon_filter_list, FLOG, path, 1) < 0) {
268 			errno = ENOENT;
269 			return 1;
270 		}
271 	}
272 
273 	return 0;
274 }
275 
is_excluded(const char * fname,int is_dir,int filter_level)276 static inline int is_excluded(const char *fname, int is_dir, int filter_level)
277 {
278 	return name_is_excluded(fname, is_dir ? NAME_IS_DIR : NAME_IS_FILE, filter_level);
279 }
280 
281 static void send_directory(int f, struct file_list *flist,
282 			   char *fbuf, int len, int flags);
283 
284 static const char *pathname, *orig_dir;
285 static int pathname_len;
286 
287 /* Make sure flist can hold at least flist->used + extra entries. */
flist_expand(struct file_list * flist,int extra)288 static void flist_expand(struct file_list *flist, int extra)
289 {
290 	struct file_struct **new_ptr;
291 
292 	if (flist->used + extra <= flist->malloced)
293 		return;
294 
295 	if (flist->malloced < FLIST_START)
296 		flist->malloced = FLIST_START;
297 	else if (flist->malloced >= FLIST_LINEAR)
298 		flist->malloced += FLIST_LINEAR;
299 	else
300 		flist->malloced *= 2;
301 
302 	/* In case count jumped or we are starting the list
303 	 * with a known size just set it. */
304 	if (flist->malloced < flist->used + extra)
305 		flist->malloced = flist->used + extra;
306 
307 	new_ptr = realloc_array(flist->files, struct file_struct *, flist->malloced);
308 
309 	if (DEBUG_GTE(FLIST, 1) && flist->malloced != FLIST_START) {
310 		rprintf(FCLIENT, "[%s] expand file_list pointer array to %s bytes, did%s move\n",
311 		    who_am_i(),
312 		    big_num(sizeof flist->files[0] * flist->malloced),
313 		    (new_ptr == flist->files) ? " not" : "");
314 	}
315 
316 	flist->files = new_ptr;
317 }
318 
flist_done_allocating(struct file_list * flist)319 static void flist_done_allocating(struct file_list *flist)
320 {
321 	void *ptr = pool_boundary(flist->file_pool, 8*1024);
322 	if (flist->pool_boundary == ptr)
323 		flist->pool_boundary = NULL; /* list didn't use any pool memory */
324 	else
325 		flist->pool_boundary = ptr;
326 }
327 
328 /* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
329  * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
330  * with dir == NULL taken to be the starting directory, and dirlen < 0
331  * indicating that strdup(dir) should be called and then the -dirlen length
332  * value checked to ensure that it is not daemon-excluded. */
change_pathname(struct file_struct * file,const char * dir,int dirlen)333 int change_pathname(struct file_struct *file, const char *dir, int dirlen)
334 {
335 	if (dirlen < 0) {
336 		char *cpy = strdup(dir);
337 		if (*cpy != '/')
338 			change_dir(orig_dir, CD_SKIP_CHDIR);
339 		if (path_is_daemon_excluded(cpy, 0))
340 			goto chdir_error;
341 		dir = cpy;
342 		dirlen = -dirlen;
343 	} else {
344 		if (file) {
345 			if (pathname == F_PATHNAME(file))
346 				return 1;
347 			dir = F_PATHNAME(file);
348 			if (dir)
349 				dirlen = strlen(dir);
350 		} else if (pathname == dir)
351 			return 1;
352 		if (dir && *dir != '/')
353 			change_dir(orig_dir, CD_SKIP_CHDIR);
354 	}
355 
356 	pathname = dir;
357 	pathname_len = dirlen;
358 
359 	if (!dir)
360 		dir = orig_dir;
361 
362 	if (!change_dir(dir, CD_NORMAL)) {
363 	  chdir_error:
364 		io_error |= IOERR_GENERAL;
365 		rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir));
366 		if (dir != orig_dir)
367 			change_dir(orig_dir, CD_NORMAL);
368 		pathname = NULL;
369 		pathname_len = 0;
370 		return 0;
371 	}
372 
373 	return 1;
374 }
375 
send_file_entry(int f,const char * fname,struct file_struct * file,const char * symlink_name,int symlink_len,int ndx,int first_ndx)376 static void send_file_entry(int f, const char *fname, struct file_struct *file,
377 #ifdef SUPPORT_LINKS
378 			    const char *symlink_name, int symlink_len,
379 #endif
380 			    int ndx, int first_ndx)
381 {
382 	static time_t modtime, atime;
383 #ifdef SUPPORT_CRTIMES
384 	static time_t crtime;
385 #endif
386 	static mode_t mode;
387 #ifdef SUPPORT_FILEFLAGS
388 	static uint32 fileflags;
389 #endif
390 #ifdef SUPPORT_HARD_LINKS
391 	static int64 dev;
392 #endif
393 	static dev_t rdev;
394 	static uint32 rdev_major;
395 	static uid_t uid;
396 	static gid_t gid;
397 	static const char *user_name, *group_name;
398 	static char lastname[MAXPATHLEN];
399 #ifdef SUPPORT_HARD_LINKS
400 	int first_hlink_ndx = -1;
401 #endif
402 	int l1, l2;
403 	int xflags;
404 
405 	/* Initialize starting value of xflags and adjust counts. */
406 	if (S_ISREG(file->mode))
407 		xflags = 0;
408 	else if (S_ISDIR(file->mode)) {
409 		stats.num_dirs++;
410 		if (protocol_version >= 30) {
411 			if (file->flags & FLAG_CONTENT_DIR)
412 				xflags = file->flags & FLAG_TOP_DIR;
413 			else if (file->flags & FLAG_IMPLIED_DIR)
414 				xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
415 			else
416 				xflags = XMIT_NO_CONTENT_DIR;
417 		} else
418 			xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
419 	} else {
420 		if (S_ISLNK(file->mode))
421 			stats.num_symlinks++;
422 		else if (IS_DEVICE(file->mode))
423 			stats.num_devices++;
424 		else if (IS_SPECIAL(file->mode))
425 			stats.num_specials++;
426 		xflags = 0;
427 	}
428 
429 	if (file->mode == mode)
430 		xflags |= XMIT_SAME_MODE;
431 	else
432 		mode = file->mode;
433 #ifdef SUPPORT_FILEFLAGS
434 	if (preserve_fileflags) {
435 		if (F_FFLAGS(file) == fileflags)
436 			xflags |= XMIT_SAME_FLAGS;
437 		else
438 			fileflags = F_FFLAGS(file);
439 	}
440 #endif
441 
442 	if (preserve_devices && IS_DEVICE(mode)) {
443 		if (protocol_version < 28) {
444 			if (tmp_rdev == rdev)
445 				xflags |= XMIT_SAME_RDEV_pre28;
446 			else
447 				rdev = tmp_rdev;
448 		} else {
449 			rdev = tmp_rdev;
450 			if ((uint32)major(rdev) == rdev_major)
451 				xflags |= XMIT_SAME_RDEV_MAJOR;
452 			else
453 				rdev_major = major(rdev);
454 			if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
455 				xflags |= XMIT_RDEV_MINOR_8_pre30;
456 		}
457 	} else if (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31) {
458 		/* Special files don't need an rdev number, so just make
459 		 * the historical transmission of the value efficient. */
460 		if (protocol_version < 28)
461 			xflags |= XMIT_SAME_RDEV_pre28;
462 		else {
463 			rdev = MAKEDEV(rdev_major, 0);
464 			xflags |= XMIT_SAME_RDEV_MAJOR;
465 			if (protocol_version < 30)
466 				xflags |= XMIT_RDEV_MINOR_8_pre30;
467 		}
468 	} else if (protocol_version < 28)
469 		rdev = MAKEDEV(0, 0);
470 	if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname))
471 		xflags |= XMIT_SAME_UID;
472 	else {
473 		uid = F_OWNER(file);
474 		if (!numeric_ids) {
475 			user_name = add_uid(uid);
476 			if (inc_recurse && user_name)
477 				xflags |= XMIT_USER_NAME_FOLLOWS;
478 		}
479 	}
480 	if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname))
481 		xflags |= XMIT_SAME_GID;
482 	else {
483 		gid = F_GROUP(file);
484 		if (!numeric_ids) {
485 			group_name = add_gid(gid);
486 			if (inc_recurse && group_name)
487 				xflags |= XMIT_GROUP_NAME_FOLLOWS;
488 		}
489 	}
490 	if (file->modtime == modtime)
491 		xflags |= XMIT_SAME_TIME;
492 	else
493 		modtime = file->modtime;
494 	if (NSEC_BUMP(file) && protocol_version >= 31)
495 		xflags |= XMIT_MOD_NSEC;
496 	if (atimes_ndx && !S_ISDIR(mode)) {
497 		if (F_ATIME(file) == atime)
498 			xflags |= XMIT_SAME_ATIME;
499 		else
500 			atime = F_ATIME(file);
501 	}
502 #ifdef SUPPORT_CRTIMES
503 	if (crtimes_ndx) {
504 		crtime = F_CRTIME(file);
505 		if (crtime == modtime)
506 			xflags |= XMIT_CRTIME_EQ_MTIME;
507 	}
508 #endif
509 
510 #ifdef SUPPORT_HARD_LINKS
511 	if (tmp_dev != -1) {
512 		if (protocol_version >= 30) {
513 			struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
514 			first_hlink_ndx = (int32)(long)np->data; /* is -1 when new */
515 			if (first_hlink_ndx < 0) {
516 				np->data = (void*)(long)(first_ndx + ndx);
517 				xflags |= XMIT_HLINK_FIRST;
518 			}
519 			if (DEBUG_GTE(HLINK, 1)) {
520 				if (first_hlink_ndx >= 0) {
521 					rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
522 						who_am_i(), first_ndx + ndx, first_hlink_ndx,
523 						first_hlink_ndx >= first_ndx ? "" : "un");
524 				} else if (DEBUG_GTE(HLINK, 3)) {
525 					rprintf(FINFO, "[%s] dev:inode for #%d is %s:%s\n",
526 						who_am_i(), first_ndx + ndx,
527 						big_num(tmp_dev), big_num(tmp_ino));
528 				}
529 			}
530 		} else {
531 			if (tmp_dev == dev) {
532 				if (protocol_version >= 28)
533 					xflags |= XMIT_SAME_DEV_pre30;
534 			} else
535 				dev = tmp_dev;
536 		}
537 		xflags |= XMIT_HLINKED;
538 	}
539 #endif
540 
541 	for (l1 = 0;
542 	    lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
543 	    l1++) {}
544 	l2 = strlen(fname+l1);
545 
546 	if (l1 > 0)
547 		xflags |= XMIT_SAME_NAME;
548 	if (l2 > 255)
549 		xflags |= XMIT_LONG_NAME;
550 
551 	/* We must avoid sending a flag value of 0 (or an initial byte of
552 	 * 0 for the older xflags protocol) or it will signal the end of
553 	 * the list.  Note that the use of XMIT_TOP_DIR on a non-dir has
554 	 * no meaning, so it's a harmless way to add a bit to the first
555 	 * flag byte. */
556 	if (xfer_flags_as_varint)
557 		write_varint(f, xflags ? xflags : XMIT_EXTENDED_FLAGS);
558 	else if (protocol_version >= 28) {
559 		if (!xflags && !S_ISDIR(mode))
560 			xflags |= XMIT_TOP_DIR;
561 		if ((xflags & 0xFF00) || !xflags) {
562 			xflags |= XMIT_EXTENDED_FLAGS;
563 			write_shortint(f, xflags);
564 		} else
565 			write_byte(f, xflags);
566 	} else {
567 		if (!(xflags & 0xFF))
568 			xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
569 		write_byte(f, xflags);
570 	}
571 	if (xflags & XMIT_SAME_NAME)
572 		write_byte(f, l1);
573 	if (xflags & XMIT_LONG_NAME)
574 		write_varint30(f, l2);
575 	else
576 		write_byte(f, l2);
577 	write_buf(f, fname + l1, l2);
578 
579 #ifdef SUPPORT_HARD_LINKS
580 	if (first_hlink_ndx >= 0) {
581 		write_varint(f, first_hlink_ndx);
582 		if (first_hlink_ndx >= first_ndx)
583 			goto the_end;
584 	}
585 #endif
586 
587 	write_varlong30(f, F_LENGTH(file), 3);
588 	if (!(xflags & XMIT_SAME_TIME)) {
589 		if (protocol_version >= 30)
590 			write_varlong(f, modtime, 4);
591 		else
592 			write_int(f, modtime);
593 	}
594 	if (xflags & XMIT_MOD_NSEC)
595 		write_varint(f, F_MOD_NSEC(file));
596 #ifdef SUPPORT_CRTIMES
597 	if (crtimes_ndx && !(xflags & XMIT_CRTIME_EQ_MTIME))
598 		write_varlong(f, crtime, 4);
599 #endif
600 	if (!(xflags & XMIT_SAME_MODE))
601 		write_int(f, to_wire_mode(mode));
602 #ifdef SUPPORT_FILEFLAGS
603 	if (preserve_fileflags && !(xflags & XMIT_SAME_FLAGS))
604 		write_int(f, (int)fileflags);
605 #endif
606 	if (atimes_ndx && !S_ISDIR(mode) && !(xflags & XMIT_SAME_ATIME))
607 		write_varlong(f, atime, 4);
608 	if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
609 		if (protocol_version < 30)
610 			write_int(f, uid);
611 		else {
612 			write_varint(f, uid);
613 			if (xflags & XMIT_USER_NAME_FOLLOWS) {
614 				int len = strlen(user_name);
615 				write_byte(f, len);
616 				write_buf(f, user_name, len);
617 			}
618 		}
619 	}
620 	if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
621 		if (protocol_version < 30)
622 			write_int(f, gid);
623 		else {
624 			write_varint(f, gid);
625 			if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
626 				int len = strlen(group_name);
627 				write_byte(f, len);
628 				write_buf(f, group_name, len);
629 			}
630 		}
631 	}
632 	if ((preserve_devices && IS_DEVICE(mode))
633 	 || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
634 		if (protocol_version < 28) {
635 			if (!(xflags & XMIT_SAME_RDEV_pre28))
636 				write_int(f, (int)rdev);
637 		} else {
638 			if (!(xflags & XMIT_SAME_RDEV_MAJOR))
639 				write_varint30(f, major(rdev));
640 			if (protocol_version >= 30)
641 				write_varint(f, minor(rdev));
642 			else if (xflags & XMIT_RDEV_MINOR_8_pre30)
643 				write_byte(f, minor(rdev));
644 			else
645 				write_int(f, minor(rdev));
646 		}
647 	}
648 
649 #ifdef SUPPORT_LINKS
650 	if (symlink_len) {
651 		write_varint30(f, symlink_len);
652 		write_buf(f, symlink_name, symlink_len);
653 	}
654 #endif
655 
656 #ifdef SUPPORT_HARD_LINKS
657 	if (tmp_dev != -1 && protocol_version < 30) {
658 		/* Older protocols expect the dev number to be transmitted
659 		 * 1-incremented so that it is never zero. */
660 		if (protocol_version < 26) {
661 			/* 32-bit dev_t and ino_t */
662 			write_int(f, (int32)(dev+1));
663 			write_int(f, (int32)tmp_ino);
664 		} else {
665 			/* 64-bit dev_t and ino_t */
666 			if (!(xflags & XMIT_SAME_DEV_pre30))
667 				write_longint(f, dev+1);
668 			write_longint(f, tmp_ino);
669 		}
670 	}
671 #endif
672 
673 	if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
674 		const char *sum;
675 		if (S_ISREG(mode))
676 			sum = tmp_sum;
677 		else {
678 			/* Prior to 28, we sent a useless set of nulls. */
679 			sum = empty_sum;
680 		}
681 		write_buf(f, sum, flist_csum_len);
682 	}
683 
684 #ifdef SUPPORT_HARD_LINKS
685   the_end:
686 #endif
687 	strlcpy(lastname, fname, MAXPATHLEN);
688 
689 	if (S_ISREG(mode) || S_ISLNK(mode))
690 		stats.total_size += F_LENGTH(file);
691 }
692 
recv_file_entry(int f,struct file_list * flist,int xflags)693 static struct file_struct *recv_file_entry(int f, struct file_list *flist, int xflags)
694 {
695 	static int64 modtime, atime;
696 #ifdef SUPPORT_CRTIMES
697 	static time_t crtime;
698 #endif
699 	static mode_t mode;
700 #ifdef SUPPORT_FILEFLAGS
701 	static uint32 fileflags;
702 #endif
703 #ifdef SUPPORT_HARD_LINKS
704 	static int64 dev;
705 #endif
706 	static dev_t rdev;
707 	static uint32 rdev_major;
708 	static uid_t uid;
709 	static gid_t gid;
710 	static uint16 gid_flags;
711 	static char lastname[MAXPATHLEN], *lastdir;
712 	static int lastdir_depth, lastdir_len = -1;
713 	static unsigned int del_hier_name_len = 0;
714 	static int in_del_hier = 0;
715 	char thisname[MAXPATHLEN];
716 	unsigned int l1 = 0, l2 = 0;
717 	int alloc_len, basename_len, linkname_len;
718 	int extra_len = file_extra_cnt * EXTRA_LEN;
719 	int first_hlink_ndx = -1;
720 	int64 file_length;
721 #ifdef CAN_SET_NSEC
722 	uint32 modtime_nsec;
723 #endif
724 	const char *basename;
725 	struct file_struct *file;
726 	alloc_pool_t *pool;
727 	char *bp;
728 
729 	if (xflags & XMIT_SAME_NAME)
730 		l1 = read_byte(f);
731 
732 	if (xflags & XMIT_LONG_NAME)
733 		l2 = read_varint30(f);
734 	else
735 		l2 = read_byte(f);
736 
737 	if (l2 >= MAXPATHLEN - l1) {
738 		rprintf(FERROR,
739 			"overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
740 			xflags, l1, l2, lastname, who_am_i());
741 		overflow_exit("recv_file_entry");
742 	}
743 
744 	strlcpy(thisname, lastname, l1 + 1);
745 	read_sbuf(f, &thisname[l1], l2);
746 	thisname[l1 + l2] = 0;
747 
748 	/* Abuse basename_len for a moment... */
749 	basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
750 
751 #ifdef ICONV_OPTION
752 	if (ic_recv != (iconv_t)-1) {
753 		xbuf outbuf, inbuf;
754 
755 		INIT_CONST_XBUF(outbuf, thisname);
756 		INIT_XBUF(inbuf, lastname, basename_len, (size_t)-1);
757 
758 		if (iconvbufs(ic_recv, &inbuf, &outbuf, ICB_INIT) < 0) {
759 			io_error |= IOERR_GENERAL;
760 			rprintf(FERROR_UTF8,
761 			    "[%s] cannot convert filename: %s (%s)\n",
762 			    who_am_i(), lastname, strerror(errno));
763 			outbuf.len = 0;
764 		}
765 		thisname[outbuf.len] = '\0';
766 	}
767 #endif
768 
769 	if (*thisname
770 	 && (clean_fname(thisname, CFN_REFUSE_DOT_DOT_DIRS) < 0 || (!relative_paths && *thisname == '/'))) {
771 		rprintf(FERROR, "ABORTING due to unsafe pathname from sender: %s\n", thisname);
772 		exit_cleanup(RERR_PROTOCOL);
773 	}
774 
775 	if (sanitize_paths)
776 		sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
777 
778 	if ((basename = strrchr(thisname, '/')) != NULL) {
779 		int len = basename++ - thisname;
780 		if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
781 			lastdir = new_array(char, len + 1);
782 			memcpy(lastdir, thisname, len);
783 			lastdir[len] = '\0';
784 			lastdir_len = len;
785 			lastdir_depth = count_dir_elements(lastdir);
786 		}
787 	} else
788 		basename = thisname;
789 	basename_len = strlen(basename) + 1; /* count the '\0' */
790 
791 #ifdef SUPPORT_HARD_LINKS
792 	if (protocol_version >= 30
793 	 && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
794 		first_hlink_ndx = read_varint(f);
795 		if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
796 			rprintf(FERROR,
797 				"hard-link reference out of range: %d (%d)\n",
798 				first_hlink_ndx, flist->ndx_start + flist->used);
799 			exit_cleanup(RERR_PROTOCOL);
800 		}
801 		if (DEBUG_GTE(HLINK, 1)) {
802 			rprintf(FINFO, "[%s] #%d hard-links #%d (%sabbrev)\n",
803 				who_am_i(), flist->used+flist->ndx_start, first_hlink_ndx,
804 				first_hlink_ndx >= flist->ndx_start ? "" : "un");
805 		}
806 		if (first_hlink_ndx >= flist->ndx_start) {
807 			struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
808 			file_length = F_LENGTH(first);
809 			modtime = first->modtime;
810 #ifdef CAN_SET_NSEC
811 			modtime_nsec = F_MOD_NSEC_or_0(first);
812 #endif
813 			mode = first->mode;
814 			if (atimes_ndx && !S_ISDIR(mode))
815 				atime = F_ATIME(first);
816 #ifdef SUPPORT_CRTIMES
817 			if (crtimes_ndx)
818 				crtime = F_CRTIME(first);
819 #endif
820 #ifdef SUPPORT_FILEFLAGS
821 			if (preserve_fileflags)
822 				fileflags = F_FFLAGS(first);
823 #endif
824 			if (preserve_uid)
825 				uid = F_OWNER(first);
826 			if (preserve_gid)
827 				gid = F_GROUP(first);
828 			if (preserve_devices && IS_DEVICE(mode)) {
829 				uint32 *devp = F_RDEV_P(first);
830 				rdev_major = DEV_MAJOR(devp);
831 				rdev = MAKEDEV(rdev_major, DEV_MINOR(devp));
832 				extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
833 			}
834 			if (preserve_links && S_ISLNK(mode))
835 				linkname_len = strlen(F_SYMLINK(first)) + 1;
836 			else
837 				linkname_len = 0;
838 			goto create_object;
839 		}
840 	}
841 #endif
842 
843 	file_length = read_varlong30(f, 3);
844 	if (!(xflags & XMIT_SAME_TIME)) {
845 		if (protocol_version >= 30) {
846 			modtime = read_varlong(f, 4);
847 #if SIZEOF_TIME_T < SIZEOF_INT64
848 			if (!am_generator && (int64)(time_t)modtime != modtime) {
849 				rprintf(FERROR_XFER,
850 				    "Time value of %s truncated on receiver.\n",
851 				    lastname);
852 			}
853 #endif
854 		} else
855 			modtime = read_int(f);
856 	}
857 	if (xflags & XMIT_MOD_NSEC)
858 #ifndef CAN_SET_NSEC
859 		(void)read_varint(f);
860 #else
861 		modtime_nsec = read_varint(f);
862 	else
863 		modtime_nsec = 0;
864 #endif
865 #ifdef SUPPORT_CRTIMES
866 	if (crtimes_ndx) {
867 		if (xflags & XMIT_CRTIME_EQ_MTIME)
868 			crtime = modtime;
869 		else
870 			crtime = read_varlong(f, 4);
871 #if SIZEOF_TIME_T < SIZEOF_INT64
872 		if (!am_generator && (int64)(time_t)crtime != crtime) {
873 			rprintf(FERROR_XFER,
874 				"Create time value of %s truncated on receiver.\n",
875 				lastname);
876 		}
877 #endif
878 	}
879 #endif
880 	if (!(xflags & XMIT_SAME_MODE))
881 		mode = from_wire_mode(read_int(f));
882 	if (atimes_ndx && !S_ISDIR(mode) && !(xflags & XMIT_SAME_ATIME)) {
883 		atime = read_varlong(f, 4);
884 #if SIZEOF_TIME_T < SIZEOF_INT64
885 		if (!am_generator && (int64)(time_t)atime != atime) {
886 			rprintf(FERROR_XFER,
887 				"Access time value of %s truncated on receiver.\n",
888 				lastname);
889 		}
890 #endif
891 	}
892 
893 	if (chmod_modes && !S_ISLNK(mode) && mode)
894 		mode = tweak_mode(mode, chmod_modes);
895 #ifdef SUPPORT_FILEFLAGS
896 	if (preserve_fileflags && !(xflags & XMIT_SAME_FLAGS))
897 		fileflags = (uint32)read_int(f);
898 #endif
899 
900 	if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
901 		if (protocol_version < 30)
902 			uid = (uid_t)read_int(f);
903 		else {
904 			uid = (uid_t)read_varint(f);
905 			if (xflags & XMIT_USER_NAME_FOLLOWS)
906 				uid = recv_user_name(f, uid);
907 			else if (inc_recurse && am_root && (!numeric_ids || usermap))
908 				uid = match_uid(uid);
909 		}
910 	}
911 	if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
912 		if (protocol_version < 30)
913 			gid = (gid_t)read_int(f);
914 		else {
915 			gid = (gid_t)read_varint(f);
916 			gid_flags = 0;
917 			if (xflags & XMIT_GROUP_NAME_FOLLOWS)
918 				gid = recv_group_name(f, gid, &gid_flags);
919 			else if (inc_recurse && (!am_root || !numeric_ids || groupmap))
920 				gid = match_gid(gid, &gid_flags);
921 		}
922 	}
923 
924 	if ((preserve_devices && IS_DEVICE(mode))
925 	 || (preserve_specials && IS_SPECIAL(mode) && protocol_version < 31)) {
926 		if (protocol_version < 28) {
927 			if (!(xflags & XMIT_SAME_RDEV_pre28))
928 				rdev = (dev_t)read_int(f);
929 		} else {
930 			uint32 rdev_minor;
931 			if (!(xflags & XMIT_SAME_RDEV_MAJOR))
932 				rdev_major = read_varint30(f);
933 			if (protocol_version >= 30)
934 				rdev_minor = read_varint(f);
935 			else if (xflags & XMIT_RDEV_MINOR_8_pre30)
936 				rdev_minor = read_byte(f);
937 			else
938 				rdev_minor = read_int(f);
939 			rdev = MAKEDEV(rdev_major, rdev_minor);
940 		}
941 		if (IS_DEVICE(mode))
942 			extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
943 		file_length = 0;
944 	} else if (protocol_version < 28)
945 		rdev = MAKEDEV(0, 0);
946 
947 #ifdef SUPPORT_LINKS
948 	if (preserve_links && S_ISLNK(mode)) {
949 		linkname_len = read_varint30(f) + 1; /* count the '\0' */
950 		if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
951 			rprintf(FERROR, "overflow: linkname_len=%d\n",
952 				linkname_len - 1);
953 			overflow_exit("recv_file_entry");
954 		}
955 #ifdef ICONV_OPTION
956 		/* We don't know how much extra room we need to convert
957 		 * the as-yet-unread symlink data, so let's hope that a
958 		 * double-size buffer is plenty. */
959 		if (sender_symlink_iconv)
960 			linkname_len *= 2;
961 #endif
962 		if (munge_symlinks)
963 			linkname_len += SYMLINK_PREFIX_LEN;
964 	}
965 	else
966 #endif
967 		linkname_len = 0;
968 
969 #ifdef SUPPORT_HARD_LINKS
970   create_object:
971 	if (preserve_hard_links) {
972 		if (protocol_version < 28 && S_ISREG(mode))
973 			xflags |= XMIT_HLINKED;
974 		if (xflags & XMIT_HLINKED)
975 			extra_len += (inc_recurse+1) * EXTRA_LEN;
976 	}
977 #endif
978 
979 #ifdef SUPPORT_ACLS
980 	/* Directories need an extra int32 for the default ACL. */
981 	if (preserve_acls && S_ISDIR(mode))
982 		extra_len += EXTRA_LEN;
983 #endif
984 
985 	if (always_checksum && S_ISREG(mode))
986 		extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
987 
988 #if SIZEOF_INT64 >= 8
989 	if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
990 		extra_len += EXTRA_LEN;
991 #endif
992 #ifdef CAN_SET_NSEC
993 	if (modtime_nsec)
994 		extra_len += EXTRA_LEN;
995 #endif
996 	if (file_length < 0) {
997 		rprintf(FERROR, "Offset underflow: file-length is negative\n");
998 		exit_cleanup(RERR_UNSUPPORTED);
999 	}
1000 
1001 	if (inc_recurse && S_ISDIR(mode)) {
1002 		if (one_file_system) {
1003 			/* Room to save the dir's device for -x */
1004 			extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
1005 		}
1006 		pool = dir_flist->file_pool;
1007 	} else
1008 		pool = flist->file_pool;
1009 
1010 #if EXTRA_ROUNDING > 0
1011 	if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1012 		extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1013 #endif
1014 
1015 	alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1016 		  + linkname_len;
1017 	bp = pool_alloc(pool, alloc_len, "recv_file_entry");
1018 
1019 	memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1020 	bp += extra_len;
1021 	file = (struct file_struct *)bp;
1022 	bp += FILE_STRUCT_LEN;
1023 
1024 	memcpy(bp, basename, basename_len);
1025 
1026 #ifdef SUPPORT_HARD_LINKS
1027 	if (xflags & XMIT_HLINKED
1028 #ifndef CAN_HARDLINK_SYMLINK
1029 	 && !S_ISLNK(mode)
1030 #endif
1031 #ifndef CAN_HARDLINK_SPECIAL
1032 	 && !IS_SPECIAL(mode) && !IS_DEVICE(mode)
1033 #endif
1034 	)
1035 		file->flags |= FLAG_HLINKED;
1036 #endif
1037 	file->modtime = (time_t)modtime;
1038 #ifdef CAN_SET_NSEC
1039 	if (modtime_nsec) {
1040 		file->flags |= FLAG_MOD_NSEC;
1041 		F_MOD_NSEC(file) = modtime_nsec;
1042 	}
1043 #endif
1044 	file->len32 = (uint32)file_length;
1045 #if SIZEOF_INT64 >= 8
1046 	if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
1047 #if SIZEOF_CAPITAL_OFF_T < 8
1048 		rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
1049 		exit_cleanup(RERR_UNSUPPORTED);
1050 #else
1051 		file->flags |= FLAG_LENGTH64;
1052 		F_HIGH_LEN(file) = (uint32)(file_length >> 32);
1053 #endif
1054 	}
1055 #endif
1056 	file->mode = mode;
1057 #ifdef SUPPORT_FILEFLAGS
1058 	if (preserve_fileflags)
1059 		F_FFLAGS(file) = fileflags;
1060 #endif
1061 	if (preserve_uid)
1062 		F_OWNER(file) = uid;
1063 	if (preserve_gid) {
1064 		F_GROUP(file) = gid;
1065 		file->flags |= gid_flags;
1066 	}
1067 	if (atimes_ndx && !S_ISDIR(mode))
1068 		F_ATIME(file) = atime;
1069 #ifdef SUPPORT_CRTIMES
1070 	if (crtimes_ndx)
1071 		F_CRTIME(file) = crtime;
1072 #endif
1073 	if (unsort_ndx)
1074 		F_NDX(file) = flist->used + flist->ndx_start;
1075 
1076 	if (basename != thisname) {
1077 		file->dirname = lastdir;
1078 		F_DEPTH(file) = lastdir_depth + 1;
1079 	} else
1080 		F_DEPTH(file) = 1;
1081 
1082 	if (S_ISDIR(mode)) {
1083 		if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
1084 			F_DEPTH(file)--;
1085 		if (protocol_version >= 30) {
1086 			if (!(xflags & XMIT_NO_CONTENT_DIR)) {
1087 				if (xflags & XMIT_TOP_DIR)
1088 					file->flags |= FLAG_TOP_DIR;
1089 				file->flags |= FLAG_CONTENT_DIR;
1090 			} else if (xflags & XMIT_TOP_DIR)
1091 				file->flags |= FLAG_IMPLIED_DIR;
1092 		} else if (xflags & XMIT_TOP_DIR) {
1093 			in_del_hier = recurse;
1094 			del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
1095 			if (relative_paths && del_hier_name_len > 2
1096 			    && lastname[del_hier_name_len-1] == '.'
1097 			    && lastname[del_hier_name_len-2] == '/')
1098 				del_hier_name_len -= 2;
1099 			file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
1100 		} else if (in_del_hier) {
1101 			if (!relative_paths || !del_hier_name_len
1102 			 || (l1 >= del_hier_name_len
1103 			  && lastname[del_hier_name_len] == '/'))
1104 				file->flags |= FLAG_CONTENT_DIR;
1105 			else
1106 				in_del_hier = 0;
1107 		}
1108 	}
1109 
1110 	if (preserve_devices && IS_DEVICE(mode)) {
1111 		uint32 *devp = F_RDEV_P(file);
1112 		DEV_MAJOR(devp) = major(rdev);
1113 		DEV_MINOR(devp) = minor(rdev);
1114 	}
1115 
1116 #ifdef SUPPORT_LINKS
1117 	if (linkname_len) {
1118 		bp += basename_len;
1119 		if (first_hlink_ndx >= flist->ndx_start) {
1120 			struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1121 			memcpy(bp, F_SYMLINK(first), linkname_len);
1122 		} else {
1123 			if (munge_symlinks) {
1124 				strlcpy(bp, SYMLINK_PREFIX, linkname_len);
1125 				bp += SYMLINK_PREFIX_LEN;
1126 				linkname_len -= SYMLINK_PREFIX_LEN;
1127 			}
1128 #ifdef ICONV_OPTION
1129 			if (sender_symlink_iconv) {
1130 				xbuf outbuf, inbuf;
1131 
1132 				alloc_len = linkname_len;
1133 				linkname_len /= 2;
1134 
1135 				/* Read the symlink data into the end of our double-sized
1136 				 * buffer and then convert it into the right spot. */
1137 				INIT_XBUF(inbuf, bp + alloc_len - linkname_len,
1138 					  linkname_len - 1, (size_t)-1);
1139 				read_sbuf(f, inbuf.buf, inbuf.len);
1140 				INIT_XBUF(outbuf, bp, 0, alloc_len);
1141 
1142 				if (iconvbufs(ic_recv, &inbuf, &outbuf, ICB_INIT) < 0) {
1143 					io_error |= IOERR_GENERAL;
1144 					rprintf(FERROR_XFER,
1145 					    "[%s] cannot convert symlink data for: %s (%s)\n",
1146 					    who_am_i(), full_fname(thisname), strerror(errno));
1147 					bp = (char*)file->basename;
1148 					*bp++ = '\0';
1149 					outbuf.len = 0;
1150 				}
1151 				bp[outbuf.len] = '\0';
1152 			} else
1153 #endif
1154 				read_sbuf(f, bp, linkname_len - 1);
1155 			if (sanitize_paths && !munge_symlinks && *bp)
1156 				sanitize_path(bp, bp, "", lastdir_depth, SP_DEFAULT);
1157 		}
1158 	}
1159 #endif
1160 
1161 #ifdef SUPPORT_HARD_LINKS
1162 	if (preserve_hard_links && xflags & XMIT_HLINKED) {
1163 		if (protocol_version >= 30) {
1164 			if (xflags & XMIT_HLINK_FIRST) {
1165 				F_HL_GNUM(file) = flist->ndx_start + flist->used;
1166 			} else
1167 				F_HL_GNUM(file) = first_hlink_ndx;
1168 		} else {
1169 			static int32 cnt = 0;
1170 			struct ht_int64_node *np;
1171 			int64 ino;
1172 			int32 ndx;
1173 			if (protocol_version < 26) {
1174 				dev = read_int(f);
1175 				ino = read_int(f);
1176 			} else {
1177 				if (!(xflags & XMIT_SAME_DEV_pre30))
1178 					dev = read_longint(f);
1179 				ino = read_longint(f);
1180 			}
1181 			np = idev_find(dev, ino);
1182 			ndx = (int32)(long)np->data; /* is -1 when new */
1183 			if (ndx < 0) {
1184 				np->data = (void*)(long)cnt;
1185 				ndx = cnt++;
1186 			}
1187 			F_HL_GNUM(file) = ndx;
1188 		}
1189 	}
1190 #endif
1191 
1192 	if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
1193 		if (S_ISREG(mode))
1194 			bp = F_SUM(file);
1195 		else {
1196 			/* Prior to 28, we get a useless set of nulls. */
1197 			bp = tmp_sum;
1198 		}
1199 		if (first_hlink_ndx >= flist->ndx_start) {
1200 			struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
1201 			memcpy(bp, F_SUM(first), flist_csum_len);
1202 		} else
1203 			read_buf(f, bp, flist_csum_len);
1204 	}
1205 
1206 #ifdef SUPPORT_ACLS
1207 	if (preserve_acls && !S_ISLNK(mode))
1208 		receive_acl(f, file);
1209 #endif
1210 #ifdef SUPPORT_XATTRS
1211 	if (preserve_xattrs)
1212 		receive_xattr(f, file);
1213 #endif
1214 
1215 	if (S_ISREG(mode) || S_ISLNK(mode))
1216 		stats.total_size += file_length;
1217 
1218 	return file;
1219 }
1220 
1221 /* Create a file_struct for a named file by reading its stat() information
1222  * and performing extensive checks against global options.
1223  *
1224  * Returns a pointer to the new file struct, or NULL if there was an error
1225  * or this file should be excluded.
1226  *
1227  * Note: Any error (here or in send_file_name) that results in the omission of
1228  * an existent source file from the file list should set
1229  * "io_error |= IOERR_GENERAL" to avoid deletion of the file from the
1230  * destination if --delete is on. */
make_file(const char * fname,struct file_list * flist,STRUCT_STAT * stp,int flags,int filter_level)1231 struct file_struct *make_file(const char *fname, struct file_list *flist,
1232 			      STRUCT_STAT *stp, int flags, int filter_level)
1233 {
1234 	static char *lastdir;
1235 	static int lastdir_len = -1;
1236 	struct file_struct *file;
1237 	char thisname[MAXPATHLEN];
1238 	char linkname[MAXPATHLEN];
1239 	int alloc_len, basename_len, linkname_len;
1240 	int extra_len = file_extra_cnt * EXTRA_LEN;
1241 	const char *basename;
1242 	alloc_pool_t *pool;
1243 	STRUCT_STAT st;
1244 	char *bp;
1245 
1246 	if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
1247 		io_error |= IOERR_GENERAL;
1248 		rprintf(FERROR_XFER, "skipping overly long name: %s\n", fname);
1249 		return NULL;
1250 	}
1251 	clean_fname(thisname, 0);
1252 	if (sanitize_paths)
1253 		sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
1254 
1255 	if (stp && (S_ISDIR(stp->st_mode) || IS_MISSING_FILE(*stp))) {
1256 		/* This is needed to handle a "symlink/." with a --relative
1257 		 * dir, or a request to delete a specific file. */
1258 		st = *stp;
1259 		*linkname = '\0'; /* make IBM code checker happy */
1260 	} else if (readlink_stat(thisname, &st, linkname) != 0) {
1261 		int save_errno = errno;
1262 		/* See if file is excluded before reporting an error. */
1263 		if (filter_level != NO_FILTERS
1264 		 && (is_excluded(thisname, 0, filter_level)
1265 		  || is_excluded(thisname, 1, filter_level))) {
1266 			if (ignore_perishable && save_errno != ENOENT)
1267 				non_perishable_cnt++;
1268 			return NULL;
1269 		}
1270 		if (save_errno == ENOENT) {
1271 #ifdef SUPPORT_LINKS
1272 			/* When our options tell us to follow a symlink that
1273 			 * points nowhere, tell the user about the symlink
1274 			 * instead of giving a "vanished" message.  We only
1275 			 * dereference a symlink if one of the --copy*links
1276 			 * options was specified, so there's no need for the
1277 			 * extra lstat() if one of these options isn't on. */
1278 			if ((copy_links || copy_unsafe_links || copy_dirlinks)
1279 			 && x_lstat(thisname, &st, NULL) == 0
1280 			 && S_ISLNK(st.st_mode)) {
1281 				io_error |= IOERR_GENERAL;
1282 				rprintf(FERROR_XFER, "symlink has no referent: %s\n",
1283 					full_fname(thisname));
1284 			} else
1285 #endif
1286 			{
1287 				enum logcode c = am_daemon && protocol_version < 28
1288 					       ? FERROR : FWARNING;
1289 				io_error |= IOERR_VANISHED;
1290 				rprintf(c, "file has vanished: %s\n",
1291 					full_fname(thisname));
1292 			}
1293 		} else {
1294 			io_error |= IOERR_GENERAL;
1295 			rsyserr(FERROR_XFER, save_errno, "readlink_stat(%s) failed",
1296 				full_fname(thisname));
1297 		}
1298 		return NULL;
1299 	} else if (IS_MISSING_FILE(st)) {
1300 		io_error |= IOERR_GENERAL;
1301 		rprintf(FINFO, "skipping file with bogus (zero) st_mode: %s\n",
1302 			full_fname(thisname));
1303 		return NULL;
1304 	}
1305 
1306 	if (filter_level == NO_FILTERS)
1307 		goto skip_filters;
1308 
1309 	if (S_ISDIR(st.st_mode)) {
1310 		if (!xfer_dirs) {
1311 			rprintf(FINFO, "skipping directory %s\n", thisname);
1312 			return NULL;
1313 		}
1314 		/* -x only affects dirs because we need to avoid recursing
1315 		 * into a mount-point directory, not to avoid copying a
1316 		 * symlinked file if -L (or similar) was specified. */
1317 		if (one_file_system && st.st_dev != filesystem_dev
1318 		 && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
1319 			if (one_file_system > 1) {
1320 				if (INFO_GTE(MOUNT, 1)) {
1321 					rprintf(FINFO,
1322 					    "[%s] skipping mount-point dir %s\n",
1323 					    who_am_i(), thisname);
1324 				}
1325 				return NULL;
1326 			}
1327 			flags |= FLAG_MOUNT_DIR;
1328 			flags &= ~FLAG_CONTENT_DIR;
1329 		}
1330 	} else
1331 		flags &= ~FLAG_CONTENT_DIR;
1332 
1333 	if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
1334 		if (ignore_perishable)
1335 			non_perishable_cnt++;
1336 		return NULL;
1337 	}
1338 
1339 	if (lp_ignore_nonreadable(module_id)) {
1340 #ifdef SUPPORT_LINKS
1341 		if (!S_ISLNK(st.st_mode))
1342 #endif
1343 			if (access(thisname, R_OK) != 0)
1344 				return NULL;
1345 	}
1346 
1347   skip_filters:
1348 
1349 	/* Only divert a directory in the main transfer. */
1350 	if (flist) {
1351 		if (flist->prev && S_ISDIR(st.st_mode)
1352 		 && flags & FLAG_DIVERT_DIRS) {
1353 			/* Room for parent/sibling/next-child info. */
1354 			extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
1355 			if (relative_paths)
1356 				extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
1357 			pool = dir_flist->file_pool;
1358 		} else
1359 			pool = flist->file_pool;
1360 	} else {
1361 #ifdef SUPPORT_ACLS
1362 		/* Directories need an extra int32 for the default ACL. */
1363 		if (preserve_acls && S_ISDIR(st.st_mode))
1364 			extra_len += EXTRA_LEN;
1365 #endif
1366 		pool = NULL;
1367 	}
1368 
1369 	if (DEBUG_GTE(FLIST, 2)) {
1370 		rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
1371 			who_am_i(), thisname, filter_level);
1372 	}
1373 
1374 	if ((basename = strrchr(thisname, '/')) != NULL) {
1375 		int len = basename++ - thisname;
1376 		if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
1377 			lastdir = new_array(char, len + 1);
1378 			memcpy(lastdir, thisname, len);
1379 			lastdir[len] = '\0';
1380 			lastdir_len = len;
1381 		}
1382 	} else
1383 		basename = thisname;
1384 	basename_len = strlen(basename) + 1; /* count the '\0' */
1385 
1386 #ifdef SUPPORT_LINKS
1387 	linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
1388 #else
1389 	linkname_len = 0;
1390 #endif
1391 
1392 #ifdef ST_MTIME_NSEC
1393 	if (st.ST_MTIME_NSEC && protocol_version >= 31)
1394 		extra_len += EXTRA_LEN;
1395 #endif
1396 #if SIZEOF_CAPITAL_OFF_T >= 8
1397 	if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
1398 		extra_len += EXTRA_LEN;
1399 #endif
1400 
1401 	if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
1402 		file_checksum(thisname, &st, tmp_sum);
1403 		if (sender_keeps_checksum)
1404 			extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
1405 	}
1406 
1407 #if EXTRA_ROUNDING > 0
1408 	if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
1409 		extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
1410 #endif
1411 
1412 	alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
1413 		  + linkname_len;
1414 	if (pool)
1415 		bp = pool_alloc(pool, alloc_len, "make_file");
1416 	else
1417 		bp = new_array(char, alloc_len);
1418 
1419 	memset(bp, 0, extra_len + FILE_STRUCT_LEN);
1420 	bp += extra_len;
1421 	file = (struct file_struct *)bp;
1422 	bp += FILE_STRUCT_LEN;
1423 
1424 	memcpy(bp, basename, basename_len);
1425 
1426 #ifdef SUPPORT_HARD_LINKS
1427 	if (preserve_hard_links && flist && flist->prev) {
1428 		if (protocol_version >= 28
1429 		 ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
1430 		 : S_ISREG(st.st_mode)) {
1431 			tmp_dev = (int64)st.st_dev;
1432 			tmp_ino = (int64)st.st_ino;
1433 		} else
1434 			tmp_dev = -1;
1435 	}
1436 #endif
1437 
1438 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1439 	if (IS_DEVICE(st.st_mode)) {
1440 		tmp_rdev = st.st_rdev;
1441 		st.st_size = 0;
1442 	} else if (IS_SPECIAL(st.st_mode))
1443 		st.st_size = 0;
1444 #endif
1445 
1446 	file->flags = flags;
1447 	file->modtime = st.st_mtime;
1448 #ifdef ST_MTIME_NSEC
1449 	if (st.ST_MTIME_NSEC && protocol_version >= 31) {
1450 		file->flags |= FLAG_MOD_NSEC;
1451 		F_MOD_NSEC(file) = st.ST_MTIME_NSEC;
1452 	}
1453 #endif
1454 	file->len32 = (uint32)st.st_size;
1455 #if SIZEOF_CAPITAL_OFF_T >= 8
1456 	if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
1457 		file->flags |= FLAG_LENGTH64;
1458 		F_HIGH_LEN(file) = (uint32)(st.st_size >> 32);
1459 	}
1460 #endif
1461 	file->mode = st.st_mode;
1462 #if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
1463 	if (fileflags_ndx)
1464 		F_FFLAGS(file) = st.st_flags;
1465 #endif
1466 	if (preserve_uid)
1467 		F_OWNER(file) = st.st_uid;
1468 	if (preserve_gid)
1469 		F_GROUP(file) = st.st_gid;
1470 	if (am_generator && st.st_uid == our_uid)
1471 		file->flags |= FLAG_OWNED_BY_US;
1472 	if (atimes_ndx && !S_ISDIR(file->mode))
1473 		F_ATIME(file) = st.st_atime;
1474 #ifdef SUPPORT_CRTIMES
1475 	if (crtimes_ndx)
1476 		F_CRTIME(file) = get_create_time(fname);
1477 #endif
1478 
1479 	if (basename != thisname)
1480 		file->dirname = lastdir;
1481 
1482 #ifdef SUPPORT_LINKS
1483 	if (linkname_len)
1484 		memcpy(bp + basename_len, linkname, linkname_len);
1485 #endif
1486 
1487 	if (am_sender)
1488 		F_PATHNAME(file) = pathname;
1489 	else if (!pool)
1490 		F_DEPTH(file) = extra_len / EXTRA_LEN;
1491 
1492 	if (basename_len == 0+1) {
1493 		if (!pool)
1494 			unmake_file(file);
1495 		return NULL;
1496 	}
1497 
1498 	if (sender_keeps_checksum && S_ISREG(st.st_mode))
1499 		memcpy(F_SUM(file), tmp_sum, flist_csum_len);
1500 
1501 	if (unsort_ndx)
1502 		F_NDX(file) = stats.num_dirs;
1503 
1504 	return file;
1505 }
1506 
get_device_size(int fd,const char * fname)1507 OFF_T get_device_size(int fd, const char *fname)
1508 {
1509 	OFF_T off = lseek(fd, 0, SEEK_END);
1510 
1511 	if (off == (OFF_T) -1) {
1512 		rsyserr(FERROR, errno, "failed to get device size via seek: %s", fname);
1513 		return 0;
1514 	}
1515 	if (lseek(fd, 0, SEEK_SET) != 0)
1516 		rsyserr(FERROR, errno, "failed to seek device back to start: %s", fname);
1517 
1518 	return off;
1519 }
1520 
1521 /* Only called for temporary file_struct entries created by make_file(). */
unmake_file(struct file_struct * file)1522 void unmake_file(struct file_struct *file)
1523 {
1524 	free(REQ_EXTRA(file, F_DEPTH(file)));
1525 }
1526 
send_file_name(int f,struct file_list * flist,const char * fname,STRUCT_STAT * stp,int flags,int filter_level)1527 static struct file_struct *send_file_name(int f, struct file_list *flist,
1528 					  const char *fname, STRUCT_STAT *stp,
1529 					  int flags, int filter_level)
1530 {
1531 	struct file_struct *file;
1532 
1533 	file = make_file(fname, flist, stp, flags, filter_level);
1534 	if (!file)
1535 		return NULL;
1536 
1537 	if (chmod_modes && !S_ISLNK(file->mode) && file->mode)
1538 		file->mode = tweak_mode(file->mode, chmod_modes);
1539 
1540 	if (f >= 0) {
1541 		char fbuf[MAXPATHLEN];
1542 #ifdef SUPPORT_LINKS
1543 		const char *symlink_name;
1544 		int symlink_len;
1545 #ifdef ICONV_OPTION
1546 		char symlink_buf[MAXPATHLEN];
1547 #endif
1548 #endif
1549 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
1550 		stat_x sx;
1551 		init_stat_x(&sx);
1552 #endif
1553 
1554 #ifdef SUPPORT_LINKS
1555 		if (preserve_links && S_ISLNK(file->mode)) {
1556 			symlink_name = F_SYMLINK(file);
1557 			symlink_len = strlen(symlink_name);
1558 			if (symlink_len == 0) {
1559 				io_error |= IOERR_GENERAL;
1560 				f_name(file, fbuf);
1561 				rprintf(FERROR_XFER,
1562 				    "skipping symlink with 0-length value: %s\n",
1563 				    full_fname(fbuf));
1564 				return NULL;
1565 			}
1566 		} else {
1567 			symlink_name = NULL;
1568 			symlink_len = 0;
1569 		}
1570 #endif
1571 
1572 #ifdef ICONV_OPTION
1573 		if (ic_send != (iconv_t)-1) {
1574 			xbuf outbuf, inbuf;
1575 
1576 			INIT_CONST_XBUF(outbuf, fbuf);
1577 
1578 			if (file->dirname) {
1579 				INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
1580 				outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
1581 				if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0)
1582 					goto convert_error;
1583 				outbuf.size += 2;
1584 				fbuf[outbuf.len++] = '/';
1585 			}
1586 
1587 			INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
1588 			if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0) {
1589 			  convert_error:
1590 				io_error |= IOERR_GENERAL;
1591 				rprintf(FERROR_XFER,
1592 				    "[%s] cannot convert filename: %s (%s)\n",
1593 				    who_am_i(), f_name(file, fbuf), strerror(errno));
1594 				return NULL;
1595 			}
1596 			fbuf[outbuf.len] = '\0';
1597 
1598 #ifdef SUPPORT_LINKS
1599 			if (symlink_len && sender_symlink_iconv) {
1600 				INIT_XBUF(inbuf, (char*)symlink_name, symlink_len, (size_t)-1);
1601 				INIT_CONST_XBUF(outbuf, symlink_buf);
1602 				if (iconvbufs(ic_send, &inbuf, &outbuf, ICB_INIT) < 0) {
1603 					io_error |= IOERR_GENERAL;
1604 					f_name(file, fbuf);
1605 					rprintf(FERROR_XFER,
1606 					    "[%s] cannot convert symlink data for: %s (%s)\n",
1607 					    who_am_i(), full_fname(fbuf), strerror(errno));
1608 					return NULL;
1609 				}
1610 				symlink_buf[outbuf.len] = '\0';
1611 
1612 				symlink_name = symlink_buf;
1613 				symlink_len = outbuf.len;
1614 			}
1615 #endif
1616 		} else
1617 #endif
1618 			f_name(file, fbuf);
1619 
1620 #ifdef SUPPORT_ACLS
1621 		if (preserve_acls && !S_ISLNK(file->mode)) {
1622 			sx.st.st_mode = file->mode;
1623 			if (get_acl(fname, &sx) < 0) {
1624 				io_error |= IOERR_GENERAL;
1625 				return NULL;
1626 			}
1627 		}
1628 #endif
1629 #ifdef SUPPORT_XATTRS
1630 		if (preserve_xattrs) {
1631 			sx.st.st_mode = file->mode;
1632 			if (get_xattr(fname, &sx) < 0) {
1633 				io_error |= IOERR_GENERAL;
1634 				return NULL;
1635 			}
1636 		}
1637 #endif
1638 
1639 		send_file_entry(f, fbuf, file,
1640 #ifdef SUPPORT_LINKS
1641 				symlink_name, symlink_len,
1642 #endif
1643 				flist->used, flist->ndx_start);
1644 
1645 #ifdef SUPPORT_ACLS
1646 		if (preserve_acls && !S_ISLNK(file->mode)) {
1647 			send_acl(f, &sx);
1648 			free_acl(&sx);
1649 		}
1650 #endif
1651 #ifdef SUPPORT_XATTRS
1652 		if (preserve_xattrs) {
1653 			F_XATTR(file) = send_xattr(f, &sx);
1654 			free_xattr(&sx);
1655 		}
1656 #endif
1657 	}
1658 
1659 	maybe_emit_filelist_progress(flist->used + flist_count_offset);
1660 
1661 	flist_expand(flist, 1);
1662 	flist->files[flist->used++] = file;
1663 
1664 	return file;
1665 }
1666 
send_if_directory(int f,struct file_list * flist,struct file_struct * file,char * fbuf,unsigned int ol,int flags)1667 static void send_if_directory(int f, struct file_list *flist,
1668 			      struct file_struct *file,
1669 			      char *fbuf, unsigned int ol,
1670 			      int flags)
1671 {
1672 	char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
1673 
1674 	if (S_ISDIR(file->mode)
1675 	    && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
1676 		void *save_filters;
1677 		unsigned int len = strlen(fbuf);
1678 		if (len > 1 && fbuf[len-1] == '/')
1679 			fbuf[--len] = '\0';
1680 		save_filters = push_local_filters(fbuf, len);
1681 		send_directory(f, flist, fbuf, len, flags);
1682 		pop_local_filters(save_filters);
1683 		fbuf[ol] = '\0';
1684 		if (is_dot_dir)
1685 			fbuf[ol-1] = '.';
1686 	}
1687 }
1688 
file_compare(const void * file1,const void * file2)1689 static int file_compare(const void *file1, const void *file2)
1690 {
1691 	return f_name_cmp(*(struct file_struct **)file1,
1692 			  *(struct file_struct **)file2);
1693 }
1694 
1695 /* The guts of a merge-sort algorithm.  This was derived from the glibc
1696  * version, but I (Wayne) changed the merge code to do less copying and
1697  * to require only half the amount of temporary memory. */
fsort_tmp(struct file_struct ** fp,size_t num,struct file_struct ** tmp)1698 static void fsort_tmp(struct file_struct **fp, size_t num,
1699 		      struct file_struct **tmp)
1700 {
1701 	struct file_struct **f1, **f2, **t;
1702 	size_t n1, n2;
1703 
1704 	n1 = num / 2;
1705 	n2 = num - n1;
1706 	f1 = fp;
1707 	f2 = fp + n1;
1708 
1709 	if (n1 > 1)
1710 		fsort_tmp(f1, n1, tmp);
1711 	if (n2 > 1)
1712 		fsort_tmp(f2, n2, tmp);
1713 
1714 	while (f_name_cmp(*f1, *f2) <= 0) {
1715 		if (!--n1)
1716 			return;
1717 		f1++;
1718 	}
1719 
1720 	t = tmp;
1721 	memcpy(t, f1, n1 * PTR_SIZE);
1722 
1723 	*f1++ = *f2++, n2--;
1724 
1725 	while (n1 > 0 && n2 > 0) {
1726 		if (f_name_cmp(*t, *f2) <= 0)
1727 			*f1++ = *t++, n1--;
1728 		else
1729 			*f1++ = *f2++, n2--;
1730 	}
1731 
1732 	if (n1 > 0)
1733 		memcpy(f1, t, n1 * PTR_SIZE);
1734 }
1735 
1736 /* This file-struct sorting routine makes sure that any identical names in
1737  * the file list stay in the same order as they were in the original list.
1738  * This is particularly vital in inc_recurse mode where we expect a sort
1739  * on the flist to match the exact order of a sort on the dir_flist. */
fsort(struct file_struct ** fp,size_t num)1740 static void fsort(struct file_struct **fp, size_t num)
1741 {
1742 	if (num <= 1)
1743 		return;
1744 
1745 	if (use_qsort)
1746 		qsort(fp, num, PTR_SIZE, file_compare);
1747 	else {
1748 		struct file_struct **tmp = new_array(struct file_struct *, (num+1) / 2);
1749 		fsort_tmp(fp, num, tmp);
1750 		free(tmp);
1751 	}
1752 }
1753 
1754 /* We take an entire set of sibling dirs from the sorted flist and link them
1755  * into the tree, setting the appropriate parent/child/sibling pointers. */
add_dirs_to_tree(int parent_ndx,struct file_list * from_flist,int dir_cnt)1756 static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
1757 			     int dir_cnt)
1758 {
1759 	int i;
1760 	int32 *dp = NULL;
1761 	int32 *parent_dp = parent_ndx < 0 ? NULL
1762 			 : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
1763 
1764 	/* The sending side is adding entries to dir_flist in sorted order, so sorted & files are the same. */
1765 	flist_expand(dir_flist, dir_cnt);
1766 	dir_flist->sorted = dir_flist->files;
1767 
1768 	for (i = 0; dir_cnt; i++) {
1769 		struct file_struct *file = from_flist->sorted[i];
1770 
1771 		if (!S_ISDIR(file->mode))
1772 			continue;
1773 
1774 		dir_flist->files[dir_flist->used++] = file;
1775 		dir_cnt--;
1776 
1777 		if (file->basename[0] == '.' && file->basename[1] == '\0')
1778 			continue;
1779 
1780 		if (dp)
1781 			DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
1782 		else if (parent_dp)
1783 			DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
1784 		else
1785 			send_dir_ndx = dir_flist->used - 1;
1786 
1787 		dp = F_DIR_NODE_P(file);
1788 		DIR_PARENT(dp) = parent_ndx;
1789 		DIR_FIRST_CHILD(dp) = -1;
1790 	}
1791 	if (dp)
1792 		DIR_NEXT_SIBLING(dp) = -1;
1793 }
1794 
interpret_stat_error(const char * fname,int is_dir)1795 static void interpret_stat_error(const char *fname, int is_dir)
1796 {
1797 	if (errno == ENOENT) {
1798 		io_error |= IOERR_VANISHED;
1799 		rprintf(FWARNING, "%s has vanished: %s\n",
1800 			is_dir ? "directory" : "file", full_fname(fname));
1801 	} else {
1802 		io_error |= IOERR_GENERAL;
1803 		rsyserr(FERROR_XFER, errno, "link_stat %s failed",
1804 			full_fname(fname));
1805 	}
1806 }
1807 
1808 /* This function is normally called by the sender, but the receiving side also
1809  * calls it from get_dirlist() with f set to -1 so that we just construct the
1810  * file list in memory without sending it over the wire.  Also, get_dirlist()
1811  * might call this with f set to -2, which also indicates that local filter
1812  * rules should be ignored. */
send_directory(int f,struct file_list * flist,char * fbuf,int len,int flags)1813 static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
1814 			   int flags)
1815 {
1816 	struct dirent *di;
1817 	unsigned remainder;
1818 	char *p;
1819 	DIR *d;
1820 	int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
1821 	int start = flist->used;
1822 	int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
1823 
1824 	assert(flist != NULL);
1825 
1826 	if (!(d = opendir(fbuf))) {
1827 		if (errno == ENOENT) {
1828 			if (am_sender) /* Can abuse this for vanished error w/ENOENT: */
1829 				interpret_stat_error(fbuf, True);
1830 			return;
1831 		}
1832 		if (errno == ENOTDIR && (flags & FLAG_PERHAPS_DIR))
1833 			return;
1834 		io_error |= IOERR_GENERAL;
1835 		rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
1836 		return;
1837 	}
1838 
1839 	p = fbuf + len;
1840 	if (len == 1 && *fbuf == '/')
1841 		remainder = MAXPATHLEN - 1;
1842 	else if (len < MAXPATHLEN-1) {
1843 		*p++ = '/';
1844 		*p = '\0';
1845 		remainder = MAXPATHLEN - (len + 1);
1846 	} else
1847 		remainder = 0;
1848 
1849 	for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
1850 		unsigned name_len;
1851 		char *dname = d_name(di);
1852 		if (dname[0] == '.' && (dname[1] == '\0'
1853 		    || (dname[1] == '.' && dname[2] == '\0')))
1854 			continue;
1855 		name_len = strlcpy(p, dname, remainder);
1856 		if (name_len >= remainder) {
1857 			char save = fbuf[len];
1858 			fbuf[len] = '\0';
1859 			io_error |= IOERR_GENERAL;
1860 			rprintf(FERROR_XFER,
1861 				"filename overflows max-path len by %u: %s/%s\n",
1862 				name_len - remainder + 1, fbuf, dname);
1863 			fbuf[len] = save;
1864 			continue;
1865 		}
1866 		if (dname[0] == '\0') {
1867 			io_error |= IOERR_GENERAL;
1868 			rprintf(FERROR_XFER,
1869 				"cannot send file with empty name in %s\n",
1870 				full_fname(fbuf));
1871 			continue;
1872 		}
1873 
1874 		send_file_name(f, flist, fbuf, NULL, flags, filter_level);
1875 	}
1876 
1877 	fbuf[len] = '\0';
1878 
1879 	if (errno) {
1880 		io_error |= IOERR_GENERAL;
1881 		rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
1882 	}
1883 
1884 	closedir(d);
1885 
1886 	if (f >= 0 && recurse && !divert_dirs) {
1887 		int i, end = flist->used - 1;
1888 		/* send_if_directory() bumps flist->used, so use "end". */
1889 		for (i = start; i <= end; i++)
1890 			send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
1891 	}
1892 }
1893 
send_implied_dirs(int f,struct file_list * flist,char * fname,char * start,char * limit,int flags,char name_type)1894 static void send_implied_dirs(int f, struct file_list *flist, char *fname,
1895 			      char *start, char *limit, int flags, char name_type)
1896 {
1897 	static char lastpath[MAXPATHLEN] = "";
1898 	static int lastpath_len = 0;
1899 	static struct file_struct *lastpath_struct = NULL;
1900 	struct file_struct *file;
1901 	item_list *relname_list;
1902 	relnamecache **rnpp;
1903 	int len, need_new_dir, depth = 0;
1904 	filter_rule_list save_filter_list = filter_list;
1905 
1906 	flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
1907 	filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */
1908 
1909 	if (inc_recurse) {
1910 		if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
1911 		 && lastpath_len == limit - fname
1912 		 && strncmp(lastpath, fname, lastpath_len) == 0)
1913 			need_new_dir = 0;
1914 		else
1915 			need_new_dir = 1;
1916 	} else {
1917 		char *tp = fname, *lp = lastpath;
1918 		/* Skip any initial directories in our path that we
1919 		 * have in common with lastpath. */
1920 		assert(start == fname);
1921 		for ( ; ; tp++, lp++) {
1922 			if (tp == limit) {
1923 				if (*lp == '/' || *lp == '\0')
1924 					goto done;
1925 				break;
1926 			}
1927 			if (*lp != *tp)
1928 				break;
1929 			if (*tp == '/') {
1930 				start = tp;
1931 				depth++;
1932 			}
1933 		}
1934 		need_new_dir = 1;
1935 	}
1936 
1937 	if (need_new_dir) {
1938 		int save_copy_links = copy_links;
1939 		int save_xfer_dirs = xfer_dirs;
1940 		char *slash;
1941 
1942 		copy_links = xfer_dirs = 1;
1943 
1944 		*limit = '\0';
1945 
1946 		for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
1947 			*slash = '\0';
1948 			file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1949 			depth++;
1950 			if (!inc_recurse && file && S_ISDIR(file->mode))
1951 				change_local_filter_dir(fname, strlen(fname), depth);
1952 			*slash = '/';
1953 		}
1954 
1955 		file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
1956 		if (inc_recurse) {
1957 			if (file && !S_ISDIR(file->mode))
1958 				file = NULL;
1959 			lastpath_struct = file;
1960 		} else if (file && S_ISDIR(file->mode))
1961 			change_local_filter_dir(fname, strlen(fname), ++depth);
1962 
1963 		strlcpy(lastpath, fname, sizeof lastpath);
1964 		lastpath_len = limit - fname;
1965 
1966 		*limit = '/';
1967 
1968 		copy_links = save_copy_links;
1969 		xfer_dirs = save_xfer_dirs;
1970 
1971 		if (!inc_recurse)
1972 			goto done;
1973 	}
1974 
1975 	if (!lastpath_struct)
1976 		goto done; /* dir must have vanished */
1977 
1978 	len = strlen(limit+1);
1979 	memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
1980 	if (!relname_list) {
1981 		relname_list = new0(item_list);
1982 		memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
1983 	}
1984 	rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
1985 	*rnpp = (relnamecache*)new_array(char, RELNAMECACHE_LEN + len + 1);
1986 	(*rnpp)->name_type = name_type;
1987 	strlcpy((*rnpp)->fname, limit+1, len + 1);
1988 
1989 done:
1990 	filter_list = save_filter_list;
1991 }
1992 
fatal_unsafe_io_error(void)1993 static NORETURN void fatal_unsafe_io_error(void)
1994 {
1995 	/* This (sadly) can only happen when pushing data because
1996 	 * the sender does not know about what kind of delete
1997 	 * is in effect on the receiving side when pulling. */
1998 	rprintf(FERROR_XFER, "FATAL I/O ERROR: dying to avoid a --delete-%s issue with a pre-3.0.7 receiver.\n",
1999 		delete_during == 2 ? "delay" : "during");
2000 	exit_cleanup(RERR_UNSUPPORTED);
2001 }
2002 
send1extra(int f,struct file_struct * file,struct file_list * flist)2003 static void send1extra(int f, struct file_struct *file, struct file_list *flist)
2004 {
2005 	char fbuf[MAXPATHLEN];
2006 	item_list *relname_list;
2007 	int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
2008 	size_t j;
2009 
2010 	f_name(file, fbuf);
2011 	dlen = strlen(fbuf);
2012 
2013 	if (!change_pathname(file, NULL, 0))
2014 		exit_cleanup(RERR_FILESELECT);
2015 
2016 	change_local_filter_dir(fbuf, dlen, send_dir_depth);
2017 
2018 	if (file->flags & FLAG_CONTENT_DIR) {
2019 		if (one_file_system) {
2020 			STRUCT_STAT st;
2021 			if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
2022 				interpret_stat_error(fbuf, True);
2023 				return;
2024 			}
2025 			filesystem_dev = st.st_dev;
2026 		}
2027 		send_directory(f, flist, fbuf, dlen, flags);
2028 	}
2029 
2030 	if (!relative_paths)
2031 		return;
2032 
2033 	memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
2034 	if (!relname_list)
2035 		return;
2036 
2037 	for (j = 0; j < relname_list->count; j++) {
2038 		char *slash;
2039 		relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
2040 		char name_type = rnp->name_type;
2041 
2042 		fbuf[dlen] = '/';
2043 		len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
2044 		free(rnp);
2045 		if (len >= (int)sizeof fbuf)
2046 			continue; /* Impossible... */
2047 
2048 		slash = strchr(fbuf+dlen+1, '/');
2049 		if (slash) {
2050 			send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
2051 			continue;
2052 		}
2053 
2054 		if (name_type != NORMAL_NAME) {
2055 			STRUCT_STAT st;
2056 			if (name_type == MISSING_NAME)
2057 				memset(&st, 0, sizeof st);
2058 			else if (link_stat(fbuf, &st, 1) != 0) {
2059 				interpret_stat_error(fbuf, True);
2060 				continue;
2061 			}
2062 			send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
2063 		} else
2064 			send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
2065 	}
2066 
2067 	free(relname_list);
2068 }
2069 
write_end_of_flist(int f,int send_io_error)2070 static void write_end_of_flist(int f, int send_io_error)
2071 {
2072 	if (xfer_flags_as_varint) {
2073 		write_varint(f, 0);
2074 		write_varint(f, send_io_error ? io_error : 0);
2075 	} else if (send_io_error) {
2076 		write_shortint(f, XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST);
2077 		write_varint(f, io_error);
2078 	} else
2079 		write_byte(f, 0);
2080 }
2081 
send_extra_file_list(int f,int at_least)2082 void send_extra_file_list(int f, int at_least)
2083 {
2084 	struct file_list *flist;
2085 	int64 start_write;
2086 	uint16 prev_flags;
2087 	int save_io_error = io_error;
2088 
2089 	if (flist_eof)
2090 		return;
2091 
2092 	if (at_least < 0)
2093 		at_least = file_total - file_old_total + 1;
2094 
2095 	/* Keep sending data until we have the requested number of
2096 	 * files in the upcoming file-lists. */
2097 	while (file_total - file_old_total < at_least) {
2098 		struct file_struct *file = dir_flist->sorted[send_dir_ndx];
2099 		int dir_ndx, dstart = stats.num_dirs;
2100 		const char *pathname = F_PATHNAME(file);
2101 		int32 *dp;
2102 
2103 		flist = flist_new(0, "send_extra_file_list");
2104 		start_write = stats.total_written;
2105 
2106 		if (unsort_ndx)
2107 			dir_ndx = F_NDX(file);
2108 		else
2109 			dir_ndx = send_dir_ndx;
2110 		write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
2111 		flist->parent_ndx = send_dir_ndx; /* the sending side must remember the sorted ndx value */
2112 
2113 		send1extra(f, file, flist);
2114 		prev_flags = file->flags;
2115 		dp = F_DIR_NODE_P(file);
2116 
2117 		/* If there are any duplicate directory names that follow, we
2118 		 * send all the dirs together in one file-list.  The dir_flist
2119 		 * tree links all the child subdirs onto the last dup dir. */
2120 		while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
2121 		    && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
2122 			send_dir_ndx = dir_ndx;
2123 			file = dir_flist->sorted[dir_ndx];
2124 			/* Try to avoid some duplicate scanning of identical dirs. */
2125 			if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
2126 				file->flags &= ~FLAG_CONTENT_DIR;
2127 			send1extra(f, file, flist);
2128 			prev_flags = file->flags;
2129 			dp = F_DIR_NODE_P(file);
2130 		}
2131 
2132 		if (io_error == save_io_error || ignore_errors)
2133 			write_end_of_flist(f, 0);
2134 		else if (use_safe_inc_flist)
2135 			write_end_of_flist(f, 1);
2136 		else {
2137 			if (delete_during)
2138 				fatal_unsafe_io_error();
2139 			write_end_of_flist(f, 0);
2140 		}
2141 
2142 		if (need_unsorted_flist) {
2143 			flist->sorted = new_array(struct file_struct *, flist->used);
2144 			memcpy(flist->sorted, flist->files, flist->used * PTR_SIZE);
2145 		} else
2146 			flist->sorted = flist->files;
2147 
2148 		flist_sort_and_clean(flist, 0);
2149 
2150 		add_dirs_to_tree(send_dir_ndx, flist, stats.num_dirs - dstart);
2151 		flist_done_allocating(flist);
2152 
2153 		file_total += flist->used;
2154 		stats.flist_size += stats.total_written - start_write;
2155 		stats.num_files += flist->used;
2156 		if (DEBUG_GTE(FLIST, 3))
2157 			output_flist(flist);
2158 
2159 		if (DIR_FIRST_CHILD(dp) >= 0) {
2160 			send_dir_ndx = DIR_FIRST_CHILD(dp);
2161 			send_dir_depth++;
2162 		} else {
2163 			while (DIR_NEXT_SIBLING(dp) < 0) {
2164 				if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
2165 					write_ndx(f, NDX_FLIST_EOF);
2166 					flist_eof = 1;
2167 					if (DEBUG_GTE(FLIST, 3))
2168 						rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2169 					change_local_filter_dir(NULL, 0, 0);
2170 					goto finish;
2171 				}
2172 				send_dir_depth--;
2173 				file = dir_flist->sorted[send_dir_ndx];
2174 				dp = F_DIR_NODE_P(file);
2175 			}
2176 			send_dir_ndx = DIR_NEXT_SIBLING(dp);
2177 		}
2178 	}
2179 
2180   finish:
2181 	if (io_error != save_io_error && protocol_version == 30 && !ignore_errors)
2182 		send_msg_int(MSG_IO_ERROR, io_error);
2183 }
2184 
send_file_list(int f,int argc,char * argv[])2185 struct file_list *send_file_list(int f, int argc, char *argv[])
2186 {
2187 	static const char *lastdir;
2188 	static int lastdir_len = -1;
2189 	int len, dirlen;
2190 	STRUCT_STAT st;
2191 	char *p, *dir;
2192 	struct file_list *flist;
2193 	struct timeval start_tv, end_tv;
2194 	int64 start_write;
2195 	int use_ff_fd = 0;
2196 	int disable_buffering, reenable_multiplex = -1;
2197 	int flags = recurse ? FLAG_CONTENT_DIR : 0;
2198 	int reading_remotely = filesfrom_host != NULL;
2199 	int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
2200 #ifdef ICONV_OPTION
2201 		     | (filesfrom_convert ? RL_CONVERT : 0)
2202 #endif
2203 		     | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
2204 	int implied_dot_dir = 0;
2205 
2206 	rprintf(FLOG, "building file list\n");
2207 	if (show_filelist_progress)
2208 		start_filelist_progress("building file list");
2209 	else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2210 		rprintf(FCLIENT, "sending incremental file list\n");
2211 
2212 	start_write = stats.total_written;
2213 	gettimeofday(&start_tv, NULL);
2214 
2215 	if (relative_paths && protocol_version >= 30)
2216 		implied_dirs = 1; /* We send flagged implied dirs */
2217 
2218 #ifdef SUPPORT_HARD_LINKS
2219 	if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
2220 		init_hard_links();
2221 #endif
2222 
2223 	flist = cur_flist = flist_new(0, "send_file_list");
2224 	if (inc_recurse) {
2225 		dir_flist = flist_new(FLIST_TEMP, "send_file_list");
2226 		flags |= FLAG_DIVERT_DIRS;
2227 	} else
2228 		dir_flist = cur_flist;
2229 
2230 	disable_buffering = io_start_buffering_out(f);
2231 	if (filesfrom_fd >= 0) {
2232 		if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
2233 			rsyserr(FERROR_XFER, errno, "change_dir %s failed",
2234 				full_fname(argv[0]));
2235 			exit_cleanup(RERR_FILESELECT);
2236 		}
2237 		if (protocol_version < 31) {
2238 			/* Older protocols send the files-from data w/o packaging
2239 			 * it in multiplexed I/O packets, so temporarily switch
2240 			 * to buffered I/O to match this behavior. */
2241 			reenable_multiplex = io_end_multiplex_in(MPLX_TO_BUFFERED);
2242 		}
2243 		use_ff_fd = 1;
2244 	}
2245 
2246 	if (!orig_dir)
2247 		orig_dir = strdup(curr_dir);
2248 
2249 	while (1) {
2250 		char fbuf[MAXPATHLEN], *fn, name_type;
2251 
2252 		if (use_ff_fd) {
2253 			if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
2254 				break;
2255 			sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2256 		} else {
2257 			if (argc-- == 0)
2258 				break;
2259 			strlcpy(fbuf, *argv++, MAXPATHLEN);
2260 			if (sanitize_paths)
2261 				sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
2262 		}
2263 
2264 		len = strlen(fbuf);
2265 		if (relative_paths) {
2266 			/* We clean up fbuf below. */
2267 			name_type = NORMAL_NAME;
2268 		} else if (!len || fbuf[len - 1] == '/') {
2269 			if (len == 2 && fbuf[0] == '.') {
2270 				/* Turn "./" into just "." rather than "./." */
2271 				fbuf[--len] = '\0';
2272 			} else {
2273 				if (len + 1 >= MAXPATHLEN)
2274 					overflow_exit("send_file_list");
2275 				fbuf[len++] = '.';
2276 				fbuf[len] = '\0';
2277 			}
2278 			name_type = DOTDIR_NAME;
2279 		} else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
2280 		    && (len == 2 || fbuf[len-3] == '/')) {
2281 			if (len + 2 >= MAXPATHLEN)
2282 				overflow_exit("send_file_list");
2283 			fbuf[len++] = '/';
2284 			fbuf[len++] = '.';
2285 			fbuf[len] = '\0';
2286 			name_type = DOTDIR_NAME;
2287 		} else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
2288 			name_type = DOTDIR_NAME;
2289 		else
2290 			name_type = NORMAL_NAME;
2291 
2292 		dir = NULL;
2293 
2294 		if (!relative_paths) {
2295 			p = strrchr(fbuf, '/');
2296 			if (p) {
2297 				*p = '\0';
2298 				if (p == fbuf)
2299 					dir = "/";
2300 				else
2301 					dir = fbuf;
2302 				len -= p - fbuf + 1;
2303 				fn = p + 1;
2304 			} else
2305 				fn = fbuf;
2306 		} else {
2307 			if ((p = strstr(fbuf, "/./")) != NULL) {
2308 				*p = '\0';
2309 				if (p == fbuf)
2310 					dir = "/";
2311 				else {
2312 					dir = fbuf;
2313 					clean_fname(dir, 0);
2314 				}
2315 				fn = p + 3;
2316 				while (*fn == '/')
2317 					fn++;
2318 				if (!*fn)
2319 					*--fn = '\0'; /* ensure room for '.' */
2320 			} else
2321 				fn = fbuf;
2322 			/* A leading ./ can be used in relative mode to affect
2323 			 * the dest dir without its name being in the path. */
2324 			if (*fn == '.' && fn[1] == '/' && fn[2] && !implied_dot_dir)
2325 				implied_dot_dir = -1;
2326 			len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH
2327 					    | CFN_DROP_TRAILING_DOT_DIR);
2328 			if (len == 1) {
2329 				if (fn[0] == '/') {
2330 					fn = "/.";
2331 					len = 2;
2332 					name_type = DOTDIR_NAME;
2333 				} else if (fn[0] == '.')
2334 					name_type = DOTDIR_NAME;
2335 			} else if (fn[len-1] == '/') {
2336 				fn[--len] = '\0';
2337 				if (len == 1 && *fn == '.')
2338 					name_type = DOTDIR_NAME;
2339 				else
2340 					name_type = SLASH_ENDING_NAME;
2341 			}
2342 			/* Reject a ".." dir in the active part of the path. */
2343 			for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
2344 				if ((p[2] == '/' || p[2] == '\0')
2345 				 && (p == fn || p[-1] == '/')) {
2346 					rprintf(FERROR,
2347 					    "found \"..\" dir in relative path: %s\n",
2348 					    fn);
2349 					exit_cleanup(RERR_SYNTAX);
2350 				}
2351 			}
2352 		}
2353 
2354 		if (!*fn) {
2355 			len = 1;
2356 			fn = ".";
2357 			name_type = DOTDIR_NAME;
2358 		}
2359 
2360 		dirlen = dir ? strlen(dir) : 0;
2361 		if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
2362 			if (!change_pathname(NULL, dir, -dirlen))
2363 				goto bad_path;
2364 			lastdir = pathname;
2365 			lastdir_len = pathname_len;
2366 		} else if (!change_pathname(NULL, lastdir, lastdir_len)) {
2367 		    bad_path:
2368 			if (implied_dot_dir < 0)
2369 				implied_dot_dir = 0;
2370 			continue;
2371 		}
2372 
2373 		if (implied_dot_dir < 0) {
2374 			implied_dot_dir = 1;
2375 			send_file_name(f, flist, ".", NULL, (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR, ALL_FILTERS);
2376 		}
2377 
2378 		if (fn != fbuf)
2379 			memmove(fbuf, fn, len + 1);
2380 
2381 		if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
2382 		 || (name_type != DOTDIR_NAME && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, SERVER_FILTERS))
2383 		 || (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
2384 			if (errno != ENOENT || missing_args == 0) {
2385 				/* This is a transfer error, but inhibit deletion
2386 				 * only if we might be omitting an existing file. */
2387 				if (errno != ENOENT)
2388 					io_error |= IOERR_GENERAL;
2389 				rsyserr(FERROR_XFER, errno, "link_stat %s failed",
2390 					full_fname(fbuf));
2391 				continue;
2392 			} else if (missing_args == 1) {
2393 				/* Just ignore the arg. */
2394 				continue;
2395 			} else /* (missing_args == 2) */ {
2396 				/* Send the arg as a "missing" entry with
2397 				 * mode 0, which tells the generator to delete it. */
2398 				memset(&st, 0, sizeof st);
2399 			}
2400 		}
2401 
2402 		/* A dot-dir should not be excluded! */
2403 		if (name_type != DOTDIR_NAME && st.st_mode != 0
2404 		 && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
2405 			continue;
2406 
2407 		if (S_ISDIR(st.st_mode) && !xfer_dirs) {
2408 			rprintf(FINFO, "skipping directory %s\n", fbuf);
2409 			continue;
2410 		}
2411 
2412 		if (inc_recurse && relative_paths && *fbuf) {
2413 			if ((p = strchr(fbuf+1, '/')) != NULL) {
2414 				if (p - fbuf == 1 && *fbuf == '.') {
2415 					if ((fn = strchr(p+1, '/')) != NULL)
2416 						p = fn;
2417 				} else
2418 					fn = p;
2419 				send_implied_dirs(f, flist, fbuf, fbuf, p, flags,
2420 						  IS_MISSING_FILE(st) ? MISSING_NAME : name_type);
2421 				if (fn == p)
2422 					continue;
2423 			}
2424 		} else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
2425 			/* Send the implied directories at the start of the
2426 			 * source spec, so we get their permissions right. */
2427 			send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
2428 		}
2429 
2430 		if (one_file_system)
2431 			filesystem_dev = st.st_dev;
2432 
2433 		if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
2434 			struct file_struct *file;
2435 			file = send_file_name(f, flist, fbuf, &st,
2436 					      FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
2437 					      NO_FILTERS);
2438 			if (!file)
2439 				continue;
2440 			if (inc_recurse) {
2441 				if (name_type == DOTDIR_NAME) {
2442 					if (send_dir_depth < 0) {
2443 						send_dir_depth = 0;
2444 						change_local_filter_dir(fbuf, len, send_dir_depth);
2445 					}
2446 					send_directory(f, flist, fbuf, len, flags);
2447 				}
2448 			} else
2449 				send_if_directory(f, flist, file, fbuf, len, flags);
2450 		} else
2451 			send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
2452 	}
2453 
2454 	if (reenable_multiplex >= 0)
2455 		io_start_multiplex_in(reenable_multiplex);
2456 
2457 	gettimeofday(&end_tv, NULL);
2458 	stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2459 			      + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2460 	if (stats.flist_buildtime == 0)
2461 		stats.flist_buildtime = 1;
2462 	start_tv = end_tv;
2463 
2464 	/* Indicate end of file list */
2465 	if (io_error == 0 || ignore_errors)
2466 		write_end_of_flist(f, 0);
2467 	else if (use_safe_inc_flist)
2468 		write_end_of_flist(f, 1);
2469 	else {
2470 		if (delete_during && inc_recurse)
2471 			fatal_unsafe_io_error();
2472 		write_end_of_flist(f, 0);
2473 	}
2474 
2475 #ifdef SUPPORT_HARD_LINKS
2476 	if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
2477 		idev_destroy();
2478 #endif
2479 
2480 	if (show_filelist_progress)
2481 		finish_filelist_progress(flist);
2482 
2483 	gettimeofday(&end_tv, NULL);
2484 	stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
2485 			     + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
2486 
2487 	/* When converting names, both sides keep an unsorted file-list array
2488 	 * because the names will differ on the sending and receiving sides
2489 	 * (both sides will use the unsorted index number for each item). */
2490 
2491 	/* Sort the list without removing any duplicates.  This allows the
2492 	 * receiving side to ask for whatever name it kept.  For incremental
2493 	 * recursion mode, the sender marks duplicate dirs so that it can
2494 	 * send them together in a single file-list. */
2495 	if (need_unsorted_flist) {
2496 		flist->sorted = new_array(struct file_struct *, flist->used);
2497 		memcpy(flist->sorted, flist->files, flist->used * PTR_SIZE);
2498 	} else
2499 		flist->sorted = flist->files;
2500 	flist_sort_and_clean(flist, 0);
2501 	file_total += flist->used;
2502 	file_old_total += flist->used;
2503 
2504 	if (numeric_ids <= 0 && !inc_recurse)
2505 		send_id_lists(f);
2506 
2507 	/* send the io_error flag */
2508 	if (protocol_version < 30)
2509 		write_int(f, ignore_errors ? 0 : io_error);
2510 	else if (!use_safe_inc_flist && io_error && !ignore_errors)
2511 		send_msg_int(MSG_IO_ERROR, io_error);
2512 
2513 	if (disable_buffering)
2514 		io_end_buffering_out(IOBUF_FREE_BUFS);
2515 
2516 	stats.flist_size = stats.total_written - start_write;
2517 	stats.num_files = flist->used;
2518 
2519 	if (DEBUG_GTE(FLIST, 3))
2520 		output_flist(flist);
2521 
2522 	if (DEBUG_GTE(FLIST, 2))
2523 		rprintf(FINFO, "send_file_list done\n");
2524 
2525 	if (inc_recurse) {
2526 		send_dir_depth = 1;
2527 		add_dirs_to_tree(-1, flist, stats.num_dirs);
2528 		if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2529 			flist->parent_ndx = -1;
2530 		flist_done_allocating(flist);
2531 		if (send_dir_ndx < 0) {
2532 			write_ndx(f, NDX_FLIST_EOF);
2533 			flist_eof = 1;
2534 			if (DEBUG_GTE(FLIST, 3))
2535 				rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2536 		}
2537 		else if (file_total == 1) {
2538 			/* If we're creating incremental file-lists and there
2539 			 * was just 1 item in the first file-list, send 1 more
2540 			 * file-list to check if this is a 1-file xfer. */
2541 			send_extra_file_list(f, 1);
2542 		}
2543 	} else {
2544 		flist_eof = 1;
2545 		if (DEBUG_GTE(FLIST, 3))
2546 			rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2547 	}
2548 
2549 	return flist;
2550 }
2551 
recv_file_list(int f,int dir_ndx)2552 struct file_list *recv_file_list(int f, int dir_ndx)
2553 {
2554 	const char *good_dirname = NULL;
2555 	struct file_list *flist;
2556 	int dstart, flags;
2557 	int64 start_read;
2558 
2559 	if (!first_flist) {
2560 		if (show_filelist_progress)
2561 			start_filelist_progress("receiving file list");
2562 		else if (inc_recurse && INFO_GTE(FLIST, 1) && !am_server)
2563 			rprintf(FCLIENT, "receiving incremental file list\n");
2564 		rprintf(FLOG, "receiving file list\n");
2565 		if (usermap)
2566 			parse_name_map(usermap, True);
2567 		if (groupmap)
2568 			parse_name_map(groupmap, False);
2569 	}
2570 
2571 	start_read = stats.total_read;
2572 
2573 #ifdef SUPPORT_HARD_LINKS
2574 	if (preserve_hard_links && !first_flist)
2575 		init_hard_links();
2576 #endif
2577 
2578 	flist = flist_new(0, "recv_file_list");
2579 
2580 	if (inc_recurse) {
2581 		if (flist->ndx_start == 1)
2582 			dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
2583 		dstart = dir_flist->used;
2584 	} else {
2585 		dir_flist = flist;
2586 		dstart = 0;
2587 	}
2588 
2589 	while (1) {
2590 		struct file_struct *file;
2591 
2592 		if (xfer_flags_as_varint) {
2593 			if ((flags = read_varint(f)) == 0) {
2594 				int err = read_varint(f);
2595 				if (!ignore_errors)
2596 					io_error |= err;
2597 				break;
2598 			}
2599 		} else {
2600 			if ((flags = read_byte(f)) == 0)
2601 				break;
2602 
2603 			if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
2604 				flags |= read_byte(f) << 8;
2605 
2606 			if (flags == (XMIT_EXTENDED_FLAGS|XMIT_IO_ERROR_ENDLIST)) {
2607 				int err;
2608 				if (!use_safe_inc_flist) {
2609 					rprintf(FERROR, "Invalid flist flag: %x\n", flags);
2610 					exit_cleanup(RERR_PROTOCOL);
2611 				}
2612 				err = read_varint(f);
2613 				if (!ignore_errors)
2614 					io_error |= err;
2615 				break;
2616 			}
2617 		}
2618 
2619 		flist_expand(flist, 1);
2620 		file = recv_file_entry(f, flist, flags);
2621 
2622 		if (inc_recurse) {
2623 			static const char empty_dir[] = "\0";
2624 			const char *cur_dir = file->dirname ? file->dirname : empty_dir;
2625 			if (relative_paths && *cur_dir == '/')
2626 				cur_dir++;
2627 			if (cur_dir != good_dirname) {
2628 				const char *d = dir_ndx >= 0 ? f_name(dir_flist->files[dir_ndx], NULL) : empty_dir;
2629 				if (strcmp(cur_dir, d) != 0) {
2630 					rprintf(FERROR,
2631 						"ABORTING due to invalid path from sender: %s/%s\n",
2632 						cur_dir, file->basename);
2633 					exit_cleanup(RERR_PROTOCOL);
2634 				}
2635 				good_dirname = cur_dir;
2636 			}
2637 		}
2638 
2639 		if (S_ISREG(file->mode)) {
2640 			/* Already counted */
2641 		} else if (S_ISDIR(file->mode)) {
2642 			if (inc_recurse) {
2643 				flist_expand(dir_flist, 1);
2644 				dir_flist->files[dir_flist->used++] = file;
2645 			}
2646 			stats.num_dirs++;
2647 		} else if (S_ISLNK(file->mode))
2648 			stats.num_symlinks++;
2649 		else if (IS_DEVICE(file->mode))
2650 			stats.num_symlinks++;
2651 		else
2652 			stats.num_specials++;
2653 
2654 		flist->files[flist->used++] = file;
2655 
2656 		maybe_emit_filelist_progress(flist->used);
2657 
2658 		if (DEBUG_GTE(FLIST, 2)) {
2659 			char *name = f_name(file, NULL);
2660 			rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
2661 		}
2662 	}
2663 	file_total += flist->used;
2664 
2665 	if (DEBUG_GTE(FLIST, 2))
2666 		rprintf(FINFO, "received %d names\n", flist->used);
2667 
2668 	if (show_filelist_progress)
2669 		finish_filelist_progress(flist);
2670 
2671 	if (need_unsorted_flist) {
2672 		/* Create an extra array of index pointers that we can sort for
2673 		 * the generator's use (for wading through the files in sorted
2674 		 * order and for calling flist_find()).  We keep the "files"
2675 		 * list unsorted for our exchange of index numbers with the
2676 		 * other side (since their names may not sort the same). */
2677 		flist->sorted = new_array(struct file_struct *, flist->used);
2678 		memcpy(flist->sorted, flist->files, flist->used * PTR_SIZE);
2679 		if (inc_recurse && dir_flist->used > dstart) {
2680 			static int dir_flist_malloced = 0;
2681 			if (dir_flist_malloced < dir_flist->malloced) {
2682 				dir_flist->sorted = realloc_array(dir_flist->sorted,
2683 							struct file_struct *,
2684 							dir_flist->malloced);
2685 				dir_flist_malloced = dir_flist->malloced;
2686 			}
2687 			memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
2688 			       (dir_flist->used - dstart) * PTR_SIZE);
2689 			fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2690 		}
2691 	} else {
2692 		flist->sorted = flist->files;
2693 		if (inc_recurse && dir_flist->used > dstart) {
2694 			dir_flist->sorted = dir_flist->files;
2695 			fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
2696 		}
2697 	}
2698 
2699 	if (inc_recurse)
2700 		flist_done_allocating(flist);
2701 	else if (f >= 0) {
2702 		recv_id_list(f, flist);
2703 		flist_eof = 1;
2704 		if (DEBUG_GTE(FLIST, 3))
2705 			rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2706 	}
2707 
2708 	/* The --relative option sends paths with a leading slash, so we need
2709 	 * to specify the strip_root option here.  We rejected leading slashes
2710 	 * for a non-relative transfer in recv_file_entry(). */
2711 	flist_sort_and_clean(flist, relative_paths);
2712 
2713 	if (protocol_version < 30) {
2714 		/* Recv the io_error flag */
2715 		int err = read_int(f);
2716 		if (!ignore_errors)
2717 			io_error |= err;
2718 	} else if (inc_recurse && flist->ndx_start == 1) {
2719 		if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
2720 			flist->parent_ndx = -1;
2721 	}
2722 
2723 	if (DEBUG_GTE(FLIST, 3))
2724 		output_flist(flist);
2725 
2726 	if (DEBUG_GTE(FLIST, 2))
2727 		rprintf(FINFO, "recv_file_list done\n");
2728 
2729 	stats.flist_size += stats.total_read - start_read;
2730 	stats.num_files += flist->used;
2731 
2732 	return flist;
2733 }
2734 
2735 /* This is only used once by the receiver if the very first file-list
2736  * has exactly one item in it. */
recv_additional_file_list(int f)2737 void recv_additional_file_list(int f)
2738 {
2739 	struct file_list *flist;
2740 	int ndx = read_ndx(f);
2741 	if (ndx == NDX_FLIST_EOF) {
2742 		flist_eof = 1;
2743 		if (DEBUG_GTE(FLIST, 3))
2744 			rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2745 		change_local_filter_dir(NULL, 0, 0);
2746 	} else {
2747 		ndx = NDX_FLIST_OFFSET - ndx;
2748 		if (ndx < 0 || ndx >= dir_flist->used) {
2749 			ndx = NDX_FLIST_OFFSET - ndx;
2750 			rprintf(FERROR,
2751 				"[%s] Invalid dir index: %d (%d - %d)\n",
2752 				who_am_i(), ndx, NDX_FLIST_OFFSET,
2753 				NDX_FLIST_OFFSET - dir_flist->used + 1);
2754 			exit_cleanup(RERR_PROTOCOL);
2755 		}
2756 		if (DEBUG_GTE(FLIST, 3)) {
2757 			rprintf(FINFO, "[%s] receiving flist for dir %d\n",
2758 				who_am_i(), ndx);
2759 		}
2760 		flist = recv_file_list(f, ndx);
2761 		flist->parent_ndx = ndx;
2762 	}
2763 }
2764 
2765 /* Search for an identically-named item in the file list.  Note that the
2766  * items must agree in their directory-ness, or no match is returned. */
flist_find(struct file_list * flist,struct file_struct * f)2767 int flist_find(struct file_list *flist, struct file_struct *f)
2768 {
2769 	int low = flist->low, high = flist->high;
2770 	int diff, mid, mid_up;
2771 
2772 	while (low <= high) {
2773 		mid = (low + high) / 2;
2774 		if (F_IS_ACTIVE(flist->sorted[mid]))
2775 			mid_up = mid;
2776 		else {
2777 			/* Scan for the next non-empty entry using the cached
2778 			 * distance values.  If the value isn't fully up-to-
2779 			 * date, update it. */
2780 			mid_up = mid + F_DEPTH(flist->sorted[mid]);
2781 			if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
2782 				do {
2783 				    mid_up += F_DEPTH(flist->sorted[mid_up]);
2784 				} while (!F_IS_ACTIVE(flist->sorted[mid_up]));
2785 				F_DEPTH(flist->sorted[mid]) = mid_up - mid;
2786 			}
2787 			if (mid_up > high) {
2788 				/* If there's nothing left above us, set high to
2789 				 * a non-empty entry below us and continue. */
2790 				high = mid - (int)flist->sorted[mid]->len32;
2791 				if (!F_IS_ACTIVE(flist->sorted[high])) {
2792 					do {
2793 					    high -= (int)flist->sorted[high]->len32;
2794 					} while (!F_IS_ACTIVE(flist->sorted[high]));
2795 					flist->sorted[mid]->len32 = mid - high;
2796 				}
2797 				continue;
2798 			}
2799 		}
2800 		diff = f_name_cmp(flist->sorted[mid_up], f);
2801 		if (diff == 0) {
2802 			if (protocol_version < 29
2803 			    && S_ISDIR(flist->sorted[mid_up]->mode)
2804 			    != S_ISDIR(f->mode))
2805 				return -1;
2806 			return mid_up;
2807 		}
2808 		if (diff < 0)
2809 			low = mid_up + 1;
2810 		else
2811 			high = mid - 1;
2812 	}
2813 	return -1;
2814 }
2815 
2816 /* Search for a name in the file list.  You must specify want_dir_match as:
2817  * 1=match directories, 0=match non-directories, or -1=match either. */
flist_find_name(struct file_list * flist,const char * fname,int want_dir_match)2818 int flist_find_name(struct file_list *flist, const char *fname, int want_dir_match)
2819 {
2820 	static struct file_struct *f;
2821 	char fbuf[MAXPATHLEN];
2822 	const char *slash = strrchr(fname, '/');
2823 	const char *basename = slash ? slash+1 : fname;
2824 
2825 	if (!f)
2826 		f = (struct file_struct*)new_array(char, FILE_STRUCT_LEN + MAXPATHLEN + 1);
2827 
2828 	memset(f, 0, FILE_STRUCT_LEN);
2829 	memcpy((void*)f->basename, basename, strlen(basename)+1);
2830 
2831 	if (slash) {
2832 		strlcpy(fbuf, fname, slash - fname + 1);
2833 		f->dirname = fbuf;
2834 	} else
2835 		f->dirname = NULL;
2836 
2837 	f->mode = want_dir_match > 0 ? S_IFDIR : S_IFREG;
2838 
2839 	if (want_dir_match < 0)
2840 		return flist_find_ignore_dirness(flist, f);
2841 	return flist_find(flist, f);
2842 }
2843 
2844 /* Search for an identically-named item in the file list.  Differs from
2845  * flist_find in that an item that agrees with "f" in directory-ness is
2846  * preferred but one that does not is still found. */
flist_find_ignore_dirness(struct file_list * flist,struct file_struct * f)2847 int flist_find_ignore_dirness(struct file_list *flist, struct file_struct *f)
2848 {
2849 	mode_t save_mode;
2850 	int ndx;
2851 
2852 	/* First look for an item that agrees in directory-ness. */
2853 	ndx = flist_find(flist, f);
2854 	if (ndx >= 0)
2855 		return ndx;
2856 
2857 	/* Temporarily flip f->mode to look for an item of opposite
2858 	 * directory-ness. */
2859 	save_mode = f->mode;
2860 	f->mode = S_ISDIR(f->mode) ? S_IFREG : S_IFDIR;
2861 	ndx = flist_find(flist, f);
2862 	f->mode = save_mode;
2863 	return ndx;
2864 }
2865 
2866 /*
2867  * Free up any resources a file_struct has allocated
2868  * and clear the file.
2869  */
clear_file(struct file_struct * file)2870 void clear_file(struct file_struct *file)
2871 {
2872 	/* The +1 zeros out the first char of the basename. */
2873 	memset(file, 0, FILE_STRUCT_LEN + 1);
2874 	/* In an empty entry, F_DEPTH() is an offset to the next non-empty
2875 	 * entry.  Likewise for len32 in the opposite direction.  We assume
2876 	 * that we're alone for now since flist_find() will adjust the counts
2877 	 * it runs into that aren't up-to-date. */
2878 	file->len32 = F_DEPTH(file) = 1;
2879 }
2880 
2881 /* Allocate a new file list. */
flist_new(int flags,const char * msg)2882 static struct file_list *flist_new(int flags, const char *msg)
2883 {
2884 	struct file_list *flist;
2885 
2886 	flist = new0(struct file_list);
2887 
2888 	if (flags & FLIST_TEMP) {
2889 		if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0, _out_of_memory, POOL_INTERN)))
2890 			out_of_memory(msg);
2891 	} else {
2892 		/* This is a doubly linked list with prev looping back to
2893 		 * the end of the list, but the last next pointer is NULL. */
2894 		if (!first_flist) {
2895 			if (!(flist->file_pool = pool_create(NORMAL_EXTENT, 0, _out_of_memory, POOL_INTERN)))
2896 				out_of_memory(msg);
2897 
2898 			flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
2899 
2900 			first_flist = cur_flist = flist->prev = flist;
2901 		} else {
2902 			struct file_list *prev = first_flist->prev;
2903 
2904 			flist->file_pool = first_flist->file_pool;
2905 
2906 			flist->ndx_start = prev->ndx_start + prev->used + 1;
2907 			flist->flist_num = prev->flist_num + 1;
2908 
2909 			flist->prev = prev;
2910 			prev->next = first_flist->prev = flist;
2911 		}
2912 		flist->pool_boundary = pool_boundary(flist->file_pool, 0);
2913 		flist_cnt++;
2914 	}
2915 
2916 	return flist;
2917 }
2918 
2919 /* Free up all elements in a flist. */
flist_free(struct file_list * flist)2920 void flist_free(struct file_list *flist)
2921 {
2922 	if (!flist->prev) {
2923 		/* Was FLIST_TEMP dir-list. */
2924 	} else if (flist == flist->prev) {
2925 		first_flist = cur_flist = NULL;
2926 		file_total = 0;
2927 		flist_cnt = 0;
2928 	} else {
2929 		if (flist == cur_flist)
2930 			cur_flist = flist->next;
2931 		if (flist == first_flist)
2932 			first_flist = first_flist->next;
2933 		else {
2934 			flist->prev->next = flist->next;
2935 			if (!flist->next)
2936 				flist->next = first_flist;
2937 		}
2938 		flist->next->prev = flist->prev;
2939 		file_total -= flist->used;
2940 		flist_cnt--;
2941 	}
2942 
2943 	if (!flist->prev || !flist_cnt)
2944 		pool_destroy(flist->file_pool);
2945 	else
2946 		pool_free_old(flist->file_pool, flist->pool_boundary);
2947 
2948 	if (flist->sorted && flist->sorted != flist->files)
2949 		free(flist->sorted);
2950 	free(flist->files);
2951 	free(flist);
2952 }
2953 
2954 /* This routine ensures we don't have any duplicate names in our file list.
2955  * duplicate names can cause corruption because of the pipelining. */
flist_sort_and_clean(struct file_list * flist,int strip_root)2956 static void flist_sort_and_clean(struct file_list *flist, int strip_root)
2957 {
2958 	char fbuf[MAXPATHLEN];
2959 	int i, prev_i;
2960 
2961 	if (!flist)
2962 		return;
2963 	if (flist->used == 0) {
2964 		flist->high = -1;
2965 		flist->low = 0;
2966 		return;
2967 	}
2968 
2969 	fsort(flist->sorted, flist->used);
2970 
2971 	if (!am_sender || inc_recurse) {
2972 		for (i = prev_i = 0; i < flist->used; i++) {
2973 			if (F_IS_ACTIVE(flist->sorted[i])) {
2974 				prev_i = i;
2975 				break;
2976 			}
2977 		}
2978 		flist->low = prev_i;
2979 	} else {
2980 		i = prev_i = flist->used - 1;
2981 		flist->low = 0;
2982 	}
2983 
2984 	while (++i < flist->used) {
2985 		int j;
2986 		struct file_struct *file = flist->sorted[i];
2987 
2988 		if (!F_IS_ACTIVE(file))
2989 			continue;
2990 		if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
2991 			j = prev_i;
2992 		else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
2993 			int save_mode = file->mode;
2994 			/* Make sure that this directory doesn't duplicate a
2995 			 * non-directory earlier in the list. */
2996 			flist->high = prev_i;
2997 			file->mode = S_IFREG;
2998 			j = flist_find(flist, file);
2999 			file->mode = save_mode;
3000 		} else
3001 			j = -1;
3002 		if (j >= 0) {
3003 			int keep, drop;
3004 			/* If one is a dir and the other is not, we want to
3005 			 * keep the dir because it might have contents in the
3006 			 * list.  Otherwise keep the first one. */
3007 			if (S_ISDIR(file->mode)) {
3008 				struct file_struct *fp = flist->sorted[j];
3009 				if (!S_ISDIR(fp->mode))
3010 					keep = i, drop = j;
3011 				else {
3012 					if (am_sender)
3013 						file->flags |= FLAG_DUPLICATE;
3014 					else { /* Make sure we merge our vital flags. */
3015 						fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
3016 						fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
3017 					}
3018 					keep = j, drop = i;
3019 				}
3020 			} else
3021 				keep = j, drop = i;
3022 
3023 			if (!am_sender) {
3024 				if (DEBUG_GTE(DUP, 1)) {
3025 					rprintf(FINFO,
3026 					    "removing duplicate name %s from file list (%d)\n",
3027 					    f_name(file, fbuf), drop + flist->ndx_start);
3028 				}
3029 				clear_file(flist->sorted[drop]);
3030 			}
3031 
3032 			if (keep == i) {
3033 				if (flist->low == drop) {
3034 					for (j = drop + 1;
3035 					     j < i && !F_IS_ACTIVE(flist->sorted[j]);
3036 					     j++) {}
3037 					flist->low = j;
3038 				}
3039 				prev_i = i;
3040 			}
3041 		} else
3042 			prev_i = i;
3043 	}
3044 	flist->high = prev_i;
3045 
3046 	if (strip_root) {
3047 		/* We need to strip off the leading slashes for relative
3048 		 * paths, but this must be done _after_ the sorting phase. */
3049 		for (i = flist->low; i <= flist->high; i++) {
3050 			struct file_struct *file = flist->sorted[i];
3051 
3052 			if (!file->dirname)
3053 				continue;
3054 			while (*file->dirname == '/')
3055 				file->dirname++;
3056 			if (!*file->dirname)
3057 				file->dirname = NULL;
3058 		}
3059 	}
3060 
3061 	if (prune_empty_dirs && !am_sender) {
3062 		int j, prev_depth = 0;
3063 
3064 		prev_i = 0; /* It's OK that this isn't really true. */
3065 
3066 		for (i = flist->low; i <= flist->high; i++) {
3067 			struct file_struct *fp, *file = flist->sorted[i];
3068 
3069 			/* This temporarily abuses the F_DEPTH() value for a
3070 			 * directory that is in a chain that might get pruned.
3071 			 * We restore the old value if it gets a reprieve. */
3072 			if (S_ISDIR(file->mode) && F_DEPTH(file)) {
3073 				/* Dump empty dirs when coming back down. */
3074 				for (j = prev_depth; j >= F_DEPTH(file); j--) {
3075 					fp = flist->sorted[prev_i];
3076 					if (F_DEPTH(fp) >= 0)
3077 						break;
3078 					prev_i = -F_DEPTH(fp)-1;
3079 					clear_file(fp);
3080 				}
3081 				prev_depth = F_DEPTH(file);
3082 				if (is_excluded(f_name(file, fbuf), 1, ALL_FILTERS)) {
3083 					/* Keep dirs through this dir. */
3084 					for (j = prev_depth-1; ; j--) {
3085 						fp = flist->sorted[prev_i];
3086 						if (F_DEPTH(fp) >= 0)
3087 							break;
3088 						prev_i = -F_DEPTH(fp)-1;
3089 						F_DEPTH(fp) = j;
3090 					}
3091 				} else
3092 					F_DEPTH(file) = -prev_i-1;
3093 				prev_i = i;
3094 			} else {
3095 				/* Keep dirs through this non-dir. */
3096 				for (j = prev_depth; ; j--) {
3097 					fp = flist->sorted[prev_i];
3098 					if (F_DEPTH(fp) >= 0)
3099 						break;
3100 					prev_i = -F_DEPTH(fp)-1;
3101 					F_DEPTH(fp) = j;
3102 				}
3103 			}
3104 		}
3105 		/* Dump all remaining empty dirs. */
3106 		while (1) {
3107 			struct file_struct *fp = flist->sorted[prev_i];
3108 			if (F_DEPTH(fp) >= 0)
3109 				break;
3110 			prev_i = -F_DEPTH(fp)-1;
3111 			clear_file(fp);
3112 		}
3113 
3114 		for (i = flist->low; i <= flist->high; i++) {
3115 			if (F_IS_ACTIVE(flist->sorted[i]))
3116 				break;
3117 		}
3118 		flist->low = i;
3119 		for (i = flist->high; i >= flist->low; i--) {
3120 			if (F_IS_ACTIVE(flist->sorted[i]))
3121 				break;
3122 		}
3123 		flist->high = i;
3124 	}
3125 }
3126 
output_flist(struct file_list * flist)3127 static void output_flist(struct file_list *flist)
3128 {
3129 	char uidbuf[16], gidbuf[16], depthbuf[16];
3130 	struct file_struct *file;
3131 	const char *root, *dir, *slash, *name, *trail;
3132 	const char *who = who_am_i();
3133 	int i;
3134 
3135 	rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
3136 		who, flist->ndx_start, flist->used, flist->low, flist->high);
3137 	for (i = 0; i < flist->used; i++) {
3138 		file = flist->files[i];
3139 		if ((am_root || am_sender) && uid_ndx) {
3140 			snprintf(uidbuf, sizeof uidbuf, " uid=%u",
3141 				 F_OWNER(file));
3142 		} else
3143 			*uidbuf = '\0';
3144 		if (gid_ndx) {
3145 			static char parens[] = "(\0)\0\0\0";
3146 			char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
3147 			snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
3148 				 pp, F_GROUP(file), pp + 2);
3149 		} else
3150 			*gidbuf = '\0';
3151 		if (!am_sender)
3152 			snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
3153 		if (F_IS_ACTIVE(file)) {
3154 			root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
3155 			if ((dir = file->dirname) == NULL)
3156 				dir = slash = "";
3157 			else
3158 				slash = "/";
3159 			name = file->basename;
3160 			trail = S_ISDIR(file->mode) ? "/" : "";
3161 		} else
3162 			root = dir = slash = name = trail = "";
3163 		rprintf(FINFO,
3164 			"[%s] i=%d %s %s%s%s%s mode=0%o len=%s%s%s flags=%x\n",
3165 			who, i + flist->ndx_start,
3166 			root, dir, slash, name, trail,
3167 			(int)file->mode, comma_num(F_LENGTH(file)),
3168 			uidbuf, gidbuf, file->flags);
3169 	}
3170 }
3171 
3172 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
3173 enum fnc_type { t_PATH, t_ITEM };
3174 
3175 static int found_prefix;
3176 
3177 /* Compare the names of two file_struct entities, similar to how strcmp()
3178  * would do if it were operating on the joined strings.
3179  *
3180  * Some differences beginning with protocol_version 29: (1) directory names
3181  * are compared with an assumed trailing slash so that they compare in a
3182  * way that would cause them to sort immediately prior to any content they
3183  * may have; (2) a directory of any name compares after a non-directory of
3184  * any name at the same depth; (3) a directory with name "." compares prior
3185  * to anything else.  These changes mean that a directory and a non-dir
3186  * with the same name will not compare as equal (protocol_version >= 29).
3187  *
3188  * The dirname component can be an empty string, but the basename component
3189  * cannot (and never is in the current codebase).  The basename component
3190  * may be NULL (for a removed item), in which case it is considered to be
3191  * after any existing item. */
f_name_cmp(const struct file_struct * f1,const struct file_struct * f2)3192 int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
3193 {
3194 	int dif;
3195 	const uchar *c1, *c2;
3196 	enum fnc_state state1, state2;
3197 	enum fnc_type type1, type2;
3198 	enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
3199 
3200 	if (!f1 || !F_IS_ACTIVE(f1)) {
3201 		if (!f2 || !F_IS_ACTIVE(f2))
3202 			return 0;
3203 		return -1;
3204 	}
3205 	if (!f2 || !F_IS_ACTIVE(f2))
3206 		return 1;
3207 
3208 	c1 = (uchar*)f1->dirname;
3209 	c2 = (uchar*)f2->dirname;
3210 	if (c1 == c2)
3211 		c1 = c2 = NULL;
3212 	if (!c1) {
3213 		type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
3214 		c1 = (const uchar*)f1->basename;
3215 		if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
3216 			type1 = t_ITEM;
3217 			state1 = s_TRAILING;
3218 			c1 = (uchar*)"";
3219 		} else
3220 			state1 = s_BASE;
3221 	} else {
3222 		type1 = t_path;
3223 		state1 = s_DIR;
3224 	}
3225 	if (!c2) {
3226 		type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3227 		c2 = (const uchar*)f2->basename;
3228 		if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3229 			type2 = t_ITEM;
3230 			state2 = s_TRAILING;
3231 			c2 = (uchar*)"";
3232 		} else
3233 			state2 = s_BASE;
3234 	} else {
3235 		type2 = t_path;
3236 		state2 = s_DIR;
3237 	}
3238 
3239 	if (type1 != type2)
3240 		return type1 == t_PATH ? 1 : -1;
3241 
3242 	do {
3243 		if (!*c1) {
3244 			switch (state1) {
3245 			case s_DIR:
3246 				state1 = s_SLASH;
3247 				c1 = (uchar*)"/";
3248 				break;
3249 			case s_SLASH:
3250 				type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
3251 				c1 = (const uchar*)f1->basename;
3252 				if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
3253 					type1 = t_ITEM;
3254 					state1 = s_TRAILING;
3255 					c1 = (uchar*)"";
3256 				} else
3257 					state1 = s_BASE;
3258 				break;
3259 			case s_BASE:
3260 				state1 = s_TRAILING;
3261 				if (type1 == t_PATH) {
3262 					c1 = (uchar*)"/";
3263 					break;
3264 				}
3265 				/* FALL THROUGH */
3266 			case s_TRAILING:
3267 				type1 = t_ITEM;
3268 				break;
3269 			}
3270 			if (*c2 && type1 != type2)
3271 				return type1 == t_PATH ? 1 : -1;
3272 		}
3273 		if (!*c2) {
3274 			switch (state2) {
3275 			case s_DIR:
3276 				state2 = s_SLASH;
3277 				c2 = (uchar*)"/";
3278 				break;
3279 			case s_SLASH:
3280 				type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
3281 				c2 = (const uchar*)f2->basename;
3282 				if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
3283 					type2 = t_ITEM;
3284 					state2 = s_TRAILING;
3285 					c2 = (uchar*)"";
3286 				} else
3287 					state2 = s_BASE;
3288 				break;
3289 			case s_BASE:
3290 				state2 = s_TRAILING;
3291 				if (type2 == t_PATH) {
3292 					c2 = (uchar*)"/";
3293 					break;
3294 				}
3295 				/* FALL THROUGH */
3296 			case s_TRAILING:
3297 				found_prefix = 1;
3298 				if (!*c1)
3299 					return 0;
3300 				type2 = t_ITEM;
3301 				break;
3302 			}
3303 			if (type1 != type2)
3304 				return type1 == t_PATH ? 1 : -1;
3305 		}
3306 	} while ((dif = (int)*c1++ - (int)*c2++) == 0);
3307 
3308 	return dif;
3309 }
3310 
3311 /* Returns 1 if f1's filename has all of f2's filename as a prefix.  This does
3312  * not match if f2's basename is not an exact match of a path element in f1.
3313  * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
f_name_has_prefix(const struct file_struct * f1,const struct file_struct * f2)3314 int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
3315 {
3316 	found_prefix = 0;
3317 	f_name_cmp(f1, f2);
3318 	return found_prefix;
3319 }
3320 
f_name_buf(void)3321 char *f_name_buf(void)
3322 {
3323 	static char names[5][MAXPATHLEN];
3324 	static unsigned int n;
3325 
3326 	n = (n + 1) % (sizeof names / sizeof names[0]);
3327 
3328 	return names[n];
3329 }
3330 
3331 /* Return a copy of the full filename of a flist entry, using the indicated
3332  * buffer or one of 5 static buffers if fbuf is NULL.  No size-checking is
3333  * done because we checked the size when creating the file_struct entry.
3334  */
f_name(const struct file_struct * f,char * fbuf)3335 char *f_name(const struct file_struct *f, char *fbuf)
3336 {
3337 	if (!f || !F_IS_ACTIVE(f))
3338 		return NULL;
3339 
3340 	if (!fbuf)
3341 		fbuf = f_name_buf();
3342 
3343 	if (f->dirname) {
3344 		int len = strlen(f->dirname);
3345 		memcpy(fbuf, f->dirname, len);
3346 		fbuf[len] = '/';
3347 		strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
3348 	} else
3349 		strlcpy(fbuf, f->basename, MAXPATHLEN);
3350 
3351 	return fbuf;
3352 }
3353 
3354 /* Do a non-recursive scan of the named directory, possibly ignoring all
3355  * exclude rules except for the daemon's.  If "dlen" is >=0, it is the length
3356  * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
3357  * buffer (the functions we call will append names onto the end, but the old
3358  * dir value will be restored on exit). */
get_dirlist(char * dirname,int dlen,int flags)3359 struct file_list *get_dirlist(char *dirname, int dlen, int flags)
3360 {
3361 	struct file_list *dirlist;
3362 	char dirbuf[MAXPATHLEN];
3363 	int save_recurse = recurse;
3364 	int save_xfer_dirs = xfer_dirs;
3365 	int save_prune_empty_dirs = prune_empty_dirs;
3366 	int senddir_fd = flags & GDL_IGNORE_FILTER_RULES ? -2 : -1;
3367 	int senddir_flags = FLAG_CONTENT_DIR;
3368 
3369 	if (dlen < 0) {
3370 		dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
3371 		if (dlen >= MAXPATHLEN)
3372 			return NULL;
3373 		dirname = dirbuf;
3374 	}
3375 
3376 	dirlist = flist_new(FLIST_TEMP, "get_dirlist");
3377 
3378 	if (flags & GDL_PERHAPS_DIR)
3379 		senddir_flags |= FLAG_PERHAPS_DIR;
3380 
3381 	recurse = 0;
3382 	xfer_dirs = 1;
3383 	send_directory(senddir_fd, dirlist, dirname, dlen, senddir_flags);
3384 	xfer_dirs = save_xfer_dirs;
3385 	recurse = save_recurse;
3386 	if (INFO_GTE(PROGRESS, 1))
3387 		flist_count_offset += dirlist->used;
3388 
3389 	prune_empty_dirs = 0;
3390 	dirlist->sorted = dirlist->files;
3391 	flist_sort_and_clean(dirlist, 0);
3392 	prune_empty_dirs = save_prune_empty_dirs;
3393 
3394 	if (DEBUG_GTE(FLIST, 3))
3395 		output_flist(dirlist);
3396 
3397 	return dirlist;
3398 }
3399