Matt's recent improvements, slightly edited.
[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
9bcaf4de
WD
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 }
1fffd582
WD
55--- old/flist.c
56+++ new/flist.c
ffc18846 57@@ -57,6 +57,7 @@ extern int non_perishable_cnt;
1fffd582
WD
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;
03019e41 64 extern struct stats stats;
ffc18846 65@@ -87,6 +88,8 @@ static int64 tmp_dev, tmp_ino;
7b80cd0e 66 #endif
70891d26 67 static char tmp_sum[MD4_SUM_LENGTH];
1fffd582
WD
68
69+struct file_list the_fattr_list;
70+
71 static char empty_sum[MD4_SUM_LENGTH];
a47d1f86 72 static int flist_count_offset; /* for --delete --progress */
1fffd582 73
ffc18846 74@@ -271,6 +274,45 @@ static mode_t from_wire_mode(int mode)
03019e41 75 return mode;
1fffd582
WD
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;
a47d1f86 82+ int64 len1 = F_LENGTH(f1), len2 = F_LENGTH(f2);
1fffd582
WD
83+ int diff;
84+
a47d1f86
WD
85+ if (!f1->basename || !S_ISREG(f1->mode) || !len1) {
86+ if (!f2->basename || !S_ISREG(f2->mode) || !len2)
1fffd582
WD
87+ return 0;
88+ return 1;
89+ }
a47d1f86 90+ if (!f2->basename || !S_ISREG(f2->mode) || !len2)
1fffd582
WD
91+ return -1;
92+
93+ /* Don't use diff for values that are longer than an int. */
a47d1f86
WD
94+ if (len1 != len2)
95+ return len1 < len2 ? -1 : 1;
1fffd582
WD
96+
97+ if (always_checksum) {
70891d26 98+ diff = u_memcmp(F_SUM(f1), F_SUM(f2), checksum_len);
1fffd582
WD
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+
fc068916
WD
117 static void send_directory(int f, struct file_list *flist, int ndx,
118 char *fbuf, int len, int flags);
1fffd582 119
9bcaf4de 120@@ -1765,6 +1807,25 @@ struct file_list *recv_file_list(int f)
1fffd582
WD
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)
70891d26 129+ out_of_memory("recv_file_list");
1fffd582
WD
130+ memcpy(the_fattr_list.files, flist->files,
131+ j * sizeof (struct file_struct *));
132+ qsort(the_fattr_list.files, j,
fc068916 133+ sizeof the_fattr_list.files[0], (int (*)())fattr_compare);
1fffd582
WD
134+ the_fattr_list.low = 0;
135+ while (j-- > 0) {
136+ struct file_struct *fp = the_fattr_list.files[j];
a47d1f86 137+ if (fp->basename && S_ISREG(fp->mode) && F_LENGTH(fp))
1fffd582
WD
138+ break;
139+ }
140+ the_fattr_list.high = j;
141+ }
142+
dd0d95fa 143 if (inc_recurse) {
fc068916
WD
144 qsort(dir_flist->files + dstart, dir_flist->count - dstart,
145 sizeof dir_flist->files[0], (int (*)())file_compare);
1fffd582
WD
146--- old/generator.c
147+++ new/generator.c
fc068916 148@@ -79,6 +79,7 @@ extern char *basis_dir[];
1fffd582
WD
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;
03019e41 155 extern int new_root_dir;
ffc18846 156@@ -96,6 +97,7 @@ extern char *backup_suffix;
1fffd582 157 extern int backup_suffix_len;
fc068916 158 extern struct file_list *cur_flist, *first_flist, *dir_flist;
1fffd582 159 extern struct filter_list_struct server_filter_list;
fc068916 160+extern struct file_list the_fattr_list;
1fffd582 161
d16b5fd6 162 int ignore_perishable = 0;
fc068916 163 int non_perishable_cnt = 0;
ffc18846 164@@ -103,6 +105,7 @@ int maybe_ATTRS_REPORT = 0;
d16b5fd6 165
fc068916 166 static dev_t dev_zero;
1fffd582
WD
167 static int deletion_count = 0; /* used to implement --max-delete */
168+static int unexplored_dirs = 1;
1071853f
WD
169 static int deldelay_size = 0, deldelay_cnt = 0;
170 static char *deldelay_buf = NULL;
171 static int deldelay_fd = -1;
ffc18846 172@@ -111,7 +114,8 @@ static int dir_tweaking;
2dbc45e7
WD
173 static int need_retouch_dir_times;
174 static const char *solo_file = NULL;
1fffd582 175
d16b5fd6
WD
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)
87d0091c 179 #define DEL_RECURSE (1<<1) /* recurse */
d16b5fd6 180 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
1fffd582 181
ffc18846 182@@ -133,11 +137,120 @@ static int is_backup_file(char *fn)
1fffd582
WD
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. */
a47d1f86 190+static int fattr_find(struct file_struct *f, char *fname)
1fffd582
WD
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];
a47d1f86
WD
200+ if (F_LENGTH(fmid) != F_LENGTH(f)) {
201+ if (F_LENGTH(fmid) < F_LENGTH(f))
1fffd582
WD
202+ low = mid + 1;
203+ else
204+ high = mid - 1;
205+ continue;
206+ }
207+ if (always_checksum) {
9bcaf4de 208+ /* We use the FLAG_FILE_SENT flag to indicate when we
a47d1f86 209+ * have computed the checksum for an entry. */
9bcaf4de 210+ if (!(f->flags & FLAG_FILE_SENT)) {
1fffd582
WD
211+ if (fmid->modtime == f->modtime
212+ && f_name_cmp(fmid, f) == 0)
213+ return -1; /* assume we can't help */
a47d1f86 214+ file_checksum(fname, (char*)F_SUM(f), F_LENGTH(f));
9bcaf4de 215+ f->flags |= FLAG_FILE_SENT;
1fffd582 216+ }
70891d26 217+ diff = u_memcmp(F_SUM(fmid), F_SUM(f), checksum_len);
1fffd582
WD
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+
a47d1f86 261+static void look_for_rename(struct file_struct *file, char *fname)
1fffd582
WD
262+{
263+ struct file_struct *fp;
264+ char *partialptr, *fn;
265+ STRUCT_STAT st;
266+ int ndx;
267+
a47d1f86 268+ if ((ndx = fattr_find(file, fname)) < 0)
1fffd582
WD
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+}
87d0091c
WD
292+
293 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
294 * delete recursively.
1fffd582 295 *
f813befd 296 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
15894839
WD
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 */
f813befd 301 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
15894839 302 {
ffc18846 303@@ -159,6 +272,8 @@ static enum delret delete_item(char *fbu
15894839
WD
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;
ffc18846 312@@ -205,6 +320,8 @@ static enum delret delete_item(char *fbu
d16b5fd6
WD
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.)
1fffd582 316+ *
87d0091c 317+ * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
1fffd582 318 */
87d0091c 319 static enum delret delete_dir_contents(char *fname, int flags)
1fffd582 320 {
ffc18846 321@@ -224,7 +341,9 @@ static enum delret delete_dir_contents(c
a47d1f86
WD
322 save_filters = push_local_filters(fname, dlen);
323
324 non_perishable_cnt = 0;
7e27b6c0 325+ file_extra_cnt += SUM_EXTRA_CNT;
a47d1f86 326 dirlist = get_dirlist(fname, dlen, 0);
7e27b6c0 327+ file_extra_cnt -= SUM_EXTRA_CNT;
a47d1f86
WD
328 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
329
330 if (!dirlist->count)
ffc18846 331@@ -261,6 +380,8 @@ static enum delret delete_dir_contents(c
d16b5fd6
WD
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))
a47d1f86 336+ look_for_rename(fp, fname);
d16b5fd6
WD
337 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
338 ret = DR_NOT_EMPTY;
339 }
ffc18846 340@@ -413,13 +534,17 @@ static void do_delayed_deletions(char *d
1fffd582
WD
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,
fc068916
WD
350- struct file_struct *file, dev_t *fs_dev)
351+ struct file_struct *file, dev_t *fs_dev, int flags)
1fffd582 352 {
1fffd582
WD
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) {
ffc18846 361@@ -430,21 +555,28 @@ static void delete_in_dir(struct file_li
1fffd582
WD
362 if (verbose > 2)
363 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
364
87d0091c 365+ flags |= DEL_RECURSE;
1fffd582
WD
366+
367 if (allowed_lull)
368 maybe_send_keepalive();
369
041d67b8 370 if (io_error && !ignore_errors) {
1fffd582
WD
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
1fffd582 386 dlen = strlen(fbuf);
fc068916 387 change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
1fffd582
WD
388
389+ if (detect_renamed)
390+ unexplored_dirs--;
391+
392 if (one_file_system) {
393 if (file->flags & FLAG_TOP_DIR)
fc068916 394 filesystem_dev = *fs_dev;
ffc18846 395@@ -454,6 +586,11 @@ static void delete_in_dir(struct file_li
1fffd582
WD
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--; ) {
ffc18846 407@@ -466,16 +603,23 @@ static void delete_in_dir(struct file_li
87d0091c 408 f_name(fp, NULL));
1fffd582 409 continue;
87d0091c 410 }
1fffd582
WD
411+ if (detect_renamed && S_ISREG(fp->mode)) {
412+ strlcpy(p, fp->basename, remainder);
a47d1f86 413+ look_for_rename(fp, fbuf);
1fffd582
WD
414+ }
415 if (flist_find(flist, fp) < 0) {
416 f_name(fp, delbuf);
1071853f 417- if (delete_during == 2) {
a47d1f86 418+ if (delete_during == 2 && !(flags & DEL_NO_DELETIONS)) {
1071853f
WD
419 if (!remember_delete(fp, delbuf))
420 break;
421 } else
f813befd 422- delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
1fffd582 423- }
f813befd 424+ delete_item(delbuf, fp->mode, NULL, flags);
1fffd582
WD
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
ffc18846 434@@ -505,9 +649,9 @@ static void do_delete_pass(struct file_l
1fffd582
WD
435 || !S_ISDIR(st.st_mode))
436 continue;
437
fc068916
WD
438- delete_in_dir(flist, fbuf, file, &st.st_dev);
439+ delete_in_dir(flist, fbuf, file, &st.st_dev, 0);
1fffd582 440 }
fc068916
WD
441- delete_in_dir(NULL, NULL, NULL, &dev_zero);
442+ delete_in_dir(NULL, NULL, NULL, &dev_zero, 0);
1fffd582
WD
443
444 if (do_progress && !am_server)
445 rprintf(FINFO, " \r");
ffc18846 446@@ -1041,6 +1185,7 @@ static int try_dests_non(struct file_str
9a70b743 447 return j;
1fffd582
WD
448 }
449
450+static struct bitbag *delayed_bits = NULL;
451 static int phase = 0;
ffc18846 452 static int dflt_perms;
1fffd582 453
ffc18846 454@@ -1237,8 +1382,12 @@ static void recv_generator(char *fname,
fc068916
WD
455 }
456 }
457 else if (delete_during && f_out != -1 && !phase && dry_run < 2
70891d26 458- && (file->flags & FLAG_XFER_DIR))
ffc18846 459- delete_in_dir(cur_flist, fname, file, &real_sx.st.st_dev);
70891d26 460+ && (file->flags & FLAG_XFER_DIR)) {
9a70b743 461+ if (detect_renamed && real_ret != 0)
1fffd582 462+ unexplored_dirs++;
ffc18846 463+ delete_in_dir(cur_flist, fname, file, &real_sx.st.st_dev,
1fffd582
WD
464+ delete_during < 0 ? DEL_NO_DELETIONS : 0);
465+ }
ffc18846 466 goto cleanup;
1fffd582
WD
467 }
468
ffc18846 469@@ -1510,8 +1659,14 @@ static void recv_generator(char *fname,
a47d1f86 470 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
ffc18846 471 goto cleanup;
81172142 472 #endif
1fffd582
WD
473- if (stat_errno == ENOENT)
474+ if (stat_errno == ENOENT) {
475+ if (detect_renamed && unexplored_dirs > 0
a47d1f86 476+ && F_LENGTH(file)) {
1fffd582
WD
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));
ffc18846
WD
484 goto cleanup;
485@@ -1818,6 +1973,12 @@ void generate_files(int f_out, const cha
fc068916
WD
486 if (verbose > 2)
487 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
1fffd582
WD
488
489+ if (detect_renamed) {
9bcaf4de 490+ delayed_bits = bitbag_create(cur_flist->count);
1fffd582
WD
491+ if (!delete_before && !delete_during)
492+ delete_during = -1;
493+ }
494+
2dbc45e7 495 if (delete_before && !solo_file && cur_flist->count > 0)
fc068916 496 do_delete_pass(cur_flist);
1071853f 497 if (delete_during == 2) {
ffc18846 498@@ -1828,7 +1989,7 @@ void generate_files(int f_out, const cha
1071853f 499 }
1fffd582
WD
500 do_progress = 0;
501
fc068916
WD
502- if (append_mode > 0 || whole_file < 0)
503+ if (append_mode > 0 || detect_renamed || whole_file < 0)
1fffd582
WD
504 whole_file = 0;
505 if (verbose >= 2) {
506 rprintf(FINFO, "delta-transmission %s\n",
ffc18846 507@@ -1855,7 +2016,7 @@ void generate_files(int f_out, const cha
fc068916
WD
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++) {
ffc18846 516@@ -1909,7 +2070,21 @@ void generate_files(int f_out, const cha
2dbc45e7 517 } while ((cur_flist = cur_flist->next) != NULL);
fc068916 518
2dbc45e7
WD
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;
1fffd582 526+
2dbc45e7 527+ for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
9bcaf4de 528+ struct file_struct *file = cur_flist->files[i];
2dbc45e7
WD
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);
1fffd582
WD
539--- old/options.c
540+++ new/options.c
03019e41 541@@ -78,6 +78,7 @@ int am_generator = 0;
a94141d9 542 int am_starting_up = 1;
1fffd582
WD
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;
ffc18846 549@@ -351,6 +352,7 @@ void usage(enum logcode F)
1fffd582
WD
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");
ffc18846 557@@ -508,6 +510,7 @@ static struct poptOption long_options[]
1fffd582
WD
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 },
ffc18846 565@@ -1397,7 +1400,7 @@ int parse_arguments(int *argc, const cha
1fffd582
WD
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) {
ffc18846 574@@ -1406,6 +1409,7 @@ int parse_arguments(int *argc, const cha
1fffd582
WD
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 }
ffc18846 582@@ -1719,6 +1723,8 @@ void server_options(char **args,int *arg
a94141d9
WD
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) {
1fffd582
WD
591--- old/rsync.yo
592+++ new/rsync.yo
ffc18846 593@@ -365,6 +365,7 @@ to the detailed description below for a
1fffd582
WD
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
ffc18846 601@@ -1305,6 +1306,15 @@ Note that the use of the bf(--delete) op
1fffd582
WD
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
ffc18846 619@@ -1026,6 +1026,32 @@ int handle_partial_dir(const char *fname
1fffd582
WD
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