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