1 The changes to generator.c were greatly simplified, making the patch
2 easier to maintain and fixing the failing test in the testsuite.
5 Be sure to run "make proto" before "make".
7 --- orig/generator.c 2004-11-03 20:30:45
8 +++ generator.c 2004-11-03 22:52:53
9 @@ -43,6 +43,7 @@ extern int ignore_times;
11 extern int io_timeout;
12 extern int protocol_version;
13 +extern int fuzzy_basis;
14 extern int always_checksum;
15 extern char *partial_dir;
16 extern char *compare_dest;
17 @@ -244,6 +245,92 @@ static void generate_and_send_sums(int f
21 +static void split_names(char *fname, char **dirname, char **basename)
23 + char *slash = strrchr(fname, '/');
27 + *basename = slash+1;
35 +static unsigned int measure_name(const char *name, const char *basename,
38 + int namelen = strlen(name);
39 + int extlen = strlen(ext);
40 + unsigned int score = 0;
42 + /* Extensions must match */
43 + if (namelen <= extlen || strcmp(name + namelen - extlen, ext) != 0)
46 + /* Now score depends on similarity of prefix */
47 + for (; *name == *basename && *name; name++, basename++)
53 +static int find_fuzzy(const char *fname, char *buf, STRUCT_STAT *st_ptr)
57 + char *basename, *dirname;
58 + char mangled_name[MAXPATHLEN];
59 + char bestname[MAXPATHLEN];
60 + unsigned int bestscore = 0;
63 + strlcpy(mangled_name, fname, sizeof mangled_name);
65 + split_names(mangled_name, &dirname, &basename);
66 + if (!(d = opendir(dirname))) {
67 + rsyserr(FERROR, errno, "recv_generator opendir(%s)", dirname);
71 + /* Get final extension, eg. .gz; never full basename though. */
72 + if (!(ext = strrchr(basename + 1, '.')))
73 + ext = basename + strlen(basename); /* ext = "" */
75 + while ((di = readdir(d)) != NULL) {
76 + const char *dname = d_name(di);
79 + if (dname[0] == '.' && (dname[1] == '\0'
80 + || (dname[1] == '.' && dname[2] == '\0')))
83 + score = measure_name(dname, basename, ext);
85 + rprintf(FINFO, "[%s] fuzzy score for %s = %u\n",
86 + who_am_i(), dname, score);
88 + if (score > bestscore) {
89 + strlcpy(bestname, dname, sizeof bestname);
95 + /* Found a candidate. */
96 + if (bestscore != 0) {
97 + pathjoin(buf, MAXPATHLEN, dirname, bestname);
99 + rprintf(FINFO, "[%s] fuzzy match %s->%s\n",
100 + who_am_i(), fname, buf);
102 + return link_stat(buf, st_ptr, 0);
109 * Acts on file number @p i from @p flist, whose name is @p fname.
110 @@ -466,6 +553,15 @@ static void recv_generator(char *fname,
114 + if (statret == -1 && fuzzy_basis) {
115 + if (find_fuzzy(fname, fnamecmpbuf, &st) == 0
116 + && S_ISREG(st.st_mode)) {
118 + fnamecmp = fnamecmpbuf;
119 + fnamecmp_type = FNAMECMP_FUZZY;
124 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
126 @@ -492,7 +588,7 @@ static void recv_generator(char *fname,
130 - if (skip_file(fnamecmp, file, &st)) {
131 + if (fnamecmp_type != FNAMECMP_FUZZY && skip_file(fnamecmp, file, &st)) {
132 if (fnamecmp_type == FNAMECMP_FNAME)
133 set_perms(fname, file, &st, PERMS_REPORT);
135 @@ -563,8 +659,24 @@ prepare_to_open:
139 - if (f_out_name >= 0)
140 + if (f_out_name >= 0) {
141 write_byte(f_out_name, fnamecmp_type);
142 + if (fnamecmp_type == FNAMECMP_FUZZY) {
143 + uchar lenbuf[3], *lb = lenbuf;
144 + int len = strlen(fnamecmpbuf);
146 +#if MAXPATHLEN > 0x7FFF
147 + *lb++ = len / 0x10000 + 0x80;
148 + *lb++ = len / 0x100;
150 + *lb++ = len / 0x100 + 0x80;
154 + write_buf(f_out_name, lenbuf, lb - lenbuf + 1);
155 + write_buf(f_out_name, fnamecmpbuf, len);
159 if (dry_run || read_batch)
161 --- orig/main.c 2004-11-03 20:30:45
162 +++ main.c 2004-07-22 00:32:31
163 @@ -48,6 +48,7 @@ extern int keep_dirlinks;
164 extern int preserve_hard_links;
165 extern int protocol_version;
167 +extern int fuzzy_basis;
168 extern int relative_paths;
169 extern int rsync_port;
170 extern int whole_file;
171 @@ -463,7 +464,7 @@ static int do_recv(int f_in,int f_out,st
174 int error_pipe[2], name_pipe[2];
175 - BOOL need_name_pipe = compare_dest && !dry_run;
176 + BOOL need_name_pipe = (compare_dest || fuzzy_basis) && !dry_run;
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. */
180 --- orig/options.c 2004-10-14 17:11:40
181 +++ options.c 2004-10-14 17:21:51
182 @@ -85,6 +85,7 @@ int safe_symlinks = 0;
183 int copy_unsafe_links = 0;
186 +int fuzzy_basis = 0;
187 size_t bwlimit_writemax = 0;
188 int delete_after = 0;
189 int only_existing = 0;
190 @@ -279,6 +280,7 @@ void usage(enum logcode F)
191 rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
192 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
193 rprintf(F," --link-dest=DIR create hardlinks to DIR for unchanged files\n");
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");
198 @@ -371,6 +373,7 @@ static struct poptOption long_options[]
199 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
200 {"compare-dest", 0, POPT_ARG_STRING, &compare_dest, 0, 0, 0 },
201 {"link-dest", 0, POPT_ARG_STRING, &compare_dest, OPT_LINK_DEST, 0, 0 },
202 + {"fuzzy", 0, POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 },
203 /* TODO: Should this take an optional int giving the compression level? */
204 {"compress", 'z', POPT_ARG_NONE, &do_compression, 0, 0, 0 },
205 {"stats", 0, POPT_ARG_NONE, &do_stats, 0, 0, 0 },
206 @@ -890,6 +893,11 @@ int parse_arguments(int *argc, const cha
207 link_dest ? "--link-dest" : "--compare-dest");
210 + if (compare_dest) {
211 + snprintf(err_buf, sizeof err_buf,
212 + "--inplace does not yet work with --fuzzy\n");
216 if (keep_partial && !partial_dir)
217 partial_dir = getenv("RSYNC_PARTIAL_DIR");
218 @@ -1156,6 +1164,9 @@ void server_options(char **args,int *arg
222 + if (fuzzy_basis && am_sender)
223 + args[ac++] = "--fuzzy";
228 --- orig/receiver.c 2004-11-03 20:30:45
229 +++ receiver.c 2004-11-03 22:54:05
230 @@ -320,6 +320,27 @@ static int receive_data(int f_in, char *
234 +static void read_gen_name(int fd, char *buf)
236 + int len = read_byte(fd);
238 +#if MAXPATHLEN > 32767
240 + read_buf(fd, (char *)lenbuf, 2);
241 + len = (len & ~0x80) * 0x10000 + lenbuf[0] * 0x100 + lenbuf[1];
243 + len = (len & ~0x80) * 0x100 + read_byte(fd);
246 + if (len >= MAXPATHLEN) {
247 + rprintf(FERROR, "bogus data on generator name pipe\n");
248 + exit_cleanup(RERR_PROTOCOL);
251 + read_sbuf(fd, buf, len);
255 static void discard_receive_data(int f_in, OFF_T length)
257 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
258 @@ -449,6 +470,10 @@ int recv_files(int f_in, struct file_lis
259 case FNAMECMP_BACKUP:
260 fnamecmp = get_backup_name(fname);
262 + case FNAMECMP_FUZZY:
263 + read_gen_name(f_in_name, fnamecmpbuf);
264 + fnamecmp = fnamecmpbuf;
266 case FNAMECMP_CMPDEST:
268 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
269 --- orig/rsync.h 2004-11-03 20:30:45
270 +++ rsync.h 2004-11-03 22:53:09
272 #define FNAMECMP_FNAME 0x80
273 #define FNAMECMP_PARTIAL_DIR 0x81
274 #define FNAMECMP_BACKUP 0x82
275 +#define FNAMECMP_FUZZY 0x83
278 /* Log-message categories. FLOG is only used on the daemon side to
279 --- orig/rsync.yo 2004-10-06 00:12:16
280 +++ rsync.yo 2004-07-03 19:27:25
281 @@ -356,6 +356,7 @@ verb(
282 -T --temp-dir=DIR create temporary files in directory DIR
283 --compare-dest=DIR also compare received files relative to DIR
284 --link-dest=DIR create hardlinks to DIR for unchanged files
285 + --fuzzy use similar file as basis if basis is gone
286 -P equivalent to --partial --progress
287 -z, --compress compress file data
288 -C, --cvs-exclude auto ignore files in the same way CVS does