File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / receiver.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, 8 months ago) by misho
Branches: rsync, MAIN
CVS tags: v3_1_2p5, HEAD
rsync 3.1.2

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

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