+--- old/compat.c
++++ new/compat.c
+@@ -41,6 +41,7 @@ extern int checksum_seed;
+ extern int basis_dir_cnt;
+ extern int prune_empty_dirs;
+ extern int protocol_version;
++extern int detect_renamed;
+ extern int protect_args;
+ extern int preserve_uid;
+ extern int preserve_gid;
+@@ -218,7 +219,7 @@ void setup_protocol(int f_out,int f_in)
+ } else if (protocol_version >= 30) {
+ if (recurse && allow_inc_recurse
+ && !delete_before && !delete_after && !delay_updates
+- && !use_qsort && !prune_empty_dirs)
++ && !use_qsort && !prune_empty_dirs && !detect_renamed)
+ inc_recurse = 1;
+ need_messages_from_generator = 1;
+ }
+ the_fattr_list.files = new_array(struct file_struct *, j);
+ if (!the_fattr_list.files)
+ out_of_memory("recv_file_list");
+ memcpy(the_fattr_list.files, flist->files,
+ j * sizeof (struct file_struct *));
+ qsort(the_fattr_list.files, j,
+ the_fattr_list.files = new_array(struct file_struct *, j);
+ if (!the_fattr_list.files)
+ out_of_memory("recv_file_list");
+ memcpy(the_fattr_list.files, flist->files,
+ j * sizeof (struct file_struct *));
+ qsort(the_fattr_list.files, j,
static int deletion_count = 0; /* used to implement --max-delete */
+static int unexplored_dirs = 1;
static int deldelay_size = 0, deldelay_cnt = 0;
static char *deldelay_buf = NULL;
static int deldelay_fd = -1;
static int deletion_count = 0; /* used to implement --max-delete */
+static int unexplored_dirs = 1;
static int deldelay_size = 0, deldelay_cnt = 0;
static char *deldelay_buf = NULL;
static int deldelay_fd = -1;
+ if (fmid->modtime == f->modtime
+ && f_name_cmp(fmid, f) == 0)
+ return -1; /* assume we can't help */
+ if (fmid->modtime == f->modtime
+ && f_name_cmp(fmid, f) == 0)
+ return -1; /* assume we can't help */
-+ /* We only use the file if we can hard-link it into our tmp dir. */
-+ if (link(fname, partialptr) == 0) {
-+ if (verbose > 2) {
-+ rprintf(FINFO, "found renamed: %s => %s\n",
-+ fname, partialptr);
++ if (!dry_run) {
++ if ((partialptr = partial_dir_fname(fn)) == NULL
++ || !handle_partial_dir(partialptr, PDIR_CREATE))
++ return;
++ /* We only use the file if we can hard-link it into our tmp dir. */
++ if (link(fname, partialptr) != 0) {
++ if (errno != EEXIST)
++ handle_partial_dir(partialptr, PDIR_DELETE);
++ return;
* its contents, otherwise just checks for content. Returns DR_SUCCESS or
* DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
* buffer is used for recursion, but returned unchanged.)
* its contents, otherwise just checks for content. Returns DR_SUCCESS or
* DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
* buffer is used for recursion, but returned unchanged.)
* all the --delete-WHEN options. Note that the fbuf pointer must point to a
* MAXPATHLEN buffer with the name of the directory in it (the functions we
* call will append names onto the end, but the old dir value will be restored
- * on exit). */
* all the --delete-WHEN options. Note that the fbuf pointer must point to a
* MAXPATHLEN buffer with the name of the directory in it (the functions we
* call will append names onto the end, but the old dir value will be restored
- * on exit). */
- static void delete_in_dir(struct file_list *flist, char *fbuf,
-- struct file_struct *file, STRUCT_STAT *stp)
-+ struct file_struct *file, STRUCT_STAT *stp, int flags)
++static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev,
++ int flags)
+ *p++ = '/';
+ remainder = MAXPATHLEN - (p - fbuf);
+
/* If an item in dirlist is not found in flist, delete it
* from the filesystem. */
+ *p++ = '/';
+ remainder = MAXPATHLEN - (p - fbuf);
+
/* If an item in dirlist is not found in flist, delete it
* from the filesystem. */
+ strlcpy(p, fp->basename, remainder);
+ look_for_rename(fp, fbuf);
+ }
+ strlcpy(p, fp->basename, remainder);
+ look_for_rename(fp, fbuf);
+ }
-- delete_in_dir(flist, fbuf, file, &st);
-+ delete_in_dir(flist, fbuf, file, &st, 0);
+- delete_in_dir(fbuf, file, &st.st_dev);
++ delete_in_dir(fbuf, file, &st.st_dev, 0);
-- delete_in_dir(NULL, NULL, NULL, NULL);
-+ delete_in_dir(NULL, NULL, NULL, NULL, 0);
+- delete_in_dir(NULL, NULL, &dev_zero);
++ delete_in_dir(NULL, NULL, &dev_zero, 0);
- /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
-@@ -1232,8 +1377,12 @@ static void recv_generator(char *fname,
- if (real_ret != 0 && one_file_system)
- real_st.st_dev = filesystem_dev;
- if (delete_during && f_out != -1 && !phase && dry_run < 2
-- && (file->flags & FLAG_XFER_DIR))
-- delete_in_dir(the_file_list, fname, file, &real_st);
-+ && (file->flags & FLAG_XFER_DIR)) {
+@@ -1345,8 +1497,12 @@ static void recv_generator(char *fname,
+ }
+ }
+ else if (delete_during && f_out != -1 && !phase && dry_run < 2
+- && (file->flags & FLAG_CONTENT_DIR))
+- delete_in_dir(fname, file, &real_sx.st.st_dev);
++ && (file->flags & FLAG_CONTENT_DIR)) {
- return;
-@@ -1688,6 +1843,12 @@ void generate_files(int f_out, struct fi
- (long)getpid(), flist->count);
- }
+ goto cleanup;
+@@ -1961,6 +2123,12 @@ void generate_files(int f_out, const cha
+ if (verbose > 2)
+ rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
-@@ -1754,7 +1915,22 @@ void generate_files(int f_out, struct fi
- }
- recv_generator(NULL, NULL, 0, 0, code, -1);
+@@ -2009,7 +2177,7 @@ void generate_files(int f_out, const cha
+ dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
+ } else
+ dirdev = MAKEDEV(0, 0);
+- delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
++ delete_in_dir(f_name(fp, fbuf), fp, &dirdev, 0);
+ }
+ }
+ }
+@@ -2054,7 +2222,21 @@ void generate_files(int f_out, const cha
+ } while ((cur_flist = cur_flist->next) != NULL);
+
-- delete_in_dir(NULL, NULL, NULL, NULL);
-+ delete_in_dir(NULL, NULL, NULL, NULL, 0);
-+
+- delete_in_dir(NULL, NULL, &dev_zero);
++ delete_in_dir(NULL, NULL, &dev_zero, 0);
+ recv_generator(fbuf, file, i, itemizing, code, f_out);
+ }
+ }
+ recv_generator(fbuf, file, i, itemizing, code, f_out);
+ }
+ }
rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
{"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
{"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
{"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
+ {"detect-renamed", 0, POPT_ARG_NONE, &detect_renamed, 0, 0, 0 },
{"fuzzy", 'y', POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 },
{"compress", 'z', POPT_ARG_NONE, 0, 'z', 0, 0 },
{"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
{"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
{"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
+ {"detect-renamed", 0, POPT_ARG_NONE, &detect_renamed, 0, 0, 0 },
{"fuzzy", 'y', POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 },
{"compress", 'z', POPT_ARG_NONE, 0, 'z', 0, 0 },
- {"compress-level", 0, POPT_ARG_INT, &def_compress_level, 'z', 0, 0 },
-@@ -1355,7 +1358,7 @@ int parse_arguments(int *argc, const cha
+ {"no-compress", 0, POPT_ARG_VAL, &do_compression, 0, 0, 0 },
+@@ -1532,7 +1535,7 @@ int parse_arguments(int *argc_p, const c
snprintf(err_buf, sizeof err_buf,
"--%s cannot be used with --%s\n",
append_mode ? "append" : "inplace",
snprintf(err_buf, sizeof err_buf,
"--%s cannot be used with --%s\n",
append_mode ? "append" : "inplace",
-@@ -1674,6 +1678,8 @@ void server_options(char **args,int *arg
- args[ac++] = "--super";
- if (size_only)
- args[ac++] = "--size-only";
-+ if (detect_renamed)
-+ args[ac++] = "--detect-renamed";
+@@ -1890,6 +1894,9 @@ void server_options(char **args, int *ar
+ args[ac++] = arg;
+ }
--modify-window=NUM compare mod-times with reduced accuracy
-T, --temp-dir=DIR create temporary files in directory DIR
-y, --fuzzy find similar file for basis if no dest file
--modify-window=NUM compare mod-times with reduced accuracy
-T, --temp-dir=DIR create temporary files in directory DIR
-y, --fuzzy find similar file for basis if no dest file
--compare-dest=DIR also compare received files relative to DIR
--copy-dest=DIR ... and include copies of unchanged files
--link-dest=DIR hardlink to files in DIR when unchanged
--compare-dest=DIR also compare received files relative to DIR
--copy-dest=DIR ... and include copies of unchanged files
--link-dest=DIR hardlink to files in DIR when unchanged
-+dit(bf(--detect-renamed)) This option tells rsync to scan the receiving
-+side for files that have been renamed, and to use any that are found as
-+alternate basis files to help speed up the transfer.
++dit(bf(--detect-renamed)) With this option, for each new source file
++(call it em(src/S)), rsync looks for a file em(dest/D) anywhere in the
++destination that passes the quick check with em(src/S). If such a em(dest/D)
++is found, rsync uses it as an alternate basis for transferring em(S). The
++idea is that if em(src/S) was renamed from em(src/D) (as opposed to em(src/S)
++passing the quick check with em(dest/D) by coincidence), the delta-transfer
++algorithm will find that all the data matches between em(src/S) and em(dest/D),
++and the transfer will be really fast.
++
+By default, alternate-basis files are hard-linked into a directory named
+".~tmp~" in each file's destination directory, but if you've specified
+the bf(--partial-dir) option, that directory will be used instead. These
+By default, alternate-basis files are hard-linked into a directory named
+".~tmp~" in each file's destination directory, but if you've specified
+the bf(--partial-dir) option, that directory will be used instead. These