+ dist = fuzzy_distance(name, len, fname, fname_len);
+ /* Add some extra weight to how well the suffixes match. */
+ dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
+ * 10;
+ if (verbose > 4) {
+ rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
+ name, (int)(dist>>16), (int)(dist&0xFFFF));
+ }
+ if (dist <= lowest_dist) {
+ lowest_dist = dist;
+ lowest_j = j;
+ }
+ }
+
+ return lowest_j;
+}
+
+/* Copy a file found in our --copy-dest handling. */
+static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
+{
+ char buf[MAXPATHLEN];
+ const char *copy_to, *partialptr;
+ int ok, fd_w;
+
+ if (inplace) {
+ /* Let copy_file open the destination in place. */
+ fd_w = -1;
+ copy_to = dest;
+ } else {
+ fd_w = open_tmpfile(buf, dest, file);
+ if (fd_w < 0)
+ return -1;
+ copy_to = buf;
+ }
+ cleanup_set(copy_to, NULL, NULL, -1, -1);
+ if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
+ if (verbose) {
+ rsyserr(FINFO, errno, "copy_file %s => %s",
+ full_fname(src), copy_to);
+ }
+ /* Try to clean up. */
+ unlink(copy_to);
+ cleanup_disable();
+ return -1;
+ }
+ partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
+ ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
+ cleanup_disable();
+ return ok ? 0 : -1;
+}
+
+/* This is only called for regular files. We return -2 if we've finished
+ * handling the file, -1 if no dest-linking occurred, or a non-negative
+ * value if we found an alternate basis file. */
+static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
+ char *cmpbuf, stat_x *sxp, int itemizing,
+ enum logcode code)
+{
+ int best_match = -1;
+ int match_level = 0;
+ int j = 0;
+
+ do {
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+ if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
+ continue;
+ switch (match_level) {
+ case 0:
+ best_match = j;
+ match_level = 1;
+ /* FALL THROUGH */
+ case 1:
+ if (!unchanged_file(cmpbuf, file, &sxp->st))
+ continue;
+ best_match = j;
+ match_level = 2;
+ /* FALL THROUGH */
+ case 2:
+ if (!unchanged_attrs(cmpbuf, file, sxp))
+ continue;
+ best_match = j;
+ match_level = 3;
+ break;
+ }
+ break;
+ } while (basis_dir[++j] != NULL);
+
+ if (!match_level)
+ return -1;
+
+ if (j != best_match) {
+ j = best_match;
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+ if (link_stat(cmpbuf, &sxp->st, 0) < 0)
+ return -1;
+ }
+
+ if (match_level == 3 && !copy_dest) {
+#ifdef SUPPORT_HARD_LINKS
+ if (link_dest) {
+ if (!hard_link_one(file, fname, cmpbuf, 1))
+ goto try_a_copy;
+ if (preserve_hard_links && F_IS_HLINKED(file))
+ finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
+ if (!maybe_ATTRS_REPORT && (verbose > 1 || stdout_format_has_i > 1)) {
+ itemize(cmpbuf, file, ndx, 1, sxp,
+ ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
+ 0, "");
+ }
+ } else
+#endif
+ if (itemizing)
+ itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
+ if (verbose > 1 && maybe_ATTRS_REPORT)
+ rprintf(FCLIENT, "%s is uptodate\n", fname);
+ return -2;
+ }
+
+ if (match_level >= 2) {
+#ifdef SUPPORT_HARD_LINKS
+ try_a_copy: /* Copy the file locally. */
+#endif
+ if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
+ return -1;
+ if (itemizing)
+ itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
+ if (maybe_ATTRS_REPORT
+ && ((!itemizing && verbose && match_level == 2)
+ || (verbose > 1 && match_level == 3))) {
+ code = match_level == 3 ? FCLIENT : FINFO;
+ rprintf(code, "%s%s\n", fname,
+ match_level == 3 ? " is uptodate" : "");
+ }
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && F_IS_HLINKED(file))
+ finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
+#endif
+ return -2;
+ }
+
+ return FNAMECMP_BASIS_DIR_LOW + j;
+}
+
+/* This is only called for non-regular files. We return -2 if we've finished
+ * handling the file, or -1 if no dest-linking occurred, or a non-negative
+ * value if we found an alternate basis file. */
+static int try_dests_non(struct file_struct *file, char *fname, int ndx,
+ char *cmpbuf, stat_x *sxp, int itemizing,
+ enum logcode code)
+{
+ char lnk[MAXPATHLEN];
+ int best_match = -1;
+ int match_level = 0;
+ enum nonregtype type;
+ uint32 *devp;
+ int len, j = 0;
+
+#ifndef SUPPORT_LINKS
+ if (S_ISLNK(file->mode))
+ return -1;
+#endif
+ if (S_ISDIR(file->mode)) {
+ type = TYPE_DIR;
+ } else if (IS_SPECIAL(file->mode))
+ type = TYPE_SPECIAL;
+ else if (IS_DEVICE(file->mode))
+ type = TYPE_DEVICE;
+#ifdef SUPPORT_LINKS
+ else if (S_ISLNK(file->mode))
+ type = TYPE_SYMLINK;
+#endif
+ else {
+ rprintf(FERROR,
+ "internal: try_dests_non() called with invalid mode (%o)\n",
+ (int)file->mode);
+ exit_cleanup(RERR_UNSUPPORTED);
+ }
+
+ do {
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+ if (link_stat(cmpbuf, &sxp->st, 0) < 0)
+ continue;
+ switch (type) {
+ case TYPE_DIR:
+ if (!S_ISDIR(sxp->st.st_mode))
+ continue;
+ break;
+ case TYPE_SPECIAL:
+ if (!IS_SPECIAL(sxp->st.st_mode))
+ continue;
+ break;
+ case TYPE_DEVICE:
+ if (!IS_DEVICE(sxp->st.st_mode))
+ continue;
+ break;
+#ifdef SUPPORT_LINKS
+ case TYPE_SYMLINK:
+ if (!S_ISLNK(sxp->st.st_mode))
+ continue;
+ break;
+#endif
+ }
+ if (match_level < 1) {
+ match_level = 1;
+ best_match = j;
+ }
+ switch (type) {
+ case TYPE_DIR:
+ break;
+ case TYPE_SPECIAL:
+ case TYPE_DEVICE:
+ devp = F_RDEV_P(file);
+ if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
+ continue;
+ break;
+#ifdef SUPPORT_LINKS
+ case TYPE_SYMLINK:
+ if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
+ continue;
+ lnk[len] = '\0';
+ if (strcmp(lnk, F_SYMLINK(file)) != 0)
+ continue;
+ break;
+#endif
+ }
+ if (match_level < 2) {
+ match_level = 2;
+ best_match = j;
+ }
+ if (unchanged_attrs(cmpbuf, file, sxp)) {
+ match_level = 3;
+ best_match = j;
+ break;
+ }
+ } while (basis_dir[++j] != NULL);
+
+ if (!match_level)
+ return -1;
+
+ if (j != best_match) {
+ j = best_match;
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+ if (link_stat(cmpbuf, &sxp->st, 0) < 0)
+ return -1;
+ }
+
+ if (match_level == 3) {
+#ifdef SUPPORT_HARD_LINKS
+ if (link_dest
+#ifndef CAN_HARDLINK_SYMLINK
+ && !S_ISLNK(file->mode)
+#endif
+#ifndef CAN_HARDLINK_SPECIAL
+ && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
+#endif
+ && !S_ISDIR(file->mode)) {
+ if (do_link(cmpbuf, fname) < 0) {
+ rsyserr(FERROR_XFER, errno,
+ "failed to hard-link %s with %s",
+ cmpbuf, fname);
+ return j;
+ }
+ if (preserve_hard_links && F_IS_HLINKED(file))
+ finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
+ } else
+#endif
+ match_level = 2;
+ if (itemizing && stdout_format_has_i
+ && (verbose > 1 || stdout_format_has_i > 1)) {
+ int chg = compare_dest && type != TYPE_DIR ? 0
+ : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
+ char *lp = match_level == 3 ? "" : NULL;
+ itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
+ }
+ if (verbose > 1 && maybe_ATTRS_REPORT) {
+ rprintf(FCLIENT, "%s%s is uptodate\n",
+ fname, type == TYPE_DIR ? "/" : "");
+ }
+ return -2;
+ }
+
+ return j;
+}
+
+static void list_file_entry(struct file_struct *f)
+{
+ char permbuf[PERMSTRING_SIZE];
+ double len;
+
+ if (!F_IS_ACTIVE(f)) {
+ /* this can happen if duplicate names were removed */
+ return;
+ }
+
+ permstring(permbuf, f->mode);
+ len = F_LENGTH(f);
+
+ /* TODO: indicate '+' if the entry has an ACL. */
+
+#ifdef SUPPORT_LINKS
+ if (preserve_links && S_ISLNK(f->mode)) {
+ rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
+ permbuf, len, timestring(f->modtime),
+ f_name(f, NULL), F_SYMLINK(f));
+ } else
+#endif
+ {
+ rprintf(FINFO, "%s %11.0f %s %s\n",
+ permbuf, len, timestring(f->modtime),
+ f_name(f, NULL));
+ }
+}
+
+static int phase = 0;
+static int dflt_perms;
+
+static int implied_dirs_are_missing;
+/* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
+static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
+{
+ return F_DEPTH(file) > F_DEPTH(subtree)
+ && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
+}
+
+/* Acts on the indicated item in cur_flist whose name is fname. If a dir,
+ * make sure it exists, and has the right permissions/timestamp info. For
+ * all other non-regular files (symlinks, etc.) we create them here. For
+ * regular files that have changed, we try to find a basis file and then
+ * start sending checksums. The ndx is the file's unique index value.