+/* 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, STRUCT_STAT *stp, int itemizing,
+ int maybe_ATTRS_REPORT, enum logcode code)
+{
+ int save_ignore_times = ignore_times;
+ int best_match = -1;
+ int match_level = 0;
+ int j = 0;
+
+ /* We can't let ignore_times affect the unchanged_file() test in
+ * an alternate-dest dir or we will never find any matches. */
+ ignore_times = 0;
+
+ do {
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+ if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
+ continue;
+ switch (match_level) {
+ case 0:
+ best_match = j;
+ match_level = 1;
+ /* FALL THROUGH */
+ case 1:
+ if (!unchanged_file(cmpbuf, file, stp))
+ continue;
+ best_match = j;
+ match_level = 2;
+ /* FALL THROUGH */
+ case 2:
+ if (!unchanged_attrs(file, stp))
+ continue;
+ if (always_checksum && preserve_times
+ && cmp_time(stp->st_mtime, file->modtime))
+ continue;
+ best_match = j;
+ match_level = 3;
+ break;
+ }
+ break;
+ } while (basis_dir[++j] != NULL);
+
+ ignore_times = save_ignore_times;
+
+ if (!match_level)
+ return -1;
+
+ if (j != best_match) {
+ j = best_match;
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+ if (link_stat(cmpbuf, stp, 0) < 0)
+ match_level = 0;
+ }
+
+ if (match_level == 3 && !copy_dest) {
+#ifdef SUPPORT_HARD_LINKS
+ if (link_dest) {
+ if (hard_link_one(file, ndx, fname, 0, stp,
+ cmpbuf, 1,
+ itemizing && verbose > 1,
+ code) < 0)
+ goto try_a_copy;
+ if (preserve_hard_links && file->link_u.links)
+ hard_link_cluster(file, ndx, itemizing, code);
+ } else
+#endif
+ if (itemizing)
+ itemize(file, ndx, 0, stp, 0, 0, NULL);
+ if (verbose > 1 && maybe_ATTRS_REPORT) {
+ rprintf(FCLIENT, "%s is uptodate\n", fname);
+ }
+ return -2;
+ }
+
+ if (match_level >= 2) {
+ try_a_copy: /* Copy the file locally. */
+ if (copy_file(cmpbuf, fname, file->mode) < 0) {
+ if (verbose) {
+ rsyserr(FINFO, errno, "copy_file %s => %s",
+ full_fname(cmpbuf), fname);
+ }
+ return -1;
+ }
+ if (itemizing)
+ itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
+ set_file_attrs(fname, file, NULL, 0);
+ 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" : "");
+ }
+ if (preserve_hard_links && file->link_u.links)
+ hard_link_cluster(file, ndx, itemizing, code);
+ 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. */
+static int try_dests_non(struct file_struct *file, char *fname, int ndx,
+ int itemizing, int maybe_ATTRS_REPORT,
+ enum logcode code)
+{
+ char fnamebuf[MAXPATHLEN];
+ STRUCT_STAT st;
+ int i = 0;
+
+ do {
+ pathjoin(fnamebuf, MAXPATHLEN, basis_dir[i], fname);
+ if (link_stat(fnamebuf, &st, 0) < 0 || S_ISDIR(st.st_mode)
+ || !unchanged_attrs(file, &st))
+ continue;
+ if (S_ISLNK(file->mode)) {
+#ifdef SUPPORT_LINKS
+ char lnk[MAXPATHLEN];
+ int len;
+ if ((len = readlink(fnamebuf, lnk, MAXPATHLEN-1)) <= 0)
+ continue;
+ lnk[len] = '\0';
+ if (strcmp(lnk, file->u.link) != 0)
+#endif
+ continue;
+ } else if (IS_SPECIAL(file->mode)) {
+ if (!IS_SPECIAL(st.st_mode) || st.st_rdev != file->u.rdev)
+ continue;
+ } else if (IS_DEVICE(file->mode)) {
+ if (!IS_DEVICE(st.st_mode) || st.st_rdev != file->u.rdev)
+ continue;
+ } else {
+ rprintf(FERROR,
+ "internal: try_dests_non() called with invalid mode (%o)\n",
+ (int)file->mode);
+ exit_cleanup(RERR_UNSUPPORTED);
+ }
+#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
+ ) {
+ if (do_link(fnamebuf, fname) < 0) {
+ rsyserr(FERROR, errno,
+ "failed to hard-link %s with %s",
+ fnamebuf, fname);
+ break;
+ }
+ if (preserve_hard_links && file->link_u.links)
+ hard_link_cluster(file, ndx, itemizing, code);
+ }
+#endif
+ if (itemizing && stdout_format_has_i && verbose > 1) {
+ int changes = compare_dest ? 0 : ITEM_LOCAL_CHANGE
+ + (link_dest ? ITEM_XNAME_FOLLOWS : 0);
+ char *lp = link_dest ? "" : NULL;
+ itemize(file, ndx, 0, &st, changes, 0, lp);
+ }
+ if (verbose > 1 && maybe_ATTRS_REPORT) {
+ rprintf(FCLIENT, "%s is uptodate\n", fname);
+ }
+ return -2;
+ } while (basis_dir[++i] != NULL);
+
+ return -1;
+}
+