File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / generator.c
Revision 1.1.1.1.2.1: download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 00:20:20 2013 UTC (10 years, 11 months ago) by misho
Branches: rsync3_0_9p0
Diff to: branchpoint 1.1.1.1: preferred, unified
patch 0

    1: /*
    2:  * Routines that are exclusive to the generator process.
    3:  *
    4:  * Copyright (C) 1996-2000 Andrew Tridgell
    5:  * Copyright (C) 1996 Paul Mackerras
    6:  * Copyright (C) 2002 Martin Pool <mbp@samba.org>
    7:  * Copyright (C) 2003-2009 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: 
   25: extern int verbose;
   26: extern int dry_run;
   27: extern int do_xfers;
   28: extern int stdout_format_has_i;
   29: extern int logfile_format_has_i;
   30: extern int am_root;
   31: extern int am_server;
   32: extern int am_daemon;
   33: extern int inc_recurse;
   34: extern int do_progress;
   35: extern int relative_paths;
   36: extern int implied_dirs;
   37: extern int keep_dirlinks;
   38: extern int preserve_acls;
   39: extern int preserve_xattrs;
   40: extern int preserve_links;
   41: extern int preserve_devices;
   42: extern int preserve_specials;
   43: extern int preserve_hard_links;
   44: extern int preserve_executability;
   45: extern int preserve_perms;
   46: extern int preserve_times;
   47: extern int delete_mode;
   48: extern int delete_before;
   49: extern int delete_during;
   50: extern int delete_after;
   51: extern int msgdone_cnt;
   52: extern int ignore_errors;
   53: extern int remove_source_files;
   54: extern int delay_updates;
   55: extern int update_only;
   56: extern int ignore_existing;
   57: extern int ignore_non_existing;
   58: extern int inplace;
   59: extern int append_mode;
   60: extern int make_backups;
   61: extern int csum_length;
   62: extern int ignore_times;
   63: extern int size_only;
   64: extern OFF_T max_size;
   65: extern OFF_T min_size;
   66: extern int io_error;
   67: extern int flist_eof;
   68: extern int allowed_lull;
   69: extern int sock_f_out;
   70: extern int ignore_timeout;
   71: extern int protocol_version;
   72: extern int file_total;
   73: extern int fuzzy_basis;
   74: extern int always_checksum;
   75: extern int checksum_len;
   76: extern char *partial_dir;
   77: extern char *basis_dir[MAX_BASIS_DIRS+1];
   78: extern int compare_dest;
   79: extern int copy_dest;
   80: extern int link_dest;
   81: extern int detect_renamed;
   82: extern int whole_file;
   83: extern int list_only;
   84: extern int read_batch;
   85: extern int safe_symlinks;
   86: extern long block_size; /* "long" because popt can't set an int32. */
   87: extern int unsort_ndx;
   88: extern int max_delete;
   89: extern int force_delete;
   90: extern int one_file_system;
   91: extern struct stats stats;
   92: extern dev_t filesystem_dev;
   93: extern mode_t orig_umask;
   94: extern uid_t our_uid;
   95: extern char *backup_dir;
   96: extern char *backup_suffix;
   97: extern int backup_suffix_len;
   98: extern struct file_list *cur_flist, *first_flist, *dir_flist;
   99: extern struct filter_list_struct daemon_filter_list;
  100: extern struct file_list the_fattr_list;
  101: 
  102: int ignore_perishable = 0;
  103: int non_perishable_cnt = 0;
  104: int maybe_ATTRS_REPORT = 0;
  105: 
  106: static dev_t dev_zero;
  107: static int deletion_count = 0; /* used to implement --max-delete */
  108: static int unexplored_dirs = 1;
  109: static int deldelay_size = 0, deldelay_cnt = 0;
  110: static char *deldelay_buf = NULL;
  111: static int deldelay_fd = -1;
  112: static int loopchk_limit;
  113: static int dir_tweaking;
  114: static int symlink_timeset_failed_flags;
  115: static int need_retouch_dir_times;
  116: static int need_retouch_dir_perms;
  117: static const char *solo_file = NULL;
  118: 
  119: /* For calling delete_item(), delete_dir_contents(), and delete_in_dir(). */
  120: #define DEL_NO_UID_WRITE 	(1<<0) /* file/dir has our uid w/o write perm */
  121: #define DEL_RECURSE		(1<<1) /* if dir, delete all contents */
  122: #define DEL_DIR_IS_EMPTY	(1<<2) /* internal delete_FUNCTIONS use only */
  123: #define DEL_FOR_FILE		(1<<3) /* making room for a replacement file */
  124: #define DEL_FOR_DIR		(1<<4) /* making room for a replacement dir */
  125: #define DEL_FOR_SYMLINK 	(1<<5) /* making room for a replacement symlink */
  126: #define DEL_FOR_DEVICE		(1<<6) /* making room for a replacement device */
  127: #define DEL_FOR_SPECIAL 	(1<<7) /* making room for a replacement special */
  128: #define DEL_NO_DELETIONS	(1<<9) /* just check for renames w/o deleting */
  129: 
  130: #define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
  131: 
  132: enum nonregtype {
  133:     TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
  134: };
  135: 
  136: enum delret {
  137:     DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
  138: };
  139: 
  140: /* Forward declarations. */
  141: static enum delret delete_dir_contents(char *fname, uint16 flags);
  142: #ifdef SUPPORT_HARD_LINKS
  143: static void handle_skipped_hlink(struct file_struct *file, int itemizing,
  144: 				 enum logcode code, int f_out);
  145: #endif
  146: 
  147: static int is_backup_file(char *fn)
  148: {
  149: 	int k = strlen(fn) - backup_suffix_len;
  150: 	return k > 0 && strcmp(fn+k, backup_suffix) == 0;
  151: }
  152: 
  153: /* Search for a regular file that matches either (1) the size & modified
  154:  * time (plus the basename, if possible) or (2) the size & checksum.  If
  155:  * we find an exact match down to the dirname, return -1 because we found
  156:  * an up-to-date file in the transfer, not a renamed file. */
  157: static int fattr_find(struct file_struct *f, char *fname)
  158: {
  159: 	int low = the_fattr_list.low, high = the_fattr_list.high;
  160: 	int mid, ok_match = -1, good_match = -1;
  161: 	struct file_struct *fmid;
  162: 	int diff;
  163: 
  164: 	while (low <= high) {
  165: 		mid = (low + high) / 2;
  166: 		fmid = the_fattr_list.files[mid];
  167: 		if (F_LENGTH(fmid) != F_LENGTH(f)) {
  168: 			if (F_LENGTH(fmid) < F_LENGTH(f))
  169: 				low = mid + 1;
  170: 			else
  171: 				high = mid - 1;
  172: 			continue;
  173: 		}
  174: 		if (always_checksum) {
  175: 			/* We use the FLAG_FILE_SENT flag to indicate when we
  176: 			 * have computed the checksum for an entry. */
  177: 			if (!(f->flags & FLAG_FILE_SENT)) {
  178: 				if (fmid->modtime == f->modtime
  179: 				 && f_name_cmp(fmid, f) == 0)
  180: 					return -1; /* assume we can't help */
  181: 				file_checksum(fname, F_SUM(f), F_LENGTH(f));
  182: 				f->flags |= FLAG_FILE_SENT;
  183: 			}
  184: 			diff = u_memcmp(F_SUM(fmid), F_SUM(f), checksum_len);
  185: 			if (diff) {
  186: 				if (diff < 0)
  187: 					low = mid + 1;
  188: 				else
  189: 					high = mid - 1;
  190: 				continue;
  191: 			}
  192: 		} else {
  193: 			if (fmid->modtime != f->modtime) {
  194: 				if (fmid->modtime < f->modtime)
  195: 					low = mid + 1;
  196: 				else
  197: 					high = mid - 1;
  198: 				continue;
  199: 			}
  200: 		}
  201: 		ok_match = mid;
  202: 		diff = u_strcmp(fmid->basename, f->basename);
  203: 		if (diff == 0) {
  204: 			good_match = mid;
  205: 			if (fmid->dirname == f->dirname)
  206: 				return -1; /* file is up-to-date */
  207: 			if (!fmid->dirname) {
  208: 				low = mid + 1;
  209: 				continue;
  210: 			}
  211: 			if (!f->dirname) {
  212: 				high = mid - 1;
  213: 				continue;
  214: 			}
  215: 			diff = u_strcmp(fmid->dirname, f->dirname);
  216: 			if (diff == 0)
  217: 				return -1; /* file is up-to-date */
  218: 		}
  219: 		if (diff < 0)
  220: 			low = mid + 1;
  221: 		else
  222: 			high = mid - 1;
  223: 	}
  224: 
  225: 	return good_match >= 0 ? good_match : ok_match;
  226: }
  227: 
  228: static void look_for_rename(struct file_struct *file, char *fname)
  229: {
  230: 	struct file_struct *fp;
  231: 	char *partialptr, *fn;
  232: 	STRUCT_STAT st;
  233: 	int ndx;
  234: 
  235: 	if (!partial_dir || (ndx = fattr_find(file, fname)) < 0)
  236: 		return;
  237: 
  238: 	fp = the_fattr_list.files[ndx];
  239: 	fn = f_name(fp, NULL);
  240: 	/* We don't provide an alternate-basis file if there is a basis file. */
  241: 	if (link_stat(fn, &st, 0) == 0)
  242: 		return;
  243: 
  244: 	if (!dry_run) {
  245: 		if ((partialptr = partial_dir_fname(fn)) == NULL
  246: 		 || !handle_partial_dir(partialptr, PDIR_CREATE))
  247: 			return;
  248: 		/* We only use the file if we can hard-link it into our tmp dir. */
  249: 		if (link(fname, partialptr) != 0) {
  250: 			if (errno != EEXIST)
  251: 				handle_partial_dir(partialptr, PDIR_DELETE);
  252: 			return;
  253: 		}
  254: 	}
  255: 
  256: 	/* I think this falls into the -vv category with "%s is uptodate", etc. */
  257: 	if (verbose > 1)
  258: 		rprintf(FINFO, "found renamed: %s => %s\n", fname, fn);
  259: }
  260: 
  261: /* Delete a file or directory.  If DEL_RECURSE is set in the flags, this will
  262:  * delete recursively.
  263:  *
  264:  * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
  265:  * a directory! (The buffer is used for recursion, but returned unchanged.)
  266:  *
  267:  * Also note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
  268:  */
  269: static enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
  270: {
  271: 	enum delret ret;
  272: 	char *what;
  273: 	int ok;
  274: 
  275: 	if (verbose > 2) {
  276: 		rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
  277: 			fbuf, (int)mode, (int)flags);
  278: 	}
  279: 
  280: 	if (flags & DEL_NO_UID_WRITE)
  281: 		do_chmod(fbuf, mode | S_IWUSR);
  282: 
  283: 	if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
  284: 		/* This only happens on the first call to delete_item() since
  285: 		 * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
  286: 		ignore_perishable = 1;
  287: 		/* If DEL_RECURSE is not set, this just reports emptiness. */
  288: 		ret = delete_dir_contents(fbuf, flags);
  289: 		ignore_perishable = 0;
  290: 		if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
  291: 			goto check_ret;
  292: 		/* OK: try to delete the directory. */
  293: 	}
  294: 	if (flags & DEL_NO_DELETIONS)
  295: 		return DR_SUCCESS;
  296: 
  297: 	if (!(flags & DEL_MAKE_ROOM) && max_delete >= 0 && ++deletion_count > max_delete)
  298: 		return DR_AT_LIMIT;
  299: 
  300: 	if (S_ISDIR(mode)) {
  301: 		what = "rmdir";
  302: 		ok = do_rmdir(fbuf) == 0;
  303: 	} else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
  304: 		what = "make_backup";
  305: 		ok = make_backup(fbuf);
  306: 	} else {
  307: 		what = "unlink";
  308: 		ok = robust_unlink(fbuf) == 0;
  309: 	}
  310: 
  311: 	if (ok) {
  312: 		if (!(flags & DEL_MAKE_ROOM))
  313: 			log_delete(fbuf, mode);
  314: 		ret = DR_SUCCESS;
  315: 	} else {
  316: 		if (S_ISDIR(mode) && errno == ENOTEMPTY) {
  317: 			rprintf(FINFO, "cannot delete non-empty directory: %s\n",
  318: 				fbuf);
  319: 			ret = DR_NOT_EMPTY;
  320: 		} else if (errno != ENOENT) {
  321: 			rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
  322: 				what, fbuf);
  323: 			ret = DR_FAILURE;
  324: 		} else {
  325: 			deletion_count--;
  326: 			ret = DR_SUCCESS;
  327: 		}
  328: 	}
  329: 
  330:   check_ret:
  331: 	if (ret != DR_SUCCESS && flags & DEL_MAKE_ROOM) {
  332: 		const char *desc;
  333: 		switch (flags & DEL_MAKE_ROOM) {
  334: 		case DEL_FOR_FILE: desc = "regular file"; break;
  335: 		case DEL_FOR_DIR: desc = "directory"; break;
  336: 		case DEL_FOR_SYMLINK: desc = "symlink"; break;
  337: 		case DEL_FOR_DEVICE: desc = "device file"; break;
  338: 		case DEL_FOR_SPECIAL: desc = "special file"; break;
  339: 		default: exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE */
  340: 		}
  341: 		rprintf(FERROR_XFER, "could not make way for new %s: %s\n",
  342: 			desc, fbuf);
  343: 	}
  344: 	return ret;
  345: }
  346: 
  347: /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
  348:  * its contents, otherwise just checks for content.  Returns DR_SUCCESS or
  349:  * DR_NOT_EMPTY.  Note that fname must point to a MAXPATHLEN buffer!  (The
  350:  * buffer is used for recursion, but returned unchanged.)
  351:  *
  352:  * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
  353:  */
  354: static enum delret delete_dir_contents(char *fname, uint16 flags)
  355: {
  356: 	struct file_list *dirlist;
  357: 	enum delret ret;
  358: 	unsigned remainder;
  359: 	void *save_filters;
  360: 	int j, dlen;
  361: 	char *p;
  362: 
  363: 	if (verbose > 3) {
  364: 		rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
  365: 			fname, flags);
  366: 	}
  367: 
  368: 	dlen = strlen(fname);
  369: 	save_filters = push_local_filters(fname, dlen);
  370: 
  371: 	non_perishable_cnt = 0;
  372: 	file_extra_cnt += SUM_EXTRA_CNT;
  373: 	dirlist = get_dirlist(fname, dlen, 0);
  374: 	file_extra_cnt -= SUM_EXTRA_CNT;
  375: 	ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
  376: 
  377: 	if (!dirlist->used)
  378: 		goto done;
  379: 
  380: 	if (!(flags & DEL_RECURSE)) {
  381: 		ret = DR_NOT_EMPTY;
  382: 		goto done;
  383: 	}
  384: 
  385: 	p = fname + dlen;
  386: 	if (dlen != 1 || *fname != '/')
  387: 		*p++ = '/';
  388: 	remainder = MAXPATHLEN - (p - fname);
  389: 
  390: 	/* We do our own recursion, so make delete_item() non-recursive. */
  391: 	flags = (flags & ~(DEL_RECURSE|DEL_MAKE_ROOM|DEL_NO_UID_WRITE))
  392: 	      | DEL_DIR_IS_EMPTY;
  393: 
  394: 	for (j = dirlist->used; j--; ) {
  395: 		struct file_struct *fp = dirlist->files[j];
  396: 
  397: 		if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
  398: 			if (verbose > 1) {
  399: 				rprintf(FINFO,
  400: 				    "mount point, %s, pins parent directory\n",
  401: 				    f_name(fp, NULL));
  402: 			}
  403: 			ret = DR_NOT_EMPTY;
  404: 			continue;
  405: 		}
  406: 
  407: 		strlcpy(p, fp->basename, remainder);
  408: 		if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
  409: 			do_chmod(fname, fp->mode | S_IWUSR);
  410: 		/* Save stack by recursing to ourself directly. */
  411: 		if (S_ISDIR(fp->mode)) {
  412: 			if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
  413: 				ret = DR_NOT_EMPTY;
  414: 		} else if (detect_renamed && S_ISREG(fp->mode))
  415: 			look_for_rename(fp, fname);
  416: 		if (delete_item(fname, fp->mode, flags) != DR_SUCCESS)
  417: 			ret = DR_NOT_EMPTY;
  418: 	}
  419: 
  420: 	fname[dlen] = '\0';
  421: 
  422:   done:
  423: 	flist_free(dirlist);
  424: 	pop_local_filters(save_filters);
  425: 
  426: 	if (ret == DR_NOT_EMPTY) {
  427: 		rprintf(FINFO, "cannot delete non-empty directory: %s\n",
  428: 			fname);
  429: 	}
  430: 	return ret;
  431: }
  432: 
  433: static int start_delete_delay_temp(void)
  434: {
  435: 	char fnametmp[MAXPATHLEN];
  436: 	int save_dry_run = dry_run;
  437: 
  438: 	dry_run = 0;
  439: 	if (!get_tmpname(fnametmp, "deldelay")
  440: 	 || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
  441: 		rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
  442: 			inc_recurse ? "" : " -- switching to --delete-after");
  443: 		delete_during = 0;
  444: 		delete_after = !inc_recurse;
  445: 		dry_run = save_dry_run;
  446: 		return 0;
  447: 	}
  448: 	unlink(fnametmp);
  449: 	dry_run = save_dry_run;
  450: 	return 1;
  451: }
  452: 
  453: static int flush_delete_delay(void)
  454: {
  455: 	if (deldelay_fd < 0 && !start_delete_delay_temp())
  456: 		return 0;
  457: 	if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
  458: 		rsyserr(FERROR, errno, "flush of delete-delay buffer");
  459: 		delete_during = 0;
  460: 		delete_after = !inc_recurse;
  461: 		close(deldelay_fd);
  462: 		return 0;
  463: 	}
  464: 	deldelay_cnt = 0;
  465: 	return 1;
  466: }
  467: 
  468: static int remember_delete(struct file_struct *file, const char *fname, int flags)
  469: {
  470: 	int len;
  471: 
  472: 	if (deldelay_cnt == deldelay_size && !flush_delete_delay())
  473: 		return 0;
  474: 
  475: 	if (flags & DEL_NO_UID_WRITE)
  476: 		deldelay_buf[deldelay_cnt++] = '!';
  477: 
  478: 	while (1) {
  479: 		len = snprintf(deldelay_buf + deldelay_cnt,
  480: 			       deldelay_size - deldelay_cnt,
  481: 			       "%x %s%c",
  482: 			       (int)file->mode, fname, '\0');
  483: 		if ((deldelay_cnt += len) <= deldelay_size)
  484: 			break;
  485: 		deldelay_cnt -= len;
  486: 		if (!flush_delete_delay())
  487: 			return 0;
  488: 	}
  489: 
  490: 	return 1;
  491: }
  492: 
  493: static int read_delay_line(char *buf, int *flags_p)
  494: {
  495: 	static int read_pos = 0;
  496: 	int j, len, mode;
  497: 	char *bp, *past_space;
  498: 
  499: 	while (1) {
  500: 		for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
  501: 		if (j < deldelay_cnt)
  502: 			break;
  503: 		if (deldelay_fd < 0) {
  504: 			if (j > read_pos)
  505: 				goto invalid_data;
  506: 			return -1;
  507: 		}
  508: 		deldelay_cnt -= read_pos;
  509: 		if (deldelay_cnt == deldelay_size)
  510: 			goto invalid_data;
  511: 		if (deldelay_cnt && read_pos) {
  512: 			memmove(deldelay_buf, deldelay_buf + read_pos,
  513: 				deldelay_cnt);
  514: 		}
  515: 		len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
  516: 			   deldelay_size - deldelay_cnt);
  517: 		if (len == 0) {
  518: 			if (deldelay_cnt) {
  519: 				rprintf(FERROR,
  520: 				    "ERROR: unexpected EOF in delete-delay file.\n");
  521: 			}
  522: 			return -1;
  523: 		}
  524: 		if (len < 0) {
  525: 			rsyserr(FERROR, errno,
  526: 				"reading delete-delay file");
  527: 			return -1;
  528: 		}
  529: 		deldelay_cnt += len;
  530: 		read_pos = 0;
  531: 	}
  532: 
  533: 	bp = deldelay_buf + read_pos;
  534: 	if (*bp == '!') {
  535: 		bp++;
  536: 		*flags_p = DEL_NO_UID_WRITE;
  537: 	} else
  538: 		*flags_p = 0;
  539: 
  540: 	if (sscanf(bp, "%x ", &mode) != 1) {
  541: 	  invalid_data:
  542: 		rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
  543: 		return -1;
  544: 	}
  545: 	past_space = strchr(bp, ' ') + 1;
  546: 	len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
  547: 	read_pos = j + 1;
  548: 
  549: 	if (len > MAXPATHLEN) {
  550: 		rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
  551: 		return -1;
  552: 	}
  553: 
  554: 	/* The caller needs the name in a MAXPATHLEN buffer, so we copy it
  555: 	 * instead of returning a pointer to our buffer. */
  556: 	memcpy(buf, past_space, len);
  557: 
  558: 	return mode;
  559: }
  560: 
  561: static void do_delayed_deletions(char *delbuf)
  562: {
  563: 	int mode, flags;
  564: 
  565: 	if (deldelay_fd >= 0) {
  566: 		if (deldelay_cnt && !flush_delete_delay())
  567: 			return;
  568: 		lseek(deldelay_fd, 0, 0);
  569: 	}
  570: 	while ((mode = read_delay_line(delbuf, &flags)) >= 0)
  571: 		delete_item(delbuf, mode, flags | DEL_RECURSE);
  572: 	if (deldelay_fd >= 0)
  573: 		close(deldelay_fd);
  574: }
  575: 
  576: /* This function is used to implement per-directory deletion, and is used by
  577:  * all the --delete-WHEN options.  Note that the fbuf pointer must point to a
  578:  * MAXPATHLEN buffer with the name of the directory in it (the functions we
  579:  * call will append names onto the end, but the old dir value will be restored
  580:  * on exit).
  581:  *
  582:  * Note:  --detect-rename may use this routine with DEL_NO_DELETIONS set!
  583:  */
  584: static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev,
  585: 			  int del_flags)
  586: {
  587: 	static int already_warned = 0;
  588: 	struct file_list *dirlist;
  589: 	char *p, delbuf[MAXPATHLEN];
  590: 	unsigned remainder;
  591: 	int dlen, i, restore_dot = 0;
  592: 
  593: 	if (!fbuf) {
  594: 		change_local_filter_dir(NULL, 0, 0);
  595: 		return;
  596: 	}
  597: 
  598: 	if (verbose > 2)
  599: 		rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
  600: 
  601: 	if (allowed_lull)
  602: 		maybe_send_keepalive();
  603: 
  604: 	if (io_error && !ignore_errors) {
  605: 		if (!already_warned) {
  606: 			rprintf(FINFO,
  607: 			    "IO error encountered -- skipping file deletion\n");
  608: 			already_warned = 1;
  609: 		}
  610: 		if (!detect_renamed)
  611: 			return;
  612: 		del_flags |= DEL_NO_DELETIONS;
  613: 	}
  614: 
  615: 	dlen = strlen(fbuf);
  616: 	change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
  617: 
  618: 	if (detect_renamed)
  619: 		unexplored_dirs--;
  620: 
  621: 	if (one_file_system) {
  622: 		if (file->flags & FLAG_TOP_DIR)
  623: 			filesystem_dev = *fs_dev;
  624: 		else if (filesystem_dev != *fs_dev)
  625: 			return;
  626: 	}
  627: 
  628: 	dirlist = get_dirlist(fbuf, dlen, 0);
  629: 
  630: 	p = fbuf + dlen;
  631: 	if (dlen == 1 && *fbuf == '.') {
  632: 		restore_dot = 1;
  633: 		p = fbuf;
  634: 	} else if (dlen != 1 || *fbuf != '/')
  635: 		*p++ = '/';
  636: 	remainder = MAXPATHLEN - (p - fbuf);
  637: 
  638: 	/* If an item in dirlist is not found in flist, delete it
  639: 	 * from the filesystem. */
  640: 	for (i = dirlist->used; i--; ) {
  641: 		struct file_struct *fp = dirlist->files[i];
  642: 		if (!F_IS_ACTIVE(fp))
  643: 			continue;
  644: 		if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
  645: 			if (verbose > 1)
  646: 				rprintf(FINFO, "cannot delete mount point: %s\n",
  647: 					f_name(fp, NULL));
  648: 			continue;
  649: 		}
  650: 		if (detect_renamed && S_ISREG(fp->mode)) {
  651: 			strlcpy(p, fp->basename, remainder);
  652: 			look_for_rename(fp, fbuf);
  653: 		}
  654: 		/* Here we want to match regardless of file type.  Replacement
  655: 		 * of a file with one of another type is handled separately by
  656: 		 * a delete_item call with a DEL_MAKE_ROOM flag. */
  657: 		if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
  658: 			int flags = DEL_RECURSE;
  659: 			if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
  660: 				flags |= DEL_NO_UID_WRITE;
  661: 			f_name(fp, delbuf);
  662: 			if (delete_during == 2 && !(del_flags & DEL_NO_DELETIONS)) {
  663: 				if (!remember_delete(fp, delbuf, del_flags | flags))
  664: 					break;
  665: 			} else
  666: 				delete_item(delbuf, fp->mode, del_flags | flags);
  667: 		} else if (detect_renamed && S_ISDIR(fp->mode))
  668: 			unexplored_dirs++;
  669: 	}
  670: 
  671: 	if (restore_dot)
  672: 		fbuf[0] = '.';
  673: 	fbuf[dlen] = '\0';
  674: 
  675: 	flist_free(dirlist);
  676: }
  677: 
  678: /* This deletes any files on the receiving side that are not present on the
  679:  * sending side.  This is used by --delete-before and --delete-after. */
  680: static void do_delete_pass(void)
  681: {
  682: 	char fbuf[MAXPATHLEN];
  683: 	STRUCT_STAT st;
  684: 	int j;
  685: 
  686: 	/* dry_run is incremented when the destination doesn't exist yet. */
  687: 	if (dry_run > 1 || list_only)
  688: 		return;
  689: 
  690: 	for (j = 0; j < cur_flist->used; j++) {
  691: 		struct file_struct *file = cur_flist->sorted[j];
  692: 
  693: 		f_name(file, fbuf);
  694: 
  695: 		if (!(file->flags & FLAG_CONTENT_DIR)) {
  696: 			change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
  697: 			continue;
  698: 		}
  699: 
  700: 		if (verbose > 1 && file->flags & FLAG_TOP_DIR)
  701: 			rprintf(FINFO, "deleting in %s\n", fbuf);
  702: 
  703: 		if (link_stat(fbuf, &st, keep_dirlinks) < 0
  704: 		 || !S_ISDIR(st.st_mode))
  705: 			continue;
  706: 
  707: 		delete_in_dir(fbuf, file, &st.st_dev, 0);
  708: 	}
  709: 	delete_in_dir(NULL, NULL, &dev_zero, 0);
  710: 
  711: 	if (do_progress && !am_server)
  712: 		rprintf(FINFO, "                    \r");
  713: }
  714: 
  715: static inline int time_differs(struct file_struct *file, stat_x *sxp)
  716: {
  717: 	return cmp_time(sxp->st.st_mtime, file->modtime);
  718: }
  719: 
  720: static inline int perms_differ(struct file_struct *file, stat_x *sxp)
  721: {
  722: 	if (preserve_perms)
  723: 		return !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS);
  724: 
  725: 	if (preserve_executability)
  726: 		return (sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0);
  727: 
  728: 	return 0;
  729: }
  730: 
  731: static inline int ownership_differs(struct file_struct *file, stat_x *sxp)
  732: {
  733: 	if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
  734: 		return 1;
  735: 
  736: 	if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
  737: 		return 1;
  738: 
  739: 	return 0;
  740: }
  741: 
  742: #ifdef SUPPORT_ACLS
  743: static inline int acls_differ(const char *fname, struct file_struct *file, stat_x *sxp)
  744: {
  745: 	if (preserve_acls) {
  746: 		if (!ACL_READY(*sxp))
  747: 			get_acl(fname, sxp);
  748: 		if (set_acl(NULL, file, sxp, file->mode))
  749: 			return 1;
  750: 	}
  751: 
  752: 	return 0;
  753: }
  754: #endif
  755: 
  756: #ifdef SUPPORT_XATTRS
  757: static inline int xattrs_differ(const char *fname, struct file_struct *file, stat_x *sxp)
  758: {
  759: 	if (preserve_xattrs) {
  760: 		if (!XATTR_READY(*sxp))
  761: 			get_xattr(fname, sxp);
  762: 		if (xattr_diff(file, sxp, 0))
  763: 			return 1;
  764: 	}
  765: 
  766: 	return 0;
  767: }
  768: #endif
  769: 
  770: int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
  771: {
  772: 	if (S_ISLNK(file->mode)) {
  773: #ifdef CAN_SET_SYMLINK_TIMES
  774: 		if (preserve_times & PRESERVE_LINK_TIMES && time_differs(file, sxp))
  775: 			return 0;
  776: #endif
  777: #ifdef CAN_CHMOD_SYMLINK
  778: 		if (perms_differ(file, sxp))
  779: 			return 0;
  780: #endif
  781: #ifdef CAN_CHOWN_SYMLINK
  782: 		if (ownership_differs(file, sxp))
  783: 			return 0;
  784: #endif
  785: #if defined SUPPORT_ACLS && 0 /* no current symlink-ACL support */
  786: 		if (acls_differ(fname, file, sxp))
  787: 			return 0;
  788: #endif
  789: #if defined SUPPORT_XATTRS && !defined NO_SYMLINK_XATTRS
  790: 		if (xattrs_differ(fname, file, sxp))
  791: 			return 0;
  792: #endif
  793: 	} else {
  794: 		if (preserve_times && time_differs(file, sxp))
  795: 			return 0;
  796: 		if (perms_differ(file, sxp))
  797: 			return 0;
  798: 		if (ownership_differs(file, sxp))
  799: 			return 0;
  800: #ifdef SUPPORT_ACLS
  801: 		if (acls_differ(fname, file, sxp))
  802: 			return 0;
  803: #endif
  804: #ifdef SUPPORT_XATTRS
  805: 		if (xattrs_differ(fname, file, sxp))
  806: 			return 0;
  807: #endif
  808: 	}
  809: 
  810: 	return 1;
  811: }
  812: 
  813: void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
  814: 	     stat_x *sxp, int32 iflags, uchar fnamecmp_type,
  815: 	     const char *xname)
  816: {
  817: 	if (statret >= 0) { /* A from-dest-dir statret can == 1! */
  818: 		int keep_time = !preserve_times ? 0
  819: 		    : S_ISDIR(file->mode) ? preserve_times & PRESERVE_DIR_TIMES
  820: 		    : S_ISLNK(file->mode) ? preserve_times & PRESERVE_LINK_TIMES
  821: 		    : 1;
  822: 
  823: 		if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
  824: 			iflags |= ITEM_REPORT_SIZE;
  825: 		if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
  826: 			if (iflags & ITEM_LOCAL_CHANGE)
  827: 				iflags |= symlink_timeset_failed_flags;
  828: 		} else if (keep_time
  829: 		 ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
  830: 		 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
  831: 		  && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
  832: 			iflags |= ITEM_REPORT_TIME;
  833: #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
  834: 		if (S_ISLNK(file->mode)) {
  835: 			;
  836: 		} else
  837: #endif
  838: 		if (preserve_perms) {
  839: 			if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
  840: 				iflags |= ITEM_REPORT_PERMS;
  841: 		} else if (preserve_executability
  842: 		 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
  843: 			iflags |= ITEM_REPORT_PERMS;
  844: 		if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
  845: 			iflags |= ITEM_REPORT_OWNER;
  846: 		if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
  847: 		    && sxp->st.st_gid != (gid_t)F_GROUP(file))
  848: 			iflags |= ITEM_REPORT_GROUP;
  849: #ifdef SUPPORT_ACLS
  850: 		if (preserve_acls && !S_ISLNK(file->mode)) {
  851: 			if (!ACL_READY(*sxp))
  852: 				get_acl(fnamecmp, sxp);
  853: 			if (set_acl(NULL, file, sxp, file->mode))
  854: 				iflags |= ITEM_REPORT_ACL;
  855: 		}
  856: #endif
  857: #ifdef SUPPORT_XATTRS
  858: 		if (preserve_xattrs) {
  859: 			if (!XATTR_READY(*sxp))
  860: 				get_xattr(fnamecmp, sxp);
  861: 			if (xattr_diff(file, sxp, 1))
  862: 				iflags |= ITEM_REPORT_XATTR;
  863: 		}
  864: #endif
  865: 	} else {
  866: #ifdef SUPPORT_XATTRS
  867: 		if (preserve_xattrs && xattr_diff(file, NULL, 1))
  868: 			iflags |= ITEM_REPORT_XATTR;
  869: #endif
  870: 		iflags |= ITEM_IS_NEW;
  871: 	}
  872: 
  873: 	iflags &= 0xffff;
  874: 	if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
  875: 	  || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
  876: 		if (protocol_version >= 29) {
  877: 			if (ndx >= 0)
  878: 				write_ndx(sock_f_out, ndx);
  879: 			write_shortint(sock_f_out, iflags);
  880: 			if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
  881: 				write_byte(sock_f_out, fnamecmp_type);
  882: 			if (iflags & ITEM_XNAME_FOLLOWS)
  883: 				write_vstring(sock_f_out, xname, strlen(xname));
  884: #ifdef SUPPORT_XATTRS
  885: 			if (preserve_xattrs && do_xfers
  886: 			 && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
  887: 				send_xattr_request(NULL, file,
  888: 					iflags & ITEM_REPORT_XATTR ? sock_f_out : -1);
  889: 			}
  890: #endif
  891: 		} else if (ndx >= 0) {
  892: 			enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
  893: 			log_item(code, file, &stats, iflags, xname);
  894: 		}
  895: 	}
  896: }
  897: 
  898: 
  899: /* Perform our quick-check heuristic for determining if a file is unchanged. */
  900: int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
  901: {
  902: 	if (st->st_size != F_LENGTH(file))
  903: 		return 0;
  904: 
  905: 	/* if always checksum is set then we use the checksum instead
  906: 	   of the file time to determine whether to sync */
  907: 	if (always_checksum > 0 && S_ISREG(st->st_mode)) {
  908: 		char sum[MAX_DIGEST_LEN];
  909: 		file_checksum(fn, sum, st->st_size);
  910: 		return memcmp(sum, F_SUM(file), checksum_len) == 0;
  911: 	}
  912: 
  913: 	if (size_only > 0)
  914: 		return 1;
  915: 
  916: 	if (ignore_times)
  917: 		return 0;
  918: 
  919: 	return cmp_time(st->st_mtime, file->modtime) == 0;
  920: }
  921: 
  922: 
  923: /*
  924:  * set (initialize) the size entries in the per-file sum_struct
  925:  * calculating dynamic block and checksum sizes.
  926:  *
  927:  * This is only called from generate_and_send_sums() but is a separate
  928:  * function to encapsulate the logic.
  929:  *
  930:  * The block size is a rounded square root of file length.
  931:  *
  932:  * The checksum size is determined according to:
  933:  *     blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
  934:  * provided by Donovan Baarda which gives a probability of rsync
  935:  * algorithm corrupting data and falling back using the whole md4
  936:  * checksums.
  937:  *
  938:  * This might be made one of several selectable heuristics.
  939:  */
  940: static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
  941: {
  942: 	int32 blength;
  943: 	int s2length;
  944: 	int64 l;
  945: 
  946: 	if (len < 0) {
  947: 		/* The file length overflowed our int64 var, so we can't process this file. */
  948: 		sum->count = -1; /* indicate overflow error */
  949: 		return;
  950: 	}
  951: 
  952: 	if (block_size)
  953: 		blength = block_size;
  954: 	else if (len <= BLOCK_SIZE * BLOCK_SIZE)
  955: 		blength = BLOCK_SIZE;
  956: 	else {
  957: 		int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
  958: 		int32 c;
  959: 		int cnt;
  960: 		for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
  961: 		if (c < 0 || c >= max_blength)
  962: 			blength = max_blength;
  963: 		else {
  964: 		    blength = 0;
  965: 		    do {
  966: 			    blength |= c;
  967: 			    if (len < (int64)blength * blength)
  968: 				    blength &= ~c;
  969: 			    c >>= 1;
  970: 		    } while (c >= 8);	/* round to multiple of 8 */
  971: 		    blength = MAX(blength, BLOCK_SIZE);
  972: 		}
  973: 	}
  974: 
  975: 	if (protocol_version < 27) {
  976: 		s2length = csum_length;
  977: 	} else if (csum_length == SUM_LENGTH) {
  978: 		s2length = SUM_LENGTH;
  979: 	} else {
  980: 		int32 c;
  981: 		int b = BLOCKSUM_BIAS;
  982: 		for (l = len; l >>= 1; b += 2) {}
  983: 		for (c = blength; (c >>= 1) && b; b--) {}
  984: 		/* add a bit, subtract rollsum, round up. */
  985: 		s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
  986: 		s2length = MAX(s2length, csum_length);
  987: 		s2length = MIN(s2length, SUM_LENGTH);
  988: 	}
  989: 
  990: 	sum->flength	= len;
  991: 	sum->blength	= blength;
  992: 	sum->s2length	= s2length;
  993: 	sum->remainder	= (int32)(len % blength);
  994: 	sum->count	= (int32)(l = (len / blength) + (sum->remainder != 0));
  995: 
  996: 	if ((int64)sum->count != l)
  997: 		sum->count = -1;
  998: 
  999: 	if (sum->count && verbose > 2) {
 1000: 		rprintf(FINFO,
 1001: 			"count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
 1002: 			(double)sum->count, (long)sum->remainder, (long)sum->blength,
 1003: 			sum->s2length, (double)sum->flength);
 1004: 	}
 1005: }
 1006: 
 1007: 
 1008: /*
 1009:  * Generate and send a stream of signatures/checksums that describe a buffer
 1010:  *
 1011:  * Generate approximately one checksum every block_len bytes.
 1012:  */
 1013: static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
 1014: {
 1015: 	int32 i;
 1016: 	struct map_struct *mapbuf;
 1017: 	struct sum_struct sum;
 1018: 	OFF_T offset = 0;
 1019: 
 1020: 	sum_sizes_sqroot(&sum, len);
 1021: 	if (sum.count < 0)
 1022: 		return -1;
 1023: 	write_sum_head(f_out, &sum);
 1024: 
 1025: 	if (append_mode > 0 && f_copy < 0)
 1026: 		return 0;
 1027: 
 1028: 	if (len > 0)
 1029: 		mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
 1030: 	else
 1031: 		mapbuf = NULL;
 1032: 
 1033: 	for (i = 0; i < sum.count; i++) {
 1034: 		int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
 1035: 		char *map = map_ptr(mapbuf, offset, n1);
 1036: 		char sum2[SUM_LENGTH];
 1037: 		uint32 sum1;
 1038: 
 1039: 		len -= n1;
 1040: 		offset += n1;
 1041: 
 1042: 		if (f_copy >= 0) {
 1043: 			full_write(f_copy, map, n1);
 1044: 			if (append_mode > 0)
 1045: 				continue;
 1046: 		}
 1047: 
 1048: 		sum1 = get_checksum1(map, n1);
 1049: 		get_checksum2(map, n1, sum2);
 1050: 
 1051: 		if (verbose > 3) {
 1052: 			rprintf(FINFO,
 1053: 				"chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
 1054: 				(double)i, (double)offset - n1, (long)n1,
 1055: 				(unsigned long)sum1);
 1056: 		}
 1057: 		write_int(f_out, sum1);
 1058: 		write_buf(f_out, sum2, sum.s2length);
 1059: 	}
 1060: 
 1061: 	if (mapbuf)
 1062: 		unmap_file(mapbuf);
 1063: 
 1064: 	return 0;
 1065: }
 1066: 
 1067: 
 1068: /* Try to find a filename in the same dir as "fname" with a similar name. */
 1069: static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
 1070: {
 1071: 	int fname_len, fname_suf_len;
 1072: 	const char *fname_suf, *fname = file->basename;
 1073: 	uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
 1074: 	int j, lowest_j = -1;
 1075: 
 1076: 	fname_len = strlen(fname);
 1077: 	fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
 1078: 
 1079: 	for (j = 0; j < dirlist->used; j++) {
 1080: 		struct file_struct *fp = dirlist->files[j];
 1081: 		const char *suf, *name;
 1082: 		int len, suf_len;
 1083: 		uint32 dist;
 1084: 
 1085: 		if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
 1086: 		 || fp->flags & FLAG_FILE_SENT)
 1087: 			continue;
 1088: 
 1089: 		name = fp->basename;
 1090: 
 1091: 		if (F_LENGTH(fp) == F_LENGTH(file)
 1092: 		    && cmp_time(fp->modtime, file->modtime) == 0) {
 1093: 			if (verbose > 4) {
 1094: 				rprintf(FINFO,
 1095: 					"fuzzy size/modtime match for %s\n",
 1096: 					name);
 1097: 			}
 1098: 			return j;
 1099: 		}
 1100: 
 1101: 		len = strlen(name);
 1102: 		suf = find_filename_suffix(name, len, &suf_len);
 1103: 
 1104: 		dist = fuzzy_distance(name, len, fname, fname_len);
 1105: 		/* Add some extra weight to how well the suffixes match. */
 1106: 		dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
 1107: 		      * 10;
 1108: 		if (verbose > 4) {
 1109: 			rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
 1110: 				name, (int)(dist>>16), (int)(dist&0xFFFF));
 1111: 		}
 1112: 		if (dist <= lowest_dist) {
 1113: 			lowest_dist = dist;
 1114: 			lowest_j = j;
 1115: 		}
 1116: 	}
 1117: 
 1118: 	return lowest_j;
 1119: }
 1120: 
 1121: /* Copy a file found in our --copy-dest handling. */
 1122: static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
 1123: {
 1124: 	char buf[MAXPATHLEN];
 1125: 	const char *copy_to, *partialptr;
 1126: 	int save_preserve_xattrs = preserve_xattrs;
 1127: 	int ok, fd_w;
 1128: 
 1129: 	if (inplace) {
 1130: 		/* Let copy_file open the destination in place. */
 1131: 		fd_w = -1;
 1132: 		copy_to = dest;
 1133: 	} else {
 1134: 		fd_w = open_tmpfile(buf, dest, file);
 1135: 		if (fd_w < 0)
 1136: 			return -1;
 1137: 		copy_to = buf;
 1138: 	}
 1139: 	cleanup_set(copy_to, NULL, NULL, -1, -1);
 1140: 	if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
 1141: 		if (verbose) {
 1142: 			rsyserr(FINFO, errno, "copy_file %s => %s",
 1143: 				full_fname(src), copy_to);
 1144: 		}
 1145: 		/* Try to clean up. */
 1146: 		unlink(copy_to);
 1147: 		cleanup_disable();
 1148: 		return -1;
 1149: 	}
 1150: 	partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
 1151: 	preserve_xattrs = 0; /* xattrs were copied with file */
 1152: 	ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
 1153: 	preserve_xattrs = save_preserve_xattrs;
 1154: 	cleanup_disable();
 1155: 	return ok ? 0 : -1;
 1156: }
 1157: 
 1158: /* This is only called for regular files.  We return -2 if we've finished
 1159:  * handling the file, -1 if no dest-linking occurred, or a non-negative
 1160:  * value if we found an alternate basis file. */
 1161: static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
 1162: 			 char *cmpbuf, stat_x *sxp, int itemizing,
 1163: 			 enum logcode code)
 1164: {
 1165: 	int best_match = -1;
 1166: 	int match_level = 0;
 1167: 	int j = 0;
 1168: 
 1169: 	do {
 1170: 		pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
 1171: 		if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
 1172: 			continue;
 1173: 		switch (match_level) {
 1174: 		case 0:
 1175: 			best_match = j;
 1176: 			match_level = 1;
 1177: 			/* FALL THROUGH */
 1178: 		case 1:
 1179: 			if (!unchanged_file(cmpbuf, file, &sxp->st))
 1180: 				continue;
 1181: 			best_match = j;
 1182: 			match_level = 2;
 1183: 			/* FALL THROUGH */
 1184: 		case 2:
 1185: 			if (!unchanged_attrs(cmpbuf, file, sxp))
 1186: 				continue;
 1187: 			best_match = j;
 1188: 			match_level = 3;
 1189: 			break;
 1190: 		}
 1191: 		break;
 1192: 	} while (basis_dir[++j] != NULL);
 1193: 
 1194: 	if (!match_level)
 1195: 		return -1;
 1196: 
 1197: 	if (j != best_match) {
 1198: 		j = best_match;
 1199: 		pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
 1200: 		if (link_stat(cmpbuf, &sxp->st, 0) < 0)
 1201: 			return -1;
 1202: 	}
 1203: 
 1204: 	if (match_level == 3 && !copy_dest) {
 1205: #ifdef SUPPORT_HARD_LINKS
 1206: 		if (link_dest) {
 1207: 			if (!hard_link_one(file, fname, cmpbuf, 1))
 1208: 				goto try_a_copy;
 1209: 			if (preserve_hard_links && F_IS_HLINKED(file))
 1210: 				finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
 1211: 			if (!maybe_ATTRS_REPORT && (verbose > 1 || stdout_format_has_i > 1)) {
 1212: 				itemize(cmpbuf, file, ndx, 1, sxp,
 1213: 					ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
 1214: 					0, "");
 1215: 			}
 1216: 		} else
 1217: #endif
 1218: 		if (itemizing)
 1219: 			itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
 1220: 		if (verbose > 1 && maybe_ATTRS_REPORT)
 1221: 			rprintf(FCLIENT, "%s is uptodate\n", fname);
 1222: 		return -2;
 1223: 	}
 1224: 
 1225: 	if (match_level >= 2) {
 1226: #ifdef SUPPORT_HARD_LINKS
 1227: 	  try_a_copy: /* Copy the file locally. */
 1228: #endif
 1229: 		if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
 1230: 			return -1;
 1231: 		if (itemizing)
 1232: 			itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
 1233: 		if (maybe_ATTRS_REPORT
 1234: 		 && ((!itemizing && verbose && match_level == 2)
 1235: 		  || (verbose > 1 && match_level == 3))) {
 1236: 			code = match_level == 3 ? FCLIENT : FINFO;
 1237: 			rprintf(code, "%s%s\n", fname,
 1238: 				match_level == 3 ? " is uptodate" : "");
 1239: 		}
 1240: #ifdef SUPPORT_HARD_LINKS
 1241: 		if (preserve_hard_links && F_IS_HLINKED(file))
 1242: 			finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
 1243: #endif
 1244: 		return -2;
 1245: 	}
 1246: 
 1247: 	return FNAMECMP_BASIS_DIR_LOW + j;
 1248: }
 1249: 
 1250: /* This is only called for non-regular files.  We return -2 if we've finished
 1251:  * handling the file, or -1 if no dest-linking occurred, or a non-negative
 1252:  * value if we found an alternate basis file. */
 1253: static int try_dests_non(struct file_struct *file, char *fname, int ndx,
 1254: 			 char *cmpbuf, stat_x *sxp, int itemizing,
 1255: 			 enum logcode code)
 1256: {
 1257: 	char lnk[MAXPATHLEN];
 1258: 	int best_match = -1;
 1259: 	int match_level = 0;
 1260: 	enum nonregtype type;
 1261: 	uint32 *devp;
 1262: 	int len, j = 0;
 1263: 
 1264: #ifndef SUPPORT_LINKS
 1265: 	if (S_ISLNK(file->mode))
 1266: 		return -1;
 1267: #endif
 1268: 	if (S_ISDIR(file->mode)) {
 1269: 		type = TYPE_DIR;
 1270: 	} else if (IS_SPECIAL(file->mode))
 1271: 		type = TYPE_SPECIAL;
 1272: 	else if (IS_DEVICE(file->mode))
 1273: 		type = TYPE_DEVICE;
 1274: #ifdef SUPPORT_LINKS
 1275: 	else if (S_ISLNK(file->mode))
 1276: 		type = TYPE_SYMLINK;
 1277: #endif
 1278: 	else {
 1279: 		rprintf(FERROR,
 1280: 			"internal: try_dests_non() called with invalid mode (%o)\n",
 1281: 			(int)file->mode);
 1282: 		exit_cleanup(RERR_UNSUPPORTED);
 1283: 	}
 1284: 
 1285: 	do {
 1286: 		pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
 1287: 		if (link_stat(cmpbuf, &sxp->st, 0) < 0)
 1288: 			continue;
 1289: 		switch (type) {
 1290: 		case TYPE_DIR:
 1291: 			if (!S_ISDIR(sxp->st.st_mode))
 1292: 				continue;
 1293: 			break;
 1294: 		case TYPE_SPECIAL:
 1295: 			if (!IS_SPECIAL(sxp->st.st_mode))
 1296: 				continue;
 1297: 			break;
 1298: 		case TYPE_DEVICE:
 1299: 			if (!IS_DEVICE(sxp->st.st_mode))
 1300: 				continue;
 1301: 			break;
 1302: #ifdef SUPPORT_LINKS
 1303: 		case TYPE_SYMLINK:
 1304: 			if (!S_ISLNK(sxp->st.st_mode))
 1305: 				continue;
 1306: 			break;
 1307: #endif
 1308: 		}
 1309: 		if (match_level < 1) {
 1310: 			match_level = 1;
 1311: 			best_match = j;
 1312: 		}
 1313: 		switch (type) {
 1314: 		case TYPE_DIR:
 1315: 		case TYPE_SPECIAL:
 1316: 			break;
 1317: 		case TYPE_DEVICE:
 1318: 			devp = F_RDEV_P(file);
 1319: 			if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
 1320: 				continue;
 1321: 			break;
 1322: #ifdef SUPPORT_LINKS
 1323: 		case TYPE_SYMLINK:
 1324: 			if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
 1325: 				continue;
 1326: 			lnk[len] = '\0';
 1327: 			if (strcmp(lnk, F_SYMLINK(file)) != 0)
 1328: 				continue;
 1329: 			break;
 1330: #endif
 1331: 		}
 1332: 		if (match_level < 2) {
 1333: 			match_level = 2;
 1334: 			best_match = j;
 1335: 		}
 1336: 		if (unchanged_attrs(cmpbuf, file, sxp)) {
 1337: 			match_level = 3;
 1338: 			best_match = j;
 1339: 			break;
 1340: 		}
 1341: 	} while (basis_dir[++j] != NULL);
 1342: 
 1343: 	if (!match_level)
 1344: 		return -1;
 1345: 
 1346: 	if (j != best_match) {
 1347: 		j = best_match;
 1348: 		pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
 1349: 		if (link_stat(cmpbuf, &sxp->st, 0) < 0)
 1350: 			return -1;
 1351: 	}
 1352: 
 1353: 	if (match_level == 3) {
 1354: #ifdef SUPPORT_HARD_LINKS
 1355: 		if (link_dest
 1356: #ifndef CAN_HARDLINK_SYMLINK
 1357: 		 && !S_ISLNK(file->mode)
 1358: #endif
 1359: #ifndef CAN_HARDLINK_SPECIAL
 1360: 		 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
 1361: #endif
 1362: 		 && !S_ISDIR(file->mode)) {
 1363: 			if (do_link(cmpbuf, fname) < 0) {
 1364: 				rsyserr(FERROR_XFER, errno,
 1365: 					"failed to hard-link %s with %s",
 1366: 					cmpbuf, fname);
 1367: 				return j;
 1368: 			}
 1369: 			if (preserve_hard_links && F_IS_HLINKED(file))
 1370: 				finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
 1371: 		} else
 1372: #endif
 1373: 			match_level = 2;
 1374: 		if (itemizing && stdout_format_has_i
 1375: 		 && (verbose > 1 || stdout_format_has_i > 1)) {
 1376: 			int chg = compare_dest && type != TYPE_DIR ? 0
 1377: 			    : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
 1378: 			char *lp = match_level == 3 ? "" : NULL;
 1379: 			itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
 1380: 		}
 1381: 		if (verbose > 1 && maybe_ATTRS_REPORT) {
 1382: 			rprintf(FCLIENT, "%s%s is uptodate\n",
 1383: 				fname, type == TYPE_DIR ? "/" : "");
 1384: 		}
 1385: 		return -2;
 1386: 	}
 1387: 
 1388: 	return j;
 1389: }
 1390: 
 1391: static void list_file_entry(struct file_struct *f)
 1392: {
 1393: 	char permbuf[PERMSTRING_SIZE];
 1394: 	double len;
 1395: 
 1396: 	if (!F_IS_ACTIVE(f)) {
 1397: 		/* this can happen if duplicate names were removed */
 1398: 		return;
 1399: 	}
 1400: 
 1401: 	permstring(permbuf, f->mode);
 1402: 	len = F_LENGTH(f);
 1403: 
 1404: 	/* TODO: indicate '+' if the entry has an ACL. */
 1405: 
 1406: #ifdef SUPPORT_LINKS
 1407: 	if (preserve_links && S_ISLNK(f->mode)) {
 1408: 		rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
 1409: 			permbuf, len, timestring(f->modtime),
 1410: 			f_name(f, NULL), F_SYMLINK(f));
 1411: 	} else
 1412: #endif
 1413: 	{
 1414: 		rprintf(FINFO, "%s %11.0f %s %s\n",
 1415: 			permbuf, len, timestring(f->modtime),
 1416: 			f_name(f, NULL));
 1417: 	}
 1418: }
 1419: 
 1420: static struct bitbag *delayed_bits = NULL;
 1421: static int phase = 0;
 1422: static int dflt_perms;
 1423: 
 1424: static int implied_dirs_are_missing;
 1425: /* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
 1426: static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
 1427: {
 1428: 	return F_DEPTH(file) > F_DEPTH(subtree)
 1429: 		&& (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
 1430: }
 1431: 
 1432: /* Acts on the indicated item in cur_flist whose name is fname.  If a dir,
 1433:  * make sure it exists, and has the right permissions/timestamp info.  For
 1434:  * all other non-regular files (symlinks, etc.) we create them here.  For
 1435:  * regular files that have changed, we try to find a basis file and then
 1436:  * start sending checksums.  The ndx is the file's unique index value.
 1437:  *
 1438:  * The fname parameter must point to a MAXPATHLEN buffer!  (e.g it gets
 1439:  * passed to delete_item(), which can use it during a recursive delete.)
 1440:  *
 1441:  * Note that f_out is set to -1 when doing final directory-permission and
 1442:  * modification-time repair. */
 1443: static void recv_generator(char *fname, struct file_struct *file, int ndx,
 1444: 			   int itemizing, enum logcode code, int f_out)
 1445: {
 1446: 	static const char *parent_dirname = "";
 1447: 	/* Missing dir not created due to --dry-run; will still be scanned. */
 1448: 	static struct file_struct *dry_missing_dir = NULL;
 1449: 	/* Missing dir whose contents are skipped altogether due to
 1450: 	 * --ignore-non-existing, daemon exclude, or mkdir failure. */
 1451: 	static struct file_struct *skip_dir = NULL;
 1452: 	static struct file_list *fuzzy_dirlist = NULL;
 1453: 	static int need_fuzzy_dirlist = 0;
 1454: 	struct file_struct *fuzzy_file = NULL;
 1455: 	int fd = -1, f_copy = -1;
 1456: 	stat_x sx, real_sx;
 1457: 	STRUCT_STAT partial_st;
 1458: 	struct file_struct *back_file = NULL;
 1459: 	int statret, real_ret, stat_errno;
 1460: 	char *fnamecmp, *partialptr, *backupptr = NULL;
 1461: 	char fnamecmpbuf[MAXPATHLEN];
 1462: 	uchar fnamecmp_type;
 1463: 	int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
 1464: 	int is_dir = !S_ISDIR(file->mode) ? 0
 1465: 		   : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
 1466: 		   : 1;
 1467: 
 1468: 	if (verbose > 2)
 1469: 		rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
 1470: 
 1471: 	if (list_only) {
 1472: 		if (is_dir < 0
 1473: 		 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
 1474: 			return;
 1475: 		list_file_entry(file);
 1476: 		return;
 1477: 	}
 1478: 
 1479: 	if (skip_dir) {
 1480: 		if (is_below(file, skip_dir)) {
 1481: 			if (is_dir)
 1482: 				file->flags |= FLAG_MISSING_DIR;
 1483: #ifdef SUPPORT_HARD_LINKS
 1484: 			else if (F_IS_HLINKED(file))
 1485: 				handle_skipped_hlink(file, itemizing, code, f_out);
 1486: #endif
 1487: 			return;
 1488: 		}
 1489: 		skip_dir = NULL;
 1490: 	}
 1491: 
 1492: #ifdef SUPPORT_ACLS
 1493: 	sx.acc_acl = sx.def_acl = NULL;
 1494: #endif
 1495: #ifdef SUPPORT_XATTRS
 1496: 	sx.xattr = NULL;
 1497: #endif
 1498: 	if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
 1499: 		if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
 1500: 			if (is_dir < 0)
 1501: 				return;
 1502: #ifdef SUPPORT_HARD_LINKS
 1503: 			if (F_IS_HLINKED(file))
 1504: 				handle_skipped_hlink(file, itemizing, code, f_out);
 1505: #endif
 1506: 			rprintf(FERROR_XFER,
 1507: 				"ERROR: daemon refused to receive %s \"%s\"\n",
 1508: 				is_dir ? "directory" : "file", fname);
 1509: 			if (is_dir)
 1510: 				goto skipping_dir_contents;
 1511: 			return;
 1512: 		}
 1513: 	}
 1514: 
 1515: 	if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
 1516: 	  parent_is_dry_missing:
 1517: 		if (fuzzy_dirlist) {
 1518: 			flist_free(fuzzy_dirlist);
 1519: 			fuzzy_dirlist = NULL;
 1520: 		}
 1521: 		parent_dirname = "";
 1522: 		statret = -1;
 1523: 		stat_errno = ENOENT;
 1524: 	} else {
 1525: 		const char *dn = file->dirname ? file->dirname : ".";
 1526: 		dry_missing_dir = NULL;
 1527: 		if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
 1528: 			if (relative_paths && !implied_dirs
 1529: 			 && do_stat(dn, &sx.st) < 0) {
 1530: 				if (dry_run)
 1531: 					goto parent_is_dry_missing;
 1532: 				if (create_directory_path(fname) < 0) {
 1533: 					rsyserr(FERROR_XFER, errno,
 1534: 						"recv_generator: mkdir %s failed",
 1535: 						full_fname(dn));
 1536: 				}
 1537: 			}
 1538: 			if (fuzzy_dirlist) {
 1539: 				flist_free(fuzzy_dirlist);
 1540: 				fuzzy_dirlist = NULL;
 1541: 			}
 1542: 			if (fuzzy_basis)
 1543: 				need_fuzzy_dirlist = 1;
 1544: #ifdef SUPPORT_ACLS
 1545: 			if (!preserve_perms)
 1546: 				dflt_perms = default_perms_for_dir(dn);
 1547: #endif
 1548: 		}
 1549: 		parent_dirname = dn;
 1550: 
 1551: 		if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
 1552: 			strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
 1553: 			fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, GDL_IGNORE_FILTER_RULES);
 1554: 			need_fuzzy_dirlist = 0;
 1555: 		}
 1556: 
 1557: 		statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
 1558: 		stat_errno = errno;
 1559: 	}
 1560: 
 1561: 	if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
 1562: 		if (is_dir) {
 1563: 			if (is_dir < 0)
 1564: 				return;
 1565: 			skip_dir = file;
 1566: 			file->flags |= FLAG_MISSING_DIR;
 1567: 		}
 1568: #ifdef SUPPORT_HARD_LINKS
 1569: 		else if (F_IS_HLINKED(file))
 1570: 			handle_skipped_hlink(file, itemizing, code, f_out);
 1571: #endif
 1572: 		if (verbose > 1) {
 1573: 			rprintf(FINFO, "not creating new %s \"%s\"\n",
 1574: 				is_dir ? "directory" : "file", fname);
 1575: 		}
 1576: 		return;
 1577: 	}
 1578: 
 1579: 	if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
 1580: 	 && !am_root && sx.st.st_uid == our_uid)
 1581: 		del_opts |= DEL_NO_UID_WRITE;
 1582: 
 1583: 	if (ignore_existing > 0 && statret == 0
 1584: 	 && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
 1585: 		if (verbose > 1 && is_dir >= 0)
 1586: 			rprintf(FINFO, "%s exists\n", fname);
 1587: #ifdef SUPPORT_HARD_LINKS
 1588: 		if (F_IS_HLINKED(file))
 1589: 			handle_skipped_hlink(file, itemizing, code, f_out);
 1590: #endif
 1591: 		goto cleanup;
 1592: 	}
 1593: 
 1594: 	fnamecmp = fname;
 1595: 
 1596: 	if (is_dir) {
 1597: 		mode_t added_perms;
 1598: 		if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
 1599: 			goto cleanup;
 1600: 		if (am_root < 0) {
 1601: 			/* For --fake-super, the dir must be useable by the copying
 1602: 			 * user, just like it would be for root. */
 1603: 			added_perms = S_IRUSR|S_IWUSR|S_IXUSR;
 1604: 		} else
 1605: 			added_perms = 0;
 1606: 		if (is_dir < 0) {
 1607: 			/* In inc_recurse mode we want to make sure any missing
 1608: 			 * directories get created while we're still processing
 1609: 			 * the parent dir (which allows us to touch the parent
 1610: 			 * dir's mtime right away).  We will handle the dir in
 1611: 			 * full later (right before we handle its contents). */
 1612: 			if (statret == 0
 1613: 			 && (S_ISDIR(sx.st.st_mode)
 1614: 			  || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
 1615: 				goto cleanup; /* Any errors get reported later. */
 1616: 			if (do_mkdir(fname, (file->mode|added_perms) & 0700) == 0)
 1617: 				file->flags |= FLAG_DIR_CREATED;
 1618: 			goto cleanup;
 1619: 		}
 1620: 		/* The file to be received is a directory, so we need
 1621: 		 * to prepare appropriately.  If there is already a
 1622: 		 * file of that name and it is *not* a directory, then
 1623: 		 * we need to delete it.  If it doesn't exist, then
 1624: 		 * (perhaps recursively) create it. */
 1625: 		if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
 1626: 			if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
 1627: 				goto skipping_dir_contents;
 1628: 			statret = -1;
 1629: 		}
 1630: 		if (dry_run && statret != 0) {
 1631: 			if (!dry_missing_dir)
 1632: 				dry_missing_dir = file;
 1633: 			file->flags |= FLAG_MISSING_DIR;
 1634: 		}
 1635: 		real_ret = statret;
 1636: 		real_sx = sx;
 1637: 		if (file->flags & FLAG_DIR_CREATED)
 1638: 			statret = -1;
 1639: 		if (!preserve_perms) { /* See comment in non-dir code below. */
 1640: 			file->mode = dest_mode(file->mode, sx.st.st_mode,
 1641: 					       dflt_perms, statret == 0);
 1642: 		}
 1643: 		if (statret != 0 && basis_dir[0] != NULL) {
 1644: 			int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
 1645: 					      itemizing, code);
 1646: 			if (j == -2) {
 1647: 				itemizing = 0;
 1648: 				code = FNONE;
 1649: 				statret = 1;
 1650: 			} else if (j >= 0) {
 1651: 				statret = 1;
 1652: 				fnamecmp = fnamecmpbuf;
 1653: 			}
 1654: 		}
 1655: 		if (itemizing && f_out != -1) {
 1656: 			itemize(fnamecmp, file, ndx, statret, &sx,
 1657: 				statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
 1658: 		}
 1659: 		if (real_ret != 0 && do_mkdir(fname,file->mode|added_perms) < 0 && errno != EEXIST) {
 1660: 			if (!relative_paths || errno != ENOENT
 1661: 			 || create_directory_path(fname) < 0
 1662: 			 || (do_mkdir(fname, file->mode|added_perms) < 0 && errno != EEXIST)) {
 1663: 				rsyserr(FERROR_XFER, errno,
 1664: 					"recv_generator: mkdir %s failed",
 1665: 					full_fname(fname));
 1666: 			  skipping_dir_contents:
 1667: 				rprintf(FERROR,
 1668: 				    "*** Skipping any contents from this failed directory ***\n");
 1669: 				skip_dir = file;
 1670: 				file->flags |= FLAG_MISSING_DIR;
 1671: 				goto cleanup;
 1672: 			}
 1673: 		}
 1674: 
 1675: #ifdef SUPPORT_XATTRS
 1676: 		if (preserve_xattrs && statret == 1)
 1677: 			copy_xattrs(fnamecmpbuf, fname);
 1678: #endif
 1679: 		if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
 1680: 		    && verbose && code != FNONE && f_out != -1)
 1681: 			rprintf(code, "%s/\n", fname);
 1682: 
 1683: 		/* We need to ensure that the dirs in the transfer have both
 1684: 		 * readable and writable permissions during the time we are
 1685: 		 * putting files within them.  This is then restored to the
 1686: 		 * former permissions after the transfer is done. */
 1687: #ifdef HAVE_CHMOD
 1688: 		if (!am_root && (file->mode & S_IRWXU) != S_IRWXU && dir_tweaking) {
 1689: 			mode_t mode = file->mode | S_IRWXU;
 1690: 			if (do_chmod(fname, mode) < 0) {
 1691: 				rsyserr(FERROR_XFER, errno,
 1692: 					"failed to modify permissions on %s",
 1693: 					full_fname(fname));
 1694: 			}
 1695: 			need_retouch_dir_perms = 1;
 1696: 		}
 1697: #endif
 1698: 
 1699: 		if (real_ret != 0 && one_file_system)
 1700: 			real_sx.st.st_dev = filesystem_dev;
 1701: 		if (inc_recurse) {
 1702: 			if (one_file_system) {
 1703: 				uint32 *devp = F_DIR_DEV_P(file);
 1704: 				DEV_MAJOR(devp) = major(real_sx.st.st_dev);
 1705: 				DEV_MINOR(devp) = minor(real_sx.st.st_dev);
 1706: 			}
 1707: 		}
 1708: 		else if (delete_during && f_out != -1 && !phase
 1709: 		    && !(file->flags & FLAG_MISSING_DIR)) {
 1710: 			if (file->flags & FLAG_CONTENT_DIR) {
 1711: 				if (detect_renamed && real_ret != 0)
 1712: 					unexplored_dirs++;
 1713: 				delete_in_dir(fname, file, &real_sx.st.st_dev,
 1714: 					      delete_during < 0 ? DEL_NO_DELETIONS : 0);
 1715: 			} else
 1716: 				change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
 1717: 		}
 1718: 		goto cleanup;
 1719: 	}
 1720: 
 1721: 	/* If we're not preserving permissions, change the file-list's
 1722: 	 * mode based on the local permissions and some heuristics. */
 1723: 	if (!preserve_perms) {
 1724: 		int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
 1725: 		file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
 1726: 				       exists);
 1727: 	}
 1728: 
 1729: #ifdef SUPPORT_HARD_LINKS
 1730: 	if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
 1731: 	 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
 1732: 		goto cleanup;
 1733: #endif
 1734: 
 1735: 	if (preserve_links && S_ISLNK(file->mode)) {
 1736: #ifdef SUPPORT_LINKS
 1737: 		const char *sl = F_SYMLINK(file);
 1738: 		if (safe_symlinks && unsafe_symlink(sl, fname)) {
 1739: 			if (verbose) {
 1740: 				if (solo_file) {
 1741: 					/* fname contains the destination path, but we
 1742: 					 * want to report the source path. */
 1743: 					fname = f_name(file, NULL);
 1744: 				}
 1745: 				rprintf(FINFO,
 1746: 					"ignoring unsafe symlink \"%s\" -> \"%s\"\n",
 1747: 					fname, sl);
 1748: 			}
 1749: 			return;
 1750: 		}
 1751: 		if (statret == 0) {
 1752: 			char lnk[MAXPATHLEN];
 1753: 			int len;
 1754: 
 1755: 			if (!S_ISLNK(sx.st.st_mode))
 1756: 				statret = -1;
 1757: 			else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
 1758: 			      && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
 1759: 				/* The link is pointing to the right place. */
 1760: 				set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
 1761: 				if (itemizing)
 1762: 					itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
 1763: #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
 1764: 				if (preserve_hard_links && F_IS_HLINKED(file))
 1765: 					finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
 1766: #endif
 1767: 				if (remove_source_files == 1)
 1768: 					goto return_with_success;
 1769: 				goto cleanup;
 1770: 			}
 1771: 			/* Not the right symlink (or not a symlink), so
 1772: 			 * delete it. */
 1773: 			if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
 1774: 				goto cleanup;
 1775: 		} else if (basis_dir[0] != NULL) {
 1776: 			int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
 1777: 					      itemizing, code);
 1778: 			if (j == -2) {
 1779: #ifndef CAN_HARDLINK_SYMLINK
 1780: 				if (link_dest) {
 1781: 					/* Resort to --copy-dest behavior. */
 1782: 				} else
 1783: #endif
 1784: 				if (!copy_dest)
 1785: 					goto cleanup;
 1786: 				itemizing = 0;
 1787: 				code = FNONE;
 1788: 			} else if (j >= 0)
 1789: 				statret = 1;
 1790: 		}
 1791: #ifdef SUPPORT_HARD_LINKS
 1792: 		if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
 1793: 			cur_flist->in_progress++;
 1794: 			goto cleanup;
 1795: 		}
 1796: #endif
 1797: 		if (do_symlink(sl, fname) != 0) {
 1798: 			rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
 1799: 				full_fname(fname), sl);
 1800: 		} else {
 1801: 			set_file_attrs(fname, file, NULL, NULL, 0);
 1802: 			if (itemizing) {
 1803: 				itemize(fname, file, ndx, statret, &sx,
 1804: 					ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
 1805: 			}
 1806: 			if (code != FNONE && verbose)
 1807: 				rprintf(code, "%s -> %s\n", fname, sl);
 1808: #ifdef SUPPORT_HARD_LINKS
 1809: 			if (preserve_hard_links && F_IS_HLINKED(file))
 1810: 				finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
 1811: #endif
 1812: 			/* This does not check remove_source_files == 1
 1813: 			 * because this is one of the items that the old
 1814: 			 * --remove-sent-files option would remove. */
 1815: 			if (remove_source_files)
 1816: 				goto return_with_success;
 1817: 		}
 1818: #endif
 1819: 		goto cleanup;
 1820: 	}
 1821: 
 1822: 	if ((am_root && preserve_devices && IS_DEVICE(file->mode))
 1823: 	 || (preserve_specials && IS_SPECIAL(file->mode))) {
 1824: 		dev_t rdev;
 1825: 		if (IS_DEVICE(file->mode)) {
 1826: 			uint32 *devp = F_RDEV_P(file);
 1827: 			rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
 1828: 		} else
 1829: 			rdev = 0;
 1830: 		if (statret == 0) {
 1831: 			int del_for_flag;
 1832: 			if (IS_DEVICE(file->mode)) {
 1833: 				if (!IS_DEVICE(sx.st.st_mode))
 1834: 					statret = -1;
 1835: 				del_for_flag = DEL_FOR_DEVICE;
 1836: 			} else {
 1837: 				if (!IS_SPECIAL(sx.st.st_mode))
 1838: 					statret = -1;
 1839: 				del_for_flag = DEL_FOR_SPECIAL;
 1840: 			}
 1841: 			if (statret == 0
 1842: 			 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
 1843: 			 && (IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev == rdev)) {
 1844: 				/* The device or special file is identical. */
 1845: 				set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
 1846: 				if (itemizing)
 1847: 					itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
 1848: #ifdef SUPPORT_HARD_LINKS
 1849: 				if (preserve_hard_links && F_IS_HLINKED(file))
 1850: 					finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
 1851: #endif
 1852: 				if (remove_source_files == 1)
 1853: 					goto return_with_success;
 1854: 				goto cleanup;
 1855: 			}
 1856: 			if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
 1857: 				goto cleanup;
 1858: 		} else if (basis_dir[0] != NULL) {
 1859: 			int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
 1860: 					      itemizing, code);
 1861: 			if (j == -2) {
 1862: #ifndef CAN_HARDLINK_SPECIAL
 1863: 				if (link_dest) {
 1864: 					/* Resort to --copy-dest behavior. */
 1865: 				} else
 1866: #endif
 1867: 				if (!copy_dest)
 1868: 					goto cleanup;
 1869: 				itemizing = 0;
 1870: 				code = FNONE;
 1871: 			} else if (j >= 0)
 1872: 				statret = 1;
 1873: 		}
 1874: #ifdef SUPPORT_HARD_LINKS
 1875: 		if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
 1876: 			cur_flist->in_progress++;
 1877: 			goto cleanup;
 1878: 		}
 1879: #endif
 1880: 		if (verbose > 2) {
 1881: 			rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
 1882: 				fname, (int)file->mode,
 1883: 				(long)major(rdev), (long)minor(rdev));
 1884: 		}
 1885: 		if (do_mknod(fname, file->mode, rdev) < 0) {
 1886: 			rsyserr(FERROR_XFER, errno, "mknod %s failed",
 1887: 				full_fname(fname));
 1888: 		} else {
 1889: 			set_file_attrs(fname, file, NULL, NULL, 0);
 1890: 			if (itemizing) {
 1891: 				itemize(fname, file, ndx, statret, &sx,
 1892: 					ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
 1893: 			}
 1894: 			if (code != FNONE && verbose)
 1895: 				rprintf(code, "%s\n", fname);
 1896: #ifdef SUPPORT_HARD_LINKS
 1897: 			if (preserve_hard_links && F_IS_HLINKED(file))
 1898: 				finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
 1899: #endif
 1900: 			if (remove_source_files == 1)
 1901: 				goto return_with_success;
 1902: 		}
 1903: 		goto cleanup;
 1904: 	}
 1905: 
 1906: 	if (!S_ISREG(file->mode)) {
 1907: 		if (solo_file)
 1908: 			fname = f_name(file, NULL);
 1909: 		rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
 1910: 		goto cleanup;
 1911: 	}
 1912: 
 1913: 	if (max_size > 0 && F_LENGTH(file) > max_size) {
 1914: 		if (verbose > 1) {
 1915: 			if (solo_file)
 1916: 				fname = f_name(file, NULL);
 1917: 			rprintf(FINFO, "%s is over max-size\n", fname);
 1918: 		}
 1919: 		goto cleanup;
 1920: 	}
 1921: 	if (min_size > 0 && F_LENGTH(file) < min_size) {
 1922: 		if (verbose > 1) {
 1923: 			if (solo_file)
 1924: 				fname = f_name(file, NULL);
 1925: 			rprintf(FINFO, "%s is under min-size\n", fname);
 1926: 		}
 1927: 		goto cleanup;
 1928: 	}
 1929: 
 1930: 	if (update_only > 0 && statret == 0
 1931: 	    && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
 1932: 		if (verbose > 1)
 1933: 			rprintf(FINFO, "%s is newer\n", fname);
 1934: #ifdef SUPPORT_HARD_LINKS
 1935: 		if (F_IS_HLINKED(file))
 1936: 			handle_skipped_hlink(file, itemizing, code, f_out);
 1937: #endif
 1938: 		goto cleanup;
 1939: 	}
 1940: 
 1941: 	fnamecmp_type = FNAMECMP_FNAME;
 1942: 
 1943: 	if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
 1944: 		if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
 1945: 			goto cleanup;
 1946: 		statret = -1;
 1947: 		stat_errno = ENOENT;
 1948: 	}
 1949: 
 1950: 	if (statret != 0 && basis_dir[0] != NULL) {
 1951: 		int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
 1952: 				      itemizing, code);
 1953: 		if (j == -2) {
 1954: 			if (remove_source_files == 1)
 1955: 				goto return_with_success;
 1956: 			goto cleanup;
 1957: 		}
 1958: 		if (j >= 0) {
 1959: 			fnamecmp = fnamecmpbuf;
 1960: 			fnamecmp_type = j;
 1961: 			statret = 0;
 1962: 		}
 1963: 	}
 1964: 
 1965: 	real_ret = statret;
 1966: 	real_sx = sx;
 1967: 
 1968: 	if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
 1969: 	    && link_stat(partialptr, &partial_st, 0) == 0
 1970: 	    && S_ISREG(partial_st.st_mode)) {
 1971: 		if (statret != 0)
 1972: 			goto prepare_to_open;
 1973: 	} else
 1974: 		partialptr = NULL;
 1975: 
 1976: 	if (statret != 0 && fuzzy_dirlist) {
 1977: 		int j = find_fuzzy(file, fuzzy_dirlist);
 1978: 		if (j >= 0) {
 1979: 			fuzzy_file = fuzzy_dirlist->files[j];
 1980: 			f_name(fuzzy_file, fnamecmpbuf);
 1981: 			if (verbose > 2) {
 1982: 				rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
 1983: 					fname, fnamecmpbuf);
 1984: 			}
 1985: 			sx.st.st_size = F_LENGTH(fuzzy_file);
 1986: 			statret = 0;
 1987: 			fnamecmp = fnamecmpbuf;
 1988: 			fnamecmp_type = FNAMECMP_FUZZY;
 1989: 		}
 1990: 	}
 1991: 
 1992: 	if (statret != 0) {
 1993: #ifdef SUPPORT_HARD_LINKS
 1994: 		if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
 1995: 			cur_flist->in_progress++;
 1996: 			goto cleanup;
 1997: 		}
 1998: #endif
 1999: 		if (stat_errno == ENOENT) {
 2000: 			if (detect_renamed && unexplored_dirs > 0
 2001: 			 && F_LENGTH(file)) {
 2002: 				bitbag_set_bit(delayed_bits, ndx);
 2003: 				return;
 2004: 			}
 2005: 			goto notify_others;
 2006: 		}
 2007: 		rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
 2008: 			full_fname(fname));
 2009: 		goto cleanup;
 2010: 	}
 2011: 
 2012: 	if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
 2013: 		;
 2014: 	else if (fnamecmp_type == FNAMECMP_FUZZY)
 2015: 		;
 2016: 	else if (unchanged_file(fnamecmp, file, &sx.st)) {
 2017: 		if (partialptr) {
 2018: 			do_unlink(partialptr);
 2019: 			handle_partial_dir(partialptr, PDIR_DELETE);
 2020: 		}
 2021: 		set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
 2022: 		if (itemizing)
 2023: 			itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
 2024: #ifdef SUPPORT_HARD_LINKS
 2025: 		if (preserve_hard_links && F_IS_HLINKED(file))
 2026: 			finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
 2027: #endif
 2028: 		if (remove_source_files != 1)
 2029: 			goto cleanup;
 2030: 	  return_with_success:
 2031: 		if (!dry_run)
 2032: 			send_msg_int(MSG_SUCCESS, ndx);
 2033: 		goto cleanup;
 2034: 	}
 2035: 
 2036: 	if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
 2037: #ifdef SUPPORT_HARD_LINKS
 2038: 		if (F_IS_HLINKED(file))
 2039: 			handle_skipped_hlink(file, itemizing, code, f_out);
 2040: #endif
 2041: 		goto cleanup;
 2042: 	}
 2043: 
 2044:   prepare_to_open:
 2045: 	if (partialptr) {
 2046: 		sx.st = partial_st;
 2047: 		fnamecmp = partialptr;
 2048: 		fnamecmp_type = FNAMECMP_PARTIAL_DIR;
 2049: 		statret = 0;
 2050: 	}
 2051: 
 2052: 	if (!do_xfers)
 2053: 		goto notify_others;
 2054: 
 2055: 	if (read_batch || whole_file) {
 2056: 		if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
 2057: 			if (!(backupptr = get_backup_name(fname)))
 2058: 				goto cleanup;
 2059: 			if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
 2060: 				goto pretend_missing;
 2061: 			if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
 2062: 				unmake_file(back_file);
 2063: 				back_file = NULL;
 2064: 				goto cleanup;
 2065: 			}
 2066: 		}
 2067: 		goto notify_others;
 2068: 	}
 2069: 
 2070: 	if (fuzzy_dirlist) {
 2071: 		int j = flist_find(fuzzy_dirlist, file);
 2072: 		if (j >= 0) /* don't use changing file as future fuzzy basis */
 2073: 			fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
 2074: 	}
 2075: 
 2076: 	/* open the file */
 2077: 	if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
 2078: 		rsyserr(FERROR, errno, "failed to open %s, continuing",
 2079: 			full_fname(fnamecmp));
 2080: 	  pretend_missing:
 2081: 		/* pretend the file didn't exist */
 2082: #ifdef SUPPORT_HARD_LINKS
 2083: 		if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
 2084: 			cur_flist->in_progress++;
 2085: 			goto cleanup;
 2086: 		}
 2087: #endif
 2088: 		statret = real_ret = -1;
 2089: 		goto notify_others;
 2090: 	}
 2091: 
 2092: 	if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
 2093: 		if (!(backupptr = get_backup_name(fname))) {
 2094: 			close(fd);
 2095: 			goto cleanup;
 2096: 		}
 2097: 		if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
 2098: 			close(fd);
 2099: 			goto pretend_missing;
 2100: 		}
 2101: 		if (robust_unlink(backupptr) && errno != ENOENT) {
 2102: 			rsyserr(FERROR_XFER, errno, "unlink %s",
 2103: 				full_fname(backupptr));
 2104: 			unmake_file(back_file);
 2105: 			back_file = NULL;
 2106: 			close(fd);
 2107: 			goto cleanup;
 2108: 		}
 2109: 		if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
 2110: 			int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
 2111: 			if (errno == ENOENT && make_bak_dir(backupptr) == 0) {
 2112: 				if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)
 2113: 					save_errno = errno ? errno : save_errno;
 2114: 				else
 2115: 					save_errno = 0;
 2116: 			}
 2117: 			if (save_errno) {
 2118: 				rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(backupptr));
 2119: 				unmake_file(back_file);
 2120: 				back_file = NULL;
 2121: 				close(fd);
 2122: 				goto cleanup;
 2123: 			}
 2124: 		}
 2125: 		fnamecmp_type = FNAMECMP_BACKUP;
 2126: 	}
 2127: 
 2128: 	if (verbose > 3) {
 2129: 		rprintf(FINFO, "gen mapped %s of size %.0f\n",
 2130: 			fnamecmp, (double)sx.st.st_size);
 2131: 	}
 2132: 
 2133: 	if (verbose > 2)
 2134: 		rprintf(FINFO, "generating and sending sums for %d\n", ndx);
 2135: 
 2136:   notify_others:
 2137: 	if (remove_source_files && !delay_updates && !phase && !dry_run)
 2138: 		increment_active_files(ndx, itemizing, code);
 2139: 	if (inc_recurse && !dry_run)
 2140: 		cur_flist->in_progress++;
 2141: #ifdef SUPPORT_HARD_LINKS
 2142: 	if (preserve_hard_links && F_IS_HLINKED(file))
 2143: 		file->flags |= FLAG_FILE_SENT;
 2144: #endif
 2145: 	write_ndx(f_out, ndx);
 2146: 	if (itemizing) {
 2147: 		int iflags = ITEM_TRANSFER;
 2148: 		if (always_checksum > 0)
 2149: 			iflags |= ITEM_REPORT_CHANGE;
 2150: 		if (fnamecmp_type != FNAMECMP_FNAME)
 2151: 			iflags |= ITEM_BASIS_TYPE_FOLLOWS;
 2152: 		if (fnamecmp_type == FNAMECMP_FUZZY)
 2153: 			iflags |= ITEM_XNAME_FOLLOWS;
 2154: 		itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
 2155: 			fuzzy_file ? fuzzy_file->basename : NULL);
 2156: #ifdef SUPPORT_ACLS
 2157: 		if (preserve_acls)
 2158: 			free_acl(&real_sx);
 2159: #endif
 2160: #ifdef SUPPORT_XATTRS
 2161: 		if (preserve_xattrs)
 2162: 			free_xattr(&real_sx);
 2163: #endif
 2164: 	}
 2165: 
 2166: 	if (!do_xfers) {
 2167: #ifdef SUPPORT_HARD_LINKS
 2168: 		if (preserve_hard_links && F_IS_HLINKED(file))
 2169: 			finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
 2170: #endif
 2171: 		goto cleanup;
 2172: 	}
 2173: 	if (read_batch)
 2174: 		goto cleanup;
 2175: 
 2176: 	if (statret != 0 || whole_file)
 2177: 		write_sum_head(f_out, NULL);
 2178: 	else if (sx.st.st_size <= 0) {
 2179: 		write_sum_head(f_out, NULL);
 2180: 		close(fd);
 2181: 	} else {
 2182: 		if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
 2183: 			rprintf(FWARNING,
 2184: 			    "WARNING: file is too large for checksum sending: %s\n",
 2185: 			    fnamecmp);
 2186: 			write_sum_head(f_out, NULL);
 2187: 		}
 2188: 		close(fd);
 2189: 	}
 2190: 
 2191:   cleanup:
 2192: 	if (back_file) {
 2193: 		int save_preserve_xattrs = preserve_xattrs;
 2194: 		if (f_copy >= 0)
 2195: 			close(f_copy);
 2196: #ifdef SUPPORT_XATTRS
 2197: 		if (preserve_xattrs) {
 2198: 			copy_xattrs(fname, backupptr);
 2199: 			preserve_xattrs = 0;
 2200: 		}
 2201: #endif
 2202: 		set_file_attrs(backupptr, back_file, NULL, NULL, 0);
 2203: 		preserve_xattrs = save_preserve_xattrs;
 2204: 		if (verbose > 1) {
 2205: 			rprintf(FINFO, "backed up %s to %s\n",
 2206: 				fname, backupptr);
 2207: 		}
 2208: 		unmake_file(back_file);
 2209: 	}
 2210: 
 2211: #ifdef SUPPORT_ACLS
 2212: 	if (preserve_acls)
 2213: 		free_acl(&sx);
 2214: #endif
 2215: #ifdef SUPPORT_XATTRS
 2216: 	if (preserve_xattrs)
 2217: 		free_xattr(&sx);
 2218: #endif
 2219: 	return;
 2220: }
 2221: 
 2222: #ifdef SUPPORT_HARD_LINKS
 2223: static void handle_skipped_hlink(struct file_struct *file, int itemizing,
 2224: 				 enum logcode code, int f_out)
 2225: {
 2226: 	char fbuf[MAXPATHLEN];
 2227: 	int new_last_ndx;
 2228: 	struct file_list *save_flist = cur_flist;
 2229: 
 2230: 	/* If we skip the last item in a chain of links and there was a
 2231: 	 * prior non-skipped hard-link waiting to finish, finish it now. */
 2232: 	if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
 2233: 		return;
 2234: 
 2235: 	file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
 2236: 	cur_flist->in_progress--; /* undo prior increment */
 2237: 	f_name(file, fbuf);
 2238: 	recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
 2239: 
 2240: 	cur_flist = save_flist;
 2241: }
 2242: #endif
 2243: 
 2244: static void touch_up_dirs(struct file_list *flist, int ndx)
 2245: {
 2246: 	static int counter = 0;
 2247: 	struct file_struct *file;
 2248: 	char *fname;
 2249: 	BOOL fix_dir_perms;
 2250: 	int i, start, end;
 2251: 
 2252: 	if (ndx < 0) {
 2253: 		start = 0;
 2254: 		end = flist->used - 1;
 2255: 	} else
 2256: 		start = end = ndx;
 2257: 
 2258: 	/* Fix any directory permissions that were modified during the
 2259: 	 * transfer and/or re-set any tweaked modified-time values. */
 2260: 	for (i = start; i <= end; i++, counter++) {
 2261: 		file = flist->files[i];
 2262: 		if (!S_ISDIR(file->mode)
 2263: 		 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
 2264: 			continue;
 2265: 		if (verbose > 3) {
 2266: 			fname = f_name(file, NULL);
 2267: 			rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
 2268: 				NS(fname), i);
 2269: 		}
 2270: 		/* Be sure not to retouch permissions with --fake-super. */
 2271: 		fix_dir_perms = !am_root && !(file->mode & S_IWUSR);
 2272: 		if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
 2273: 		 || !(need_retouch_dir_times || fix_dir_perms))
 2274: 			continue;
 2275: 		fname = f_name(file, NULL);
 2276: 		if (fix_dir_perms)
 2277: 			do_chmod(fname, file->mode);
 2278: 		if (need_retouch_dir_times) {
 2279: 			STRUCT_STAT st;
 2280: 			if (link_stat(fname, &st, 0) == 0
 2281: 			 && cmp_time(st.st_mtime, file->modtime) != 0)
 2282: 				set_modtime(fname, file->modtime, file->mode);
 2283: 		}
 2284: 		if (counter >= loopchk_limit) {
 2285: 			if (allowed_lull)
 2286: 				maybe_send_keepalive();
 2287: 			else
 2288: 				maybe_flush_socket(0);
 2289: 			counter = 0;
 2290: 		}
 2291: 	}
 2292: }
 2293: 
 2294: void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
 2295: {
 2296: 	struct file_struct *file;
 2297: 	struct file_list *flist;
 2298: 	char fbuf[MAXPATHLEN];
 2299: 	int ndx;
 2300: 
 2301: 	while (1) {
 2302: #ifdef SUPPORT_HARD_LINKS
 2303: 		if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
 2304: 			int send_failed = (ndx == -2);
 2305: 			if (send_failed)
 2306: 				ndx = get_hlink_num();
 2307: 			flist = flist_for_ndx(ndx, "check_for_finished_files.1");
 2308: 			file = flist->files[ndx - flist->ndx_start];
 2309: 			assert(file->flags & FLAG_HLINKED);
 2310: 			if (send_failed)
 2311: 				handle_skipped_hlink(file, itemizing, code, sock_f_out);
 2312: 			else
 2313: 				finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
 2314: 			flist->in_progress--;
 2315: 			continue;
 2316: 		}
 2317: #endif
 2318: 
 2319: 		if (check_redo && (ndx = get_redo_num()) != -1) {
 2320: 			csum_length = SUM_LENGTH;
 2321: 			max_size = -max_size;
 2322: 			min_size = -min_size;
 2323: 			ignore_existing = -ignore_existing;
 2324: 			ignore_non_existing = -ignore_non_existing;
 2325: 			update_only = -update_only;
 2326: 			always_checksum = -always_checksum;
 2327: 			size_only = -size_only;
 2328: 			append_mode = -append_mode;
 2329: 			make_backups = -make_backups; /* avoid dup backup w/inplace */
 2330: 			ignore_times++;
 2331: 
 2332: 			flist = cur_flist;
 2333: 			cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
 2334: 
 2335: 			file = cur_flist->files[ndx - cur_flist->ndx_start];
 2336: 			if (solo_file)
 2337: 				strlcpy(fbuf, solo_file, sizeof fbuf);
 2338: 			else
 2339: 				f_name(file, fbuf);
 2340: 			recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
 2341: 			cur_flist->to_redo--;
 2342: 
 2343: 			cur_flist = flist;
 2344: 
 2345: 			csum_length = SHORT_SUM_LENGTH;
 2346: 			max_size = -max_size;
 2347: 			min_size = -min_size;
 2348: 			ignore_existing = -ignore_existing;
 2349: 			ignore_non_existing = -ignore_non_existing;
 2350: 			update_only = -update_only;
 2351: 			always_checksum = -always_checksum;
 2352: 			size_only = -size_only;
 2353: 			append_mode = -append_mode;
 2354: 			make_backups = -make_backups;
 2355: 			ignore_times--;
 2356: 			continue;
 2357: 		}
 2358: 
 2359: 		if (cur_flist == first_flist)
 2360: 			break;
 2361: 
 2362: 		/* We only get here if inc_recurse is enabled. */
 2363: 		if (first_flist->in_progress || first_flist->to_redo)
 2364: 			break;
 2365: 
 2366: 		write_ndx(sock_f_out, NDX_DONE);
 2367: 		if (!read_batch)
 2368: 			maybe_flush_socket(1);
 2369: 
 2370: 		if (delete_during == 2 || !dir_tweaking) {
 2371: 			/* Skip directory touch-up. */
 2372: 		} else if (first_flist->parent_ndx >= 0)
 2373: 			touch_up_dirs(dir_flist, first_flist->parent_ndx);
 2374: 
 2375: 		flist_free(first_flist); /* updates first_flist */
 2376: 	}
 2377: }
 2378: 
 2379: void generate_files(int f_out, const char *local_name)
 2380: {
 2381: 	int i, ndx, next_loopchk = 0;
 2382: 	char fbuf[MAXPATHLEN];
 2383: 	int itemizing;
 2384: 	enum logcode code;
 2385: 	int save_do_progress = do_progress;
 2386: 
 2387: 	if (protocol_version >= 29) {
 2388: 		itemizing = 1;
 2389: 		maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
 2390: 		code = logfile_format_has_i ? FNONE : FLOG;
 2391: 	} else if (am_daemon) {
 2392: 		itemizing = logfile_format_has_i && do_xfers;
 2393: 		maybe_ATTRS_REPORT = ATTRS_REPORT;
 2394: 		code = itemizing || !do_xfers ? FCLIENT : FINFO;
 2395: 	} else if (!am_server) {
 2396: 		itemizing = stdout_format_has_i;
 2397: 		maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
 2398: 		code = itemizing ? FNONE : FINFO;
 2399: 	} else {
 2400: 		itemizing = 0;
 2401: 		maybe_ATTRS_REPORT = ATTRS_REPORT;
 2402: 		code = FINFO;
 2403: 	}
 2404: 	solo_file = local_name;
 2405: 	dir_tweaking = !(list_only || solo_file || dry_run);
 2406: 	need_retouch_dir_times = preserve_times & PRESERVE_DIR_TIMES;
 2407: 	loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
 2408: 	symlink_timeset_failed_flags = ITEM_REPORT_TIME
 2409: 	    | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
 2410: 	implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
 2411: 
 2412: 	if (verbose > 2)
 2413: 		rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
 2414: 
 2415: 	if (detect_renamed) {
 2416: 		delayed_bits = bitbag_create(cur_flist->used);
 2417: 		if (!delete_before && !delete_during)
 2418: 			delete_during = -1;
 2419: 	}
 2420: 
 2421: 	if (delete_before && !solo_file && cur_flist->used > 0)
 2422: 		do_delete_pass();
 2423: 	if (delete_during == 2) {
 2424: 		deldelay_size = BIGPATHBUFLEN * 4;
 2425: 		deldelay_buf = new_array(char, deldelay_size);
 2426: 		if (!deldelay_buf)
 2427: 			out_of_memory("delete-delay");
 2428: 	}
 2429: 	do_progress = 0;
 2430: 
 2431: 	if (append_mode > 0 || detect_renamed || whole_file < 0)
 2432: 		whole_file = 0;
 2433: 	if (verbose >= 2) {
 2434: 		rprintf(FINFO, "delta-transmission %s\n",
 2435: 			whole_file
 2436: 			? "disabled for local transfer or --whole-file"
 2437: 			: "enabled");
 2438: 	}
 2439: 
 2440: 	/* Since we often fill up the outgoing socket and then just sit around
 2441: 	 * waiting for the other 2 processes to do their thing, we don't want
 2442: 	 * to exit on a timeout.  If the data stops flowing, the receiver will
 2443: 	 * notice that and let us know via the message pipe (or its closing). */
 2444: 	ignore_timeout = 1;
 2445: 
 2446: 	dflt_perms = (ACCESSPERMS & ~orig_umask);
 2447: 
 2448: 	do {
 2449: #ifdef SUPPORT_HARD_LINKS
 2450: 		if (preserve_hard_links && inc_recurse) {
 2451: 			while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
 2452: 				wait_for_receiver();
 2453: 		}
 2454: #endif
 2455: 
 2456: 		if (inc_recurse && cur_flist->parent_ndx >= 0) {
 2457: 			struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
 2458: 			if (solo_file)
 2459: 				strlcpy(fbuf, solo_file, sizeof fbuf);
 2460: 			else
 2461: 				f_name(fp, fbuf);
 2462: 			ndx = cur_flist->ndx_start - 1;
 2463: 			recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
 2464: 			if (delete_during && dry_run < 2 && !list_only
 2465: 			 && !(fp->flags & FLAG_MISSING_DIR)) {
 2466: 				if (fp->flags & FLAG_CONTENT_DIR) {
 2467: 					dev_t dirdev;
 2468: 					if (one_file_system) {
 2469: 						uint32 *devp = F_DIR_DEV_P(fp);
 2470: 						dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
 2471: 					} else
 2472: 						dirdev = MAKEDEV(0, 0);
 2473: 					delete_in_dir(fbuf, fp, &dirdev, 0);
 2474: 				} else
 2475: 					change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
 2476: 			}
 2477: 		}
 2478: 		for (i = cur_flist->low; i <= cur_flist->high; i++) {
 2479: 			struct file_struct *file = cur_flist->sorted[i];
 2480: 
 2481: 			if (!F_IS_ACTIVE(file))
 2482: 				continue;
 2483: 
 2484: 			if (unsort_ndx)
 2485: 				ndx = F_NDX(file);
 2486: 			else
 2487: 				ndx = i + cur_flist->ndx_start;
 2488: 
 2489: 			if (solo_file)
 2490: 				strlcpy(fbuf, solo_file, sizeof fbuf);
 2491: 			else
 2492: 				f_name(file, fbuf);
 2493: 			recv_generator(fbuf, file, ndx, itemizing, code, f_out);
 2494: 
 2495: 			check_for_finished_files(itemizing, code, 0);
 2496: 
 2497: 			if (i + cur_flist->ndx_start >= next_loopchk) {
 2498: 				if (allowed_lull)
 2499: 					maybe_send_keepalive();
 2500: 				else
 2501: 					maybe_flush_socket(0);
 2502: 				next_loopchk += loopchk_limit;
 2503: 			}
 2504: 		}
 2505: 
 2506: 		if (!inc_recurse) {
 2507: 			write_ndx(f_out, NDX_DONE);
 2508: 			break;
 2509: 		}
 2510: 
 2511: 		while (1) {
 2512: 			check_for_finished_files(itemizing, code, 1);
 2513: 			if (cur_flist->next || flist_eof)
 2514: 				break;
 2515: 			wait_for_receiver();
 2516: 		}
 2517: 	} while ((cur_flist = cur_flist->next) != NULL);
 2518: 
 2519: 	if (delete_during)
 2520: 		delete_in_dir(NULL, NULL, &dev_zero, 0);
 2521: 	if (detect_renamed) {
 2522: 		if (delete_during < 0)
 2523: 			delete_during = 0;
 2524: 		detect_renamed = 0;
 2525: 
 2526: 		for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
 2527: 			struct file_struct *file = cur_flist->files[i];
 2528: 			if (local_name)
 2529: 				strlcpy(fbuf, local_name, sizeof fbuf);
 2530: 			else
 2531: 				f_name(file, fbuf);
 2532: 			recv_generator(fbuf, file, i, itemizing, code, f_out);
 2533: 		}
 2534: 	}
 2535: 	phase++;
 2536: 	if (verbose > 2)
 2537: 		rprintf(FINFO, "generate_files phase=%d\n", phase);
 2538: 
 2539: 	while (1) {
 2540: 		check_for_finished_files(itemizing, code, 1);
 2541: 		if (msgdone_cnt)
 2542: 			break;
 2543: 		wait_for_receiver();
 2544: 	}
 2545: 
 2546: 	phase++;
 2547: 	if (verbose > 2)
 2548: 		rprintf(FINFO, "generate_files phase=%d\n", phase);
 2549: 
 2550: 	write_ndx(f_out, NDX_DONE);
 2551: 
 2552: 	/* Reduce round-trip lag-time for a useless delay-updates phase. */
 2553: 	if (protocol_version >= 29 && !delay_updates)
 2554: 		write_ndx(f_out, NDX_DONE);
 2555: 
 2556: 	/* Read MSG_DONE for the redo phase (and any prior messages). */
 2557: 	while (1) {
 2558: 		check_for_finished_files(itemizing, code, 0);
 2559: 		if (msgdone_cnt > 1)
 2560: 			break;
 2561: 		wait_for_receiver();
 2562: 	}
 2563: 
 2564: 	if (protocol_version >= 29) {
 2565: 		phase++;
 2566: 		if (verbose > 2)
 2567: 			rprintf(FINFO, "generate_files phase=%d\n", phase);
 2568: 		if (delay_updates)
 2569: 			write_ndx(f_out, NDX_DONE);
 2570: 		/* Read MSG_DONE for delay-updates phase & prior messages. */
 2571: 		while (msgdone_cnt == 2)
 2572: 			wait_for_receiver();
 2573: 	}
 2574: 
 2575: 	do_progress = save_do_progress;
 2576: 	if (delete_during == 2)
 2577: 		do_delayed_deletions(fbuf);
 2578: 	if (delete_after && !solo_file && file_total > 0)
 2579: 		do_delete_pass();
 2580: 
 2581: 	if ((need_retouch_dir_perms || need_retouch_dir_times)
 2582: 	 && dir_tweaking && (!inc_recurse || delete_during == 2))
 2583: 		touch_up_dirs(dir_flist, -1);
 2584: 
 2585: 	if (max_delete >= 0 && deletion_count > max_delete) {
 2586: 		rprintf(FWARNING,
 2587: 			"Deletions stopped due to --max-delete limit (%d skipped)\n",
 2588: 			deletion_count - max_delete);
 2589: 		io_error |= IOERR_DEL_LIMIT;
 2590: 	}
 2591: 
 2592: 	if (verbose > 2)
 2593: 		rprintf(FINFO, "generate_files finished\n");
 2594: }

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