Fixed failing hunks.
[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 After applying this patch, run these commands for a successful build:
23
24     ./prepare-source
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/flist.c
36 +++ new/flist.c
37 @@ -55,6 +55,7 @@ extern int implied_dirs;
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;
44  extern const char *io_write_phase;
45 @@ -73,6 +74,8 @@ int checksum_len;
46  dev_t filesystem_dev; /* used to implement -x */
47  unsigned int file_struct_len;
48  
49 +struct file_list the_fattr_list;
50 +
51  static char empty_sum[MD4_SUM_LENGTH];
52  static int flist_count_offset;
53  
54 @@ -259,6 +262,44 @@ static mode_t from_wire_mode(int mode)
55         return (mode_t)mode;
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;
62 +       int diff;
63 +
64 +       if (!f1->basename || !S_ISREG(f1->mode) || !f1->length) {
65 +               if (!f2->basename || !S_ISREG(f2->mode) || !f2->length)
66 +                       return 0;
67 +               return 1;
68 +       }
69 +       if (!f2->basename || !S_ISREG(f2->mode) || !f2->length)
70 +               return -1;
71 +
72 +       /* Don't use diff for values that are longer than an int. */
73 +       if (f1->length != f2->length)
74 +               return f1->length < f2->length ? -1 : 1;
75 +
76 +       if (always_checksum) {
77 +               diff = u_memcmp(f1->u.sum, f2->u.sum, checksum_len);
78 +               if (diff)
79 +                       return diff;
80 +       } else if (f1->modtime != f2->modtime)
81 +               return f1->modtime < f2->modtime ? -1 : 1;
82 +
83 +       diff = u_strcmp(f1->basename, f2->basename);
84 +       if (diff)
85 +               return diff;
86 +
87 +       if (f1->dirname == f2->dirname)
88 +               return 0;
89 +       if (!f1->dirname)
90 +               return -1;
91 +       if (!f2->dirname)
92 +               return 1;
93 +       return u_strcmp(f1->dirname, f2->dirname);
94 +}
95 +
96  static void send_directory(int f, struct file_list *flist,
97                            char *fbuf, int len);
98  
99 @@ -1387,6 +1428,25 @@ struct file_list *recv_file_list(int f)
100  
101         clean_flist(flist, relative_paths, 1);
102  
103 +       if (detect_renamed) {
104 +               int j = flist->count;
105 +               the_fattr_list.count = j;
106 +               the_fattr_list.files = new_array(struct file_struct *, j);
107 +               if (!the_fattr_list.files)
108 +                       goto oom;
109 +               memcpy(the_fattr_list.files, flist->files,
110 +                      j * sizeof (struct file_struct *));
111 +               qsort(the_fattr_list.files, j,
112 +                   sizeof the_fattr_list.files[0], (int (*)())fattr_compare);
113 +               the_fattr_list.low = 0;
114 +               while (j-- > 0) {
115 +                       struct file_struct *fp = the_fattr_list.files[j];
116 +                       if (fp->basename && S_ISREG(fp->mode) && fp->length)
117 +                               break;
118 +               }
119 +               the_fattr_list.high = j;
120 +       }
121 +
122         if (f >= 0) {
123                 recv_uid_list(f, flist);
124  
125 --- old/generator.c
126 +++ new/generator.c
127 @@ -77,6 +77,7 @@ extern char *basis_dir[];
128  extern int compare_dest;
129  extern int copy_dest;
130  extern int link_dest;
131 +extern int detect_renamed;
132  extern int whole_file;
133  extern int list_only;
134  extern int read_batch;
135 @@ -91,12 +92,15 @@ extern char *backup_dir;
136  extern char *backup_suffix;
137  extern int backup_suffix_len;
138  extern struct file_list *the_file_list;
139 +extern struct file_list the_fattr_list;
140  extern struct filter_list_struct server_filter_list;
141  
142  static int deletion_count = 0; /* used to implement --max-delete */
143 +static int unexplored_dirs = 1;
144  
145 -/* For calling delete_file() */
146 +/* For calling delete_item() and delete_in_dir() */
147  #define DEL_FORCE_RECURSE      (1<<1) /* recurse even w/o --force */
148 +#define DEL_NO_DELETIONS       (1<<2)
149  #define DEL_TERSE              (1<<3)
150  
151  
152 @@ -106,12 +110,120 @@ static int is_backup_file(char *fn)
153         return k > 0 && strcmp(fn+k, backup_suffix) == 0;
154  }
155  
156 +/* Search for a regular file that matches either (1) the size & modified
157 + * time (plus the basename, if possible) or (2) the size & checksum.  If
158 + * we find an exact match down to the dirname, return -1 because we found
159 + * an up-to-date file in the transfer, not a renamed file. */
160 +static int fattr_find(struct file_struct *f, char *fname, alloc_pool_t pool)
161 +{
162 +       int low = the_fattr_list.low, high = the_fattr_list.high;
163 +       int mid, ok_match = -1, good_match = -1;
164 +       struct file_struct *fmid;
165 +       int diff;
166 +
167 +       while (low <= high) {
168 +               mid = (low + high) / 2;
169 +               fmid = the_fattr_list.files[mid];
170 +               if (fmid->length != f->length) {
171 +                       if (fmid->length < f->length)
172 +                               low = mid + 1;
173 +                       else
174 +                               high = mid - 1;
175 +                       continue;
176 +               }
177 +               if (always_checksum) {
178 +                       if (!f->u.sum) {
179 +                               if (fmid->modtime == f->modtime
180 +                                && f_name_cmp(fmid, f) == 0)
181 +                                       return -1; /* assume we can't help */
182 +                               f->u.sum = pool_alloc(pool, MD4_SUM_LENGTH,
183 +                                                     "fattr_find");
184 +                               file_checksum(fname, f->u.sum, f->length);
185 +                       }
186 +                       diff = u_memcmp(fmid->u.sum, f->u.sum, checksum_len);
187 +                       if (diff) {
188 +                               if (diff < 0)
189 +                                       low = mid + 1;
190 +                               else
191 +                                       high = mid - 1;
192 +                               continue;
193 +                       }
194 +               } else {
195 +                       if (fmid->modtime != f->modtime) {
196 +                               if (fmid->modtime < f->modtime)
197 +                                       low = mid + 1;
198 +                               else
199 +                                       high = mid - 1;
200 +                               continue;
201 +                       }
202 +               }
203 +               ok_match = mid;
204 +               diff = u_strcmp(fmid->basename, f->basename);
205 +               if (diff == 0) {
206 +                       good_match = mid;
207 +                       if (fmid->dirname == f->dirname)
208 +                               return -1; /* file is up-to-date */
209 +                       if (!fmid->dirname) {
210 +                               low = mid + 1;
211 +                               continue;
212 +                       }
213 +                       if (!f->dirname) {
214 +                               high = mid - 1;
215 +                               continue;
216 +                       }
217 +                       diff = u_strcmp(fmid->dirname, f->dirname);
218 +                       if (diff == 0)
219 +                               return -1; /* file is up-to-date */
220 +               }
221 +               if (diff < 0)
222 +                       low = mid + 1;
223 +               else
224 +                       high = mid - 1;
225 +       }
226 +
227 +       return good_match >= 0 ? good_match : ok_match;
228 +}
229 +
230 +static void look_for_rename(struct file_struct *file, char *fname,
231 +                           alloc_pool_t pool)
232 +{
233 +       struct file_struct *fp;
234 +       char *partialptr, *fn;
235 +       STRUCT_STAT st;
236 +       int ndx;
237 +
238 +       if ((ndx = fattr_find(file, fname, pool)) < 0)
239 +               return;
240 +
241 +       fp = the_fattr_list.files[ndx];
242 +       fn = f_name(fp, NULL);
243 +       /* We don't provide an alternate-basis file if there is a basis file. */
244 +       if (link_stat(fn, &st, 0) == 0)
245 +               return;
246 +       if ((partialptr = partial_dir_fname(fn)) == NULL
247 +        || !handle_partial_dir(partialptr, PDIR_CREATE))
248 +               return;
249 +
250 +       /* We only use the file if we can hard-link it into our tmp dir. */
251 +       if (link(fname, partialptr) == 0) {
252 +               if (verbose > 2) {
253 +                       rprintf(FINFO, "found renamed: %s => %s\n",
254 +                               fname, partialptr);
255 +               }
256 +               return;
257 +       }
258 +
259 +       if (errno != EEXIST)
260 +               handle_partial_dir(partialptr, PDIR_DELETE);
261 +}
262  
263  /* Delete a file or directory.  If DEL_FORCE_RECURSE is set in the flags, or if
264   * force_delete is set, this will delete recursively.
265   *
266   * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's
267   * a directory! (The buffer is used for recursion, but returned unchanged.)
268 + *
269 + * Also Note:  --detect-rename may use this routine with DEL_NO_DELETIONS set!
270   */
271  static int delete_item(char *fname, int mode, int flags)
272  {
273 @@ -122,6 +234,8 @@ static int delete_item(char *fname, int 
274         char *p;
275  
276         if (!S_ISDIR(mode)) {
277 +               if (flags & DEL_NO_DELETIONS)
278 +                       return 0;
279                 if (max_delete && ++deletion_count > max_delete)
280                         return 0;
281                 if (make_backups && (backup_dir || !is_backup_file(fname)))
282 @@ -144,6 +258,7 @@ static int delete_item(char *fname, int 
283  
284         zap_dir = flags & DEL_FORCE_RECURSE || force_delete;
285         if ((max_delete && ++deletion_count > max_delete)
286 +           || flags & DEL_NO_DELETIONS
287             || (dry_run && zap_dir)) {
288                 ok = 0;
289                 errno = ENOTEMPTY;
290 @@ -186,6 +301,8 @@ static int delete_item(char *fname, int 
291                         continue;
292  
293                 strlcpy(p, fp->basename, remainder);
294 +               if (detect_renamed && S_ISREG(fp->mode))
295 +                       look_for_rename(fp, fname, dirlist->file_pool);
296                 delete_item(fname, fp->mode, flags & ~DEL_TERSE);
297         }
298         flist_free(dirlist);
299 @@ -194,7 +311,8 @@ static int delete_item(char *fname, int 
300  
301         pop_local_filters(save_filters);
302  
303 -       if (max_delete && ++deletion_count > max_delete)
304 +       if (flags & DEL_NO_DELETIONS
305 +        || (max_delete && ++deletion_count > max_delete))
306                 return 0;
307  
308         if (do_rmdir(fname) == 0) {
309 @@ -214,15 +332,19 @@ static int delete_item(char *fname, int 
310   * all the --delete-WHEN options.  Note that the fbuf pointer must point to a
311   * MAXPATHLEN buffer with the name of the directory in it (the functions we
312   * call will append names onto the end, but the old dir value will be restored
313 - * on exit). */
314 + * on exit).
315 + *
316 + * Note:  --detect-rename may use this routine with DEL_NO_DELETIONS set!
317 + */
318  static void delete_in_dir(struct file_list *flist, char *fbuf,
319 -                         struct file_struct *file, STRUCT_STAT *stp)
320 +                         struct file_struct *file, STRUCT_STAT *stp, int flags)
321  {
322         static int min_depth = MAXPATHLEN, cur_depth = -1;
323         static void *filt_array[MAXPATHLEN/2+1];
324         static int already_warned = 0;
325         struct file_list *dirlist;
326 -       char delbuf[MAXPATHLEN];
327 +       char *p, delbuf[MAXPATHLEN];
328 +       unsigned remainder;
329         int dlen, i;
330  
331         if (!flist) {
332 @@ -236,6 +358,8 @@ static void delete_in_dir(struct file_li
333         if (verbose > 2)
334                 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
335  
336 +       flags |= DEL_FORCE_RECURSE;
337 +
338         if (allowed_lull)
339                 maybe_send_keepalive();
340  
341 @@ -243,12 +367,14 @@ static void delete_in_dir(struct file_li
342                 return; /* Impossible... */
343  
344         if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
345 -               if (already_warned)
346 +               if (!already_warned) {
347 +                       rprintf(FINFO,
348 +                           "IO error encountered -- skipping file deletion\n");
349 +                       already_warned = 1;
350 +               }
351 +               if (!detect_renamed)
352                         return;
353 -               rprintf(FINFO,
354 -                       "IO error encountered -- skipping file deletion\n");
355 -               already_warned = 1;
356 -               return;
357 +               flags |= DEL_NO_DELETIONS;
358         }
359  
360         while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
361 @@ -259,6 +385,9 @@ static void delete_in_dir(struct file_li
362         dlen = strlen(fbuf);
363         filt_array[cur_depth] = push_local_filters(fbuf, dlen);
364  
365 +       if (detect_renamed)
366 +               unexplored_dirs--;
367 +
368         if (one_file_system) {
369                 if (file->flags & FLAG_TOP_DIR)
370                         filesystem_dev = stp->st_dev;
371 @@ -268,18 +397,30 @@ static void delete_in_dir(struct file_li
372  
373         dirlist = get_dirlist(fbuf, dlen, 0);
374  
375 +       p = fbuf + dlen;
376 +       if (dlen != 1 || *fbuf != '/')
377 +               *p++ = '/';
378 +       remainder = MAXPATHLEN - (p - fbuf);
379 +
380         /* If an item in dirlist is not found in flist, delete it
381          * from the filesystem. */
382         for (i = dirlist->count; i--; ) {
383                 struct file_struct *fp = dirlist->files[i];
384                 if (!fp->basename || fp->flags & FLAG_MOUNT_POINT)
385                         continue;
386 +               if (detect_renamed && S_ISREG(fp->mode)) {
387 +                       strlcpy(p, fp->basename, remainder);
388 +                       look_for_rename(fp, fbuf, dirlist->file_pool);
389 +               }
390                 if (flist_find(flist, fp) < 0) {
391                         f_name(fp, delbuf);
392 -                       delete_item(delbuf, fp->mode, DEL_FORCE_RECURSE);
393 -               }
394 +                       delete_item(delbuf, fp->mode, flags);
395 +               } else if (detect_renamed && S_ISDIR(fp->mode))
396 +                       unexplored_dirs++;
397         }
398  
399 +       fbuf[dlen] = '\0';
400 +
401         flist_free(dirlist);
402  }
403  
404 @@ -309,9 +450,9 @@ static void do_delete_pass(struct file_l
405                  || !S_ISDIR(st.st_mode))
406                         continue;
407  
408 -               delete_in_dir(flist, fbuf, file, &st);
409 +               delete_in_dir(flist, fbuf, file, &st, 0);
410         }
411 -       delete_in_dir(NULL, NULL, NULL, NULL);
412 +       delete_in_dir(NULL, NULL, NULL, NULL, 0);
413  
414         if (do_progress && !am_server)
415                 rprintf(FINFO, "                    \r");
416 @@ -756,6 +897,7 @@ static int try_dests_non(struct file_str
417         return -1;
418  }
419  
420 +static struct bitbag *delayed_bits = NULL;
421  static int phase = 0;
422  
423  /* Acts on the_file_list->file's ndx'th item, whose name is fname.  If a dir,
424 @@ -910,8 +1052,12 @@ static void recv_generator(char *fname, 
425                     && verbose && code && f_out != -1)
426                         rprintf(code, "%s/\n", fname);
427                 if (delete_during && f_out != -1 && !phase && dry_run < 2
428 -                   && (file->flags & FLAG_DEL_HERE))
429 -                       delete_in_dir(the_file_list, fname, file, &st);
430 +                   && (file->flags & FLAG_DEL_HERE)) {
431 +                       if (detect_renamed && statret != 0)
432 +                               unexplored_dirs++;
433 +                       delete_in_dir(the_file_list, fname, file, &st,
434 +                                     delete_during < 0 ? DEL_NO_DELETIONS : 0);
435 +               }
436                 return;
437         }
438  
439 @@ -1150,8 +1296,14 @@ static void recv_generator(char *fname, 
440                     && hard_link_check(file, ndx, fname, statret, &st,
441                                        itemizing, code, HL_SKIP))
442                         return;
443 -               if (stat_errno == ENOENT)
444 +               if (stat_errno == ENOENT) {
445 +                       if (detect_renamed && unexplored_dirs > 0
446 +                        && file->length) {
447 +                               bitbag_set_bit(delayed_bits, ndx);
448 +                               return;
449 +                       }
450                         goto notify_others;
451 +               }
452                 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
453                         full_fname(fname));
454                 return;
455 @@ -1329,11 +1481,17 @@ void generate_files(int f_out, struct fi
456                         (long)getpid(), flist->count);
457         }
458  
459 +       if (detect_renamed) {
460 +               delayed_bits = bitbag_create(flist->count);
461 +               if (!delete_before && !delete_during)
462 +                       delete_during = -1;
463 +       }
464 +
465         if (delete_before && !local_name && flist->count > 0)
466                 do_delete_pass(flist);
467         do_progress = 0;
468  
469 -       if (append_mode || whole_file < 0)
470 +       if (append_mode || detect_renamed || whole_file < 0)
471                 whole_file = 0;
472         if (verbose >= 2) {
473                 rprintf(FINFO, "delta-transmission %s\n",
474 @@ -1388,7 +1546,23 @@ void generate_files(int f_out, struct fi
475         }
476         recv_generator(NULL, NULL, 0, 0, 0, code, -1);
477         if (delete_during)
478 -               delete_in_dir(NULL, NULL, NULL, NULL);
479 +               delete_in_dir(NULL, NULL, NULL, NULL, 0);
480 +
481 +       if (detect_renamed) {
482 +               if (delete_during < 0)
483 +                       delete_during = 0;
484 +               detect_renamed = 0;
485 +
486 +               for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
487 +                       struct file_struct *file = flist->files[i];
488 +                       if (local_name)
489 +                               strlcpy(fbuf, local_name, sizeof fbuf);
490 +                       else
491 +                               f_name(file, fbuf);
492 +                       recv_generator(fbuf, file, i, itemizing,
493 +                                      maybe_ATTRS_REPORT, code, f_out);
494 +               }
495 +       }
496  
497         phase++;
498         csum_length = SUM_LENGTH;
499 --- old/options.c
500 +++ new/options.c
501 @@ -76,6 +76,7 @@ int am_generator = 0;
502  int am_starting_up = 1;
503  int relative_paths = -1;
504  int implied_dirs = 1;
505 +int detect_renamed = 0;
506  int numeric_ids = 0;
507  int allow_8bit_chars = 0;
508  int force_delete = 0;
509 @@ -334,6 +335,7 @@ void usage(enum logcode F)
510    rprintf(F,"     --modify-window=NUM     compare mod-times with reduced accuracy\n");
511    rprintf(F," -T, --temp-dir=DIR          create temporary files in directory DIR\n");
512    rprintf(F," -y, --fuzzy                 find similar file for basis if no dest file\n");
513 +  rprintf(F,"     --detect-renamed        try to find renamed files to speed up the transfer\n");
514    rprintf(F,"     --compare-dest=DIR      also compare destination files relative to DIR\n");
515    rprintf(F,"     --copy-dest=DIR         ... and include copies of unchanged files\n");
516    rprintf(F,"     --link-dest=DIR         hardlink to files in DIR when unchanged\n");
517 @@ -481,6 +483,7 @@ static struct poptOption long_options[] 
518    {"compare-dest",     0,  POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
519    {"copy-dest",        0,  POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
520    {"link-dest",        0,  POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
521 +  {"detect-renamed",   0,  POPT_ARG_NONE,   &detect_renamed, 0, 0, 0 },
522    {"fuzzy",           'y', POPT_ARG_NONE,   &fuzzy_basis, 0, 0, 0 },
523    {"compress",        'z', POPT_ARG_NONE,   0, 'z', 0, 0 },
524    {"compress-level",   0,  POPT_ARG_INT,    &def_compress_level, 'z', 0, 0 },
525 @@ -1340,7 +1343,7 @@ int parse_arguments(int *argc, const cha
526                 inplace = 1;
527         }
528  
529 -       if (delay_updates && !partial_dir)
530 +       if ((delay_updates || detect_renamed) && !partial_dir)
531                 partial_dir = tmp_partialdir;
532  
533         if (inplace) {
534 @@ -1349,6 +1352,7 @@ int parse_arguments(int *argc, const cha
535                         snprintf(err_buf, sizeof err_buf,
536                                  "--%s cannot be used with --%s\n",
537                                  append_mode ? "append" : "inplace",
538 +                                detect_renamed ? "detect-renamed" :
539                                  delay_updates ? "delay-updates" : "partial-dir");
540                         return 0;
541                 }
542 @@ -1651,6 +1655,8 @@ void server_options(char **args,int *arg
543                         args[ac++] = "--super";
544                 if (size_only)
545                         args[ac++] = "--size-only";
546 +               if (detect_renamed)
547 +                       args[ac++] = "--detect-renamed";
548         }
549  
550         if (modify_window_set) {
551 --- old/rsync.yo
552 +++ new/rsync.yo
553 @@ -362,6 +362,7 @@ to the detailed description below for a 
554       --modify-window=NUM     compare mod-times with reduced accuracy
555   -T, --temp-dir=DIR          create temporary files in directory DIR
556   -y, --fuzzy                 find similar file for basis if no dest file
557 +     --detect-renamed        try to find renamed files to speed the xfer
558       --compare-dest=DIR      also compare received files relative to DIR
559       --copy-dest=DIR         ... and include copies of unchanged files
560       --link-dest=DIR         hardlink to files in DIR when unchanged
561 @@ -1240,6 +1241,15 @@ Note that the use of the bf(--delete) op
562  fuzzy-match files, so either use bf(--delete-after) or specify some
563  filename exclusions if you need to prevent this.
564  
565 +dit(bf(--detect-renamed)) This option tells rsync to scan the receiving
566 +side for files that have been renamed, and to use any that are found as
567 +alternate basis files to help speed up the transfer.
568 +By default, alternate-basis files are hard-linked into a directory named
569 +".~tmp~" in each file's destination directory, but if you've specified
570 +the bf(--partial-dir) option, that directory will be used instead.  These
571 +potential alternate-basis files will be removed as the transfer progresses.
572 +This option conflicts with bf(--inplace) and bf(--append).
573 +
574  dit(bf(--compare-dest=DIR)) This option instructs rsync to use em(DIR) on
575  the destination machine as an additional hierarchy to compare destination
576  files against doing transfers (if the files are missing in the destination
577 --- old/util.c
578 +++ new/util.c
579 @@ -1013,6 +1013,32 @@ int handle_partial_dir(const char *fname
580         return 1;
581  }
582  
583 +/* We need to supply our own strcmp function for file list comparisons
584 + * to ensure that signed/unsigned usage is consistent between machines. */
585 +int u_strcmp(const char *p1, const char *p2)
586 +{
587 +        for ( ; *p1; p1++, p2++) {
588 +               if (*p1 != *p2)
589 +                       break;
590 +       }
591 +
592 +       return (int)*(uchar*)p1 - (int)*(uchar*)p2;
593 +}
594 +
595 +/* We need a memcmp function compares unsigned-byte values. */
596 +int u_memcmp(const void *p1, const void *p2, size_t len)
597 +{
598 +       const uchar *u1 = p1;
599 +       const uchar *u2 = p2;
600 +
601 +       while (len--) {
602 +               if (*u1 != *u2)
603 +                       return (int)*u1 - (int)*u2;
604 +       }
605 +
606 +       return 0;
607 +}
608 +
609  /**
610   * Determine if a symlink points outside the current directory tree.
611   * This is considered "unsafe" because e.g. when mirroring somebody