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