- Updated to apply to latest source.
[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
a47d1f86 37@@ -55,6 +55,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;
a47d1f86 45@@ -80,6 +81,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
a47d1f86 54@@ -262,6 +265,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+
97 static void send_directory(int f, struct file_list *flist,
98 char *fbuf, int len);
99
a47d1f86 100@@ -1501,6 +1543,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,
113+ sizeof the_fattr_list.files[0], (int (*)())fattr_compare);
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+
123 if (f >= 0) {
124 recv_uid_list(f, flist);
125
126--- old/generator.c
127+++ new/generator.c
81172142 128@@ -76,6 +76,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;
7b80cd0e 136@@ -91,6 +92,7 @@ extern char *backup_dir;
1fffd582
WD
137 extern char *backup_suffix;
138 extern int backup_suffix_len;
139 extern struct file_list *the_file_list;
140+extern struct file_list the_fattr_list;
141 extern struct filter_list_struct server_filter_list;
142
d16b5fd6 143 int ignore_perishable = 0;
7b80cd0e
WD
144@@ -98,12 +100,14 @@ int non_perishable_cnt = 0;
145 int maybe_ATTRS_REPORT = 0;
d16b5fd6 146
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;
81172142 152 static BOOL solo_file = 0;
1fffd582 153
d16b5fd6
WD
154-/* For calling delete_item() and delete_dir_contents(). */
155+/* For calling delete_item(), delete_dir_contents(), and delete_in_dir(). */
156+#define DEL_NO_DELETIONS (1<<0)
87d0091c 157 #define DEL_RECURSE (1<<1) /* recurse */
d16b5fd6 158 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
1fffd582 159
a47d1f86 160@@ -125,11 +129,120 @@ static int is_backup_file(char *fn)
1fffd582
WD
161 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
162 }
163
164+/* Search for a regular file that matches either (1) the size & modified
165+ * time (plus the basename, if possible) or (2) the size & checksum. If
166+ * we find an exact match down to the dirname, return -1 because we found
167+ * an up-to-date file in the transfer, not a renamed file. */
a47d1f86 168+static int fattr_find(struct file_struct *f, char *fname)
1fffd582
WD
169+{
170+ int low = the_fattr_list.low, high = the_fattr_list.high;
171+ int mid, ok_match = -1, good_match = -1;
172+ struct file_struct *fmid;
173+ int diff;
174+
175+ while (low <= high) {
176+ mid = (low + high) / 2;
177+ fmid = the_fattr_list.files[mid];
a47d1f86
WD
178+ if (F_LENGTH(fmid) != F_LENGTH(f)) {
179+ if (F_LENGTH(fmid) < F_LENGTH(f))
1fffd582
WD
180+ low = mid + 1;
181+ else
182+ high = mid - 1;
183+ continue;
184+ }
185+ if (always_checksum) {
a47d1f86
WD
186+ /* We use the FLAG_SENT flag to indicate when we
187+ * have computed the checksum for an entry. */
188+ if (!(f->flags & FLAG_SENT)) {
1fffd582
WD
189+ if (fmid->modtime == f->modtime
190+ && f_name_cmp(fmid, f) == 0)
191+ return -1; /* assume we can't help */
a47d1f86
WD
192+ file_checksum(fname, (char*)F_SUM(f), F_LENGTH(f));
193+ f->flags |= FLAG_SENT;
1fffd582 194+ }
70891d26 195+ diff = u_memcmp(F_SUM(fmid), F_SUM(f), checksum_len);
1fffd582
WD
196+ if (diff) {
197+ if (diff < 0)
198+ low = mid + 1;
199+ else
200+ high = mid - 1;
201+ continue;
202+ }
203+ } else {
204+ if (fmid->modtime != f->modtime) {
205+ if (fmid->modtime < f->modtime)
206+ low = mid + 1;
207+ else
208+ high = mid - 1;
209+ continue;
210+ }
211+ }
212+ ok_match = mid;
213+ diff = u_strcmp(fmid->basename, f->basename);
214+ if (diff == 0) {
215+ good_match = mid;
216+ if (fmid->dirname == f->dirname)
217+ return -1; /* file is up-to-date */
218+ if (!fmid->dirname) {
219+ low = mid + 1;
220+ continue;
221+ }
222+ if (!f->dirname) {
223+ high = mid - 1;
224+ continue;
225+ }
226+ diff = u_strcmp(fmid->dirname, f->dirname);
227+ if (diff == 0)
228+ return -1; /* file is up-to-date */
229+ }
230+ if (diff < 0)
231+ low = mid + 1;
232+ else
233+ high = mid - 1;
234+ }
235+
236+ return good_match >= 0 ? good_match : ok_match;
237+}
238+
a47d1f86 239+static void look_for_rename(struct file_struct *file, char *fname)
1fffd582
WD
240+{
241+ struct file_struct *fp;
242+ char *partialptr, *fn;
243+ STRUCT_STAT st;
244+ int ndx;
245+
a47d1f86 246+ if ((ndx = fattr_find(file, fname)) < 0)
1fffd582
WD
247+ return;
248+
249+ fp = the_fattr_list.files[ndx];
250+ fn = f_name(fp, NULL);
251+ /* We don't provide an alternate-basis file if there is a basis file. */
252+ if (link_stat(fn, &st, 0) == 0)
253+ return;
254+ if ((partialptr = partial_dir_fname(fn)) == NULL
255+ || !handle_partial_dir(partialptr, PDIR_CREATE))
256+ return;
257+
258+ /* We only use the file if we can hard-link it into our tmp dir. */
259+ if (link(fname, partialptr) == 0) {
260+ if (verbose > 2) {
261+ rprintf(FINFO, "found renamed: %s => %s\n",
262+ fname, partialptr);
263+ }
264+ return;
265+ }
266+
267+ if (errno != EEXIST)
268+ handle_partial_dir(partialptr, PDIR_DELETE);
269+}
87d0091c
WD
270+
271 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
272 * delete recursively.
1fffd582 273 *
f813befd 274 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
15894839
WD
275 * a directory! (The buffer is used for recursion, but returned unchanged.)
276+ *
277+ * Also note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
278 */
f813befd 279 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
15894839 280 {
a47d1f86 281@@ -151,6 +264,8 @@ static enum delret delete_item(char *fbu
15894839
WD
282 goto check_ret;
283 /* OK: try to delete the directory. */
284 }
285+ if (flags & DEL_NO_DELETIONS)
286+ return DR_SUCCESS;
287
288 if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
289 return DR_AT_LIMIT;
a47d1f86 290@@ -197,6 +312,8 @@ static enum delret delete_item(char *fbu
d16b5fd6
WD
291 * its contents, otherwise just checks for content. Returns DR_SUCCESS or
292 * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
293 * buffer is used for recursion, but returned unchanged.)
1fffd582 294+ *
87d0091c 295+ * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
1fffd582 296 */
87d0091c 297 static enum delret delete_dir_contents(char *fname, int flags)
1fffd582 298 {
a47d1f86
WD
299@@ -216,7 +333,9 @@ static enum delret delete_dir_contents(c
300 save_filters = push_local_filters(fname, dlen);
301
302 non_perishable_cnt = 0;
7e27b6c0 303+ file_extra_cnt += SUM_EXTRA_CNT;
a47d1f86 304 dirlist = get_dirlist(fname, dlen, 0);
7e27b6c0 305+ file_extra_cnt -= SUM_EXTRA_CNT;
a47d1f86
WD
306 ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
307
308 if (!dirlist->count)
309@@ -253,6 +372,8 @@ static enum delret delete_dir_contents(c
d16b5fd6
WD
310 if (S_ISDIR(fp->mode)
311 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
312 ret = DR_NOT_EMPTY;
313+ if (detect_renamed && S_ISREG(fp->mode))
a47d1f86 314+ look_for_rename(fp, fname);
d16b5fd6
WD
315 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
316 ret = DR_NOT_EMPTY;
317 }
a47d1f86 318@@ -405,15 +526,19 @@ static void do_delayed_deletions(char *d
1fffd582
WD
319 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
320 * MAXPATHLEN buffer with the name of the directory in it (the functions we
321 * call will append names onto the end, but the old dir value will be restored
322- * on exit). */
323+ * on exit).
324+ *
325+ * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
326+ */
327 static void delete_in_dir(struct file_list *flist, char *fbuf,
328- struct file_struct *file, STRUCT_STAT *stp)
329+ struct file_struct *file, STRUCT_STAT *stp, int flags)
330 {
331 static int min_depth = MAXPATHLEN, cur_depth = -1;
332 static void *filt_array[MAXPATHLEN/2+1];
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) {
a47d1f86 341@@ -427,6 +552,8 @@ 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
a47d1f86 350@@ -434,12 +561,14 @@ static void delete_in_dir(struct file_li
1fffd582
WD
351 return; /* Impossible... */
352
041d67b8 353 if (io_error && !ignore_errors) {
1fffd582
WD
354- if (already_warned)
355+ if (!already_warned) {
356+ rprintf(FINFO,
357+ "IO error encountered -- skipping file deletion\n");
358+ already_warned = 1;
359+ }
360+ if (!detect_renamed)
361 return;
362- rprintf(FINFO,
363- "IO error encountered -- skipping file deletion\n");
364- already_warned = 1;
365- return;
366+ flags |= DEL_NO_DELETIONS;
367 }
368
a47d1f86
WD
369 while (cur_depth >= F_DEPTH(file) && cur_depth >= min_depth)
370@@ -450,6 +579,9 @@ static void delete_in_dir(struct file_li
1fffd582
WD
371 dlen = strlen(fbuf);
372 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
373
374+ if (detect_renamed)
375+ unexplored_dirs--;
376+
377 if (one_file_system) {
378 if (file->flags & FLAG_TOP_DIR)
379 filesystem_dev = stp->st_dev;
a47d1f86 380@@ -459,6 +591,11 @@ static void delete_in_dir(struct file_li
1fffd582
WD
381
382 dirlist = get_dirlist(fbuf, dlen, 0);
383
384+ p = fbuf + dlen;
385+ if (dlen != 1 || *fbuf != '/')
386+ *p++ = '/';
387+ remainder = MAXPATHLEN - (p - fbuf);
388+
389 /* If an item in dirlist is not found in flist, delete it
390 * from the filesystem. */
391 for (i = dirlist->count; i--; ) {
a47d1f86 392@@ -471,16 +608,23 @@ static void delete_in_dir(struct file_li
87d0091c 393 f_name(fp, NULL));
1fffd582 394 continue;
87d0091c 395 }
1fffd582
WD
396+ if (detect_renamed && S_ISREG(fp->mode)) {
397+ strlcpy(p, fp->basename, remainder);
a47d1f86 398+ look_for_rename(fp, fbuf);
1fffd582
WD
399+ }
400 if (flist_find(flist, fp) < 0) {
401 f_name(fp, delbuf);
1071853f 402- if (delete_during == 2) {
a47d1f86 403+ if (delete_during == 2 && !(flags & DEL_NO_DELETIONS)) {
1071853f
WD
404 if (!remember_delete(fp, delbuf))
405 break;
406 } else
f813befd 407- delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
1fffd582 408- }
f813befd 409+ delete_item(delbuf, fp->mode, NULL, flags);
1fffd582
WD
410+ } else if (detect_renamed && S_ISDIR(fp->mode))
411+ unexplored_dirs++;
412 }
413
414+ fbuf[dlen] = '\0';
415+
416 flist_free(dirlist);
417 }
418
a47d1f86 419@@ -510,9 +654,9 @@ static void do_delete_pass(struct file_l
1fffd582
WD
420 || !S_ISDIR(st.st_mode))
421 continue;
422
423- delete_in_dir(flist, fbuf, file, &st);
424+ delete_in_dir(flist, fbuf, file, &st, 0);
425 }
426- delete_in_dir(NULL, NULL, NULL, NULL);
427+ delete_in_dir(NULL, NULL, NULL, NULL, 0);
428
429 if (do_progress && !am_server)
430 rprintf(FINFO, " \r");
a47d1f86 431@@ -1047,6 +1191,7 @@ static int try_dests_non(struct file_str
9a70b743 432 return j;
1fffd582
WD
433 }
434
435+static struct bitbag *delayed_bits = NULL;
436 static int phase = 0;
437
438 /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
a47d1f86 439@@ -1232,8 +1377,12 @@ static void recv_generator(char *fname,
9a70b743
WD
440 if (real_ret != 0 && one_file_system)
441 real_st.st_dev = filesystem_dev;
1fffd582 442 if (delete_during && f_out != -1 && !phase && dry_run < 2
70891d26 443- && (file->flags & FLAG_XFER_DIR))
9a70b743 444- delete_in_dir(the_file_list, fname, file, &real_st);
70891d26 445+ && (file->flags & FLAG_XFER_DIR)) {
9a70b743 446+ if (detect_renamed && real_ret != 0)
1fffd582 447+ unexplored_dirs++;
9a70b743 448+ delete_in_dir(the_file_list, fname, file, &real_st,
1fffd582
WD
449+ delete_during < 0 ? DEL_NO_DELETIONS : 0);
450+ }
451 return;
452 }
453
a47d1f86
WD
454@@ -1497,8 +1646,14 @@ static void recv_generator(char *fname,
455 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1fffd582 456 return;
81172142 457 #endif
1fffd582
WD
458- if (stat_errno == ENOENT)
459+ if (stat_errno == ENOENT) {
460+ if (detect_renamed && unexplored_dirs > 0
a47d1f86 461+ && F_LENGTH(file)) {
1fffd582
WD
462+ bitbag_set_bit(delayed_bits, ndx);
463+ return;
464+ }
465 goto notify_others;
466+ }
467 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
468 full_fname(fname));
469 return;
a47d1f86 470@@ -1688,6 +1843,12 @@ void generate_files(int f_out, struct fi
1fffd582
WD
471 (long)getpid(), flist->count);
472 }
473
474+ if (detect_renamed) {
475+ delayed_bits = bitbag_create(flist->count);
476+ if (!delete_before && !delete_during)
477+ delete_during = -1;
478+ }
479+
480 if (delete_before && !local_name && flist->count > 0)
481 do_delete_pass(flist);
1071853f 482 if (delete_during == 2) {
a47d1f86 483@@ -1698,7 +1859,7 @@ void generate_files(int f_out, struct fi
1071853f 484 }
1fffd582
WD
485 do_progress = 0;
486
487- if (append_mode || whole_file < 0)
488+ if (append_mode || detect_renamed || whole_file < 0)
489 whole_file = 0;
490 if (verbose >= 2) {
491 rprintf(FINFO, "delta-transmission %s\n",
a47d1f86 492@@ -1754,7 +1915,22 @@ void generate_files(int f_out, struct fi
1fffd582 493 }
7b80cd0e 494 recv_generator(NULL, NULL, 0, 0, code, -1);
1fffd582
WD
495 if (delete_during)
496- delete_in_dir(NULL, NULL, NULL, NULL);
497+ delete_in_dir(NULL, NULL, NULL, NULL, 0);
498+
499+ if (detect_renamed) {
500+ if (delete_during < 0)
501+ delete_during = 0;
502+ detect_renamed = 0;
503+
504+ for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
505+ struct file_struct *file = flist->files[i];
506+ if (local_name)
507+ strlcpy(fbuf, local_name, sizeof fbuf);
508+ else
509+ f_name(file, fbuf);
a47d1f86 510+ recv_generator(fbuf, file, i, itemizing, code, f_out);
1fffd582
WD
511+ }
512+ }
513
514 phase++;
515 csum_length = SUM_LENGTH;
516--- old/options.c
517+++ new/options.c
03019e41 518@@ -78,6 +78,7 @@ int am_generator = 0;
a94141d9 519 int am_starting_up = 1;
1fffd582
WD
520 int relative_paths = -1;
521 int implied_dirs = 1;
522+int detect_renamed = 0;
523 int numeric_ids = 0;
524 int allow_8bit_chars = 0;
525 int force_delete = 0;
a47d1f86 526@@ -342,6 +343,7 @@ void usage(enum logcode F)
1fffd582
WD
527 rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
528 rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
529 rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
530+ rprintf(F," --detect-renamed try to find renamed files to speed up the transfer\n");
531 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
532 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
533 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
a47d1f86 534@@ -496,6 +498,7 @@ static struct poptOption long_options[]
1fffd582
WD
535 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
536 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
537 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
538+ {"detect-renamed", 0, POPT_ARG_NONE, &detect_renamed, 0, 0, 0 },
539 {"fuzzy", 'y', POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 },
540 {"compress", 'z', POPT_ARG_NONE, 0, 'z', 0, 0 },
541 {"compress-level", 0, POPT_ARG_INT, &def_compress_level, 'z', 0, 0 },
a47d1f86 542@@ -1355,7 +1358,7 @@ int parse_arguments(int *argc, const cha
1fffd582
WD
543 inplace = 1;
544 }
545
546- if (delay_updates && !partial_dir)
547+ if ((delay_updates || detect_renamed) && !partial_dir)
548 partial_dir = tmp_partialdir;
549
550 if (inplace) {
a47d1f86 551@@ -1364,6 +1367,7 @@ int parse_arguments(int *argc, const cha
1fffd582
WD
552 snprintf(err_buf, sizeof err_buf,
553 "--%s cannot be used with --%s\n",
554 append_mode ? "append" : "inplace",
555+ detect_renamed ? "detect-renamed" :
556 delay_updates ? "delay-updates" : "partial-dir");
557 return 0;
558 }
a47d1f86 559@@ -1674,6 +1678,8 @@ void server_options(char **args,int *arg
a94141d9
WD
560 args[ac++] = "--super";
561 if (size_only)
562 args[ac++] = "--size-only";
563+ if (detect_renamed)
564+ args[ac++] = "--detect-renamed";
565 }
566
567 if (modify_window_set) {
1fffd582
WD
568--- old/rsync.yo
569+++ new/rsync.yo
f813befd 570@@ -364,6 +364,7 @@ to the detailed description below for a
1fffd582
WD
571 --modify-window=NUM compare mod-times with reduced accuracy
572 -T, --temp-dir=DIR create temporary files in directory DIR
573 -y, --fuzzy find similar file for basis if no dest file
574+ --detect-renamed try to find renamed files to speed the xfer
575 --compare-dest=DIR also compare received files relative to DIR
576 --copy-dest=DIR ... and include copies of unchanged files
577 --link-dest=DIR hardlink to files in DIR when unchanged
f813befd 578@@ -1272,6 +1273,15 @@ Note that the use of the bf(--delete) op
1fffd582
WD
579 fuzzy-match files, so either use bf(--delete-after) or specify some
580 filename exclusions if you need to prevent this.
581
582+dit(bf(--detect-renamed)) This option tells rsync to scan the receiving
583+side for files that have been renamed, and to use any that are found as
584+alternate basis files to help speed up the transfer.
585+By default, alternate-basis files are hard-linked into a directory named
586+".~tmp~" in each file's destination directory, but if you've specified
587+the bf(--partial-dir) option, that directory will be used instead. These
588+potential alternate-basis files will be removed as the transfer progresses.
589+This option conflicts with bf(--inplace) and bf(--append).
590+
591 dit(bf(--compare-dest=DIR)) This option instructs rsync to use em(DIR) on
592 the destination machine as an additional hierarchy to compare destination
593 files against doing transfers (if the files are missing in the destination
594--- old/util.c
595+++ new/util.c
f813befd 596@@ -1027,6 +1027,32 @@ int handle_partial_dir(const char *fname
1fffd582
WD
597 return 1;
598 }
599
600+/* We need to supply our own strcmp function for file list comparisons
601+ * to ensure that signed/unsigned usage is consistent between machines. */
602+int u_strcmp(const char *p1, const char *p2)
603+{
604+ for ( ; *p1; p1++, p2++) {
605+ if (*p1 != *p2)
606+ break;
607+ }
608+
609+ return (int)*(uchar*)p1 - (int)*(uchar*)p2;
610+}
611+
612+/* We need a memcmp function compares unsigned-byte values. */
613+int u_memcmp(const void *p1, const void *p2, size_t len)
614+{
615+ const uchar *u1 = p1;
616+ const uchar *u2 = p2;
617+
618+ while (len--) {
619+ if (*u1 != *u2)
620+ return (int)*u1 - (int)*u2;
621+ }
622+
623+ return 0;
624+}
625+
626 /**
627 * Determine if a symlink points outside the current directory tree.
628 * This is considered "unsafe" because e.g. when mirroring somebody