| 1 | Depends-On-Patch: g2r-basis-filename.diff |
| 2 | |
| 3 | The changes to generator.c were greatly simplified, making the patch |
| 4 | easier to maintain and fixing the failing test in the testsuite. |
| 5 | Very lightly tested. |
| 6 | |
| 7 | Be sure to run "make proto" before "make". |
| 8 | |
| 9 | --- orig/generator.c 2004-10-09 04:06:49 |
| 10 | +++ generator.c 2004-09-23 15:33:08 |
| 11 | @@ -43,6 +43,7 @@ extern int ignore_times; |
| 12 | extern int size_only; |
| 13 | extern int io_timeout; |
| 14 | extern int protocol_version; |
| 15 | +extern int fuzzy_basis; |
| 16 | extern int always_checksum; |
| 17 | extern char *partial_dir; |
| 18 | extern char *compare_dest; |
| 19 | @@ -244,6 +245,92 @@ static void generate_and_send_sums(int f |
| 20 | } |
| 21 | |
| 22 | |
| 23 | +static void split_names(char *fname, char **dirname, char **basename) |
| 24 | +{ |
| 25 | + char *slash = strrchr(fname, '/'); |
| 26 | + if (slash) { |
| 27 | + *dirname = fname; |
| 28 | + *slash = '\0'; |
| 29 | + *basename = slash+1; |
| 30 | + } else { |
| 31 | + *basename = fname; |
| 32 | + *dirname = "."; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +static unsigned int measure_name(const char *name, const char *basename, |
| 38 | + const char *ext) |
| 39 | +{ |
| 40 | + int namelen = strlen(name); |
| 41 | + int extlen = strlen(ext); |
| 42 | + unsigned int score = 0; |
| 43 | + |
| 44 | + /* Extensions must match */ |
| 45 | + if (namelen <= extlen || strcmp(name + namelen - extlen, ext) != 0) |
| 46 | + return 0; |
| 47 | + |
| 48 | + /* Now score depends on similarity of prefix */ |
| 49 | + for (; *name == *basename && *name; name++, basename++) |
| 50 | + score++; |
| 51 | + return score; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +static int find_fuzzy(const char *fname, char *buf, STRUCT_STAT *st_ptr) |
| 56 | +{ |
| 57 | + DIR *d; |
| 58 | + struct dirent *di; |
| 59 | + char *basename, *dirname; |
| 60 | + char mangled_name[MAXPATHLEN]; |
| 61 | + char bestname[MAXPATHLEN]; |
| 62 | + unsigned int bestscore = 0; |
| 63 | + const char *ext; |
| 64 | + |
| 65 | + strlcpy(mangled_name, fname, sizeof mangled_name); |
| 66 | + |
| 67 | + split_names(mangled_name, &dirname, &basename); |
| 68 | + if (!(d = opendir(dirname))) { |
| 69 | + rsyserr(FERROR, errno, "recv_generator opendir(%s)", dirname); |
| 70 | + return -1; |
| 71 | + } |
| 72 | + |
| 73 | + /* Get final extension, eg. .gz; never full basename though. */ |
| 74 | + if (!(ext = strrchr(basename + 1, '.'))) |
| 75 | + ext = basename + strlen(basename); /* ext = "" */ |
| 76 | + |
| 77 | + while ((di = readdir(d)) != NULL) { |
| 78 | + const char *dname = d_name(di); |
| 79 | + unsigned int score; |
| 80 | + |
| 81 | + if (dname[0] == '.' && (dname[1] == '\0' |
| 82 | + || (dname[1] == '.' && dname[2] == '\0'))) |
| 83 | + continue; |
| 84 | + |
| 85 | + score = measure_name(dname, basename, ext); |
| 86 | + if (verbose > 4) { |
| 87 | + rprintf(FINFO, "[%s] fuzzy score for %s = %u\n", |
| 88 | + who_am_i(), dname, score); |
| 89 | + } |
| 90 | + if (score > bestscore) { |
| 91 | + strlcpy(bestname, dname, sizeof bestname); |
| 92 | + bestscore = score; |
| 93 | + } |
| 94 | + } |
| 95 | + closedir(d); |
| 96 | + |
| 97 | + /* Found a candidate. */ |
| 98 | + if (bestscore != 0) { |
| 99 | + pathjoin(buf, MAXPATHLEN, dirname, bestname); |
| 100 | + if (verbose > 2) { |
| 101 | + rprintf(FINFO, "[%s] fuzzy match %s->%s\n", |
| 102 | + who_am_i(), fname, buf); |
| 103 | + } |
| 104 | + return link_stat(buf, st_ptr, 0); |
| 105 | + } |
| 106 | + return -1; |
| 107 | +} |
| 108 | + |
| 109 | |
| 110 | /* |
| 111 | * Acts on file number @p i from @p flist, whose name is @p fname. |
| 112 | @@ -466,6 +553,15 @@ static void recv_generator(char *fname, |
| 113 | } else |
| 114 | partialptr = NULL; |
| 115 | |
| 116 | + if (statret == -1 && fuzzy_basis) { |
| 117 | + if (find_fuzzy(fname, fnamecmpbuf, &st) == 0 |
| 118 | + && S_ISREG(st.st_mode)) { |
| 119 | + statret = 0; |
| 120 | + fnamecmp = fnamecmpbuf; |
| 121 | + fnamecmp_type = G2R_FUZZY; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | if (statret == -1) { |
| 126 | if (preserve_hard_links && hard_link_check(file, HL_SKIP)) |
| 127 | return; |
| 128 | @@ -492,7 +588,7 @@ static void recv_generator(char *fname, |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | - if (skip_file(fnamecmp, file, &st)) { |
| 133 | + if (fnamecmp_type != G2R_FUZZY && skip_file(fnamecmp, file, &st)) { |
| 134 | if (fnamecmp_type == G2R_FNAME) |
| 135 | set_perms(fname, file, &st, PERMS_REPORT); |
| 136 | return; |
| 137 | @@ -564,6 +660,21 @@ prepare_to_open: |
| 138 | notify_others: |
| 139 | if (f_out_name >= 0) { |
| 140 | write_byte(f_out_name, fnamecmp_type); |
| 141 | + if (fnamecmp_type == G2R_FUZZY) { |
| 142 | + uchar lenbuf[3], *lb = lenbuf; |
| 143 | + int len = strlen(fnamecmpbuf); |
| 144 | + if (len > 0x7F) { |
| 145 | +#if MAXPATHLEN > 0x7FFF |
| 146 | + *lb++ = len / 0x10000 + 0x80; |
| 147 | + *lb++ = len / 0x100; |
| 148 | +#else |
| 149 | + *lb++ = len / 0x100 + 0x80; |
| 150 | +#endif |
| 151 | + } |
| 152 | + *lb = len; |
| 153 | + write_buf(f_out_name, lenbuf, lb - lenbuf + 1); |
| 154 | + write_buf(f_out_name, fnamecmpbuf, len); |
| 155 | + } |
| 156 | io_flush(NORMAL_FLUSH); /* XXX make this more efficient! */ |
| 157 | } |
| 158 | |
| 159 | --- orig/main.c 2004-10-09 03:25:43 |
| 160 | +++ main.c 2004-07-22 00:32:31 |
| 161 | @@ -48,6 +48,7 @@ extern int keep_dirlinks; |
| 162 | extern int preserve_hard_links; |
| 163 | extern int protocol_version; |
| 164 | extern int recurse; |
| 165 | +extern int fuzzy_basis; |
| 166 | extern int relative_paths; |
| 167 | extern int rsync_port; |
| 168 | extern int whole_file; |
| 169 | @@ -463,7 +464,7 @@ static int do_recv(int f_in,int f_out,st |
| 170 | int pid; |
| 171 | int status = 0; |
| 172 | int error_pipe[2], name_pipe[2]; |
| 173 | - BOOL need_name_pipe = compare_dest && !dry_run; |
| 174 | + BOOL need_name_pipe = (compare_dest || fuzzy_basis) && !dry_run; |
| 175 | |
| 176 | /* The receiving side mustn't obey this, or an existing symlink that |
| 177 | * points to an identical file won't be replaced by the referent. */ |
| 178 | --- orig/options.c 2004-10-14 17:11:40 |
| 179 | +++ options.c 2004-10-14 17:21:51 |
| 180 | @@ -85,6 +85,7 @@ int safe_symlinks = 0; |
| 181 | int copy_unsafe_links = 0; |
| 182 | int size_only = 0; |
| 183 | int bwlimit = 0; |
| 184 | +int fuzzy_basis = 0; |
| 185 | size_t bwlimit_writemax = 0; |
| 186 | int delete_after = 0; |
| 187 | int only_existing = 0; |
| 188 | @@ -279,6 +280,7 @@ void usage(enum logcode F) |
| 189 | rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n"); |
| 190 | rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n"); |
| 191 | rprintf(F," --link-dest=DIR create hardlinks to DIR for unchanged files\n"); |
| 192 | + rprintf(F," --fuzzy use similar file as basis if basis doesn't exist\n"); |
| 193 | rprintf(F," -P equivalent to --partial --progress\n"); |
| 194 | rprintf(F," -z, --compress compress file data\n"); |
| 195 | rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n"); |
| 196 | @@ -371,6 +373,7 @@ static struct poptOption long_options[] |
| 197 | {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 }, |
| 198 | {"compare-dest", 0, POPT_ARG_STRING, &compare_dest, 0, 0, 0 }, |
| 199 | {"link-dest", 0, POPT_ARG_STRING, &compare_dest, OPT_LINK_DEST, 0, 0 }, |
| 200 | + {"fuzzy", 0, POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 }, |
| 201 | /* TODO: Should this take an optional int giving the compression level? */ |
| 202 | {"compress", 'z', POPT_ARG_NONE, &do_compression, 0, 0, 0 }, |
| 203 | {"stats", 0, POPT_ARG_NONE, &do_stats, 0, 0, 0 }, |
| 204 | @@ -890,6 +893,11 @@ int parse_arguments(int *argc, const cha |
| 205 | link_dest ? "--link-dest" : "--compare-dest"); |
| 206 | return 0; |
| 207 | } |
| 208 | + if (compare_dest) { |
| 209 | + snprintf(err_buf, sizeof err_buf, |
| 210 | + "--inplace does not yet work with --fuzzy\n"); |
| 211 | + return 0; |
| 212 | + } |
| 213 | } else { |
| 214 | if (keep_partial && !partial_dir) |
| 215 | partial_dir = getenv("RSYNC_PARTIAL_DIR"); |
| 216 | @@ -1156,6 +1164,9 @@ void server_options(char **args,int *arg |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | + if (fuzzy_basis && am_sender) |
| 221 | + args[ac++] = "--fuzzy"; |
| 222 | + |
| 223 | *argc = ac; |
| 224 | return; |
| 225 | |
| 226 | --- orig/receiver.c 2004-09-07 21:57:20 |
| 227 | +++ receiver.c 2004-07-30 18:21:38 |
| 228 | @@ -319,6 +319,27 @@ static int receive_data(int f_in, char * |
| 229 | } |
| 230 | |
| 231 | |
| 232 | +static void read_gen_name(int fd, char *buf) |
| 233 | +{ |
| 234 | + int len = read_byte(fd); |
| 235 | + if (len & 0x80) { |
| 236 | +#if MAXPATHLEN > 32767 |
| 237 | + uchar lenbuf[2]; |
| 238 | + read_buf(fd, (char *)lenbuf, 2); |
| 239 | + len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1]; |
| 240 | +#else |
| 241 | + len = (len & ~0x80) * 0x100 + read_byte(fd); |
| 242 | +#endif |
| 243 | + } |
| 244 | + if (len >= MAXPATHLEN) { |
| 245 | + rprintf(FERROR, "bogus data on generator name pipe\n"); |
| 246 | + exit_cleanup(RERR_PROTOCOL); |
| 247 | + } |
| 248 | + |
| 249 | + read_sbuf(fd, buf, len); |
| 250 | +} |
| 251 | + |
| 252 | + |
| 253 | static void discard_receive_data(int f_in, OFF_T length) |
| 254 | { |
| 255 | receive_data(f_in, NULL, -1, 0, NULL, -1, length); |
| 256 | @@ -448,6 +469,10 @@ int recv_files(int f_in, struct file_lis |
| 257 | case G2R_BACKUP: |
| 258 | fnamecmp = get_backup_name(fname); |
| 259 | break; |
| 260 | + case G2R_FUZZY: |
| 261 | + read_gen_name(f_in_name, fnamecmpbuf); |
| 262 | + fnamecmp = fnamecmpbuf; |
| 263 | + break; |
| 264 | default: |
| 265 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, |
| 266 | compare_dest, fname); |
| 267 | --- orig/rsync.h 2004-09-07 21:52:22 |
| 268 | +++ rsync.h 2004-09-07 22:02:37 |
| 269 | @@ -122,6 +122,7 @@ |
| 270 | #define G2R_FNAME 0x80 |
| 271 | #define G2R_PARTIAL_DIR 0x81 |
| 272 | #define G2R_BACKUP 0x82 |
| 273 | +#define G2R_FUZZY 0x83 |
| 274 | |
| 275 | |
| 276 | /* Log-message categories. FLOG is only used on the daemon side to |
| 277 | --- orig/rsync.yo 2004-10-06 00:12:16 |
| 278 | +++ rsync.yo 2004-07-03 19:27:25 |
| 279 | @@ -356,6 +356,7 @@ verb( |
| 280 | -T --temp-dir=DIR create temporary files in directory DIR |
| 281 | --compare-dest=DIR also compare received files relative to DIR |
| 282 | --link-dest=DIR create hardlinks to DIR for unchanged files |
| 283 | + --fuzzy use similar file as basis if basis is gone |
| 284 | -P equivalent to --partial --progress |
| 285 | -z, --compress compress file data |
| 286 | -C, --cvs-exclude auto ignore files in the same way CVS does |