File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / flist.c
Revision 1.1.1.4 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:32:36 2021 UTC (3 years, 3 months ago) by misho
Branches: rsync, MAIN
CVS tags: v3_2_3, HEAD
rsync 3.2.3

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>