1 CAUTION: This patch compiles, but is otherwise totally untested!
3 This patch also implements --times-only.
5 Implementation details for the --source-filter and -dest-filter options:
7 - These options open a *HUGE* security hole in daemon mode unless they
8 are refused in your rsyncd.conf!
10 - Filtering disables rsync alogrithm. (This should be fixed.)
12 - Source filter makes temporary files in /tmp. (Should be overridable.)
14 - If source filter fails, data is send unfiltered. (Should be changed
17 - Failure of destination filter, causes data loss!!! (Should be changed
20 - If filter changes size of file, you should use --times-only option to
21 prevent repeated transfers of unchanged files.
23 - If the COMMAND contains single quotes, option-passing breaks. (Needs
26 You should run "make proto" before running "make".
28 --- orig/generator.c 2006-01-20 21:12:17
29 +++ generator.c 2005-08-17 07:28:01
30 @@ -59,6 +59,7 @@ extern int append_mode;
31 extern int make_backups;
32 extern int csum_length;
33 extern int ignore_times;
34 +extern int times_only;
36 extern OFF_T max_size;
37 extern OFF_T min_size;
38 @@ -378,7 +379,7 @@ void itemize(struct file_struct *file, i
39 /* Perform our quick-check heuristic for determining if a file is unchanged. */
40 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
42 - if (st->st_size != file->length)
43 + if (!times_only && st->st_size != file->length)
46 /* if always checksum is set then we use the checksum instead
47 --- orig/options.c 2006-01-21 07:55:00
48 +++ options.c 2006-01-21 08:09:48
49 @@ -93,6 +93,7 @@ int keep_partial = 0;
50 int safe_symlinks = 0;
51 int copy_unsafe_links = 0;
54 int daemon_bwlimit = 0;
57 @@ -142,6 +143,8 @@ char *basis_dir[MAX_BASIS_DIRS+1];
58 char *config_file = NULL;
59 char *shell_cmd = NULL;
60 char *log_format = NULL;
61 +char *source_filter = NULL;
62 +char *dest_filter = NULL;
63 char *password_file = NULL;
64 char *rsync_path = RSYNC_PATH;
65 char *backup_dir = NULL;
66 @@ -321,6 +324,7 @@ void usage(enum logcode F)
67 rprintf(F," --timeout=TIME set I/O timeout in seconds\n");
68 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
69 rprintf(F," --size-only skip files that match in size\n");
70 + rprintf(F," --times-only skip files that match in mod-time\n");
71 rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
72 rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
73 rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
74 @@ -355,6 +359,8 @@ void usage(enum logcode F)
75 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
76 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
77 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
78 + rprintf(F," --source-filter=COMMAND filter file through COMMAND at source\n");
79 + rprintf(F," --dest-filter=COMMAND filter file through COMMAND at destination\n");
80 rprintf(F," --protocol=NUM force an older protocol version to be used\n");
82 rprintf(F," -4, --ipv4 prefer IPv4\n");
83 @@ -425,6 +431,7 @@ static struct poptOption long_options[]
84 {"chmod", 0, POPT_ARG_STRING, &chmod_mode, 0, 0, 0 },
85 {"ignore-times", 'I', POPT_ARG_NONE, &ignore_times, 0, 0, 0 },
86 {"size-only", 0, POPT_ARG_NONE, &size_only, 0, 0, 0 },
87 + {"times-only", 0, POPT_ARG_NONE, ×_only , 0, 0, 0 },
88 {"one-file-system", 'x', POPT_ARG_NONE, 0, 'x', 0, 0 },
89 {"update", 'u', POPT_ARG_NONE, &update_only, 0, 0, 0 },
90 {"existing", 0, POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
91 @@ -496,6 +503,8 @@ static struct poptOption long_options[]
92 {"password-file", 0, POPT_ARG_STRING, &password_file, 0, 0, 0 },
93 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
94 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
95 + {"source-filter", 0, POPT_ARG_STRING, &source_filter, 0, 0, 0 },
96 + {"dest-filter", 0, POPT_ARG_STRING, &dest_filter, 0, 0, 0 },
97 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
98 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
99 {"server", 0, POPT_ARG_NONE, &am_server, 0, 0, 0 },
100 @@ -1331,6 +1340,16 @@ int parse_arguments(int *argc, const cha
104 + if (source_filter || dest_filter) {
105 + if (whole_file == 0) {
106 + snprintf(err_buf, sizeof err_buf,
107 + "--no-whole-file cannot be used with --%s-filter\n",
108 + source_filter ? "source" : "dest");
117 @@ -1570,6 +1589,25 @@ void server_options(char **args,int *arg
118 args[ac++] = "--only-write-batch=X";
121 + if (times_only && am_sender)
122 + args[ac++] = "--times-only";
124 + if (source_filter && !am_sender) {
125 + /* Need to single quote the arg to keep the remote shell
126 + * from splitting it. FIXME: breaks if command has single quotes. */
127 + if (asprintf(&arg, "--source-filter='%s'", source_filter) < 0)
132 + if (dest_filter && am_sender) {
133 + /* Need to single quote the arg to keep the remote shell
134 + * from splitting it. FIXME: breaks if command has single quotes. */
135 + if (asprintf(&arg, "--dest-filter='%s'", dest_filter) < 0)
141 args[ac++] = "--size-only";
143 --- orig/pipe.c 2006-01-21 08:03:40
144 +++ pipe.c 2006-01-14 08:34:59
145 @@ -157,3 +157,77 @@ pid_t local_child(int argc, char **argv,
150 +pid_t run_filter(char *command[], int out, int *pipe_to_filter)
156 + print_child_argv(command);
158 + if (pipe(pipefds) < 0) {
159 + rsyserr(FERROR, errno, "pipe");
160 + exit_cleanup(RERR_IPC);
165 + rsyserr(FERROR, errno, "fork");
166 + exit_cleanup(RERR_IPC);
170 + if (dup2(pipefds[0], STDIN_FILENO) < 0
171 + || close(pipefds[1]) < 0
172 + || dup2(out, STDOUT_FILENO) < 0) {
173 + rsyserr(FERROR, errno, "Failed dup/close");
174 + exit_cleanup(RERR_IPC);
177 + set_blocking(STDIN_FILENO);
179 + set_blocking(STDOUT_FILENO);
180 + execvp(command[0], command);
181 + rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
182 + exit_cleanup(RERR_IPC);
185 + if (close(pipefds[0]) < 0) {
186 + rsyserr(FERROR, errno, "Failed to close");
187 + exit_cleanup(RERR_IPC);
190 + *pipe_to_filter = pipefds[1];
195 +pid_t run_filter_on_file(char *command[], int out, int in)
200 + print_child_argv(command);
204 + rsyserr(FERROR, errno, "fork");
205 + exit_cleanup(RERR_IPC);
209 + if (dup2(in, STDIN_FILENO) < 0
210 + || dup2(out, STDOUT_FILENO) < 0) {
211 + rsyserr(FERROR, errno, "Failed to dup2");
212 + exit_cleanup(RERR_IPC);
215 + set_blocking(STDOUT_FILENO);
216 + execvp(command[0], command);
217 + rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
218 + exit_cleanup(RERR_IPC);
223 --- orig/receiver.c 2006-01-14 20:27:09
224 +++ receiver.c 2005-08-17 07:57:33
225 @@ -53,6 +53,7 @@ extern int inplace;
226 extern int delay_updates;
227 extern struct stats stats;
228 extern char *log_format;
229 +extern char *dest_filter;
231 extern char *partial_dir;
232 extern char *basis_dir[];
233 @@ -411,6 +412,8 @@ int recv_files(int f_in, struct file_lis
234 : !am_server && log_format_has_i;
235 int max_phase = protocol_version >= 29 ? 2 : 1;
238 + char *filter_argv[MAX_FILTER_ARGS + 1];
241 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
242 @@ -423,6 +426,23 @@ int recv_files(int f_in, struct file_lis
244 init_delayed_bits(flist->count);
250 + for (p = strtok(dest_filter, sep), i = 0;
251 + p && i < MAX_FILTER_ARGS;
252 + p = strtok(0, sep))
253 + filter_argv[i++] = p;
254 + filter_argv[i] = NULL;
257 + "Too many arguments to dest-filter (> %d)\n",
259 + exit_cleanup(RERR_SYNTAX);
266 @@ -665,6 +685,9 @@ int recv_files(int f_in, struct file_lis
267 else if (!am_server && verbose && do_progress)
268 rprintf(FINFO, "%s\n", fname);
271 + pid = run_filter(filter_argv, fd2, &fd2);
274 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
275 fname, fd2, file->length);
276 @@ -680,6 +703,16 @@ int recv_files(int f_in, struct file_lis
277 exit_cleanup(RERR_FILEIO);
282 + wait_process(pid, &status);
284 + rprintf(FERROR, "filter %s exited code: %d\n",
285 + dest_filter, status);
290 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
291 finish_transfer(fname, fnametmp, file, recv_ok, 1);
292 if (partialptr != fname && fnamecmp == partialptr) {
293 --- orig/rsync.h 2006-01-14 20:27:10
294 +++ rsync.h 2005-08-17 07:10:11
296 #define IOERR_DEL_LIMIT (1<<2)
298 #define MAX_ARGS 1000
299 +#define MAX_FILTER_ARGS 100
300 #define MAX_BASIS_DIRS 20
301 #define MAX_SERVER_ARGS (MAX_BASIS_DIRS*2 + 100)
303 --- orig/rsync.yo 2006-01-21 08:12:23
304 +++ rsync.yo 2005-08-17 07:08:21
305 @@ -350,6 +350,7 @@ to the detailed description below for a
306 --timeout=TIME set I/O timeout in seconds
307 -I, --ignore-times don't skip files that match size and time
308 --size-only skip files that match in size
309 + --times-only skip files that match in mod-time
310 --modify-window=NUM compare mod-times with reduced accuracy
311 -T, --temp-dir=DIR create temporary files in directory DIR
312 -y, --fuzzy find similar file for basis if no dest file
313 @@ -384,6 +385,8 @@ to the detailed description below for a
314 --write-batch=FILE write a batched update to FILE
315 --only-write-batch=FILE like --write-batch but w/o updating dest
316 --read-batch=FILE read a batched update from FILE
317 + --source-filter=COMMAND filter file through COMMAND at source
318 + --dest-filter=COMMAND filter file through COMMAND at destination
319 --protocol=NUM force an older protocol version to be used
320 --checksum-seed=NUM set block/file checksum seed (advanced)
321 -4, --ipv4 prefer IPv4
322 @@ -1430,6 +1433,33 @@ file previously generated by bf(--write-
323 If em(FILE) is bf(-), the batch data will be read from standard input.
324 See the "BATCH MODE" section for details.
326 +dit(bf(--source-filter=COMMAND)) This option allows the user to specify a
327 +filter program that will be applied to the contents of all transferred
328 +regular files before the data is sent to destination. COMMAND will receive
329 +the data on its standard input and it should write the filtered data to
330 +standard output. COMMAND should exit non-zero if it cannot process the
331 +data or if it encounters an error when writing the data to stdout.
333 +Example: --source-filter="gzip -9" will cause remote files to be
335 +Use of --source-filter automatically enables --whole-file.
336 +If your filter does not output the same number of bytes that it received on
337 +input, you should use --times-only to disable size and content checks on
338 +subsequent rsync runs.
340 +dit(bf(--dest-filter=COMMAND)) This option allows you to specify a filter
341 +program that will be applied to the contents of all transferred regular
342 +files before the data is written to disk. COMMAND will receive the data on
343 +its standard input and it should write the filtered data to standard
344 +output. COMMAND should exit non-zero if it cannot process the data or if
345 +it encounters an error when writing the data to stdout.
347 +Example: --dest-filter="gzip -9" will cause remote files to be compressed.
348 +Use of --dest-filter automatically enables --whole-file.
349 +If your filter does not output the same number of bytes that it
350 +received on input, you should use --times-only to disable size and
351 +content checks on subsequent rsync runs.
353 dit(bf(--protocol=NUM)) Force an older protocol version to be used. This
354 is useful for creating a batch file that is compatible with an older
355 version of rsync. For instance, if rsync 2.6.4 is being used with the
356 --- orig/sender.c 2006-01-14 20:27:10
357 +++ sender.c 2005-08-17 07:40:49
358 @@ -41,6 +41,7 @@ extern int write_batch;
359 extern struct stats stats;
360 extern struct file_list *the_file_list;
361 extern char *log_format;
362 +extern char *source_filter;
366 @@ -219,6 +220,26 @@ void send_files(struct file_list *flist,
367 : !am_server && log_format_has_i;
368 int f_xfer = write_batch < 0 ? batch_fd : f_out;
370 + char *filter_argv[MAX_FILTER_ARGS + 1];
372 + int unlink_tmp = 0;
374 + if (source_filter) {
378 + for (p = strtok(source_filter, sep), i = 0;
379 + p && i < MAX_FILTER_ARGS;
380 + p = strtok(0, sep))
381 + filter_argv[i++] = p;
382 + filter_argv[i] = NULL;
385 + "Too many arguments to source-filter (> %d)\n",
387 + exit_cleanup(RERR_SYNTAX);
392 rprintf(FINFO, "send_files starting\n");
393 @@ -293,6 +314,7 @@ void send_files(struct file_list *flist,
398 fd = do_open(fname, O_RDONLY, 0);
400 if (errno == ENOENT) {
401 @@ -321,6 +343,33 @@ void send_files(struct file_list *flist,
405 + if (source_filter) {
407 + char *tmpl = "/tmp/rsync-filtered_sourceXXXXXX";
409 + tmp = strdup(tmpl);
410 + fd2 = mkstemp(tmp);
412 + rprintf(FERROR, "mkstemp %s failed: %s\n",
413 + tmp, strerror(errno));
416 + pid_t pid = run_filter_on_file(filter_argv, fd2, fd);
419 + wait_process(pid, &status);
422 + "bypassing source filter %s; exited with code: %d\n",
423 + source_filter, status);
424 + fd = do_open(fname, O_RDONLY, 0);
426 + fd = do_open(tmp, O_RDONLY, 0);
433 int32 read_size = MAX(s->blength * 3, MAX_MAP_SIZE);
434 mbuf = map_file(fd, st.st_size, read_size, s->blength);
435 @@ -363,6 +412,8 @@ void send_files(struct file_list *flist,