1 This patch adds the --detect-renamed option which makes rsync notice files
2 that either (1) match in size & modify-time (plus the basename, if possible)
3 or (2) match in size & checksum (when --checksum was also specified) and use
4 each match as an alternate basis file to speed up the transfer.
6 The algorithm attempts to scan the receiving-side's files in an efficient
7 manner. If --delete[-before] is enabled, we'll take advantage of the
8 pre-transfer delete pass to prepare any alternate-basis-file matches we
9 might find. If --delete-before is not enabled, rsync does the rename scan
10 during the regular file-sending scan (scanning each directory right before
11 the generator starts updating files from that dir). In this latter mode,
12 rsync might delay the updating of a file (if no alternate-basis match was
13 yet found) until the full scan of the receiving side is complete, at which
14 point any delayed files are processed.
16 I chose to hard-link the alternate-basis files into a ".~tmp~" subdir that
17 takes advantage of rsync's pre-existing partial-dir logic. This uses less
18 memory than trying to keep track of the matches internally, and also allows
19 any deletions or file-updates to occur normally without interfering with
20 these alternate-basis discoveries.
22 To use this patch, run these commands for a successful build:
24 patch -p1 <patches/detect-renamed.diff
25 ./configure (optional if already run)
30 We need to never return a match from fattr_find() that has a basis
31 file. This will ensure that we don't try to give a renamed file to
32 a file that can't use it, while missing out on giving it to a file
37 @@ -56,6 +56,7 @@ extern int non_perishable_cnt;
38 extern int prune_empty_dirs;
39 extern int copy_links;
40 extern int copy_unsafe_links;
41 +extern int detect_renamed;
42 extern int protocol_version;
43 extern int sanitize_paths;
44 extern struct stats stats;
45 @@ -86,6 +87,8 @@ static int64 tmp_dev, tmp_ino;
47 static char tmp_sum[MD4_SUM_LENGTH];
49 +struct file_list the_fattr_list;
51 static char empty_sum[MD4_SUM_LENGTH];
52 static int flist_count_offset; /* for --delete --progress */
54 @@ -268,6 +271,45 @@ static mode_t from_wire_mode(int mode)
58 +static int fattr_compare(struct file_struct **file1, struct file_struct **file2)
60 + struct file_struct *f1 = *file1;
61 + struct file_struct *f2 = *file2;
62 + int64 len1 = F_LENGTH(f1), len2 = F_LENGTH(f2);
65 + if (!f1->basename || !S_ISREG(f1->mode) || !len1) {
66 + if (!f2->basename || !S_ISREG(f2->mode) || !len2)
70 + if (!f2->basename || !S_ISREG(f2->mode) || !len2)
73 + /* Don't use diff for values that are longer than an int. */
75 + return len1 < len2 ? -1 : 1;
77 + if (always_checksum) {
78 + diff = u_memcmp(F_SUM(f1), F_SUM(f2), checksum_len);
81 + } else if (f1->modtime != f2->modtime)
82 + return f1->modtime < f2->modtime ? -1 : 1;
84 + diff = u_strcmp(f1->basename, f2->basename);
88 + if (f1->dirname == f2->dirname)
94 + return u_strcmp(f1->dirname, f2->dirname);
97 static void send_directory(int f, struct file_list *flist, int ndx,
98 char *fbuf, int len, int flags);
100 @@ -1706,6 +1748,25 @@ struct file_list *recv_file_list(int f)
102 clean_flist(flist, relative_paths, 1);
104 + if (detect_renamed) {
105 + int j = flist->count;
106 + the_fattr_list.count = j;
107 + the_fattr_list.files = new_array(struct file_struct *, j);
108 + if (!the_fattr_list.files)
109 + out_of_memory("recv_file_list");
110 + memcpy(the_fattr_list.files, flist->files,
111 + j * sizeof (struct file_struct *));
112 + qsort(the_fattr_list.files, j,
113 + sizeof the_fattr_list.files[0], (int (*)())fattr_compare);
114 + the_fattr_list.low = 0;
116 + struct file_struct *fp = the_fattr_list.files[j];
117 + if (fp->basename && S_ISREG(fp->mode) && F_LENGTH(fp))
120 + the_fattr_list.high = j;
124 qsort(dir_flist->files + dstart, dir_flist->count - dstart,
125 sizeof dir_flist->files[0], (int (*)())file_compare);
128 @@ -79,6 +79,7 @@ extern char *basis_dir[];
129 extern int compare_dest;
130 extern int copy_dest;
131 extern int link_dest;
132 +extern int detect_renamed;
133 extern int whole_file;
134 extern int list_only;
135 extern int new_root_dir;
136 @@ -95,6 +96,7 @@ extern char *backup_suffix;
137 extern int backup_suffix_len;
138 extern struct file_list *cur_flist, *first_flist, *dir_flist;
139 extern struct filter_list_struct server_filter_list;
140 +extern struct file_list the_fattr_list;
142 int ignore_perishable = 0;
143 int non_perishable_cnt = 0;
144 @@ -102,12 +104,14 @@ int maybe_ATTRS_REPORT = 0;
146 static dev_t dev_zero;
147 static int deletion_count = 0; /* used to implement --max-delete */
148 +static int unexplored_dirs = 1;
149 static int deldelay_size = 0, deldelay_cnt = 0;
150 static char *deldelay_buf = NULL;
151 static int deldelay_fd = -1;
152 static BOOL solo_file = 0;
154 -/* For calling delete_item() and delete_dir_contents(). */
155 +/* For calling delete_item(), delete_dir_contents(), and delete_in_dir(). */
156 +#define DEL_NO_DELETIONS (1<<0)
157 #define DEL_RECURSE (1<<1) /* recurse */
158 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
160 @@ -129,11 +133,120 @@ static int is_backup_file(char *fn)
161 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
164 +/* Search for a regular file that matches either (1) the size & modified
165 + * time (plus the basename, if possible) or (2) the size & checksum. If
166 + * we find an exact match down to the dirname, return -1 because we found
167 + * an up-to-date file in the transfer, not a renamed file. */
168 +static int fattr_find(struct file_struct *f, char *fname)
170 + int low = the_fattr_list.low, high = the_fattr_list.high;
171 + int mid, ok_match = -1, good_match = -1;
172 + struct file_struct *fmid;
175 + while (low <= high) {
176 + mid = (low + high) / 2;
177 + fmid = the_fattr_list.files[mid];
178 + if (F_LENGTH(fmid) != F_LENGTH(f)) {
179 + if (F_LENGTH(fmid) < F_LENGTH(f))
185 + if (always_checksum) {
186 + /* We use the FLAG_SENT flag to indicate when we
187 + * have computed the checksum for an entry. */
188 + if (!(f->flags & FLAG_SENT)) {
189 + if (fmid->modtime == f->modtime
190 + && f_name_cmp(fmid, f) == 0)
191 + return -1; /* assume we can't help */
192 + file_checksum(fname, (char*)F_SUM(f), F_LENGTH(f));
193 + f->flags |= FLAG_SENT;
195 + diff = u_memcmp(F_SUM(fmid), F_SUM(f), checksum_len);
204 + if (fmid->modtime != f->modtime) {
205 + if (fmid->modtime < f->modtime)
213 + diff = u_strcmp(fmid->basename, f->basename);
216 + if (fmid->dirname == f->dirname)
217 + return -1; /* file is up-to-date */
218 + if (!fmid->dirname) {
226 + diff = u_strcmp(fmid->dirname, f->dirname);
228 + return -1; /* file is up-to-date */
236 + return good_match >= 0 ? good_match : ok_match;
239 +static void look_for_rename(struct file_struct *file, char *fname)
241 + struct file_struct *fp;
242 + char *partialptr, *fn;
246 + if ((ndx = fattr_find(file, fname)) < 0)
249 + fp = the_fattr_list.files[ndx];
250 + fn = f_name(fp, NULL);
251 + /* We don't provide an alternate-basis file if there is a basis file. */
252 + if (link_stat(fn, &st, 0) == 0)
254 + if ((partialptr = partial_dir_fname(fn)) == NULL
255 + || !handle_partial_dir(partialptr, PDIR_CREATE))
258 + /* We only use the file if we can hard-link it into our tmp dir. */
259 + if (link(fname, partialptr) == 0) {
261 + rprintf(FINFO, "found renamed: %s => %s\n",
262 + fname, partialptr);
267 + if (errno != EEXIST)
268 + handle_partial_dir(partialptr, PDIR_DELETE);
271 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
272 * delete recursively.
274 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
275 * a directory! (The buffer is used for recursion, but returned unchanged.)
277 + * Also note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
279 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
281 @@ -155,6 +268,8 @@ static enum delret delete_item(char *fbu
283 /* OK: try to delete the directory. */
285 + if (flags & DEL_NO_DELETIONS)
288 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
290 @@ -201,6 +316,8 @@ static enum delret delete_item(char *fbu
291 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
292 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
293 * buffer is used for recursion, but returned unchanged.)
295 + * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
297 static enum delret delete_dir_contents(char *fname, int flags)
299 @@ -220,7 +337,9 @@ static enum delret delete_dir_contents(c
300 save_filters = push_local_filters(fname, dlen);
302 non_perishable_cnt = 0;
303 + file_extra_cnt += SUM_EXTRA_CNT;
304 dirlist = get_dirlist(fname, dlen, 0);
305 + file_extra_cnt -= SUM_EXTRA_CNT;
306 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
309 @@ -257,6 +376,8 @@ static enum delret delete_dir_contents(c
310 if (S_ISDIR(fp->mode)
311 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
313 + if (detect_renamed && S_ISREG(fp->mode))
314 + look_for_rename(fp, fname);
315 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
318 @@ -409,13 +530,17 @@ static void do_delayed_deletions(char *d
319 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
320 * MAXPATHLEN buffer with the name of the directory in it (the functions we
321 * call will append names onto the end, but the old dir value will be restored
325 + * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
327 static void delete_in_dir(struct file_list *flist, char *fbuf,
328 - struct file_struct *file, dev_t *fs_dev)
329 + struct file_struct *file, dev_t *fs_dev, int flags)
331 static int already_warned = 0;
332 struct file_list *dirlist;
333 - char delbuf[MAXPATHLEN];
334 + char *p, delbuf[MAXPATHLEN];
335 + unsigned remainder;
339 @@ -426,21 +551,28 @@ static void delete_in_dir(struct file_li
341 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
343 + flags |= DEL_RECURSE;
346 maybe_send_keepalive();
348 if (io_error && !ignore_errors) {
349 - if (already_warned)
350 + if (!already_warned) {
352 + "IO error encountered -- skipping file deletion\n");
353 + already_warned = 1;
355 + if (!detect_renamed)
358 - "IO error encountered -- skipping file deletion\n");
359 - already_warned = 1;
361 + flags |= DEL_NO_DELETIONS;
365 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
367 + if (detect_renamed)
370 if (one_file_system) {
371 if (file->flags & FLAG_TOP_DIR)
372 filesystem_dev = *fs_dev;
373 @@ -450,6 +582,11 @@ static void delete_in_dir(struct file_li
375 dirlist = get_dirlist(fbuf, dlen, 0);
378 + if (dlen != 1 || *fbuf != '/')
380 + remainder = MAXPATHLEN - (p - fbuf);
382 /* If an item in dirlist is not found in flist, delete it
383 * from the filesystem. */
384 for (i = dirlist->count; i--; ) {
385 @@ -462,16 +599,23 @@ static void delete_in_dir(struct file_li
389 + if (detect_renamed && S_ISREG(fp->mode)) {
390 + strlcpy(p, fp->basename, remainder);
391 + look_for_rename(fp, fbuf);
393 if (flist_find(flist, fp) < 0) {
395 - if (delete_during == 2) {
396 + if (delete_during == 2 && !(flags & DEL_NO_DELETIONS)) {
397 if (!remember_delete(fp, delbuf))
400 - delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
402 + delete_item(delbuf, fp->mode, NULL, flags);
403 + } else if (detect_renamed && S_ISDIR(fp->mode))
412 @@ -501,9 +645,9 @@ static void do_delete_pass(struct file_l
413 || !S_ISDIR(st.st_mode))
416 - delete_in_dir(flist, fbuf, file, &st.st_dev);
417 + delete_in_dir(flist, fbuf, file, &st.st_dev, 0);
419 - delete_in_dir(NULL, NULL, NULL, &dev_zero);
420 + delete_in_dir(NULL, NULL, NULL, &dev_zero, 0);
422 if (do_progress && !am_server)
423 rprintf(FINFO, " \r");
424 @@ -1038,6 +1182,7 @@ static int try_dests_non(struct file_str
428 +static struct bitbag *delayed_bits = NULL;
429 static int phase = 0;
431 /* Acts on cur_flist->file's ndx'th item, whose name is fname. If a dir,
432 @@ -1229,8 +1374,12 @@ static void recv_generator(char *fname,
435 else if (delete_during && f_out != -1 && !phase && dry_run < 2
436 - && (file->flags & FLAG_XFER_DIR))
437 - delete_in_dir(cur_flist, fname, file, &real_st.st_dev);
438 + && (file->flags & FLAG_XFER_DIR)) {
439 + if (detect_renamed && real_ret != 0)
441 + delete_in_dir(cur_flist, fname, file, &real_st.st_dev,
442 + delete_during < 0 ? DEL_NO_DELETIONS : 0);
447 @@ -1494,8 +1643,14 @@ static void recv_generator(char *fname,
448 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
451 - if (stat_errno == ENOENT)
452 + if (stat_errno == ENOENT) {
453 + if (detect_renamed && unexplored_dirs > 0
454 + && F_LENGTH(file)) {
455 + bitbag_set_bit(delayed_bits, ndx);
460 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
463 @@ -1717,6 +1872,12 @@ void generate_files(int f_out, char *loc
465 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
467 + if (detect_renamed) {
468 + delayed_bits = bitbag_create(flist->count);
469 + if (!delete_before && !delete_during)
470 + delete_during = -1;
473 if (delete_before && !local_name && cur_flist->count > 0)
474 do_delete_pass(cur_flist);
475 if (delete_during == 2) {
476 @@ -1727,7 +1888,7 @@ void generate_files(int f_out, char *loc
480 - if (append_mode > 0 || whole_file < 0)
481 + if (append_mode > 0 || detect_renamed || whole_file < 0)
484 rprintf(FINFO, "delta-transmission %s\n",
485 @@ -1752,7 +1913,7 @@ void generate_files(int f_out, char *loc
486 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
488 dirdev = MAKEDEV(0, 0);
489 - delete_in_dir(cur_flist, f_name(fp, fbuf), fp, &dirdev);
490 + delete_in_dir(cur_flist, f_name(fp, fbuf), fp, &dirdev, 0);
493 for (i = cur_flist->low; i <= cur_flist->high; i++) {
494 @@ -1798,7 +1959,21 @@ void generate_files(int f_out, char *loc
498 - delete_in_dir(NULL, NULL, NULL, &dev_zero);
499 + delete_in_dir(NULL, NULL, NULL, &dev_zero, 0);
500 + if (detect_renamed) {
501 + if (delete_during < 0)
503 + detect_renamed = 0;
505 + for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
506 + struct file_struct *file = flist->files[i];
508 + strlcpy(fbuf, local_name, sizeof fbuf);
510 + f_name(file, fbuf);
511 + recv_generator(fbuf, file, i, itemizing, code, f_out);
516 rprintf(FINFO, "generate_files phase=%d\n",
519 @@ -78,6 +78,7 @@ int am_generator = 0;
520 int am_starting_up = 1;
521 int relative_paths = -1;
522 int implied_dirs = 1;
523 +int detect_renamed = 0;
525 int allow_8bit_chars = 0;
526 int force_delete = 0;
527 @@ -343,6 +344,7 @@ void usage(enum logcode F)
528 rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
529 rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
530 rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
531 + rprintf(F," --detect-renamed try to find renamed files to speed up the transfer\n");
532 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
533 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
534 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
535 @@ -497,6 +499,7 @@ static struct poptOption long_options[]
536 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
537 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
538 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
539 + {"detect-renamed", 0, POPT_ARG_NONE, &detect_renamed, 0, 0, 0 },
540 {"fuzzy", 'y', POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 },
541 {"compress", 'z', POPT_ARG_NONE, 0, 'z', 0, 0 },
542 {"compress-level", 0, POPT_ARG_INT, &def_compress_level, 'z', 0, 0 },
543 @@ -1368,7 +1371,7 @@ int parse_arguments(int *argc, const cha
547 - if (delay_updates && !partial_dir)
548 + if ((delay_updates || detect_renamed) && !partial_dir)
549 partial_dir = tmp_partialdir;
552 @@ -1377,6 +1380,7 @@ int parse_arguments(int *argc, const cha
553 snprintf(err_buf, sizeof err_buf,
554 "--%s cannot be used with --%s\n",
555 append_mode ? "append" : "inplace",
556 + detect_renamed ? "detect-renamed" :
557 delay_updates ? "delay-updates" : "partial-dir");
560 @@ -1690,6 +1694,8 @@ void server_options(char **args,int *arg
561 args[ac++] = "--super";
563 args[ac++] = "--size-only";
564 + if (detect_renamed)
565 + args[ac++] = "--detect-renamed";
568 if (modify_window_set) {
571 @@ -364,6 +364,7 @@ to the detailed description below for a
572 --modify-window=NUM compare mod-times with reduced accuracy
573 -T, --temp-dir=DIR create temporary files in directory DIR
574 -y, --fuzzy find similar file for basis if no dest file
575 + --detect-renamed try to find renamed files to speed the xfer
576 --compare-dest=DIR also compare received files relative to DIR
577 --copy-dest=DIR ... and include copies of unchanged files
578 --link-dest=DIR hardlink to files in DIR when unchanged
579 @@ -1296,6 +1297,15 @@ Note that the use of the bf(--delete) op
580 fuzzy-match files, so either use bf(--delete-after) or specify some
581 filename exclusions if you need to prevent this.
583 +dit(bf(--detect-renamed)) This option tells rsync to scan the receiving
584 +side for files that have been renamed, and to use any that are found as
585 +alternate basis files to help speed up the transfer.
586 +By default, alternate-basis files are hard-linked into a directory named
587 +".~tmp~" in each file's destination directory, but if you've specified
588 +the bf(--partial-dir) option, that directory will be used instead. These
589 +potential alternate-basis files will be removed as the transfer progresses.
590 +This option conflicts with bf(--inplace) and bf(--append).
592 dit(bf(--compare-dest=DIR)) This option instructs rsync to use em(DIR) on
593 the destination machine as an additional hierarchy to compare destination
594 files against doing transfers (if the files are missing in the destination
597 @@ -1027,6 +1027,32 @@ int handle_partial_dir(const char *fname
601 +/* We need to supply our own strcmp function for file list comparisons
602 + * to ensure that signed/unsigned usage is consistent between machines. */
603 +int u_strcmp(const char *p1, const char *p2)
605 + for ( ; *p1; p1++, p2++) {
610 + return (int)*(uchar*)p1 - (int)*(uchar*)p2;
613 +/* We need a memcmp function compares unsigned-byte values. */
614 +int u_memcmp(const void *p1, const void *p2, size_t len)
616 + const uchar *u1 = p1;
617 + const uchar *u2 = p2;
621 + return (int)*u1 - (int)*u2;
628 * Determine if a symlink points outside the current directory tree.
629 * This is considered "unsafe" because e.g. when mirroring somebody