Commit | Line | Data |
---|---|---|
824abc86 | 1 | Depends-On-Patch: g2r-basis-filename.diff |
8c5b8235 | 2 | |
824abc86 | 3 | The changes to generator.c were greatly simplified, making the patch |
8c5b8235 | 4 | easier to maintain and fixing the failing test in the testsuite. |
58118c25 | 5 | Very lightly tested. |
241013b4 | 6 | |
824abc86 WD |
7 | Be sure to run "make proto" before "make". |
8 | ||
a6587818 | 9 | --- orig/generator.c 2004-09-20 19:57:58 |
dd39e657 | 10 | +++ generator.c 2004-09-23 15:33:08 |
f48a237e | 11 | @@ -43,6 +43,7 @@ extern int ignore_times; |
58118c25 WD |
12 | extern int size_only; |
13 | extern int io_timeout; | |
14 | extern int protocol_version; | |
09fb2223 | 15 | +extern int fuzzy_basis; |
58118c25 | 16 | extern int always_checksum; |
b952a177 | 17 | extern char *partial_dir; |
58118c25 | 18 | extern char *compare_dest; |
dd39e657 | 19 | @@ -244,6 +245,92 @@ static void generate_and_send_sums(int f |
58118c25 WD |
20 | } |
21 | ||
22 | ||
47dd7a31 WD |
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 | + | |
58118c25 | 36 | + |
47dd7a31 WD |
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 | + | |
58118c25 | 54 | + |
09fb2223 | 55 | +static int find_fuzzy(const char *fname, char *buf, STRUCT_STAT *st_ptr) |
47dd7a31 WD |
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 | + | |
09fb2223 | 65 | + strlcpy(mangled_name, fname, sizeof mangled_name); |
47dd7a31 WD |
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. */ | |
dd39e657 | 74 | + if (!(ext = strrchr(basename + 1, '.'))) |
47dd7a31 WD |
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) { | |
8c5b8235 WD |
87 | + rprintf(FINFO, "[%s] fuzzy score for %s = %u\n", |
88 | + who_am_i(), dname, score); | |
47dd7a31 WD |
89 | + } |
90 | + if (score > bestscore) { | |
8c5b8235 | 91 | + strlcpy(bestname, dname, sizeof bestname); |
47dd7a31 WD |
92 | + bestscore = score; |
93 | + } | |
94 | + } | |
95 | + closedir(d); | |
96 | + | |
97 | + /* Found a candidate. */ | |
98 | + if (bestscore != 0) { | |
8c5b8235 | 99 | + pathjoin(buf, MAXPATHLEN, dirname, bestname); |
47dd7a31 | 100 | + if (verbose > 2) { |
8c5b8235 | 101 | + rprintf(FINFO, "[%s] fuzzy match %s->%s\n", |
09fb2223 | 102 | + who_am_i(), fname, buf); |
47dd7a31 | 103 | + } |
58118c25 | 104 | + return link_stat(buf, st_ptr, 0); |
47dd7a31 WD |
105 | + } |
106 | + return -1; | |
107 | +} | |
58118c25 WD |
108 | + |
109 | ||
110 | /* | |
111 | * Acts on file number @p i from @p flist, whose name is @p fname. | |
dd39e657 WD |
112 | @@ -256,11 +343,11 @@ static void generate_and_send_sums(int f |
113 | static void recv_generator(char *fname, struct file_struct *file, int i, | |
114 | int f_out, int f_out_name) | |
115 | { | |
116 | - int fd = -1, f_copy; | |
117 | + int fd = -1, f_copy = -1; | |
118 | STRUCT_STAT st, partial_st; | |
119 | - struct file_struct *back_file; | |
120 | + struct file_struct *back_file = NULL; | |
121 | int statret, stat_errno; | |
122 | - char *fnamecmp, *partialptr, *backupptr; | |
123 | + char *fnamecmp, *partialptr, *backupptr = NULL; | |
124 | char fnamecmpbuf[MAXPATHLEN]; | |
125 | uchar fnamecmp_type; | |
126 | ||
127 | @@ -468,6 +555,15 @@ static void recv_generator(char *fname, | |
f48a237e WD |
128 | } else |
129 | partialptr = NULL; | |
824abc86 | 130 | |
09fb2223 WD |
131 | + if (statret == -1 && fuzzy_basis) { |
132 | + if (find_fuzzy(fname, fnamecmpbuf, &st) == 0 | |
133 | + && S_ISREG(st.st_mode)) { | |
134 | + statret = 0; | |
135 | + fnamecmp = fnamecmpbuf; | |
136 | + fnamecmp_type = G2R_FUZZY; | |
137 | + } | |
824abc86 WD |
138 | + } |
139 | + | |
09fb2223 WD |
140 | if (statret == -1) { |
141 | if (preserve_hard_links && hard_link_check(file, HL_SKIP)) | |
7628f156 | 142 | return; |
dd39e657 | 143 | @@ -494,7 +590,7 @@ static void recv_generator(char *fname, |
241013b4 MP |
144 | return; |
145 | } | |
146 | ||
ec8473c6 | 147 | - if (skip_file(fnamecmp, file, &st)) { |
09fb2223 WD |
148 | + if (fnamecmp_type != G2R_FUZZY && skip_file(fnamecmp, file, &st)) { |
149 | if (fnamecmp_type == G2R_FNAME) | |
8c5b8235 WD |
150 | set_perms(fname, file, &st, PERMS_REPORT); |
151 | return; | |
dd39e657 WD |
152 | @@ -553,10 +649,6 @@ prepare_to_open: |
153 | return; | |
154 | } | |
155 | fnamecmp_type = G2R_BACKUP; | |
156 | - } else { | |
157 | - backupptr = NULL; | |
158 | - back_file = NULL; | |
159 | - f_copy = -1; | |
160 | } | |
161 | ||
162 | if (verbose > 3) { | |
163 | @@ -570,6 +662,21 @@ prepare_to_open: | |
09fb2223 WD |
164 | notify_others: |
165 | if (f_out_name >= 0) { | |
166 | write_byte(f_out_name, fnamecmp_type); | |
167 | + if (fnamecmp_type == G2R_FUZZY) { | |
168 | + uchar lenbuf[3], *lb = lenbuf; | |
169 | + int len = strlen(fnamecmpbuf); | |
170 | + if (len > 0x7F) { | |
171 | +#if MAXPATHLEN > 0x7FFF | |
172 | + *lb++ = len / 0x10000 + 0x80; | |
173 | + *lb++ = len / 0x100; | |
174 | +#else | |
175 | + *lb++ = len / 0x100 + 0x80; | |
176 | +#endif | |
177 | + } | |
178 | + *lb = len; | |
179 | + write_buf(f_out_name, lenbuf, lb - lenbuf + 1); | |
180 | + write_buf(f_out_name, fnamecmpbuf, len); | |
181 | + } | |
182 | io_flush(NORMAL_FLUSH); /* XXX make this more efficient! */ | |
183 | } | |
184 | ||
2f5fa77e WD |
185 | --- orig/main.c 2004-07-22 00:10:43 |
186 | +++ main.c 2004-07-22 00:32:31 | |
d5753a22 | 187 | @@ -48,6 +48,7 @@ extern int keep_dirlinks; |
495f1899 WD |
188 | extern int preserve_hard_links; |
189 | extern int protocol_version; | |
190 | extern int recurse; | |
09fb2223 | 191 | +extern int fuzzy_basis; |
495f1899 WD |
192 | extern int relative_paths; |
193 | extern int rsync_port; | |
194 | extern int whole_file; | |
f635ed27 | 195 | @@ -463,7 +464,7 @@ static int do_recv(int f_in,int f_out,st |
495f1899 WD |
196 | int pid; |
197 | int status = 0; | |
198 | int error_pipe[2], name_pipe[2]; | |
c2530f70 | 199 | - BOOL need_name_pipe = compare_dest && !dry_run; |
09fb2223 | 200 | + BOOL need_name_pipe = (compare_dest || fuzzy_basis) && !dry_run; |
495f1899 | 201 | |
d5753a22 WD |
202 | /* The receiving side mustn't obey this, or an existing symlink that |
203 | * points to an identical file won't be replaced by the referent. */ | |
dd39e657 WD |
204 | --- orig/options.c 2004-09-22 08:47:31 |
205 | +++ options.c 2004-09-23 14:59:46 | |
f635ed27 | 206 | @@ -85,6 +85,7 @@ int safe_symlinks = 0; |
f6c3b300 WD |
207 | int copy_unsafe_links = 0; |
208 | int size_only = 0; | |
209 | int bwlimit = 0; | |
09fb2223 | 210 | +int fuzzy_basis = 0; |
f6c3b300 WD |
211 | size_t bwlimit_writemax = 0; |
212 | int delete_after = 0; | |
213 | int only_existing = 0; | |
f635ed27 | 214 | @@ -279,6 +280,7 @@ void usage(enum logcode F) |
c2530f70 | 215 | rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n"); |
f0533c4c WD |
216 | rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n"); |
217 | rprintf(F," --link-dest=DIR create hardlinks to DIR for unchanged files\n"); | |
218 | + rprintf(F," --fuzzy use similar file as basis if basis doesn't exist\n"); | |
219 | rprintf(F," -P equivalent to --partial --progress\n"); | |
220 | rprintf(F," -z, --compress compress file data\n"); | |
221 | rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n"); | |
f635ed27 | 222 | @@ -378,6 +380,7 @@ static struct poptOption long_options[] |
f0533c4c WD |
223 | {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 }, |
224 | {"compare-dest", 0, POPT_ARG_STRING, &compare_dest, 0, 0, 0 }, | |
225 | {"link-dest", 0, POPT_ARG_STRING, &compare_dest, OPT_LINK_DEST, 0, 0 }, | |
09fb2223 | 226 | + {"fuzzy", 0, POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 }, |
f0533c4c WD |
227 | /* TODO: Should this take an optional int giving the compression level? */ |
228 | {"compress", 'z', POPT_ARG_NONE, &do_compression, 0, 0, 0 }, | |
229 | {"daemon", 0, POPT_ARG_NONE, &daemon_opt, 0, 0, 0 }, | |
dd39e657 WD |
230 | @@ -833,6 +836,11 @@ int parse_arguments(int *argc, const cha |
231 | link_dest ? "--link-dest" : "--compare-dest"); | |
232 | return 0; | |
233 | } | |
234 | + if (compare_dest) { | |
235 | + snprintf(err_buf, sizeof err_buf, | |
236 | + "--inplace does not yet work with --fuzzy\n"); | |
237 | + return 0; | |
238 | + } | |
239 | } else { | |
240 | if (keep_partial && !partial_dir) | |
241 | partial_dir = getenv("RSYNC_PARTIAL_DIR"); | |
242 | @@ -1099,6 +1107,9 @@ void server_options(char **args,int *arg | |
f74d2272 | 243 | } |
241013b4 | 244 | } |
7b675ff5 | 245 | |
09fb2223 | 246 | + if (fuzzy_basis && am_sender) |
241013b4 | 247 | + args[ac++] = "--fuzzy"; |
7b675ff5 | 248 | + |
241013b4 | 249 | *argc = ac; |
f74d2272 | 250 | return; |
7b675ff5 | 251 | |
f48a237e | 252 | --- orig/receiver.c 2004-09-07 21:57:20 |
09fb2223 | 253 | +++ receiver.c 2004-07-30 18:21:38 |
a6587818 | 254 | @@ -319,6 +319,27 @@ static int receive_data(int f_in, char * |
09fb2223 WD |
255 | } |
256 | ||
257 | ||
258 | +static void read_gen_name(int fd, char *buf) | |
259 | +{ | |
260 | + int len = read_byte(fd); | |
261 | + if (len & 0x80) { | |
262 | +#if MAXPATHLEN > 32767 | |
263 | + uchar lenbuf[2]; | |
264 | + read_buf(fd, (char *)lenbuf, 2); | |
265 | + len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1]; | |
266 | +#else | |
267 | + len = (len & ~0x80) * 0x100 + read_byte(fd); | |
268 | +#endif | |
269 | + } | |
270 | + if (len >= MAXPATHLEN) { | |
271 | + rprintf(FERROR, "bogus data on generator name pipe\n"); | |
272 | + exit_cleanup(RERR_PROTOCOL); | |
273 | + } | |
274 | + | |
275 | + read_sbuf(fd, buf, len); | |
276 | +} | |
277 | + | |
278 | + | |
279 | static void discard_receive_data(int f_in, OFF_T length) | |
280 | { | |
281 | receive_data(f_in, NULL, -1, 0, NULL, -1, length); | |
a6587818 | 282 | @@ -448,6 +469,10 @@ int recv_files(int f_in, struct file_lis |
f48a237e WD |
283 | case G2R_BACKUP: |
284 | fnamecmp = get_backup_name(fname); | |
09fb2223 WD |
285 | break; |
286 | + case G2R_FUZZY: | |
287 | + read_gen_name(f_in_name, fnamecmpbuf); | |
288 | + fnamecmp = fnamecmpbuf; | |
289 | + break; | |
290 | default: | |
291 | pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, | |
292 | compare_dest, fname); | |
f48a237e WD |
293 | --- orig/rsync.h 2004-09-07 21:52:22 |
294 | +++ rsync.h 2004-09-07 22:02:37 | |
dd39e657 | 295 | @@ -122,6 +122,7 @@ |
09fb2223 WD |
296 | #define G2R_FNAME 0x80 |
297 | #define G2R_PARTIAL_DIR 0x81 | |
f48a237e WD |
298 | #define G2R_BACKUP 0x82 |
299 | +#define G2R_FUZZY 0x83 | |
09fb2223 WD |
300 | |
301 | ||
302 | /* Log-message categories. FLOG is only used on the daemon side to | |
dd39e657 | 303 | --- orig/rsync.yo 2004-09-22 08:47:31 |
824abc86 | 304 | +++ rsync.yo 2004-07-03 19:27:25 |
67c08134 | 305 | @@ -356,6 +356,7 @@ verb( |
f0533c4c WD |
306 | -T --temp-dir=DIR create temporary files in directory DIR |
307 | --compare-dest=DIR also compare received files relative to DIR | |
308 | --link-dest=DIR create hardlinks to DIR for unchanged files | |
309 | + --fuzzy use similar file as basis if basis is gone | |
310 | -P equivalent to --partial --progress | |
311 | -z, --compress compress file data | |
312 | -C, --cvs-exclude auto ignore files in the same way CVS does |