+ /* If we're not preserving permissions, change the file-list's
+ * mode based on the local permissions and some heuristics. */
+ if (!preserve_perms) {
+ int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
+ file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
+ exists);
+ }
+
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
+ && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
+ goto cleanup;
+#endif
+
+ if (preserve_links && S_ISLNK(file->mode)) {
+#ifdef SUPPORT_LINKS
+ const char *sl = F_SYMLINK(file);
+ if (safe_symlinks && unsafe_symlink(sl, fname)) {
+ if (INFO_GTE(NAME, 1)) {
+ if (solo_file) {
+ /* fname contains the destination path, but we
+ * want to report the source path. */
+ fname = f_name(file, NULL);
+ }
+ rprintf(FINFO,
+ "ignoring unsafe symlink \"%s\" -> \"%s\"\n",
+ fname, sl);
+ }
+ return;
+ }
+ if (statret == 0) {
+ char lnk[MAXPATHLEN];
+ int len;
+
+ if (S_ISLNK(sx.st.st_mode)
+ && (len = do_readlink(fname, lnk, MAXPATHLEN-1)) > 0
+ && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
+ /* The link is pointing to the right place. */
+ set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
+ if (itemizing)
+ itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
+#if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
+ if (preserve_hard_links && F_IS_HLINKED(file))
+ finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
+#endif
+ if (remove_source_files == 1)
+ goto return_with_success;
+ goto cleanup;
+ }
+ } else if (basis_dir[0] != NULL) {
+ int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
+ itemizing, code);
+ if (j == -2) {
+#ifndef CAN_HARDLINK_SYMLINK
+ if (link_dest) {
+ /* Resort to --copy-dest behavior. */
+ } else
+#endif
+ if (!copy_dest)
+ goto cleanup;
+ itemizing = 0;
+ code = FNONE;
+ } else if (j >= 0)
+ statret = 1;
+ }
+ if (atomic_create(file, fname, sl, MAKEDEV(0, 0), &sx, statret == 0 ? DEL_FOR_SYMLINK : 0)) {
+ set_file_attrs(fname, file, NULL, NULL, 0);
+ if (itemizing) {
+ if (statret == 0 && !S_ISLNK(sx.st.st_mode))
+ statret = -1;
+ itemize(fname, file, ndx, statret, &sx,
+ ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
+ }
+ if (code != FNONE && INFO_GTE(NAME, 1))
+ rprintf(code, "%s -> %s\n", fname, sl);
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && F_IS_HLINKED(file))
+ finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
+#endif
+ /* This does not check remove_source_files == 1
+ * because this is one of the items that the old
+ * --remove-sent-files option would remove. */
+ if (remove_source_files)
+ goto return_with_success;
+ }
+#endif
+ goto cleanup;
+ }
+
+ if ((am_root && preserve_devices && IS_DEVICE(file->mode))
+ || (preserve_specials && IS_SPECIAL(file->mode))) {
+ dev_t rdev;
+ int del_for_flag = 0;
+ if (IS_DEVICE(file->mode)) {
+ uint32 *devp = F_RDEV_P(file);
+ rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
+ } else
+ rdev = 0;
+ if (statret == 0) {
+ if (IS_DEVICE(file->mode)) {
+ if (!IS_DEVICE(sx.st.st_mode))
+ statret = -1;
+ del_for_flag = DEL_FOR_DEVICE;
+ } else {
+ if (!IS_SPECIAL(sx.st.st_mode))
+ statret = -1;
+ del_for_flag = DEL_FOR_SPECIAL;
+ }
+ if (statret == 0
+ && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
+ && (IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev == rdev)) {
+ /* The device or special file is identical. */
+ set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
+ if (itemizing)
+ itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && F_IS_HLINKED(file))
+ finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
+#endif
+ if (remove_source_files == 1)
+ goto return_with_success;
+ goto cleanup;
+ }
+ } else if (basis_dir[0] != NULL) {
+ int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
+ itemizing, code);
+ if (j == -2) {
+#ifndef CAN_HARDLINK_SPECIAL
+ if (link_dest) {
+ /* Resort to --copy-dest behavior. */
+ } else
+#endif
+ if (!copy_dest)
+ goto cleanup;
+ itemizing = 0;
+ code = FNONE;
+ } else if (j >= 0)
+ statret = 1;
+ }
+ if (DEBUG_GTE(GENR, 1)) {
+ rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
+ fname, (int)file->mode,
+ (long)major(rdev), (long)minor(rdev));
+ }
+ if (atomic_create(file, fname, NULL, rdev, &sx, del_for_flag)) {
+ set_file_attrs(fname, file, NULL, NULL, 0);
+ if (itemizing) {
+ itemize(fname, file, ndx, statret, &sx,
+ ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
+ }
+ if (code != FNONE && INFO_GTE(NAME, 1))
+ rprintf(code, "%s\n", fname);
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && F_IS_HLINKED(file))
+ finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
+#endif
+ if (remove_source_files == 1)
+ goto return_with_success;
+ }
+ goto cleanup;
+ }
+
+ if (!S_ISREG(file->mode)) {
+ if (solo_file)
+ fname = f_name(file, NULL);
+ rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
+ goto cleanup;
+ }
+
+ if (max_size > 0 && F_LENGTH(file) > max_size) {
+ if (INFO_GTE(SKIP, 1)) {
+ if (solo_file)
+ fname = f_name(file, NULL);
+ rprintf(FINFO, "%s is over max-size\n", fname);
+ }
+ goto cleanup;
+ }
+ if (min_size > 0 && F_LENGTH(file) < min_size) {
+ if (INFO_GTE(SKIP, 1)) {
+ if (solo_file)
+ fname = f_name(file, NULL);
+ rprintf(FINFO, "%s is under min-size\n", fname);
+ }
+ goto cleanup;
+ }
+
+ if (update_only > 0 && statret == 0
+ && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
+ if (INFO_GTE(SKIP, 1))
+ rprintf(FINFO, "%s is newer\n", fname);
+#ifdef SUPPORT_HARD_LINKS
+ if (F_IS_HLINKED(file))
+ handle_skipped_hlink(file, itemizing, code, f_out);
+#endif
+ goto cleanup;
+ }
+
+ fnamecmp_type = FNAMECMP_FNAME;
+
+ if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
+ if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
+ goto cleanup;
+ statret = -1;
+ stat_errno = ENOENT;
+ }
+
+ if (statret != 0 && basis_dir[0] != NULL) {
+ int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
+ itemizing, code);
+ if (j == -2) {
+ if (remove_source_files == 1)
+ goto return_with_success;
+ goto cleanup;
+ }
+ if (j >= 0) {
+ fnamecmp = fnamecmpbuf;
+ fnamecmp_type = j;
+ statret = 0;
+ }
+ }
+
+ real_ret = statret;
+ real_sx = sx;
+
+ if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
+ && link_stat(partialptr, &partial_st, 0) == 0
+ && S_ISREG(partial_st.st_mode)) {
+ if (statret != 0)
+ goto prepare_to_open;
+ } else
+ partialptr = NULL;
+
+ if (statret != 0 && fuzzy_dirlist) {
+ int j = find_fuzzy(file, fuzzy_dirlist);
+ if (j >= 0) {
+ fuzzy_file = fuzzy_dirlist->files[j];
+ f_name(fuzzy_file, fnamecmpbuf);
+ if (DEBUG_GTE(FUZZY, 1)) {
+ rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
+ fname, fnamecmpbuf);
+ }
+ sx.st.st_size = F_LENGTH(fuzzy_file);
+ statret = 0;
+ fnamecmp = fnamecmpbuf;
+ fnamecmp_type = FNAMECMP_FUZZY;
+ }
+ }
+
+ if (statret != 0) {
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
+ cur_flist->in_progress++;
+ goto cleanup;
+ }
+#endif
+ if (stat_errno == ENOENT)
+ goto notify_others;
+ rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
+ full_fname(fname));
+ goto cleanup;
+ }
+
+ if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
+ ;
+ else if (fnamecmp_type == FNAMECMP_FUZZY)
+ ;
+ else if (unchanged_file(fnamecmp, file, &sx.st)) {
+ if (partialptr) {
+ do_unlink(partialptr);
+ handle_partial_dir(partialptr, PDIR_DELETE);
+ }
+ set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
+ if (itemizing)
+ itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && F_IS_HLINKED(file))
+ finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
+#endif
+ if (remove_source_files != 1)
+ goto cleanup;
+ return_with_success:
+ if (!dry_run)
+ send_msg_int(MSG_SUCCESS, ndx);
+ goto cleanup;
+ }
+
+ if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
+#ifdef SUPPORT_HARD_LINKS
+ if (F_IS_HLINKED(file))
+ handle_skipped_hlink(file, itemizing, code, f_out);
+#endif
+ goto cleanup;
+ }