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

    1: /*
    2:  * Routines only used by the receiving process.
    3:  *
    4:  * Copyright (C) 1996-2000 Andrew Tridgell
    5:  * Copyright (C) 1996 Paul Mackerras
    6:  * Copyright (C) 2003-2020 Wayne Davison
    7:  *
    8:  * This program is free software; you can redistribute it and/or modify
    9:  * it under the terms of the GNU General Public License as published by
   10:  * the Free Software Foundation; either version 3 of the License, or
   11:  * (at your option) any later version.
   12:  *
   13:  * This program is distributed in the hope that it will be useful,
   14:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   15:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16:  * GNU General Public License for more details.
   17:  *
   18:  * You should have received a copy of the GNU General Public License along
   19:  * with this program; if not, visit the http://fsf.org website.
   20:  */
   21: 
   22: #include "rsync.h"
   23: #include "inums.h"
   24: 
   25: extern int dry_run;
   26: extern int do_xfers;
   27: extern int use_db;
   28: extern int db_lax;
   29: extern int am_root;
   30: extern int am_server;
   31: extern int inc_recurse;
   32: extern int log_before_transfer;
   33: extern int stdout_format_has_i;
   34: extern int logfile_format_has_i;
   35: extern int want_xattr_optim;
   36: extern int csum_length;
   37: extern int read_batch;
   38: extern int write_batch;
   39: extern int batch_gen_fd;
   40: extern int protocol_version;
   41: extern int relative_paths;
   42: extern int preserve_hard_links;
   43: extern int preserve_perms;
   44: extern int write_devices;
   45: extern int preserve_xattrs;
   46: extern int do_fsync;
   47: extern int basis_dir_cnt;
   48: extern int make_backups;
   49: extern int cleanup_got_literal;
   50: extern int remove_source_files;
   51: extern int append_mode;
   52: extern int sparse_files;
   53: extern int preallocate_files;
   54: extern int keep_partial;
   55: extern int checksum_seed;
   56: extern int checksum_files;
   57: extern int whole_file;
   58: extern int inplace;
   59: extern int inplace_partial;
   60: extern int allowed_lull;
   61: extern int delay_updates;
   62: extern int xfersum_type;
   63: extern BOOL want_progress_now;
   64: extern mode_t orig_umask;
   65: extern struct stats stats;
   66: extern char *tmpdir;
   67: extern char *dest_filter;
   68: extern char *partial_dir;
   69: extern char *basis_dir[MAX_BASIS_DIRS+1];
   70: extern char sender_file_sum[MAX_DIGEST_LEN];
   71: extern struct file_list *cur_flist, *first_flist, *dir_flist;
   72: extern filter_rule_list daemon_filter_list;
   73: extern OFF_T preallocated_len;
   74: 
   75: static struct bitbag *delayed_bits = NULL;
   76: static int phase = 0, redoing = 0;
   77: static flist_ndx_list batch_redo_list;
   78: /* This is non-0 when we are updating the basis file or an identical copy: */
   79: static int updating_basis_or_equiv;
   80: 
   81: #define TMPNAME_SUFFIX ".XXXXXX"
   82: #define TMPNAME_SUFFIX_LEN ((int)sizeof TMPNAME_SUFFIX - 1)
   83: #define MAX_UNIQUE_NUMBER 999999
   84: #define MAX_UNIQUE_LOOP 100
   85: 
   86: /* get_tmpname() - create a tmp filename for a given filename
   87:  *
   88:  * If a tmpdir is defined, use that as the directory to put it in.  Otherwise,
   89:  * the tmp filename is in the same directory as the given name.  Note that
   90:  * there may be no directory at all in the given name!
   91:  *
   92:  * The tmp filename is basically the given filename with a dot prepended, and
   93:  * .XXXXXX appended (for mkstemp() to put its unique gunk in).  We take care
   94:  * to not exceed either the MAXPATHLEN or NAME_MAX, especially the last, as
   95:  * the basename basically becomes 8 characters longer.  In such a case, the
   96:  * original name is shortened sufficiently to make it all fit.
   97:  *
   98:  * If the make_unique arg is True, the XXXXXX string is replaced with a unique
   99:  * string that doesn't exist at the time of the check.  This is intended to be
  100:  * used for creating hard links, symlinks, devices, and special files, since
  101:  * normal files should be handled by mkstemp() for safety.
  102:  *
  103:  * Of course, the only reason the file is based on the original name is to
  104:  * make it easier to figure out what purpose a temp file is serving when a
  105:  * transfer is in progress. */
  106: int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
  107: {
  108: 	int maxname, length = 0;
  109: 	const char *f;
  110: 	char *suf;
  111: 
  112: 	if (tmpdir) {
  113: 		/* Note: this can't overflow, so the return value is safe */
  114: 		length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
  115: 		fnametmp[length++] = '/';
  116: 	}
  117: 
  118: 	if ((f = strrchr(fname, '/')) != NULL) {
  119: 		++f;
  120: 		if (!tmpdir) {
  121: 			length = f - fname;
  122: 			/* copy up to and including the slash */
  123: 			strlcpy(fnametmp, fname, length + 1);
  124: 		}
  125: 	} else
  126: 		f = fname;
  127: 
  128: 	if (!tmpdir) { /* using a tmpdir avoids the leading dot on our temp names */
  129: 		if (*f == '.') /* avoid an extra leading dot for OS X's sake */
  130: 			f++;
  131: 		fnametmp[length++] = '.';
  132: 	}
  133: 
  134: 	/* The maxname value is bufsize, and includes space for the '\0'.
  135: 	 * NAME_MAX needs an extra -1 for the name's leading dot. */
  136: 	maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN,
  137: 		      NAME_MAX - 1 - TMPNAME_SUFFIX_LEN);
  138: 
  139: 	if (maxname < 0) {
  140: 		rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
  141: 		fnametmp[0] = '\0';
  142: 		return 0;
  143: 	}
  144: 
  145: 	if (maxname) {
  146: 		int added = strlcpy(fnametmp + length, f, maxname);
  147: 		if (added >= maxname)
  148: 			added = maxname - 1;
  149: 		suf = fnametmp + length + added;
  150: 
  151: 		/* Trim any dangling high-bit chars if the first-trimmed char (if any) is
  152: 		 * also a high-bit char, just in case we cut into a multi-byte sequence.
  153: 		 * We are guaranteed to stop because of the leading '.' we added. */
  154: 		if ((int)f[added] & 0x80) {
  155: 			while ((int)suf[-1] & 0x80)
  156: 				suf--;
  157: 		}
  158: 		/* trim one trailing dot before our suffix's dot */
  159: 		if (suf[-1] == '.')
  160: 			suf--;
  161: 	} else
  162: 		suf = fnametmp + length - 1; /* overwrite the leading dot with suffix's dot */
  163: 
  164: 	if (make_unique) {
  165: 		static unsigned counter_limit;
  166: 		unsigned counter;
  167: 
  168: 		if (!counter_limit) {
  169: 			counter_limit = (unsigned)getpid() + MAX_UNIQUE_LOOP;
  170: 			if (counter_limit > MAX_UNIQUE_NUMBER || counter_limit < MAX_UNIQUE_LOOP)
  171: 				counter_limit = MAX_UNIQUE_LOOP;
  172: 		}
  173: 		counter = counter_limit - MAX_UNIQUE_LOOP;
  174: 
  175: 		/* This doesn't have to be very good because we don't need
  176: 		 * to worry about someone trying to guess the values:  all
  177: 		 * a conflict will do is cause a device, special file, hard
  178: 		 * link, or symlink to fail to be created.  Also: avoid
  179: 		 * using mktemp() due to gcc's annoying warning. */
  180: 		while (1) {
  181: 			snprintf(suf, TMPNAME_SUFFIX_LEN+1, ".%d", counter);
  182: 			if (access(fnametmp, 0) < 0)
  183: 				break;
  184: 			if (++counter >= counter_limit)
  185: 				return 0;
  186: 		}
  187: 	} else
  188: 		memcpy(suf, TMPNAME_SUFFIX, TMPNAME_SUFFIX_LEN+1);
  189: 
  190: 	return 1;
  191: }
  192: 
  193: /* Opens a temporary file for writing.
  194:  * Success: Writes name into fnametmp, returns fd.
  195:  * Failure: Clobbers fnametmp, returns -1.
  196:  * Calling cleanup_set() is the caller's job. */
  197: int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
  198: {
  199: 	int fd;
  200: 	mode_t added_perms;
  201: 
  202: 	if (!get_tmpname(fnametmp, fname, False))
  203: 		return -1;
  204: 
  205: 	if (am_root < 0) {
  206: 		/* For --fake-super, the file must be useable by the copying
  207: 		 * user, just like it would be for root. */
  208: 		added_perms = S_IRUSR|S_IWUSR;
  209: 	} else {
  210: 		/* For a normal copy, we need to be able to tweak things like xattrs. */
  211: 		added_perms = S_IWUSR;
  212: 	}
  213: 
  214: 	/* We initially set the perms without the setuid/setgid bits or group
  215: 	 * access to ensure that there is no race condition.  They will be
  216: 	 * correctly updated after the right owner and group info is set.
  217: 	 * (Thanks to snabb@epipe.fi for pointing this out.) */
  218: 	fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
  219: 
  220: #if 0
  221: 	/* In most cases parent directories will already exist because their
  222: 	 * information should have been previously transferred, but that may
  223: 	 * not be the case with -R */
  224: 	if (fd == -1 && relative_paths && errno == ENOENT
  225: 	 && make_path(fnametmp, ACCESSPERMS, MKP_SKIP_SLASH | MKP_DROP_NAME) == 0) {
  226: 		/* Get back to name with XXXXXX in it. */
  227: 		get_tmpname(fnametmp, fname, False);
  228: 		fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
  229: 	}
  230: #endif
  231: 
  232: 	if (fd == -1) {
  233: 		rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
  234: 			full_fname(fnametmp));
  235: 		return -1;
  236: 	}
  237: 
  238: 	return fd;
  239: }
  240: 
  241: static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
  242: 			const char *fname, int fd, struct file_struct *file, int inplace_sizing)
  243: {
  244: 	static char file_sum1[MAX_DIGEST_LEN];
  245: 	struct map_struct *mapbuf;
  246: 	struct sum_struct sum;
  247: 	int sum_len;
  248: 	int32 len;
  249: 	OFF_T total_size = F_LENGTH(file);
  250: 	OFF_T offset = 0;
  251: 	OFF_T offset2;
  252: 	char *data;
  253: 	int32 i;
  254: 	char *map = NULL;
  255: 
  256: #ifdef SUPPORT_PREALLOCATION
  257: 	if (preallocate_files && fd != -1 && total_size > 0 && (!inplace_sizing || total_size > size_r)) {
  258: 		/* Try to preallocate enough space for file's eventual length.  Can
  259: 		 * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
  260: 		if ((preallocated_len = do_fallocate(fd, 0, total_size)) < 0)
  261: 			rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
  262: 	} else
  263: #endif
  264: 	if (inplace_sizing) {
  265: #ifdef HAVE_FTRUNCATE
  266: 		/* The most compatible way to create a sparse file is to start with no length. */
  267: 		if (sparse_files > 0 && whole_file && fd >= 0 && do_ftruncate(fd, 0) == 0)
  268: 			preallocated_len = 0;
  269: 		else
  270: #endif
  271: 			preallocated_len = size_r;
  272: 	} else
  273: 		preallocated_len = 0;
  274: 
  275: 	read_sum_head(f_in, &sum);
  276: 
  277: 	if (fd_r >= 0 && size_r > 0) {
  278: 		int32 read_size = MAX(sum.blength * 2, 16*1024);
  279: 		mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
  280: 		if (DEBUG_GTE(DELTASUM, 2)) {
  281: 			rprintf(FINFO, "recv mapped %s of size %s\n",
  282: 				fname_r, big_num(size_r));
  283: 		}
  284: 	} else
  285: 		mapbuf = NULL;
  286: 
  287: 	sum_init(xfersum_type, checksum_seed);
  288: 
  289: 	if (append_mode > 0) {
  290: 		OFF_T j;
  291: 		sum.flength = (OFF_T)sum.count * sum.blength;
  292: 		if (sum.remainder)
  293: 			sum.flength -= sum.blength - sum.remainder;
  294: 		if (append_mode == 2 && mapbuf) {
  295: 			for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
  296: 				if (INFO_GTE(PROGRESS, 1))
  297: 					show_progress(offset, total_size);
  298: 				sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
  299: 					   CHUNK_SIZE);
  300: 				offset = j;
  301: 			}
  302: 			if (offset < sum.flength) {
  303: 				int32 len = (int32)(sum.flength - offset);
  304: 				if (INFO_GTE(PROGRESS, 1))
  305: 					show_progress(offset, total_size);
  306: 				sum_update(map_ptr(mapbuf, offset, len), len);
  307: 			}
  308: 		}
  309: 		offset = sum.flength;
  310: 		if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
  311: 			rsyserr(FERROR_XFER, errno, "lseek of %s returned %s, not %s",
  312: 				full_fname(fname), big_num(j), big_num(offset));
  313: 			exit_cleanup(RERR_FILEIO);
  314: 		}
  315: 	}
  316: 
  317: 	while ((i = recv_token(f_in, &data)) != 0) {
  318: 		if (INFO_GTE(PROGRESS, 1))
  319: 			show_progress(offset, total_size);
  320: 
  321: 		if (allowed_lull)
  322: 			maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH | MSK_ACTIVE_RECEIVER);
  323: 
  324: 		if (i > 0) {
  325: 			if (DEBUG_GTE(DELTASUM, 3)) {
  326: 				rprintf(FINFO,"data recv %d at %s\n",
  327: 					i, big_num(offset));
  328: 			}
  329: 
  330: 			stats.literal_data += i;
  331: 			cleanup_got_literal = 1;
  332: 
  333: 			sum_update(data, i);
  334: 
  335: 			if (fd != -1 && write_file(fd, 0, offset, data, i) != i)
  336: 				goto report_write_error;
  337: 			offset += i;
  338: 			continue;
  339: 		}
  340: 
  341: 		i = -(i+1);
  342: 		offset2 = i * (OFF_T)sum.blength;
  343: 		len = sum.blength;
  344: 		if (i == (int)sum.count-1 && sum.remainder != 0)
  345: 			len = sum.remainder;
  346: 
  347: 		stats.matched_data += len;
  348: 
  349: 		if (DEBUG_GTE(DELTASUM, 3)) {
  350: 			rprintf(FINFO,
  351: 				"chunk[%d] of size %ld at %s offset=%s%s\n",
  352: 				i, (long)len, big_num(offset2), big_num(offset),
  353: 				updating_basis_or_equiv && offset == offset2 ? " (seek)" : "");
  354: 		}
  355: 
  356: 		if (mapbuf) {
  357: 			map = map_ptr(mapbuf,offset2,len);
  358: 
  359: 			see_token(map, len);
  360: 			sum_update(map, len);
  361: 		}
  362: 
  363: 		if (updating_basis_or_equiv) {
  364: 			if (offset == offset2 && fd != -1) {
  365: 				if (skip_matched(fd, offset, map, len) < 0)
  366: 					goto report_write_error;
  367: 				offset += len;
  368: 				continue;
  369: 			}
  370: 		}
  371: 		if (fd != -1 && map && write_file(fd, 0, offset, map, len) != (int)len)
  372: 			goto report_write_error;
  373: 		offset += len;
  374: 	}
  375: 
  376: 	if (fd != -1 && offset > 0) {
  377: 		if (sparse_files > 0) {
  378: 			if (sparse_end(fd, offset) != 0)
  379: 				goto report_write_error;
  380: 		} else if (flush_write_file(fd) < 0) {
  381: 		    report_write_error:
  382: 			rsyserr(FERROR_XFER, errno, "write failed on %s", full_fname(fname));
  383: 			exit_cleanup(RERR_FILEIO);
  384: 		}
  385: 	}
  386: 
  387: #ifdef HAVE_FTRUNCATE
  388: 	/* inplace: New data could be shorter than old data.
  389: 	 * preallocate_files: total_size could have been an overestimate.
  390: 	 *     Cut off any extra preallocated zeros from dest file. */
  391: 	if ((inplace_sizing || preallocated_len > offset) && fd != -1 && !IS_DEVICE(file->mode)) {
  392: 		if (do_ftruncate(fd, offset) < 0)
  393: 			rsyserr(FERROR_XFER, errno, "ftruncate failed on %s", full_fname(fname));
  394: 	}
  395: #endif
  396: 
  397: 	if (INFO_GTE(PROGRESS, 1))
  398: 		end_progress(total_size);
  399: 
  400: 	sum_len = sum_end(file_sum1);
  401: 
  402: 	if (do_fsync && fd != -1 && fsync(fd) != 0) {
  403: 		rsyserr(FERROR, errno, "fsync failed on %s",
  404: 			full_fname(fname));
  405: 		exit_cleanup(RERR_FILEIO);
  406: 	}
  407: 
  408: 	if (mapbuf)
  409: 		unmap_file(mapbuf);
  410: 
  411: 	read_buf(f_in, sender_file_sum, sum_len);
  412: 	if (DEBUG_GTE(DELTASUM, 2))
  413: 		rprintf(FINFO,"got file_sum\n");
  414: 	if (fd != -1 && memcmp(file_sum1, sender_file_sum, sum_len) != 0)
  415: 		return 0;
  416: 	return 1;
  417: }
  418: 
  419: 
  420: static void discard_receive_data(int f_in, struct file_struct *file)
  421: {
  422: 	receive_data(f_in, NULL, -1, 0, NULL, -1, file, 0);
  423: }
  424: 
  425: static void handle_delayed_updates(char *local_name)
  426: {
  427: 	char *fname, *partialptr;
  428: 	int ndx;
  429: 
  430: 	for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
  431: 		struct file_struct *file = cur_flist->files[ndx];
  432: 		fname = local_name ? local_name : f_name(file, NULL);
  433: 		if ((partialptr = partial_dir_fname(fname)) != NULL) {
  434: 			if (make_backups > 1 && !make_backup(fname, False))
  435: 				continue;
  436: 			if (DEBUG_GTE(RECV, 1)) {
  437: 				rprintf(FINFO, "renaming %s to %s\n",
  438: 					partialptr, fname);
  439: 			}
  440: 			/* We don't use robust_rename() here because the
  441: 			 * partial-dir must be on the same drive. */
  442: 			if (do_rename(partialptr, fname) < 0) {
  443: 				rsyserr(FERROR_XFER, errno,
  444: 					"rename failed for %s (from %s)",
  445: 					full_fname(fname), partialptr);
  446: 			} else {
  447: 				if (remove_source_files || checksum_files & CSF_UPDATE
  448: 				 || (preserve_hard_links && F_IS_HLINKED(file)))
  449: 					send_msg_int(MSG_SUCCESS, ndx);
  450: 				handle_partial_dir(partialptr, PDIR_DELETE);
  451: 			}
  452: 		}
  453: 	}
  454: }
  455: 
  456: static void no_batched_update(int ndx, BOOL is_redo)
  457: {
  458: 	struct file_list *flist = flist_for_ndx(ndx, "no_batched_update");
  459: 	struct file_struct *file = flist->files[ndx - flist->ndx_start];
  460: 
  461: 	rprintf(FERROR_XFER, "(No batched update for%s \"%s\")\n",
  462: 		is_redo ? " resend of" : "", f_name(file, NULL));
  463: 
  464: 	if (inc_recurse && !dry_run)
  465: 		send_msg_int(MSG_NO_SEND, ndx);
  466: }
  467: 
  468: static int we_want_redo(int desired_ndx)
  469: {
  470: 	static int redo_ndx = -1;
  471: 
  472: 	while (redo_ndx < desired_ndx) {
  473: 		if (redo_ndx >= 0)
  474: 			no_batched_update(redo_ndx, True);
  475: 		if ((redo_ndx = flist_ndx_pop(&batch_redo_list)) < 0)
  476: 			return 0;
  477: 	}
  478: 
  479: 	if (redo_ndx == desired_ndx) {
  480: 		redo_ndx = -1;
  481: 		return 1;
  482: 	}
  483: 
  484: 	return 0;
  485: }
  486: 
  487: static int gen_wants_ndx(int desired_ndx, int flist_num)
  488: {
  489: 	static int next_ndx = -1;
  490: 	static int done_cnt = 0;
  491: 	static BOOL got_eof = False;
  492: 
  493: 	if (got_eof)
  494: 		return 0;
  495: 
  496: 	/* TODO: integrate gen-reading I/O into perform_io() so this is not needed? */
  497: 	io_flush(FULL_FLUSH);
  498: 
  499: 	while (next_ndx < desired_ndx) {
  500: 		if (inc_recurse && flist_num <= done_cnt)
  501: 			return 0;
  502: 		if (next_ndx >= 0)
  503: 			no_batched_update(next_ndx, False);
  504: 		if ((next_ndx = read_int(batch_gen_fd)) < 0) {
  505: 			if (inc_recurse) {
  506: 				done_cnt++;
  507: 				continue;
  508: 			}
  509: 			got_eof = True;
  510: 			return 0;
  511: 		}
  512: 	}
  513: 
  514: 	if (next_ndx == desired_ndx) {
  515: 		next_ndx = -1;
  516: 		return 1;
  517: 	}
  518: 
  519: 	return 0;
  520: }
  521: 
  522: /**
  523:  * main routine for receiver process.
  524:  *
  525:  * Receiver process runs on the same host as the generator process. */
  526: int recv_files(int f_in, int f_out, char *local_name)
  527: {
  528: 	int fd1,fd2;
  529: 	STRUCT_STAT st;
  530: 	int iflags, xlen;
  531: 	char *fname, fbuf[MAXPATHLEN];
  532: 	char xname[MAXPATHLEN];
  533: 	char *fnametmp, fnametmpbuf[MAXPATHLEN];
  534: 	char *fnamecmp, *partialptr;
  535: 	char fnamecmpbuf[MAXPATHLEN];
  536: 	char *filter_argv[MAX_FILTER_ARGS + 1];
  537: 	uchar fnamecmp_type;
  538: 	struct file_struct *file;
  539: 	int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
  540: 	enum logcode log_code = log_before_transfer ? FLOG : FINFO;
  541: 	int max_phase = protocol_version >= 29 ? 2 : 1;
  542: 	int dflt_perms = (ACCESSPERMS & ~orig_umask);
  543: #ifdef SUPPORT_ACLS
  544: 	const char *parent_dirname = "";
  545: #endif
  546: 	int ndx, recv_ok, one_inplace;
  547: 	pid_t pid = 0;
  548: 
  549: 	if (DEBUG_GTE(RECV, 1))
  550: 		rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
  551: 
  552: 	if (delay_updates)
  553: 		delayed_bits = bitbag_create(cur_flist->used + 1);
  554: 
  555: 	if (use_db && (append_mode == 1 || protocol_version < 30))
  556: 		use_db = 0; /* We can't note finished md5 values */
  557: 
  558: 	if (dest_filter) {
  559: 		char *p;
  560: 		char *sep = " \t";
  561: 		int i;
  562: 		for (p = strtok(dest_filter, sep), i = 0;
  563: 		     p && i < MAX_FILTER_ARGS;
  564: 		     p = strtok(0, sep))
  565: 			filter_argv[i++] = p;
  566: 		filter_argv[i] = NULL;
  567: 		if (p) {
  568: 			rprintf(FERROR,
  569: 				"Too many arguments to dest-filter (> %d)\n",
  570: 				MAX_FILTER_ARGS);
  571: 			exit_cleanup(RERR_SYNTAX);
  572: 		}
  573: 	}
  574: 
  575: 	progress_init();
  576: 
  577: 	while (1) {
  578: 		cleanup_disable();
  579: 
  580: 		/* This call also sets cur_flist. */
  581: 		ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
  582: 					 xname, &xlen);
  583: 		if (ndx == NDX_DONE) {
  584: 			if (!am_server && cur_flist) {
  585: 				set_current_file_index(NULL, 0);
  586: 				if (INFO_GTE(PROGRESS, 2))
  587: 					end_progress(0);
  588: 			}
  589: 			if (inc_recurse && first_flist) {
  590: 				if (read_batch) {
  591: 					ndx = first_flist->used + first_flist->ndx_start;
  592: 					gen_wants_ndx(ndx, first_flist->flist_num);
  593: 				}
  594: 				flist_free(first_flist);
  595: 				if (first_flist)
  596: 					continue;
  597: 			} else if (read_batch && first_flist) {
  598: 				ndx = first_flist->used;
  599: 				gen_wants_ndx(ndx, first_flist->flist_num);
  600: 			}
  601: 			if (++phase > max_phase)
  602: 				break;
  603: 			if (DEBUG_GTE(RECV, 1))
  604: 				rprintf(FINFO, "recv_files phase=%d\n", phase);
  605: 			if (phase == 2 && delay_updates)
  606: 				handle_delayed_updates(local_name);
  607: 			write_int(f_out, NDX_DONE);
  608: 			continue;
  609: 		}
  610: 
  611: 		if (ndx - cur_flist->ndx_start >= 0)
  612: 			file = cur_flist->files[ndx - cur_flist->ndx_start];
  613: 		else
  614: 			file = dir_flist->files[cur_flist->parent_ndx];
  615: 		fname = local_name ? local_name : f_name(file, fbuf);
  616: 
  617: 		if (DEBUG_GTE(RECV, 1))
  618: 			rprintf(FINFO, "recv_files(%s)\n", fname);
  619: 
  620: 		if (daemon_filter_list.head && (*fname != '.' || fname[1] != '\0')
  621: 		 && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
  622: 			rprintf(FERROR, "attempt to hack rsync failed.\n");
  623: 			exit_cleanup(RERR_PROTOCOL);
  624: 		}
  625: 
  626: #ifdef SUPPORT_XATTRS
  627: 		if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
  628: 		 && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
  629: 			recv_xattr_request(file, f_in);
  630: #endif
  631: 
  632: 		if (!(iflags & ITEM_TRANSFER)) {
  633: 			maybe_log_item(file, iflags, itemizing, xname);
  634: #ifdef SUPPORT_XATTRS
  635: 			if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
  636: 			 && !BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))
  637: 				set_file_attrs(fname, file, NULL, fname, 0);
  638: #endif
  639: 			if (iflags & ITEM_IS_NEW) {
  640: 				stats.created_files++;
  641: 				if (S_ISREG(file->mode)) {
  642: 					/* Nothing further to count. */
  643: 				} else if (S_ISDIR(file->mode))
  644: 					stats.created_dirs++;
  645: #ifdef SUPPORT_LINKS
  646: 				else if (S_ISLNK(file->mode))
  647: 					stats.created_symlinks++;
  648: #endif
  649: 				else if (IS_DEVICE(file->mode))
  650: 					stats.created_devices++;
  651: 				else
  652: 					stats.created_specials++;
  653: 			}
  654: 			continue;
  655: 		}
  656: 		if (phase == 2) {
  657: 			rprintf(FERROR,
  658: 				"got transfer request in phase 2 [%s]\n",
  659: 				who_am_i());
  660: 			exit_cleanup(RERR_PROTOCOL);
  661: 		}
  662: 
  663: 		if (file->flags & FLAG_FILE_SENT) {
  664: 			if (csum_length == SHORT_SUM_LENGTH) {
  665: 				if (keep_partial && !partial_dir)
  666: 					make_backups = -make_backups; /* prevents double backup */
  667: 				if (append_mode)
  668: 					sparse_files = -sparse_files;
  669: 				append_mode = -append_mode;
  670: 				csum_length = SUM_LENGTH;
  671: 				redoing = 1;
  672: 			}
  673: 		} else {
  674: 			if (csum_length != SHORT_SUM_LENGTH) {
  675: 				if (keep_partial && !partial_dir)
  676: 					make_backups = -make_backups;
  677: 				if (append_mode)
  678: 					sparse_files = -sparse_files;
  679: 				append_mode = -append_mode;
  680: 				csum_length = SHORT_SUM_LENGTH;
  681: 				redoing = 0;
  682: 			}
  683: 			if (iflags & ITEM_IS_NEW)
  684: 				stats.created_files++;
  685: 		}
  686: 
  687: 		if (!am_server)
  688: 			set_current_file_index(file, ndx);
  689: 		stats.xferred_files++;
  690: 		stats.total_transferred_size += F_LENGTH(file);
  691: 
  692: 		cleanup_got_literal = 0;
  693: 
  694: 		if (read_batch) {
  695: 			int wanted = redoing
  696: 				   ? we_want_redo(ndx)
  697: 				   : gen_wants_ndx(ndx, cur_flist->flist_num);
  698: 			if (!wanted) {
  699: 				rprintf(FINFO,
  700: 					"(Skipping batched update for%s \"%s\")\n",
  701: 					redoing ? " resend of" : "",
  702: 					fname);
  703: 				discard_receive_data(f_in, file);
  704: 				file->flags |= FLAG_FILE_SENT;
  705: 				continue;
  706: 			}
  707: 		}
  708: 
  709: 		remember_initial_stats();
  710: 
  711: 		if (!do_xfers) { /* log the transfer */
  712: 			log_item(FCLIENT, file, iflags, NULL);
  713: 			if (read_batch)
  714: 				discard_receive_data(f_in, file);
  715: 			continue;
  716: 		}
  717: 		if (write_batch < 0) {
  718: 			log_item(FCLIENT, file, iflags, NULL);
  719: 			if (!am_server)
  720: 				discard_receive_data(f_in, file);
  721: 			if (inc_recurse)
  722: 				send_msg_int(MSG_SUCCESS, ndx);
  723: 			continue;
  724: 		}
  725: 
  726: 		partialptr = partial_dir ? partial_dir_fname(fname) : fname;
  727: 
  728: 		if (protocol_version >= 29) {
  729: 			switch (fnamecmp_type) {
  730: 			case FNAMECMP_FNAME:
  731: 				fnamecmp = fname;
  732: 				break;
  733: 			case FNAMECMP_PARTIAL_DIR:
  734: 				fnamecmp = partialptr;
  735: 				break;
  736: 			case FNAMECMP_BACKUP:
  737: 				fnamecmp = get_backup_name(fname);
  738: 				break;
  739: 			case FNAMECMP_FUZZY:
  740: 				if (file->dirname) {
  741: 					pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, file->dirname, xname);
  742: 					fnamecmp = fnamecmpbuf;
  743: 				} else
  744: 					fnamecmp = xname;
  745: 				break;
  746: 			default:
  747: 				if (fnamecmp_type > FNAMECMP_FUZZY && fnamecmp_type-FNAMECMP_FUZZY <= basis_dir_cnt) {
  748: 					fnamecmp_type -= FNAMECMP_FUZZY + 1;
  749: 					if (file->dirname) {
  750: 						stringjoin(fnamecmpbuf, sizeof fnamecmpbuf,
  751: 							   basis_dir[fnamecmp_type], "/", file->dirname, "/", xname, NULL);
  752: 					} else
  753: 						pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, basis_dir[fnamecmp_type], xname);
  754: 				} else if (fnamecmp_type >= basis_dir_cnt) {
  755: 					rprintf(FERROR,
  756: 						"invalid basis_dir index: %d.\n",
  757: 						fnamecmp_type);
  758: 					exit_cleanup(RERR_PROTOCOL);
  759: 				} else
  760: 					pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, basis_dir[fnamecmp_type], fname);
  761: 				fnamecmp = fnamecmpbuf;
  762: 				break;
  763: 			}
  764: 			if (!fnamecmp || (daemon_filter_list.head
  765: 			  && check_filter(&daemon_filter_list, FLOG, fnamecmp, 0) < 0)) {
  766: 				fnamecmp = fname;
  767: 				fnamecmp_type = FNAMECMP_FNAME;
  768: 			}
  769: 		} else {
  770: 			/* Reminder: --inplace && --partial-dir are never
  771: 			 * enabled at the same time. */
  772: 			if (inplace && make_backups > 1) {
  773: 				if (!(fnamecmp = get_backup_name(fname)))
  774: 					fnamecmp = fname;
  775: 				else
  776: 					fnamecmp_type = FNAMECMP_BACKUP;
  777: 			} else if (partial_dir && partialptr)
  778: 				fnamecmp = partialptr;
  779: 			else
  780: 				fnamecmp = fname;
  781: 		}
  782: 
  783: 		/* open the file */
  784: 		fd1 = do_open(fnamecmp, O_RDONLY, 0);
  785: 
  786: 		if (fd1 == -1 && protocol_version < 29) {
  787: 			if (fnamecmp != fname) {
  788: 				fnamecmp = fname;
  789: 				fnamecmp_type = FNAMECMP_FNAME;
  790: 				fd1 = do_open(fnamecmp, O_RDONLY, 0);
  791: 			}
  792: 
  793: 			if (fd1 == -1 && basis_dir[0]) {
  794: 				/* pre-29 allowed only one alternate basis */
  795: 				pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
  796: 					 basis_dir[0], fname);
  797: 				fnamecmp = fnamecmpbuf;
  798: 				fnamecmp_type = FNAMECMP_BASIS_DIR_LOW;
  799: 				fd1 = do_open(fnamecmp, O_RDONLY, 0);
  800: 			}
  801: 		}
  802: 
  803: 		one_inplace = inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR;
  804: 		updating_basis_or_equiv = one_inplace
  805: 		    || (inplace && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP));
  806: 
  807: 		if (fd1 == -1) {
  808: 			st.st_mode = 0;
  809: 			st.st_size = 0;
  810: 		} else if (do_fstat(fd1,&st) != 0) {
  811: 			rsyserr(FERROR_XFER, errno, "fstat %s failed",
  812: 				full_fname(fnamecmp));
  813: 			discard_receive_data(f_in, file);
  814: 			close(fd1);
  815: 			if (inc_recurse)
  816: 				send_msg_int(MSG_NO_SEND, ndx);
  817: 			continue;
  818: 		}
  819: 
  820: 		if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
  821: 			/* this special handling for directories
  822: 			 * wouldn't be necessary if robust_rename()
  823: 			 * and the underlying robust_unlink could cope
  824: 			 * with directories
  825: 			 */
  826: 			rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
  827: 				full_fname(fnamecmp));
  828: 			discard_receive_data(f_in, file);
  829: 			close(fd1);
  830: 			if (inc_recurse)
  831: 				send_msg_int(MSG_NO_SEND, ndx);
  832: 			continue;
  833: 		}
  834: 
  835: 		if (fd1 != -1 && !(S_ISREG(st.st_mode) || (write_devices && IS_DEVICE(st.st_mode)))) {
  836: 			close(fd1);
  837: 			fd1 = -1;
  838: 		}
  839: 
  840: 		if (fd1 != -1 && IS_DEVICE(st.st_mode) && st.st_size == 0)
  841: 			st.st_size = get_device_size(fd1, fname);
  842: 
  843: 		/* If we're not preserving permissions, change the file-list's
  844: 		 * mode based on the local permissions and some heuristics. */
  845: 		if (!preserve_perms) {
  846: 			int exists = fd1 != -1;
  847: #ifdef SUPPORT_ACLS
  848: 			const char *dn = file->dirname ? file->dirname : ".";
  849: 			if (parent_dirname != dn
  850: 			 && strcmp(parent_dirname, dn) != 0) {
  851: 				dflt_perms = default_perms_for_dir(dn);
  852: 				parent_dirname = dn;
  853: 			}
  854: #endif
  855: 			file->mode = dest_mode(file->mode, st.st_mode, dflt_perms, exists);
  856: 		}
  857: 
  858: 		/* We now check to see if we are writing the file "inplace" */
  859: 		if (inplace || one_inplace)  {
  860: 			fnametmp = one_inplace ? partialptr : fname;
  861: 			fd2 = do_open(fnametmp, O_WRONLY|O_CREAT, 0600);
  862: 			if (fd2 == -1) {
  863: 				rsyserr(FERROR_XFER, errno, "open %s failed",
  864: 					full_fname(fnametmp));
  865: 			} else if (updating_basis_or_equiv)
  866: 				cleanup_set(NULL, NULL, file, fd1, fd2);
  867: 		} else {
  868: 			fnametmp = fnametmpbuf;
  869: 			fd2 = open_tmpfile(fnametmp, fname, file);
  870: 			if (fd2 != -1)
  871: 				cleanup_set(fnametmp, partialptr, file, fd1, fd2);
  872: 		}
  873: 
  874: 		if (fd2 == -1) {
  875: 			discard_receive_data(f_in, file);
  876: 			if (fd1 != -1)
  877: 				close(fd1);
  878: 			if (inc_recurse)
  879: 				send_msg_int(MSG_NO_SEND, ndx);
  880: 			continue;
  881: 		}
  882: 
  883: 		/* log the transfer */
  884: 		if (log_before_transfer)
  885: 			log_item(FCLIENT, file, iflags, NULL);
  886: 		else if (!am_server && INFO_GTE(NAME, 1) && INFO_EQ(PROGRESS, 1))
  887: 			rprintf(FINFO, "%s\n", fname);
  888: 
  889: 		if (dest_filter)
  890: 			pid = run_filter(filter_argv, fd2, &fd2);
  891: 
  892: 		/* recv file data */
  893: 		recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, fname, fd2, file, inplace || one_inplace);
  894: 
  895: 		log_item(log_code, file, iflags, NULL);
  896: 		if (want_progress_now)
  897: 			instant_progress(fname);
  898: 
  899: 		if (fd1 != -1)
  900: 			close(fd1);
  901: 		if (close(fd2) < 0) {
  902: 			rsyserr(FERROR, errno, "close failed on %s",
  903: 				full_fname(fnametmp));
  904: 			exit_cleanup(RERR_FILEIO);
  905: 		}
  906: 
  907: 		if (dest_filter) {
  908: 			int status;
  909: 			wait_process_with_flush(pid, &status);
  910: 			if (status != 0) {
  911: 				rprintf(FERROR, "filter %s exited code: %d\n",
  912: 					dest_filter, status);
  913: 				continue;
  914: 			}
  915: 		}
  916: 
  917: 		if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
  918: 			if (partialptr == fname)
  919: 				partialptr = NULL;
  920: 			if (!finish_transfer(fname, fnametmp, fnamecmp, partialptr, file, recv_ok, 1))
  921: 				recv_ok = -1;
  922: 			else if (fnamecmp == partialptr) {
  923: 				if (!one_inplace)
  924: 					do_unlink(partialptr);
  925: 				handle_partial_dir(partialptr, PDIR_DELETE);
  926: 			}
  927: 			if (use_db && do_lstat(fname, &st) == 0)
  928: 				db_set_checksum(5, &st, sender_file_sum);
  929: 		} else if (keep_partial && partialptr && !one_inplace) {
  930: 			if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
  931: 				rprintf(FERROR,
  932: 					"Unable to create partial-dir for %s -- discarding %s.\n",
  933: 					local_name ? local_name : f_name(file, NULL),
  934: 					recv_ok ? "completed file" : "partial file");
  935: 				do_unlink(fnametmp);
  936: 				recv_ok = -1;
  937: 			} else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
  938: 						    file, recv_ok, !partial_dir))
  939: 				recv_ok = -1;
  940: 			else if (delay_updates && recv_ok) {
  941: 				bitbag_set_bit(delayed_bits, ndx);
  942: 				if (use_db && do_lstat(partialptr, &st) == 0)
  943: 					db_set_checksum(5, &st, sender_file_sum);
  944: 				recv_ok = 2;
  945: 			} else
  946: 				partialptr = NULL;
  947: 		} else if (!one_inplace)
  948: 			do_unlink(fnametmp);
  949: 
  950: 		cleanup_disable();
  951: 
  952: 		if (read_batch)
  953: 			file->flags |= FLAG_FILE_SENT;
  954: 
  955: 		switch (recv_ok) {
  956: 		case 2:
  957: 			break;
  958: 		case 1:
  959: 			if (remove_source_files || inc_recurse || checksum_files & CSF_UPDATE
  960: 			 || (preserve_hard_links && F_IS_HLINKED(file)))
  961: 				send_msg_int(MSG_SUCCESS, ndx);
  962: 			break;
  963: 		case 0: {
  964: 			enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
  965: 			if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
  966: 				char *errstr, *redostr, *keptstr;
  967: 				if (!(keep_partial && partialptr) && !inplace)
  968: 					keptstr = "discarded";
  969: 				else if (partial_dir)
  970: 					keptstr = "put into partial-dir";
  971: 				else
  972: 					keptstr = "retained";
  973: 				if (msgtype == FERROR_XFER) {
  974: 					errstr = "ERROR";
  975: 					redostr = "";
  976: 				} else {
  977: 					errstr = "WARNING";
  978: 					redostr = read_batch ? " (may try again)"
  979: 							     : " (will try again)";
  980: 				}
  981: 				rprintf(msgtype,
  982: 					"%s: %s failed verification -- update %s%s.\n",
  983: 					errstr, local_name ? f_name(file, NULL) : fname,
  984: 					keptstr, redostr);
  985: 			}
  986: 			if (!redoing) {
  987: 				if (read_batch)
  988: 					flist_ndx_push(&batch_redo_list, ndx);
  989: 				send_msg_int(MSG_REDO, ndx);
  990: 				file->flags |= FLAG_FILE_SENT;
  991: 			} else if (inc_recurse)
  992: 				send_msg_int(MSG_NO_SEND, ndx);
  993: 			break;
  994: 		}
  995: 		case -1:
  996: 			if (inc_recurse)
  997: 				send_msg_int(MSG_NO_SEND, ndx);
  998: 			break;
  999: 		}
 1000: 	}
 1001: 	if (make_backups < 0)
 1002: 		make_backups = -make_backups;
 1003: 
 1004: 	if (phase == 2 && delay_updates) /* for protocol_version < 29 */
 1005: 		handle_delayed_updates(local_name);
 1006: 
 1007: 	if (DEBUG_GTE(RECV, 1))
 1008: 		rprintf(FINFO,"recv_files finished\n");
 1009: 
 1010: 	return 0;
 1011: }

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