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