File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / flist.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Nov 1 09:54:32 2016 UTC (7 years, 7 months ago) by misho
Branches: rsync, MAIN
CVS tags: v3_1_2p5, HEAD
rsync 3.1.2

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

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