+ return -1;
+}
+
+/* Figure out if a prior entry is still there or if we just have a
+ * cached name for it. Never called with a FLAG_HLINK_FIRST entry. */
+static char *check_prior(int prev_ndx, int gnum, struct file_list **flist_p)
+{
+ struct file_list *flist = flist_for_ndx(prev_ndx);
+ struct ht_int32_node *node;
+
+ if (flist) {
+ *flist_p = flist;
+ return NULL;
+ }
+
+ node = hashtable_find(prior_hlinks, gnum, 0);
+ assert(node != NULL && node->data);
+ assert(CVAL(node->data, 0) != 0);
+ return node->data;
+}
+
+/* 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 = F_HL_PREV(file);
+
+ prev_name = realname = check_prior(prev_ndx, gnum, &flist);
+
+ if (!prev_name) {
+ struct file_struct *prev_file = flist->files[prev_ndx - flist->ndx_start];
+
+ /* Is the previous link is not complete yet? */
+ if (!(prev_file->flags & FLAG_HLINK_DONE)) {
+ /* Is the previous link being transferred? */
+ if (prev_file->flags & FLAG_FILE_SENT) {
+ /* Add ourselves to the list of files that will be
+ * updated when the transfer completes, and mark
+ * ourself as waiting for the transfer. */
+ F_HL_PREV(file) = F_HL_PREV(prev_file);
+ F_HL_PREV(prev_file) = ndx;
+ file->flags |= FLAG_FILE_SENT;
+ cur_flist->in_progress++;
+ return 1;
+ }
+ return 0;
+ }
+
+ /* There is a finished file to link with! */
+ if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
+ /* The previous previous is FIRST when prev is not. */
+ prev_ndx = F_HL_PREV(prev_file);
+ prev_name = realname = check_prior(prev_ndx, gnum, &flist);
+ /* Update our previous pointer to point to the FIRST. */
+ F_HL_PREV(file) = prev_ndx;
+ }
+
+ if (!prev_name) {
+ int alt_dest;
+
+ prev_file = flist->files[prev_ndx - flist->ndx_start];
+ /* F_HL_PREV() is alt_dest value when DONE && FIRST. */
+ alt_dest = F_HL_PREV(prev_file);
+
+ if (alt_dest >= 0 && dry_run) {
+ pathjoin(namebuf, MAXPATHLEN, basis_dir[alt_dest],
+ f_name(prev_file, NULL));
+ prev_name = namebuf;
+ realname = f_name(prev_file, altbuf);
+ } else {
+ prev_name = f_name(prev_file, namebuf);
+ realname = prev_name;
+ }
+ }
+ }
+
+ if (link_stat(prev_name, &prev_st, 0) < 0) {
+ rsyserr(FERROR, errno, "stat %s failed",
+ full_fname(prev_name));
+ return -1;
+ }
+
+ if (statret < 0 && basis_dir[0] != NULL) {
+ /* If we match an alt-dest item, we don't output this as a change. */
+ char cmpbuf[MAXPATHLEN];
+ stat_x alt_sx;
+ int j = 0;
+#ifdef SUPPORT_ACLS
+ alt_sx.acc_acl = alt_sx.def_acl = NULL;
+#endif
+ do {
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+ if (link_stat(cmpbuf, &alt_sx.st, 0) < 0)
+ continue;
+ if (link_dest) {
+ if (prev_st.st_dev != alt_sx.st.st_dev
+ || prev_st.st_ino != alt_sx.st.st_ino)
+ continue;
+ statret = 1;
+ if (verbose < 2 || !stdout_format_has_i) {
+ itemizing = 0;
+ code = FNONE;
+ if (verbose > 1 && maybe_ATTRS_REPORT)
+ rprintf(FCLIENT, "%s is uptodate\n", fname);
+ }
+ break;
+ }
+ if (!unchanged_file(cmpbuf, file, &alt_sx.st))
+ continue;
+ statret = 1;
+ if (unchanged_attrs(cmpbuf, file, &alt_sx))
+ break;
+ } while (basis_dir[++j] != NULL);
+ if (statret == 1) {
+ sxp->st = alt_sx.st;
+#ifdef SUPPORT_ACLS
+ if (preserve_acls && !S_ISLNK(file->mode)) {
+ if (!ACL_READY(*sxp))
+ get_acl(cmpbuf, sxp);
+ else {
+ sxp->acc_acl = alt_sx.acc_acl;
+ sxp->def_acl = alt_sx.def_acl;
+ }
+ }
+#endif
+ }
+#ifdef SUPPORT_ACLS
+ else if (preserve_acls)
+ free_acl(&alt_sx);