3 I greatly simplified the changes to generator.c, making the patch
4 easier to maintain and fixing the failing test in the testsuite.
5 Very lightly tested (by me).
7 --- Makefile.in 15 May 2004 00:48:11 -0000 1.101
8 +++ Makefile.in 29 Jun 2004 17:46:12 -0000
9 @@ -32,7 +32,7 @@ ZLIBOBJ=zlib/deflate.o zlib/infblock.o z
10 zlib/inflate.o zlib/inftrees.o zlib/infutil.o zlib/trees.o \
11 zlib/zutil.o zlib/adler32.o
12 OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
13 - main.o checksum.o match.o syscall.o log.o backup.o
14 + main.o checksum.o match.o syscall.o log.o backup.o alternate.o
15 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
16 fileio.o batch.o clientname.o
17 OBJS3=progress.o pipe.o
18 --- /dev/null 1 Jan 1970 00:00:00 -0000
19 +++ alternate.c 29 Jun 2004 17:46:12 -0000
23 +extern char *compare_dest;
26 +/* Alternate methods for opening files, if local doesn't exist */
27 +/* Sanity check that we are about to open regular file */
28 +static int do_open_regular(char *fname)
32 + if (do_stat(fname, &st) == 0 && S_ISREG(st.st_mode))
33 + return do_open(fname, O_RDONLY, 0);
38 +static void split_names(char *fname, char **dirname, char **basename)
40 + char *slash = strrchr(fname, '/');
44 + *basename = slash+1;
51 +static unsigned int measure_name(const char *name, const char *basename,
54 + int namelen = strlen(name);
55 + int extlen = strlen(ext);
56 + unsigned int score = 0;
58 + /* Extensions must match */
59 + if (namelen <= extlen || strcmp(name + namelen - extlen, ext) != 0)
62 + /* Now score depends on similarity of prefix */
63 + for (; *name == *basename && *name; name++, basename++)
68 +int check_alternate_base_fuzzy(char **fname_ptr, char *buf,
69 + STRUCT_STAT *st_ptr)
73 + char *basename, *dirname;
74 + char mangled_name[MAXPATHLEN];
75 + char bestname[MAXPATHLEN];
76 + unsigned int bestscore = 0;
79 + strlcpy(mangled_name, *fname_ptr, sizeof mangled_name);
81 + split_names(mangled_name, &dirname, &basename);
82 + if (!(d = opendir(dirname))) {
83 + rsyserr(FERROR, errno, "recv_generator opendir(%s)", dirname);
87 + /* Get final extension, eg. .gz; never full basename though. */
88 + ext = strrchr(basename + 1, '.');
90 + ext = basename + strlen(basename); /* ext = "" */
92 + while ((di = readdir(d)) != NULL) {
93 + const char *dname = d_name(di);
96 + if (dname[0] == '.' && (dname[1] == '\0'
97 + || (dname[1] == '.' && dname[2] == '\0')))
100 + score = measure_name(dname, basename, ext);
102 + rprintf(FINFO, "[%s] fuzzy score for %s = %u\n",
103 + who_am_i(), dname, score);
105 + if (score > bestscore) {
106 + strlcpy(bestname, dname, sizeof bestname);
112 + /* Found a candidate. */
113 + if (bestscore != 0) {
114 + pathjoin(buf, MAXPATHLEN, dirname, bestname);
116 + rprintf(FINFO, "[%s] fuzzy match %s->%s\n",
117 + who_am_i(), *fname_ptr, buf);
121 + return link_stat(buf, st_ptr, 0);
122 + return do_open_regular(buf);
126 --- generator.c 29 Jun 2004 16:22:54 -0000 1.91
127 +++ generator.c 29 Jun 2004 17:46:12 -0000
128 @@ -41,6 +41,7 @@ extern int ignore_times;
129 extern int size_only;
130 extern int io_timeout;
131 extern int protocol_version;
133 extern int always_checksum;
134 extern char *compare_dest;
135 extern int link_dest;
136 @@ -271,7 +272,7 @@ void recv_generator(char *fname, struct
139 struct map_struct *mapbuf;
141 + int statret, fuzzy_file = 0;
143 char fnamecmpbuf[MAXPATHLEN];
145 @@ -447,6 +448,15 @@ void recv_generator(char *fname, struct
146 fnamecmp = fnamecmpbuf;
149 + if (statret == -1 && fuzzy) {
150 + statret = check_alternate_base_fuzzy(&fnamecmp, fnamecmpbuf,
152 + if (!S_ISREG(st.st_mode))
159 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
161 @@ -489,7 +499,7 @@ void recv_generator(char *fname, struct
165 - if (skip_file(fname, file, &st)) {
166 + if (!fuzzy_file && skip_file(fname, file, &st)) {
167 if (fnamecmp == fname)
168 set_perms(fname, file, &st, PERMS_REPORT);
170 --- options.c 20 Jun 2004 19:47:05 -0000 1.157
171 +++ options.c 29 Jun 2004 17:46:13 -0000
172 @@ -94,6 +94,7 @@ int ignore_errors = 0;
173 int modify_window = 0;
174 int blocking_io = -1;
175 int checksum_seed = 0;
177 unsigned int block_size = 0;
180 @@ -270,6 +271,7 @@ void usage(enum logcode F)
181 rprintf(F," -T --temp-dir=DIR create temporary files in directory DIR\n");
182 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
183 rprintf(F," --link-dest=DIR create hardlinks to DIR for unchanged files\n");
184 + rprintf(F," --fuzzy use similar file as basis if basis doesn't exist\n");
185 rprintf(F," -P equivalent to --partial --progress\n");
186 rprintf(F," -z, --compress compress file data\n");
187 rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n");
188 @@ -368,6 +370,7 @@ static struct poptOption long_options[]
189 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
190 {"compare-dest", 0, POPT_ARG_STRING, &compare_dest, 0, 0, 0 },
191 {"link-dest", 0, POPT_ARG_STRING, &compare_dest, OPT_LINK_DEST, 0, 0 },
192 + {"fuzzy", 0, POPT_ARG_NONE, &fuzzy, 0, 0, 0 },
193 /* TODO: Should this take an optional int giving the compression level? */
194 {"compress", 'z', POPT_ARG_NONE, &do_compression, 0, 0, 0 },
195 {"daemon", 0, POPT_ARG_NONE, &daemon_opt, 0, 0, 0 },
196 @@ -989,6 +992,9 @@ void server_options(char **args,int *arg
200 + if (fuzzy && am_sender)
201 + args[ac++] = "--fuzzy";
206 --- receiver.c 29 Jun 2004 15:12:01 -0000 1.83
207 +++ receiver.c 29 Jun 2004 17:46:13 -0000
208 @@ -48,6 +48,7 @@ extern int ignore_errors;
209 extern int orig_umask;
210 extern int keep_partial;
211 extern int checksum_seed;
214 static void delete_one(char *fn, int is_dir)
216 @@ -377,6 +378,11 @@ int recv_files(int f_in,struct file_list
217 fd1 = do_open(fnamecmp, O_RDONLY, 0);
220 + if (fd1 == -1 && fuzzy) {
221 + fd1 = check_alternate_base_fuzzy(&fnamecmp, fnamecmpbuf,
225 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
226 rsyserr(FERROR, errno, "fstat %s failed",
227 full_fname(fnamecmp));
228 --- rsync.yo 5 Jun 2004 16:16:30 -0000 1.171
229 +++ rsync.yo 29 Jun 2004 17:46:14 -0000
230 @@ -325,6 +325,7 @@ verb(
231 -T --temp-dir=DIR create temporary files in directory DIR
232 --compare-dest=DIR also compare received files relative to DIR
233 --link-dest=DIR create hardlinks to DIR for unchanged files
234 + --fuzzy use similar file as basis if basis is gone
235 -P equivalent to --partial --progress
236 -z, --compress compress file data
237 -C, --cvs-exclude auto ignore files in the same way CVS does