+
+/* 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, STRUCT_STAT *stp, int itemizing,
+ enum logcode code)
+{
+ STRUCT_STAT prev_st;
+ char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname;
+ int alt_dest, prev_ndx = F_HL_PREV(file);
+ struct file_struct *prev_file = FPTR(prev_ndx);
+
+ /* 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_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_SENT;
+ return 1;
+ }
+ return 0;
+ }
+
+ /* There is a finished file to link with! */
+ if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
+ /* The previous previous will be marked with FIRST. */
+ prev_ndx = F_HL_PREV(prev_file);
+ prev_file = FPTR(prev_ndx);
+ /* Update our previous pointer to point to the first. */
+ F_HL_PREV(file) = prev_ndx;
+ }
+ alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */
+ if (alt_dest >= 0 && dry_run) {
+ pathjoin(prev_name, MAXPATHLEN, basis_dir[alt_dest],
+ f_name(prev_file, NULL));
+ f_name(prev_file, altbuf);
+ realname = altbuf;
+ } else {
+ f_name(prev_file, prev_name);
+ 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];
+ STRUCT_STAT alt_st;
+ int j = 0;
+ do {
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+ if (link_stat(cmpbuf, &alt_st, 0) < 0)
+ continue;
+ if (link_dest) {
+ if (prev_st.st_dev != alt_st.st_dev
+ || prev_st.st_ino != alt_st.st_ino)
+ continue;
+ statret = 1;
+ *stp = alt_st;
+ 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_st))
+ continue;
+ statret = 1;
+ *stp = alt_st;
+ if (unchanged_attrs(file, &alt_st))
+ break;
+ } while (basis_dir[++j] != NULL);
+ }
+
+ if (maybe_hard_link(file, ndx, fname, statret, stp, prev_name, &prev_st,
+ realname, itemizing, code) < 0)
+ return -1;
+
+ if (remove_source_files == 1 && do_xfers)
+ send_msg_int(MSG_SUCCESS, ndx);
+
+ return 1;
+}
+
+int hard_link_one(struct file_struct *file, const char *fname,
+ const char *oldname, int terse)
+{
+ if (do_link(oldname, fname) < 0) {
+ enum logcode code;
+ if (terse) {
+ if (!verbose)
+ return -1;
+ code = FINFO;
+ } else
+ code = FERROR;
+ rsyserr(code, errno, "link %s => %s failed",
+ full_fname(fname), oldname);
+ return 0;
+ }
+
+ file->flags |= FLAG_HLINK_DONE;
+
+ return 1;
+}
+
+void finish_hard_link(struct file_struct *file, const char *fname,
+ STRUCT_STAT *stp, int itemizing, enum logcode code,
+ int alt_dest)
+{
+ STRUCT_STAT st, prev_st;
+ char alt_name[MAXPATHLEN], *prev_name;
+ const char *our_name;
+ int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
+
+ if (stp == NULL && prev_ndx >= 0) {
+ if (link_stat(fname, &st, 0) < 0) {
+ rsyserr(FERROR, errno, "stat %s failed",
+ full_fname(fname));
+ return;
+ }
+ stp = &st;
+ }
+
+ /* FIRST combined with DONE means we were the first to get done. */
+ file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
+ F_HL_PREV(file) = alt_dest;
+ if (alt_dest >= 0 && dry_run) {
+ pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
+ f_name(file, NULL));
+ our_name = alt_name;
+ } else
+ our_name = fname;
+
+ while ((ndx = prev_ndx) >= 0) {
+ file = FPTR(ndx);
+ file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
+ prev_ndx = F_HL_PREV(file);
+ prev_name = f_name(file, NULL);
+ prev_statret = link_stat(prev_name, &prev_st, 0);
+ if (maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_st,
+ our_name, stp, fname, itemizing, code) < 0)
+ continue;
+ if (remove_source_files == 1 && do_xfers)
+ send_msg_int(MSG_SUCCESS, ndx);
+ }
+}
+#endif