706ccd9e5ede4d12f8507d0ec194415b17a66439
[rsync-patches.git] / detect-renamed.diff
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.
5
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.
15
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.
21
22 To use this patch, run these commands for a successful build:
23
24     patch -p1 <patches/detect-renamed.diff
25     ./configure                                 (optional if already run)
26     make
27
28 TODO:
29
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
33   that could use it.
34
35 --- old/compat.c
36 +++ new/compat.c
37 @@ -41,6 +41,7 @@ extern int checksum_seed;
38  extern int basis_dir_cnt;
39  extern int prune_empty_dirs;
40  extern int protocol_version;
41 +extern int detect_renamed;
42  extern int protect_args;
43  extern int preserve_uid;
44  extern int preserve_gid;
45 @@ -107,6 +108,7 @@ void set_allow_inc_recurse(void)
46                 allow_inc_recurse = 0;
47         else if (!am_sender
48          && (delete_before || delete_after
49 +         || detect_renamed
50           || delay_updates || prune_empty_dirs))
51                 allow_inc_recurse = 0;
52         else if (am_server && !local_server
53 --- old/flist.c
54 +++ new/flist.c
55 @@ -61,6 +61,7 @@ extern int non_perishable_cnt;
56  extern int prune_empty_dirs;
57  extern int copy_links;
58  extern int copy_unsafe_links;
59 +extern int detect_renamed;
60  extern int protocol_version;
61  extern int sanitize_paths;
62  extern int need_unsorted_flist;
63 @@ -117,6 +118,8 @@ static int64 tmp_dev, tmp_ino;
64  #endif
65  static char tmp_sum[MAX_DIGEST_LEN];
66  
67 +struct file_list the_fattr_list;
68 +
69  static char empty_sum[MAX_DIGEST_LEN];
70  static int flist_count_offset; /* for --delete --progress */
71  static int dir_count = 0;
72 @@ -256,6 +259,45 @@ static int is_excluded(char *fname, int 
73         return 0;
74  }
75  
76 +static int fattr_compare(struct file_struct **file1, struct file_struct **file2)
77 +{
78 +       struct file_struct *f1 = *file1;
79 +       struct file_struct *f2 = *file2;
80 +       int64 len1 = F_LENGTH(f1), len2 = F_LENGTH(f2);
81 +       int diff;
82 +
83 +       if (!f1->basename || !S_ISREG(f1->mode) || !len1) {
84 +               if (!f2->basename || !S_ISREG(f2->mode) || !len2)
85 +                       return 0;
86 +               return 1;
87 +       }
88 +       if (!f2->basename || !S_ISREG(f2->mode) || !len2)
89 +               return -1;
90 +
91 +       /* Don't use diff for values that are longer than an int. */
92 +       if (len1 != len2)
93 +               return len1 < len2 ? -1 : 1;
94 +
95 +       if (always_checksum) {
96 +               diff = u_memcmp(F_SUM(f1), F_SUM(f2), checksum_len);
97 +               if (diff)
98 +                       return diff;
99 +       } else if (f1->modtime != f2->modtime)
100 +               return f1->modtime < f2->modtime ? -1 : 1;
101 +
102 +       diff = u_strcmp(f1->basename, f2->basename);
103 +       if (diff)
104 +               return diff;
105 +
106 +       if (f1->dirname == f2->dirname)
107 +               return 0;
108 +       if (!f1->dirname)
109 +               return -1;
110 +       if (!f2->dirname)
111 +               return 1;
112 +       return u_strcmp(f1->dirname, f2->dirname);
113 +}
114 +
115  static void send_directory(int f, struct file_list *flist,
116                            char *fbuf, int len, int flags);
117  
118 @@ -2169,6 +2211,25 @@ struct file_list *recv_file_list(int f)
119  
120         clean_flist(flist, relative_paths);
121  
122 +       if (detect_renamed) {
123 +               int j = flist->used;
124 +               the_fattr_list.used = j;
125 +               the_fattr_list.files = new_array(struct file_struct *, j);
126 +               if (!the_fattr_list.files)
127 +                       out_of_memory("recv_file_list");
128 +               memcpy(the_fattr_list.files, flist->files,
129 +                      j * sizeof (struct file_struct *));
130 +               qsort(the_fattr_list.files, j,
131 +                     sizeof the_fattr_list.files[0], (int (*)())fattr_compare);
132 +               the_fattr_list.low = 0;
133 +               while (j-- > 0) {
134 +                       struct file_struct *fp = the_fattr_list.files[j];
135 +                       if (fp->basename && S_ISREG(fp->mode) && F_LENGTH(fp))
136 +                               break;
137 +               }
138 +               the_fattr_list.high = j;
139 +       }
140 +
141         if (protocol_version < 30) {
142                 /* Recv the io_error flag */
143                 if (ignore_errors)
144 --- old/generator.c
145 +++ new/generator.c
146 @@ -79,6 +79,7 @@ extern char *basis_dir[];
147  extern int compare_dest;
148  extern int copy_dest;
149  extern int link_dest;
150 +extern int detect_renamed;
151  extern int whole_file;
152  extern int list_only;
153  extern int read_batch;
154 @@ -96,6 +97,7 @@ extern char *backup_suffix;
155  extern int backup_suffix_len;
156  extern struct file_list *cur_flist, *first_flist, *dir_flist;
157  extern struct filter_list_struct server_filter_list;
158 +extern struct file_list the_fattr_list;
159  
160  int ignore_perishable = 0;
161  int non_perishable_cnt = 0;
162 @@ -103,6 +105,7 @@ int maybe_ATTRS_REPORT = 0;
163  
164  static dev_t dev_zero;
165  static int deletion_count = 0; /* used to implement --max-delete */
166 +static int unexplored_dirs = 1;
167  static int deldelay_size = 0, deldelay_cnt = 0;
168  static char *deldelay_buf = NULL;
169  static int deldelay_fd = -1;
170 @@ -112,7 +115,8 @@ static int need_retouch_dir_times;
171  static int need_retouch_dir_perms;
172  static const char *solo_file = NULL;
173  
174 -/* For calling delete_item() and delete_dir_contents(). */
175 +/* For calling delete_item(), delete_dir_contents(), and delete_in_dir(). */
176 +#define DEL_NO_DELETIONS       (1<<0)
177  #define DEL_RECURSE            (1<<1) /* recurse */
178  #define DEL_DIR_IS_EMPTY       (1<<2) /* internal delete_FUNCTIONS use only */
179  
180 @@ -134,11 +138,121 @@ static int is_backup_file(char *fn)
181         return k > 0 && strcmp(fn+k, backup_suffix) == 0;
182  }
183  
184 +/* Search for a regular file that matches either (1) the size & modified
185 + * time (plus the basename, if possible) or (2) the size & checksum.  If
186 + * we find an exact match down to the dirname, return -1 because we found
187 + * an up-to-date file in the transfer, not a renamed file. */
188 +static int fattr_find(struct file_struct *f, char *fname)
189 +{
190 +       int low = the_fattr_list.low, high = the_fattr_list.high;
191 +       int mid, ok_match = -1, good_match = -1;
192 +       struct file_struct *fmid;
193 +       int diff;
194 +
195 +       while (low <= high) {
196 +               mid = (low + high) / 2;
197 +               fmid = the_fattr_list.files[mid];
198 +               if (F_LENGTH(fmid) != F_LENGTH(f)) {
199 +                       if (F_LENGTH(fmid) < F_LENGTH(f))
200 +                               low = mid + 1;
201 +                       else
202 +                               high = mid - 1;
203 +                       continue;
204 +               }
205 +               if (always_checksum) {
206 +                       /* We use the FLAG_FILE_SENT flag to indicate when we
207 +                        * have computed the checksum for an entry. */
208 +                       if (!(f->flags & FLAG_FILE_SENT)) {
209 +                               if (fmid->modtime == f->modtime
210 +                                && f_name_cmp(fmid, f) == 0)
211 +                                       return -1; /* assume we can't help */
212 +                               file_checksum(fname, F_SUM(f), F_LENGTH(f));
213 +                               f->flags |= FLAG_FILE_SENT;
214 +                       }
215 +                       diff = u_memcmp(F_SUM(fmid), F_SUM(f), checksum_len);
216 +                       if (diff) {
217 +                               if (diff < 0)
218 +                                       low = mid + 1;
219 +                               else
220 +                                       high = mid - 1;
221 +                               continue;
222 +                       }
223 +               } else {
224 +                       if (fmid->modtime != f->modtime) {
225 +                               if (fmid->modtime < f->modtime)
226 +                                       low = mid + 1;
227 +                               else
228 +                                       high = mid - 1;
229 +                               continue;
230 +                       }
231 +               }
232 +               ok_match = mid;
233 +               diff = u_strcmp(fmid->basename, f->basename);
234 +               if (diff == 0) {
235 +                       good_match = mid;
236 +                       if (fmid->dirname == f->dirname)
237 +                               return -1; /* file is up-to-date */
238 +                       if (!fmid->dirname) {
239 +                               low = mid + 1;
240 +                               continue;
241 +                       }
242 +                       if (!f->dirname) {
243 +                               high = mid - 1;
244 +                               continue;
245 +                       }
246 +                       diff = u_strcmp(fmid->dirname, f->dirname);
247 +                       if (diff == 0)
248 +                               return -1; /* file is up-to-date */
249 +               }
250 +               if (diff < 0)
251 +                       low = mid + 1;
252 +               else
253 +                       high = mid - 1;
254 +       }
255 +
256 +       return good_match >= 0 ? good_match : ok_match;
257 +}
258 +
259 +static void look_for_rename(struct file_struct *file, char *fname)
260 +{
261 +       struct file_struct *fp;
262 +       char *partialptr, *fn;
263 +       STRUCT_STAT st;
264 +       int ndx;
265 +
266 +       if (!partial_dir || (ndx = fattr_find(file, fname)) < 0)
267 +               return;
268 +
269 +       fp = the_fattr_list.files[ndx];
270 +       fn = f_name(fp, NULL);
271 +       /* We don't provide an alternate-basis file if there is a basis file. */
272 +       if (link_stat(fn, &st, 0) == 0)
273 +               return;
274 +
275 +       if (!dry_run) {
276 +               if ((partialptr = partial_dir_fname(fn)) == NULL
277 +                || !handle_partial_dir(partialptr, PDIR_CREATE))
278 +                       return;
279 +               /* We only use the file if we can hard-link it into our tmp dir. */
280 +               if (link(fname, partialptr) != 0) {
281 +                       if (errno != EEXIST)
282 +                               handle_partial_dir(partialptr, PDIR_DELETE);
283 +                       return;
284 +               }
285 +       }
286 +
287 +       /* I think this falls into the -vv category with "%s is uptodate", etc. */
288 +       if (verbose > 1)
289 +               rprintf(FINFO, "found renamed: %s => %s\n", fname, fn);
290 +}
291 +
292  /* Delete a file or directory.  If DEL_RECURSE is set in the flags, this will
293   * delete recursively.
294   *
295   * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
296   * a directory! (The buffer is used for recursion, but returned unchanged.)
297 + *
298 + * Also note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
299   */
300  static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
301  {
302 @@ -160,6 +274,8 @@ static enum delret delete_item(char *fbu
303                         goto check_ret;
304                 /* OK: try to delete the directory. */
305         }
306 +       if (flags & DEL_NO_DELETIONS)
307 +               return DR_SUCCESS;
308  
309         if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
310                 return DR_AT_LIMIT;
311 @@ -206,6 +322,8 @@ static enum delret delete_item(char *fbu
312   * its contents, otherwise just checks for content.  Returns DR_SUCCESS or
313   * DR_NOT_EMPTY.  Note that fname must point to a MAXPATHLEN buffer!  (The
314   * buffer is used for recursion, but returned unchanged.)
315 + *
316 + * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
317   */
318  static enum delret delete_dir_contents(char *fname, int flags)
319  {
320 @@ -225,7 +343,9 @@ static enum delret delete_dir_contents(c
321         save_filters = push_local_filters(fname, dlen);
322  
323         non_perishable_cnt = 0;
324 +       file_extra_cnt += SUM_EXTRA_CNT;
325         dirlist = get_dirlist(fname, dlen, 0);
326 +       file_extra_cnt -= SUM_EXTRA_CNT;
327         ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
328  
329         if (!dirlist->used)
330 @@ -262,6 +382,8 @@ static enum delret delete_dir_contents(c
331                 if (S_ISDIR(fp->mode)
332                  && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
333                         ret = DR_NOT_EMPTY;
334 +               if (detect_renamed && S_ISREG(fp->mode))
335 +                       look_for_rename(fp, fname);
336                 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
337                         ret = DR_NOT_EMPTY;
338         }
339 @@ -414,13 +536,18 @@ static void do_delayed_deletions(char *d
340   * all the --delete-WHEN options.  Note that the fbuf pointer must point to a
341   * MAXPATHLEN buffer with the name of the directory in it (the functions we
342   * call will append names onto the end, but the old dir value will be restored
343 - * on exit). */
344 -static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
345 + * on exit).
346 + *
347 + * Note:  --detect-rename may use this routine with DEL_NO_DELETIONS set!
348 + */
349 +static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev,
350 +                         int flags)
351  {
352         static int already_warned = 0;
353         struct file_list *dirlist;
354 -       char delbuf[MAXPATHLEN];
355 -       int dlen, i;
356 +       char *p, delbuf[MAXPATHLEN];
357 +       unsigned remainder;
358 +       int dlen, i, restore_dot = 0;
359  
360         if (!fbuf) {
361                 change_local_filter_dir(NULL, 0, 0);
362 @@ -430,21 +557,28 @@ static void delete_in_dir(char *fbuf, st
363         if (verbose > 2)
364                 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
365  
366 +       flags |= DEL_RECURSE;
367 +
368         if (allowed_lull)
369                 maybe_send_keepalive();
370  
371         if (io_error && !ignore_errors) {
372 -               if (already_warned)
373 +               if (!already_warned) {
374 +                       rprintf(FINFO,
375 +                           "IO error encountered -- skipping file deletion\n");
376 +                       already_warned = 1;
377 +               }
378 +               if (!detect_renamed)
379                         return;
380 -               rprintf(FINFO,
381 -                       "IO error encountered -- skipping file deletion\n");
382 -               already_warned = 1;
383 -               return;
384 +               flags |= DEL_NO_DELETIONS;
385         }
386  
387         dlen = strlen(fbuf);
388         change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
389  
390 +       if (detect_renamed)
391 +               unexplored_dirs--;
392 +
393         if (one_file_system) {
394                 if (file->flags & FLAG_TOP_DIR)
395                         filesystem_dev = *fs_dev;
396 @@ -454,6 +588,14 @@ static void delete_in_dir(char *fbuf, st
397  
398         dirlist = get_dirlist(fbuf, dlen, 0);
399  
400 +       p = fbuf + dlen;
401 +       if (dlen == 1 && *fbuf == '.') {
402 +               restore_dot = 1;
403 +               p = fbuf;
404 +       } else if (dlen != 1 || *fbuf != '/')
405 +               *p++ = '/';
406 +       remainder = MAXPATHLEN - (p - fbuf);
407 +
408         /* If an item in dirlist is not found in flist, delete it
409          * from the filesystem. */
410         for (i = dirlist->used; i--; ) {
411 @@ -466,16 +608,25 @@ static void delete_in_dir(char *fbuf, st
412                                         f_name(fp, NULL));
413                         continue;
414                 }
415 +               if (detect_renamed && S_ISREG(fp->mode)) {
416 +                       strlcpy(p, fp->basename, remainder);
417 +                       look_for_rename(fp, fbuf);
418 +               }
419                 if (flist_find(cur_flist, fp) < 0) {
420                         f_name(fp, delbuf);
421 -                       if (delete_during == 2) {
422 +                       if (delete_during == 2 && !(flags & DEL_NO_DELETIONS)) {
423                                 if (!remember_delete(fp, delbuf))
424                                         break;
425                         } else
426 -                               delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
427 -               }
428 +                               delete_item(delbuf, fp->mode, NULL, flags);
429 +               } else if (detect_renamed && S_ISDIR(fp->mode))
430 +                       unexplored_dirs++;
431         }
432  
433 +       if (restore_dot)
434 +               fbuf[0] = '.';
435 +       fbuf[dlen] = '\0';
436 +
437         flist_free(dirlist);
438  }
439  
440 @@ -505,9 +656,9 @@ static void do_delete_pass(void)
441                  || !S_ISDIR(st.st_mode))
442                         continue;
443  
444 -               delete_in_dir(fbuf, file, &st.st_dev);
445 +               delete_in_dir(fbuf, file, &st.st_dev, 0);
446         }
447 -       delete_in_dir(NULL, NULL, &dev_zero);
448 +       delete_in_dir(NULL, NULL, &dev_zero, 0);
449  
450         if (do_progress && !am_server)
451                 rprintf(FINFO, "                    \r");
452 @@ -1139,6 +1290,7 @@ static void list_file_entry(struct file_
453         }
454  }
455  
456 +static struct bitbag *delayed_bits = NULL;
457  static int phase = 0;
458  static int dflt_perms;
459  
460 @@ -1383,8 +1535,12 @@ static void recv_generator(char *fname, 
461                         }
462                 }
463                 else if (delete_during && f_out != -1 && !phase && dry_run < 2
464 -                   && (file->flags & FLAG_CONTENT_DIR))
465 -                       delete_in_dir(fname, file, &real_sx.st.st_dev);
466 +                   && (file->flags & FLAG_CONTENT_DIR)) {
467 +                       if (detect_renamed && real_ret != 0)
468 +                               unexplored_dirs++;
469 +                       delete_in_dir(fname, file, &real_sx.st.st_dev,
470 +                                     delete_during < 0 ? DEL_NO_DELETIONS : 0);
471 +               }
472                 goto cleanup;
473         }
474  
475 @@ -1662,8 +1818,14 @@ static void recv_generator(char *fname, 
476                         goto cleanup;
477                 }
478  #endif
479 -               if (stat_errno == ENOENT)
480 +               if (stat_errno == ENOENT) {
481 +                       if (detect_renamed && unexplored_dirs > 0
482 +                        && F_LENGTH(file)) {
483 +                               bitbag_set_bit(delayed_bits, ndx);
484 +                               return;
485 +                       }
486                         goto notify_others;
487 +               }
488                 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
489                         full_fname(fname));
490                 goto cleanup;
491 @@ -2000,6 +2162,12 @@ void generate_files(int f_out, const cha
492         if (verbose > 2)
493                 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
494  
495 +       if (detect_renamed) {
496 +               delayed_bits = bitbag_create(cur_flist->used);
497 +               if (!delete_before && !delete_during)
498 +                       delete_during = -1;
499 +       }
500 +
501         if (delete_before && !solo_file && cur_flist->used > 0)
502                 do_delete_pass();
503         if (delete_during == 2) {
504 @@ -2010,7 +2178,7 @@ void generate_files(int f_out, const cha
505         }
506         do_progress = 0;
507  
508 -       if (append_mode > 0 || whole_file < 0)
509 +       if (append_mode > 0 || detect_renamed || whole_file < 0)
510                 whole_file = 0;
511         if (verbose >= 2) {
512                 rprintf(FINFO, "delta-transmission %s\n",
513 @@ -2048,7 +2216,7 @@ void generate_files(int f_out, const cha
514                                                 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
515                                         } else
516                                                 dirdev = MAKEDEV(0, 0);
517 -                                       delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
518 +                                       delete_in_dir(f_name(fp, fbuf), fp, &dirdev, 0);
519                                 }
520                         }
521                 }
522 @@ -2091,7 +2259,21 @@ void generate_files(int f_out, const cha
523         } while ((cur_flist = cur_flist->next) != NULL);
524  
525         if (delete_during)
526 -               delete_in_dir(NULL, NULL, &dev_zero);
527 +               delete_in_dir(NULL, NULL, &dev_zero, 0);
528 +       if (detect_renamed) {
529 +               if (delete_during < 0)
530 +                       delete_during = 0;
531 +               detect_renamed = 0;
532 +
533 +               for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
534 +                       struct file_struct *file = cur_flist->files[i];
535 +                       if (local_name)
536 +                               strlcpy(fbuf, local_name, sizeof fbuf);
537 +                       else
538 +                               f_name(file, fbuf);
539 +                       recv_generator(fbuf, file, i, itemizing, code, f_out);
540 +               }
541 +       }
542         phase++;
543         if (verbose > 2)
544                 rprintf(FINFO, "generate_files phase=%d\n", phase);
545 --- old/options.c
546 +++ new/options.c
547 @@ -80,6 +80,7 @@ int am_generator = 0;
548  int am_starting_up = 1;
549  int relative_paths = -1;
550  int implied_dirs = 1;
551 +int detect_renamed = 0;
552  int numeric_ids = 0;
553  int allow_8bit_chars = 0;
554  int force_delete = 0;
555 @@ -383,6 +384,7 @@ void usage(enum logcode F)
556    rprintf(F,"     --modify-window=NUM     compare mod-times with reduced accuracy\n");
557    rprintf(F," -T, --temp-dir=DIR          create temporary files in directory DIR\n");
558    rprintf(F," -y, --fuzzy                 find similar file for basis if no dest file\n");
559 +  rprintf(F,"     --detect-renamed        try to find renamed files to speed up the transfer\n");
560    rprintf(F,"     --compare-dest=DIR      also compare destination files relative to DIR\n");
561    rprintf(F,"     --copy-dest=DIR         ... and include copies of unchanged files\n");
562    rprintf(F,"     --link-dest=DIR         hardlink to files in DIR when unchanged\n");
563 @@ -561,6 +563,7 @@ static struct poptOption long_options[] 
564    {"compare-dest",     0,  POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
565    {"copy-dest",        0,  POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
566    {"link-dest",        0,  POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
567 +  {"detect-renamed",   0,  POPT_ARG_NONE,   &detect_renamed, 0, 0, 0 },
568    {"fuzzy",           'y', POPT_ARG_NONE,   &fuzzy_basis, 0, 0, 0 },
569    {"compress",        'z', POPT_ARG_NONE,   0, 'z', 0, 0 },
570    {"no-compress",      0,  POPT_ARG_VAL,    &do_compression, 0, 0, 0 },
571 @@ -1532,7 +1535,7 @@ int parse_arguments(int *argc_p, const c
572                 inplace = 1;
573         }
574  
575 -       if (delay_updates && !partial_dir)
576 +       if ((delay_updates || detect_renamed) && !partial_dir)
577                 partial_dir = tmp_partialdir;
578  
579         if (inplace) {
580 @@ -1541,6 +1544,7 @@ int parse_arguments(int *argc_p, const c
581                         snprintf(err_buf, sizeof err_buf,
582                                  "--%s cannot be used with --%s\n",
583                                  append_mode ? "append" : "inplace",
584 +                                detect_renamed ? "detect-renamed" :
585                                  delay_updates ? "delay-updates" : "partial-dir");
586                         return 0;
587                 }
588 @@ -1885,6 +1889,8 @@ void server_options(char **args, int *ar
589                         args[ac++] = "--super";
590                 if (size_only)
591                         args[ac++] = "--size-only";
592 +               if (detect_renamed)
593 +                       args[ac++] = "--detect-renamed";
594         } else {
595                 if (skip_compress) {
596                         if (asprintf(&arg, "--skip-compress=%s", skip_compress) < 0)
597 --- old/rsync.yo
598 +++ new/rsync.yo
599 @@ -384,6 +384,7 @@ to the detailed description below for a 
600       --modify-window=NUM     compare mod-times with reduced accuracy
601   -T, --temp-dir=DIR          create temporary files in directory DIR
602   -y, --fuzzy                 find similar file for basis if no dest file
603 +     --detect-renamed        try to find renamed files to speed the xfer
604       --compare-dest=DIR      also compare received files relative to DIR
605       --copy-dest=DIR         ... and include copies of unchanged files
606       --link-dest=DIR         hardlink to files in DIR when unchanged
607 @@ -1438,6 +1439,21 @@ Note that the use of the bf(--delete) op
608  fuzzy-match files, so either use bf(--delete-after) or specify some
609  filename exclusions if you need to prevent this.
610  
611 +dit(bf(--detect-renamed)) With this option, for each new source file
612 +(call it em(src/S)), rsync looks for a file em(dest/D) anywhere in the
613 +destination that passes the quick check with em(src/S).  If such a em(dest/D)
614 +is found, rsync uses it as an alternate basis for transferring em(S).  The
615 +idea is that if em(src/S) was renamed from em(src/D) (as opposed to em(src/S)
616 +passing the quick check with em(dest/D) by coincidence), the delta-transfer
617 +algorithm will find that all the data matches between em(src/S) and em(dest/D),
618 +and the transfer will be really fast.
619 +
620 +By default, alternate-basis files are hard-linked into a directory named
621 +".~tmp~" in each file's destination directory, but if you've specified
622 +the bf(--partial-dir) option, that directory will be used instead.  These
623 +potential alternate-basis files will be removed as the transfer progresses.
624 +This option conflicts with bf(--inplace) and bf(--append).
625 +
626  dit(bf(--compare-dest=DIR)) This option instructs rsync to use em(DIR) on
627  the destination machine as an additional hierarchy to compare destination
628  files against doing transfers (if the files are missing in the destination
629 --- old/util.c
630 +++ new/util.c
631 @@ -1042,6 +1042,32 @@ int handle_partial_dir(const char *fname
632         return 1;
633  }
634  
635 +/* We need to supply our own strcmp function for file list comparisons
636 + * to ensure that signed/unsigned usage is consistent between machines. */
637 +int u_strcmp(const char *p1, const char *p2)
638 +{
639 +        for ( ; *p1; p1++, p2++) {
640 +               if (*p1 != *p2)
641 +                       break;
642 +       }
643 +
644 +       return (int)*(uchar*)p1 - (int)*(uchar*)p2;
645 +}
646 +
647 +/* We need a memcmp function compares unsigned-byte values. */
648 +int u_memcmp(const void *p1, const void *p2, size_t len)
649 +{
650 +       const uchar *u1 = p1;
651 +       const uchar *u2 = p2;
652 +
653 +       while (len--) {
654 +               if (*u1 != *u2)
655 +                       return (int)*u1 - (int)*u2;
656 +       }
657 +
658 +       return 0;
659 +}
660 +
661  /**
662   * Determine if a symlink points outside the current directory tree.
663   * This is considered "unsafe" because e.g. when mirroring somebody