+--- 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;
+ }
-@@ -70,6 +71,8 @@ int checksum_len;
- dev_t filesystem_dev; /* used to implement -x */
- unsigned int file_struct_len;
+@@ -113,6 +114,8 @@ static int64 tmp_dev, tmp_ino;
+ #endif
+ static char tmp_sum[MAX_DIGEST_LEN];
- static char empty_sum[MD4_SUM_LENGTH];
- static int flist_count_offset;
-
-@@ -252,6 +255,44 @@ static mode_t from_wire_mode(int mode)
- return mode;
+ static char empty_sum[MAX_DIGEST_LEN];
+ static int flist_count_offset; /* for --delete --progress */
+ static int dir_count = 0;
+@@ -252,6 +255,45 @@ static int is_excluded(char *fname, int
+ return 0;
}
+static int fattr_compare(struct file_struct **file1, struct file_struct **file2)
+{
+ struct file_struct *f1 = *file1;
+ struct file_struct *f2 = *file2;
}
+static int fattr_compare(struct file_struct **file1, struct file_struct **file2)
+{
+ struct file_struct *f1 = *file1;
+ struct file_struct *f2 = *file2;
+ memcpy(the_fattr_list.files, flist->files,
+ j * sizeof (struct file_struct *));
+ qsort(the_fattr_list.files, j,
+ memcpy(the_fattr_list.files, flist->files,
+ j * sizeof (struct file_struct *));
+ qsort(the_fattr_list.files, j,
- static FILE *delete_delay_fp = NULL;
+ static int deldelay_size = 0, deldelay_cnt = 0;
+ static char *deldelay_buf = NULL;
+ static int deldelay_fd = -1;
+@@ -114,7 +117,8 @@ static int need_retouch_dir_times;
+ static int need_retouch_dir_perms;
+ static const char *solo_file = NULL;
+ * time (plus the basename, if possible) or (2) the size & checksum. If
+ * we find an exact match down to the dirname, return -1 because we found
+ * an up-to-date file in the transfer, not a renamed file. */
+ * time (plus the basename, if possible) or (2) the size & checksum. If
+ * we find an exact match down to the dirname, return -1 because we found
+ * an up-to-date file in the transfer, not a renamed file. */
+{
+ int low = the_fattr_list.low, high = the_fattr_list.high;
+ int mid, ok_match = -1, good_match = -1;
+{
+ int low = the_fattr_list.low, high = the_fattr_list.high;
+ int mid, ok_match = -1, good_match = -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 */
-+ f->u.sum = pool_alloc(pool, MD4_SUM_LENGTH,
-+ "fattr_find");
-+ file_checksum(fname, f->u.sum, f->length);
++ file_checksum(fname, F_SUM(f), F_LENGTH(f));
++ f->flags |= FLAG_FILE_SENT;
-+ /* 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.)
-@@ -249,6 +366,8 @@ static enum delret delete_dir_contents(c
+@@ -227,7 +345,9 @@ static enum delret delete_dir_contents(c
+ save_filters = push_local_filters(fname, dlen);
+
+ non_perishable_cnt = 0;
++ file_extra_cnt += SUM_EXTRA_CNT;
+ dirlist = get_dirlist(fname, dlen, 0);
++ file_extra_cnt -= SUM_EXTRA_CNT;
+ ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
+
+ if (!dirlist->used)
+@@ -264,6 +384,8 @@ static enum delret delete_dir_contents(c
* 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. */
-- if (delete_delay_fp)
-+ if (delete_delay_fp && !(flags & DEL_NO_DELETIONS))
- fprintf(delete_delay_fp, "%o %s%c", fp->mode, delbuf, '\0');
- else
+- if (delete_during == 2) {
++ if (delete_during == 2 && !(flags & DEL_NO_DELETIONS)) {
+ if (!remember_delete(fp, delbuf))
+ break;
+ } else
- delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
- }
+ delete_item(delbuf, fp->mode, NULL, flags);
- delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
- }
+ delete_item(delbuf, fp->mode, NULL, flags);
-- 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,
-@@ -1154,8 +1297,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_DEL_HERE))
-- delete_in_dir(the_file_list, fname, file, &real_st);
-+ && (file->flags & FLAG_DEL_HERE)) {
+@@ -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)) {
-@@ -1407,8 +1554,14 @@ static void recv_generator(char *fname,
- && hard_link_check(file, ndx, fname, statret, &st,
- itemizing, code, HL_SKIP))
- return;
+@@ -1624,8 +1780,14 @@ static void recv_generator(char *fname,
+ goto cleanup;
+ }
+ #endif
- return;
-@@ -1594,13 +1747,19 @@ 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());
- if (delete_before && !local_name && flist->count > 0)
- do_delete_pass(flist);
- if (delete_during == 2)
- start_delete_temp();
+ if (delete_before && !solo_file && cur_flist->used > 0)
+ do_delete_pass();
+ if (delete_during == 2) {
+@@ -1971,7 +2139,7 @@ void generate_files(int f_out, const cha
+ }
-@@ -1655,7 +1814,23 @@ void generate_files(int f_out, struct fi
- }
- recv_generator(NULL, NULL, 0, 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,
-+ maybe_ATTRS_REPORT, 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 },
-@@ -1360,7 +1363,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",
-@@ -1679,6 +1683,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