+/* Analyze the hard-links in the file-list by creating a list of all the
+ * items that have hlink data, sorting them, and matching up identical
+ * values into clusters. These will be a single linked list from last
+ * to first when we're done. */
+void match_hard_links(struct file_list *flist)
+{
+ if (!list_only && flist->used) {
+ int i, ndx_count = 0;
+ int32 *ndx_list;
+
+ if (!(ndx_list = new_array(int32, flist->used)))
+ out_of_memory("match_hard_links");
+
+ for (i = 0; i < flist->used; i++) {
+ if (F_IS_HLINKED(flist->sorted[i]))
+ ndx_list[ndx_count++] = i;
+ }
+
+ hlink_flist = flist;
+
+ if (ndx_count)
+ match_gnums(ndx_list, ndx_count);
+
+ free(ndx_list);
+ }
+ if (protocol_version < 30)
+ idev_destroy();
+}
+
+static int maybe_hard_link(struct file_struct *file, int ndx,
+ const char *fname, int statret, stat_x *sxp,
+ const char *oldname, STRUCT_STAT *old_stp,
+ const char *realname, int itemizing, enum logcode code)
+{
+ if (statret == 0) {
+ if (sxp->st.st_dev == old_stp->st_dev
+ && sxp->st.st_ino == old_stp->st_ino) {
+ if (itemizing) {
+ itemize(fname, file, ndx, statret, sxp,
+ ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
+ 0, "");
+ }
+ if (verbose > 1 && maybe_ATTRS_REPORT)
+ rprintf(FCLIENT, "%s is uptodate\n", fname);
+ file->flags |= FLAG_HLINK_DONE;
+ return 0;
+ }
+ if (make_backups > 0) {
+ if (!make_backup(fname))
+ return -1;
+ } else if (robust_unlink(fname)) {
+ rsyserr(FERROR_XFER, errno, "unlink %s failed",
+ full_fname(fname));
+ return -1;
+ }
+ }
+
+ if (hard_link_one(file, fname, oldname, 0)) {
+ if (itemizing) {
+ itemize(fname, file, ndx, statret, sxp,
+ ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
+ realname);
+ }
+ if (code != FNONE && verbose)
+ rprintf(code, "%s => %s\n", fname, realname);
+ return 0;
+ }
+ return -1;
+}
+
+/* Figure out if a prior entry is still there or if we just have a
+ * cached name for it. */
+static char *check_prior(struct file_struct *file, int gnum,
+ int *prev_ndx_p, struct file_list **flist_p)
+{
+ struct file_struct *fp;
+ struct ht_int32_node *node;
+ int prev_ndx = F_HL_PREV(file);
+
+ while (1) {
+ struct file_list *flist;
+ if (prev_ndx < 0
+ || (flist = flist_for_ndx(prev_ndx, NULL)) == NULL)
+ break;
+ fp = flist->files[prev_ndx - flist->ndx_start];
+ if (!(fp->flags & FLAG_SKIP_HLINK)) {
+ *prev_ndx_p = prev_ndx;
+ *flist_p = flist;
+ return NULL;
+ }
+ F_HL_PREV(file) = prev_ndx = F_HL_PREV(fp);
+ }
+
+ if (inc_recurse
+ && (node = hashtable_find(prior_hlinks, gnum, 0)) != NULL) {
+ assert(node->data != NULL);
+ if (CVAL(node->data, 0) != 0) {
+ *prev_ndx_p = -1;
+ *flist_p = NULL;
+ return node->data;
+ }
+ /* The prior file must have been skipped. */
+ F_HL_PREV(file) = -1;
+ }
+
+ *prev_ndx_p = -1;
+ *flist_p = NULL;
+ return NULL;
+}
+
+/* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns:
+ * 0 = process the file, 1 = skip the file, -1 = error occurred. */
+int hard_link_check(struct file_struct *file, int ndx, const char *fname,
+ int statret, stat_x *sxp, int itemizing,
+ enum logcode code)
+{
+ STRUCT_STAT prev_st;
+ char namebuf[MAXPATHLEN], altbuf[MAXPATHLEN];
+ char *realname, *prev_name;
+ struct file_list *flist;
+ int gnum = inc_recurse ? F_HL_GNUM(file) : -1;
+ int prev_ndx;
+
+ prev_name = realname = check_prior(file, gnum, &prev_ndx, &flist);
+
+ if (!prev_name) {
+ struct file_struct *prev_file;