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 @@ -53,6 +53,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 @@ -70,6 +71,8 @@ int checksum_len;
46 dev_t filesystem_dev; /* used to implement -x */
47 unsigned int file_struct_len;
49 +struct file_list the_fattr_list;
51 static char empty_sum[MD4_SUM_LENGTH];
52 static int flist_count_offset;
54 @@ -252,6 +255,44 @@ 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;
64 + if (!f1->basename || !S_ISREG(f1->mode) || !f1->length) {
65 + if (!f2->basename || !S_ISREG(f2->mode) || !f2->length)
69 + if (!f2->basename || !S_ISREG(f2->mode) || !f2->length)
72 + /* Don't use diff for values that are longer than an int. */
73 + if (f1->length != f2->length)
74 + return f1->length < f2->length ? -1 : 1;
76 + if (always_checksum) {
77 + diff = u_memcmp(f1->u.sum, f2->u.sum, checksum_len);
80 + } else if (f1->modtime != f2->modtime)
81 + return f1->modtime < f2->modtime ? -1 : 1;
83 + diff = u_strcmp(f1->basename, f2->basename);
87 + if (f1->dirname == f2->dirname)
93 + return u_strcmp(f1->dirname, f2->dirname);
96 static void send_directory(int f, struct file_list *flist,
99 @@ -1388,6 +1429,25 @@ struct file_list *recv_file_list(int f)
101 clean_flist(flist, relative_paths, 1);
103 + if (detect_renamed) {
104 + int j = flist->count;
105 + the_fattr_list.count = j;
106 + the_fattr_list.files = new_array(struct file_struct *, j);
107 + if (!the_fattr_list.files)
109 + memcpy(the_fattr_list.files, flist->files,
110 + j * sizeof (struct file_struct *));
111 + qsort(the_fattr_list.files, j,
112 + sizeof the_fattr_list.files[0], (int (*)())fattr_compare);
113 + the_fattr_list.low = 0;
115 + struct file_struct *fp = the_fattr_list.files[j];
116 + if (fp->basename && S_ISREG(fp->mode) && fp->length)
119 + the_fattr_list.high = j;
123 recv_uid_list(f, flist);
127 @@ -76,6 +76,7 @@ extern char *basis_dir[];
128 extern int compare_dest;
129 extern int copy_dest;
130 extern int link_dest;
131 +extern int detect_renamed;
132 extern int whole_file;
133 extern int list_only;
134 extern int new_root_dir;
135 @@ -91,14 +92,17 @@ extern char *backup_dir;
136 extern char *backup_suffix;
137 extern int backup_suffix_len;
138 extern struct file_list *the_file_list;
139 +extern struct file_list the_fattr_list;
140 extern struct filter_list_struct server_filter_list;
142 int ignore_perishable = 0;
143 int non_perishable_cnt = 0;
145 static int deletion_count = 0; /* used to implement --max-delete */
146 +static int unexplored_dirs = 1;
148 -/* For calling delete_item() and delete_dir_contents(). */
149 +/* For calling delete_item(), delete_dir_contents(), and delete_in_dir(). */
150 +#define DEL_NO_DELETIONS (1<<0)
151 #define DEL_RECURSE (1<<1) /* recurse */
152 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
154 @@ -120,11 +124,120 @@ static int is_backup_file(char *fn)
155 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
158 +/* Search for a regular file that matches either (1) the size & modified
159 + * time (plus the basename, if possible) or (2) the size & checksum. If
160 + * we find an exact match down to the dirname, return -1 because we found
161 + * an up-to-date file in the transfer, not a renamed file. */
162 +static int fattr_find(struct file_struct *f, char *fname, alloc_pool_t pool)
164 + int low = the_fattr_list.low, high = the_fattr_list.high;
165 + int mid, ok_match = -1, good_match = -1;
166 + struct file_struct *fmid;
169 + while (low <= high) {
170 + mid = (low + high) / 2;
171 + fmid = the_fattr_list.files[mid];
172 + if (fmid->length != f->length) {
173 + if (fmid->length < f->length)
179 + if (always_checksum) {
181 + if (fmid->modtime == f->modtime
182 + && f_name_cmp(fmid, f) == 0)
183 + return -1; /* assume we can't help */
184 + f->u.sum = pool_alloc(pool, MD4_SUM_LENGTH,
186 + file_checksum(fname, f->u.sum, f->length);
188 + diff = u_memcmp(fmid->u.sum, f->u.sum, checksum_len);
197 + if (fmid->modtime != f->modtime) {
198 + if (fmid->modtime < f->modtime)
206 + diff = u_strcmp(fmid->basename, f->basename);
209 + if (fmid->dirname == f->dirname)
210 + return -1; /* file is up-to-date */
211 + if (!fmid->dirname) {
219 + diff = u_strcmp(fmid->dirname, f->dirname);
221 + return -1; /* file is up-to-date */
229 + return good_match >= 0 ? good_match : ok_match;
232 +static void look_for_rename(struct file_struct *file, char *fname,
235 + struct file_struct *fp;
236 + char *partialptr, *fn;
240 + if ((ndx = fattr_find(file, fname, pool)) < 0)
243 + fp = the_fattr_list.files[ndx];
244 + fn = f_name(fp, NULL);
245 + /* We don't provide an alternate-basis file if there is a basis file. */
246 + if (link_stat(fn, &st, 0) == 0)
248 + if ((partialptr = partial_dir_fname(fn)) == NULL
249 + || !handle_partial_dir(partialptr, PDIR_CREATE))
252 + /* We only use the file if we can hard-link it into our tmp dir. */
253 + if (link(fname, partialptr) == 0) {
255 + rprintf(FINFO, "found renamed: %s => %s\n",
256 + fname, partialptr);
261 + if (errno != EEXIST)
262 + handle_partial_dir(partialptr, PDIR_DELETE);
265 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
266 * delete recursively.
268 * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's
269 * a directory! (The buffer is used for recursion, but returned unchanged.)
271 + * Also note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
273 static enum delret delete_item(char *fname, int mode, char *replace, int flags)
275 @@ -146,6 +259,8 @@ static enum delret delete_item(char *fna
277 /* OK: try to delete the directory. */
279 + if (flags & DEL_NO_DELETIONS)
282 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
284 @@ -192,6 +307,8 @@ static enum delret delete_item(char *fna
285 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
286 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
287 * buffer is used for recursion, but returned unchanged.)
289 + * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
291 static enum delret delete_dir_contents(char *fname, int flags)
293 @@ -248,6 +365,8 @@ static enum delret delete_dir_contents(c
294 if (S_ISDIR(fp->mode)
295 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
297 + if (detect_renamed && S_ISREG(fp->mode))
298 + look_for_rename(fp, fname, dirlist->file_pool);
299 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
302 @@ -270,15 +389,19 @@ static enum delret delete_dir_contents(c
303 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
304 * MAXPATHLEN buffer with the name of the directory in it (the functions we
305 * call will append names onto the end, but the old dir value will be restored
309 + * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
311 static void delete_in_dir(struct file_list *flist, char *fbuf,
312 - struct file_struct *file, STRUCT_STAT *stp)
313 + struct file_struct *file, STRUCT_STAT *stp, int flags)
315 static int min_depth = MAXPATHLEN, cur_depth = -1;
316 static void *filt_array[MAXPATHLEN/2+1];
317 static int already_warned = 0;
318 struct file_list *dirlist;
319 - char delbuf[MAXPATHLEN];
320 + char *p, delbuf[MAXPATHLEN];
321 + unsigned remainder;
325 @@ -292,6 +415,8 @@ static void delete_in_dir(struct file_li
327 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
329 + flags |= DEL_RECURSE;
332 maybe_send_keepalive();
334 @@ -299,12 +424,14 @@ static void delete_in_dir(struct file_li
335 return; /* Impossible... */
337 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
338 - if (already_warned)
339 + if (!already_warned) {
341 + "IO error encountered -- skipping file deletion\n");
342 + already_warned = 1;
344 + if (!detect_renamed)
347 - "IO error encountered -- skipping file deletion\n");
348 - already_warned = 1;
350 + flags |= DEL_NO_DELETIONS;
353 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
354 @@ -315,6 +442,9 @@ static void delete_in_dir(struct file_li
356 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
358 + if (detect_renamed)
361 if (one_file_system) {
362 if (file->flags & FLAG_TOP_DIR)
363 filesystem_dev = stp->st_dev;
364 @@ -324,6 +454,11 @@ static void delete_in_dir(struct file_li
366 dirlist = get_dirlist(fbuf, dlen, 0);
369 + if (dlen != 1 || *fbuf != '/')
371 + remainder = MAXPATHLEN - (p - fbuf);
373 /* If an item in dirlist is not found in flist, delete it
374 * from the filesystem. */
375 for (i = dirlist->count; i--; ) {
376 @@ -336,12 +471,19 @@ static void delete_in_dir(struct file_li
380 + if (detect_renamed && S_ISREG(fp->mode)) {
381 + strlcpy(p, fp->basename, remainder);
382 + look_for_rename(fp, fbuf, dirlist->file_pool);
384 if (flist_find(flist, fp) < 0) {
386 - delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
388 + delete_item(delbuf, fp->mode, NULL, flags);
389 + } else if (detect_renamed && S_ISDIR(fp->mode))
398 @@ -371,9 +513,9 @@ static void do_delete_pass(struct file_l
399 || !S_ISDIR(st.st_mode))
402 - delete_in_dir(flist, fbuf, file, &st);
403 + delete_in_dir(flist, fbuf, file, &st, 0);
405 - delete_in_dir(NULL, NULL, NULL, NULL);
406 + delete_in_dir(NULL, NULL, NULL, NULL, 0);
408 if (do_progress && !am_server)
409 rprintf(FINFO, " \r");
410 @@ -902,6 +1044,7 @@ static int try_dests_non(struct file_str
414 +static struct bitbag *delayed_bits = NULL;
415 static int phase = 0;
417 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
418 @@ -1087,8 +1230,12 @@ static void recv_generator(char *fname,
419 if (real_ret != 0 && one_file_system)
420 real_st.st_dev = filesystem_dev;
421 if (delete_during && f_out != -1 && !phase && dry_run < 2
422 - && (file->flags & FLAG_DEL_HERE))
423 - delete_in_dir(the_file_list, fname, file, &real_st);
424 + && (file->flags & FLAG_DEL_HERE)) {
425 + if (detect_renamed && real_ret != 0)
427 + delete_in_dir(the_file_list, fname, file, &real_st,
428 + delete_during < 0 ? DEL_NO_DELETIONS : 0);
433 @@ -1340,8 +1487,14 @@ static void recv_generator(char *fname,
434 && hard_link_check(file, ndx, fname, statret, &st,
435 itemizing, code, HL_SKIP))
437 - if (stat_errno == ENOENT)
438 + if (stat_errno == ENOENT) {
439 + if (detect_renamed && unexplored_dirs > 0
441 + bitbag_set_bit(delayed_bits, ndx);
446 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
449 @@ -1527,11 +1680,17 @@ void generate_files(int f_out, struct fi
450 (long)getpid(), flist->count);
453 + if (detect_renamed) {
454 + delayed_bits = bitbag_create(flist->count);
455 + if (!delete_before && !delete_during)
456 + delete_during = -1;
459 if (delete_before && !local_name && flist->count > 0)
460 do_delete_pass(flist);
463 - if (append_mode || whole_file < 0)
464 + if (append_mode || detect_renamed || whole_file < 0)
467 rprintf(FINFO, "delta-transmission %s\n",
468 @@ -1586,7 +1745,23 @@ void generate_files(int f_out, struct fi
470 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
472 - delete_in_dir(NULL, NULL, NULL, NULL);
473 + delete_in_dir(NULL, NULL, NULL, NULL, 0);
475 + if (detect_renamed) {
476 + if (delete_during < 0)
478 + detect_renamed = 0;
480 + for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
481 + struct file_struct *file = flist->files[i];
483 + strlcpy(fbuf, local_name, sizeof fbuf);
485 + f_name(file, fbuf);
486 + recv_generator(fbuf, file, i, itemizing,
487 + maybe_ATTRS_REPORT, code, f_out);
492 csum_length = SUM_LENGTH;
495 @@ -78,6 +78,7 @@ int am_generator = 0;
496 int am_starting_up = 1;
497 int relative_paths = -1;
498 int implied_dirs = 1;
499 +int detect_renamed = 0;
501 int allow_8bit_chars = 0;
502 int force_delete = 0;
503 @@ -346,6 +347,7 @@ void usage(enum logcode F)
504 rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
505 rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
506 rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
507 + rprintf(F," --detect-renamed try to find renamed files to speed up the transfer\n");
508 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
509 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
510 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
511 @@ -499,6 +501,7 @@ static struct poptOption long_options[]
512 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
513 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
514 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
515 + {"detect-renamed", 0, POPT_ARG_NONE, &detect_renamed, 0, 0, 0 },
516 {"fuzzy", 'y', POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 },
517 {"compress", 'z', POPT_ARG_NONE, 0, 'z', 0, 0 },
518 {"compress-level", 0, POPT_ARG_INT, &def_compress_level, 'z', 0, 0 },
519 @@ -1362,7 +1365,7 @@ int parse_arguments(int *argc, const cha
523 - if (delay_updates && !partial_dir)
524 + if ((delay_updates || detect_renamed) && !partial_dir)
525 partial_dir = tmp_partialdir;
528 @@ -1371,6 +1374,7 @@ int parse_arguments(int *argc, const cha
529 snprintf(err_buf, sizeof err_buf,
530 "--%s cannot be used with --%s\n",
531 append_mode ? "append" : "inplace",
532 + detect_renamed ? "detect-renamed" :
533 delay_updates ? "delay-updates" : "partial-dir");
536 @@ -1674,6 +1678,8 @@ void server_options(char **args,int *arg
537 args[ac++] = "--super";
539 args[ac++] = "--size-only";
540 + if (detect_renamed)
541 + args[ac++] = "--detect-renamed";
544 if (modify_window_set) {
547 @@ -363,6 +363,7 @@ to the detailed description below for a
548 --modify-window=NUM compare mod-times with reduced accuracy
549 -T, --temp-dir=DIR create temporary files in directory DIR
550 -y, --fuzzy find similar file for basis if no dest file
551 + --detect-renamed try to find renamed files to speed the xfer
552 --compare-dest=DIR also compare received files relative to DIR
553 --copy-dest=DIR ... and include copies of unchanged files
554 --link-dest=DIR hardlink to files in DIR when unchanged
555 @@ -1265,6 +1266,15 @@ Note that the use of the bf(--delete) op
556 fuzzy-match files, so either use bf(--delete-after) or specify some
557 filename exclusions if you need to prevent this.
559 +dit(bf(--detect-renamed)) This option tells rsync to scan the receiving
560 +side for files that have been renamed, and to use any that are found as
561 +alternate basis files to help speed up the transfer.
562 +By default, alternate-basis files are hard-linked into a directory named
563 +".~tmp~" in each file's destination directory, but if you've specified
564 +the bf(--partial-dir) option, that directory will be used instead. These
565 +potential alternate-basis files will be removed as the transfer progresses.
566 +This option conflicts with bf(--inplace) and bf(--append).
568 dit(bf(--compare-dest=DIR)) This option instructs rsync to use em(DIR) on
569 the destination machine as an additional hierarchy to compare destination
570 files against doing transfers (if the files are missing in the destination
573 @@ -1025,6 +1025,32 @@ int handle_partial_dir(const char *fname
577 +/* We need to supply our own strcmp function for file list comparisons
578 + * to ensure that signed/unsigned usage is consistent between machines. */
579 +int u_strcmp(const char *p1, const char *p2)
581 + for ( ; *p1; p1++, p2++) {
586 + return (int)*(uchar*)p1 - (int)*(uchar*)p2;
589 +/* We need a memcmp function compares unsigned-byte values. */
590 +int u_memcmp(const void *p1, const void *p2, size_t len)
592 + const uchar *u1 = p1;
593 + const uchar *u2 = p2;
597 + return (int)*u1 - (int)*u2;
604 * Determine if a symlink points outside the current directory tree.
605 * This is considered "unsafe" because e.g. when mirroring somebody