- for (i = 0; i < hlink_count; i++) {
- file = hlink_list[i];
- hlink1 = f_name_to(file, fbuf, sizeof fbuf);
- if (link_stat(hlink1, &st1) != 0)
- continue;
- while ((file = file->F_NEXT) != NULL) {
- hlink2 = f_name(file);
- if (link_stat(hlink2, &st2) == 0) {
- if (st2.st_dev == st1.st_dev
- && st2.st_ino == st1.st_ino)
- continue;
- if (robust_unlink(hlink2)) {
- if (verbose > 0) {
- rprintf(FINFO,
- "unlink %s failed: %s\n",
- full_fname(hlink2),
- strerror(errno));
- }
+/* 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, statx *sxp, 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_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;
+ 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];
+ statx 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)