Fixes from Matt.
[rsync/rsync-patches.git] / source-filter_dest-filter.diff
CommitLineData
33d38bc8
WD
1CAUTION: This patch compiles, but is otherwise totally untested!
2
3This patch also implements --times-only.
4
5Implementation details for the --source-filter and -dest-filter options:
6
7 - These options open a *HUGE* security hole in daemon mode unless they
8 are refused in your rsyncd.conf!
9
10 - Filtering disables rsync alogrithm. (This should be fixed.)
11
12 - Source filter makes temporary files in /tmp. (Should be overridable.)
13
14 - If source filter fails, data is send unfiltered. (Should be changed
15 to abort.)
16
17 - Failure of destination filter, causes data loss!!! (Should be changed
18 to abort.)
19
20 - If filter changes size of file, you should use --times-only option to
21 prevent repeated transfers of unchanged files.
22
23 - If the COMMAND contains single quotes, option-passing breaks. (Needs
24 to be fixed.)
25
03019e41 26To use this patch, run these commands for a successful build:
33d38bc8 27
03019e41 28 patch -p1 <patches/source-filter_dest-filter.diff
27e96866 29 ./prepare-source
03019e41 30 ./configure (optional if already run)
27e96866
WD
31 make
32
9a7eef96
WD
33--- old/generator.c
34+++ new/generator.c
9c85142a 35@@ -61,6 +61,7 @@ extern int append_mode;
33d38bc8
WD
36 extern int make_backups;
37 extern int csum_length;
38 extern int ignore_times;
39+extern int times_only;
40 extern int size_only;
41 extern OFF_T max_size;
93ca4d27 42 extern OFF_T min_size;
7bfcb297 43@@ -619,7 +620,7 @@ void itemize(const char *fnamecmp, struc
33d38bc8 44 /* Perform our quick-check heuristic for determining if a file is unchanged. */
93ca4d27 45 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
33d38bc8 46 {
1aa236e1
WD
47- if (st->st_size != F_LENGTH(file))
48+ if (!times_only && st->st_size != F_LENGTH(file))
33d38bc8
WD
49 return 0;
50
51 /* if always checksum is set then we use the checksum instead
614291be
WD
52--- old/main.c
53+++ new/main.c
44cedd19 54@@ -133,7 +133,7 @@ pid_t wait_process(pid_t pid, int *statu
614291be
WD
55 }
56
57 /* Wait for a process to exit, calling io_flush while waiting. */
58-static void wait_process_with_flush(pid_t pid, int *exit_code_ptr)
59+void wait_process_with_flush(pid_t pid, int *exit_code_ptr)
60 {
61 pid_t waited_pid;
62 int status;
9a7eef96
WD
63--- old/options.c
64+++ new/options.c
44cedd19 65@@ -103,6 +103,7 @@ int keep_partial = 0;
33d38bc8
WD
66 int safe_symlinks = 0;
67 int copy_unsafe_links = 0;
68 int size_only = 0;
69+int times_only = 0;
70 int daemon_bwlimit = 0;
71 int bwlimit = 0;
72 int fuzzy_basis = 0;
44cedd19 73@@ -160,6 +161,8 @@ char *logfile_name = NULL;
55c1a3b7 74 char *logfile_format = NULL;
a859733e 75 char *stdout_format = NULL;
55c1a3b7 76 char *password_file = NULL;
33d38bc8
WD
77+char *source_filter = NULL;
78+char *dest_filter = NULL;
33d38bc8
WD
79 char *rsync_path = RSYNC_PATH;
80 char *backup_dir = NULL;
55c1a3b7 81 char backup_dir_buf[MAXPATHLEN];
44cedd19 82@@ -380,6 +383,7 @@ void usage(enum logcode F)
33d38bc8
WD
83 rprintf(F," --timeout=TIME set I/O timeout in seconds\n");
84 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
85 rprintf(F," --size-only skip files that match in size\n");
86+ rprintf(F," --times-only skip files that match in mod-time\n");
87 rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
88 rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
89 rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
44cedd19 90@@ -419,6 +423,8 @@ void usage(enum logcode F)
33d38bc8
WD
91 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
92 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
93 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
94+ rprintf(F," --source-filter=COMMAND filter file through COMMAND at source\n");
95+ rprintf(F," --dest-filter=COMMAND filter file through COMMAND at destination\n");
96 rprintf(F," --protocol=NUM force an older protocol version to be used\n");
9c85142a
WD
97 #ifdef ICONV_OPTION
98 rprintf(F," --iconv=CONVERT_SPEC request charset conversion of filesnames\n");
7bfcb297 99@@ -520,6 +526,7 @@ static struct poptOption long_options[]
27e96866 100 {"chmod", 0, POPT_ARG_STRING, 0, OPT_CHMOD, 0, 0 },
33d38bc8
WD
101 {"ignore-times", 'I', POPT_ARG_NONE, &ignore_times, 0, 0, 0 },
102 {"size-only", 0, POPT_ARG_NONE, &size_only, 0, 0, 0 },
103+ {"times-only", 0, POPT_ARG_NONE, &times_only , 0, 0, 0 },
e0e47893 104 {"one-file-system", 'x', POPT_ARG_NONE, 0, 'x', 0, 0 },
489b0a72 105 {"update", 'u', POPT_ARG_NONE, &update_only, 0, 0, 0 },
93ca4d27 106 {"existing", 0, POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
7bfcb297 107@@ -619,6 +626,8 @@ static struct poptOption long_options[]
489b0a72
WD
108 {"password-file", 0, POPT_ARG_STRING, &password_file, 0, 0, 0 },
109 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
110 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
33d38bc8
WD
111+ {"source-filter", 0, POPT_ARG_STRING, &source_filter, 0, 0, 0 },
112+ {"dest-filter", 0, POPT_ARG_STRING, &dest_filter, 0, 0, 0 },
113 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
114 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
27e96866 115 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
44cedd19 116@@ -1577,6 +1586,16 @@ int parse_arguments(int *argc_p, const c
33d38bc8
WD
117 }
118 }
119
120+ if (source_filter || dest_filter) {
121+ if (whole_file == 0) {
122+ snprintf(err_buf, sizeof err_buf,
123+ "--no-whole-file cannot be used with --%s-filter\n",
124+ source_filter ? "source" : "dest");
125+ return 0;
126+ }
127+ whole_file = 1;
128+ }
129+
130 if (files_from) {
131 char *h, *p;
132 int q;
44cedd19 133@@ -1897,6 +1916,25 @@ void server_options(char **args, int *ar
7bfcb297 134 }
33d38bc8
WD
135 }
136
137+ if (times_only && am_sender)
138+ args[ac++] = "--times-only";
139+
140+ if (source_filter && !am_sender) {
141+ /* Need to single quote the arg to keep the remote shell
142+ * from splitting it. FIXME: breaks if command has single quotes. */
143+ if (asprintf(&arg, "--source-filter='%s'", source_filter) < 0)
144+ goto oom;
145+ args[ac++] = arg;
146+ }
147+
148+ if (dest_filter && am_sender) {
149+ /* Need to single quote the arg to keep the remote shell
150+ * from splitting it. FIXME: breaks if command has single quotes. */
151+ if (asprintf(&arg, "--dest-filter='%s'", dest_filter) < 0)
152+ goto oom;
153+ args[ac++] = arg;
154+ }
155+
ccc3a12c
WD
156 if (modify_window_set) {
157 if (asprintf(&arg, "--modify-window=%d", modify_window) < 0)
158 goto oom;
9a7eef96
WD
159--- old/pipe.c
160+++ new/pipe.c
7bfcb297 161@@ -164,3 +164,77 @@ pid_t local_child(int argc, char **argv,
33d38bc8
WD
162
163 return pid;
164 }
165+
166+pid_t run_filter(char *command[], int out, int *pipe_to_filter)
167+{
168+ pid_t pid;
169+ int pipefds[2];
170+
171+ if (verbose >= 2)
7bfcb297 172+ print_child_argv("opening connection using:", command);
33d38bc8
WD
173+
174+ if (pipe(pipefds) < 0) {
175+ rsyserr(FERROR, errno, "pipe");
176+ exit_cleanup(RERR_IPC);
177+ }
178+
179+ pid = do_fork();
180+ if (pid == -1) {
181+ rsyserr(FERROR, errno, "fork");
182+ exit_cleanup(RERR_IPC);
183+ }
184+
185+ if (pid == 0) {
186+ if (dup2(pipefds[0], STDIN_FILENO) < 0
187+ || close(pipefds[1]) < 0
188+ || dup2(out, STDOUT_FILENO) < 0) {
189+ rsyserr(FERROR, errno, "Failed dup/close");
190+ exit_cleanup(RERR_IPC);
191+ }
192+ umask(orig_umask);
193+ set_blocking(STDIN_FILENO);
194+ if (blocking_io)
195+ set_blocking(STDOUT_FILENO);
196+ execvp(command[0], command);
93ca4d27 197+ rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
33d38bc8
WD
198+ exit_cleanup(RERR_IPC);
199+ }
200+
201+ if (close(pipefds[0]) < 0) {
202+ rsyserr(FERROR, errno, "Failed to close");
203+ exit_cleanup(RERR_IPC);
204+ }
205+
206+ *pipe_to_filter = pipefds[1];
207+
208+ return pid;
209+}
210+
211+pid_t run_filter_on_file(char *command[], int out, int in)
212+{
213+ pid_t pid;
214+
215+ if (verbose >= 2)
7bfcb297 216+ print_child_argv("opening connection using:", command);
33d38bc8
WD
217+
218+ pid = do_fork();
219+ if (pid == -1) {
220+ rsyserr(FERROR, errno, "fork");
221+ exit_cleanup(RERR_IPC);
222+ }
223+
224+ if (pid == 0) {
225+ if (dup2(in, STDIN_FILENO) < 0
226+ || dup2(out, STDOUT_FILENO) < 0) {
227+ rsyserr(FERROR, errno, "Failed to dup2");
228+ exit_cleanup(RERR_IPC);
229+ }
230+ if (blocking_io)
231+ set_blocking(STDOUT_FILENO);
232+ execvp(command[0], command);
93ca4d27 233+ rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
33d38bc8
WD
234+ exit_cleanup(RERR_IPC);
235+ }
236+
237+ return pid;
238+}
9a7eef96
WD
239--- old/receiver.c
240+++ new/receiver.c
7bfcb297 241@@ -52,6 +52,7 @@ extern int delay_updates;
9c85142a
WD
242 extern mode_t orig_umask;
243 extern struct stats stats;
33d38bc8 244 extern char *tmpdir;
9c85142a 245+extern char *dest_filter;
33d38bc8 246 extern char *partial_dir;
dd0d95fa 247 extern char *basis_dir[];
9c85142a 248 extern struct file_list *cur_flist, *first_flist, *dir_flist;
7bfcb297 249@@ -357,6 +358,8 @@ int recv_files(int f_in, char *local_nam
5ff5e82f
WD
250 const char *parent_dirname = "";
251 #endif
81ecd8e0 252 int ndx, recv_ok;
33d38bc8
WD
253+ pid_t pid = 0;
254+ char *filter_argv[MAX_FILTER_ARGS + 1];
255
256 if (verbose > 2)
9c85142a 257 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
7bfcb297 258@@ -366,6 +369,23 @@ int recv_files(int f_in, char *local_nam
60a8bf36
WD
259
260 updating_basis = inplace;
33d38bc8
WD
261
262+ if (dest_filter) {
263+ char *p;
264+ char *sep = " \t";
265+ int i;
266+ for (p = strtok(dest_filter, sep), i = 0;
267+ p && i < MAX_FILTER_ARGS;
268+ p = strtok(0, sep))
269+ filter_argv[i++] = p;
270+ filter_argv[i] = NULL;
271+ if (p) {
272+ rprintf(FERROR,
273+ "Too many arguments to dest-filter (> %d)\n",
274+ MAX_FILTER_ARGS);
275+ exit_cleanup(RERR_SYNTAX);
276+ }
277+ }
278+
279 while (1) {
280 cleanup_disable();
281
7bfcb297 282@@ -663,6 +683,9 @@ int recv_files(int f_in, char *local_nam
33d38bc8 283 else if (!am_server && verbose && do_progress)
93ca4d27 284 rprintf(FINFO, "%s\n", fname);
33d38bc8
WD
285
286+ if (dest_filter)
287+ pid = run_filter(filter_argv, fd2, &fd2);
288+
289 /* recv file data */
290 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
1aa236e1 291 fname, fd2, F_LENGTH(file));
7bfcb297 292@@ -677,6 +700,16 @@ int recv_files(int f_in, char *local_nam
33d38bc8
WD
293 exit_cleanup(RERR_FILEIO);
294 }
295
296+ if (dest_filter) {
297+ int status;
614291be 298+ wait_process_with_flush(pid, &status);
33d38bc8
WD
299+ if (status != 0) {
300+ rprintf(FERROR, "filter %s exited code: %d\n",
301+ dest_filter, status);
302+ continue;
303+ }
304+ }
305+
306 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
55c1a3b7
WD
307 char *temp_copy_name;
308 if (partialptr == fname)
9a7eef96
WD
309--- old/rsync.h
310+++ new/rsync.h
7bfcb297 311@@ -128,6 +128,7 @@
33d38bc8
WD
312 #define IOERR_DEL_LIMIT (1<<2)
313
314 #define MAX_ARGS 1000
315+#define MAX_FILTER_ARGS 100
316 #define MAX_BASIS_DIRS 20
317 #define MAX_SERVER_ARGS (MAX_BASIS_DIRS*2 + 100)
318
9a7eef96
WD
319--- old/rsync.yo
320+++ new/rsync.yo
7bfcb297 321@@ -386,6 +386,7 @@ to the detailed description below for a
33d38bc8
WD
322 --timeout=TIME set I/O timeout in seconds
323 -I, --ignore-times don't skip files that match size and time
324 --size-only skip files that match in size
325+ --times-only skip files that match in mod-time
326 --modify-window=NUM compare mod-times with reduced accuracy
327 -T, --temp-dir=DIR create temporary files in directory DIR
328 -y, --fuzzy find similar file for basis if no dest file
44cedd19 329@@ -425,6 +426,8 @@ to the detailed description below for a
33d38bc8
WD
330 --write-batch=FILE write a batched update to FILE
331 --only-write-batch=FILE like --write-batch but w/o updating dest
332 --read-batch=FILE read a batched update from FILE
333+ --source-filter=COMMAND filter file through COMMAND at source
334+ --dest-filter=COMMAND filter file through COMMAND at destination
335 --protocol=NUM force an older protocol version to be used
9c85142a 336 --iconv=CONVERT_SPEC request charset conversion of filesnames
33d38bc8 337 --checksum-seed=NUM set block/file checksum seed (advanced)
44cedd19 338@@ -1959,6 +1962,33 @@ file previously generated by bf(--write-
93ca4d27 339 If em(FILE) is bf(-), the batch data will be read from standard input.
33d38bc8
WD
340 See the "BATCH MODE" section for details.
341
342+dit(bf(--source-filter=COMMAND)) This option allows the user to specify a
343+filter program that will be applied to the contents of all transferred
344+regular files before the data is sent to destination. COMMAND will receive
345+the data on its standard input and it should write the filtered data to
346+standard output. COMMAND should exit non-zero if it cannot process the
347+data or if it encounters an error when writing the data to stdout.
348+
349+Example: --source-filter="gzip -9" will cause remote files to be
350+compressed.
351+Use of --source-filter automatically enables --whole-file.
352+If your filter does not output the same number of bytes that it received on
353+input, you should use --times-only to disable size and content checks on
354+subsequent rsync runs.
355+
356+dit(bf(--dest-filter=COMMAND)) This option allows you to specify a filter
357+program that will be applied to the contents of all transferred regular
358+files before the data is written to disk. COMMAND will receive the data on
359+its standard input and it should write the filtered data to standard
360+output. COMMAND should exit non-zero if it cannot process the data or if
361+it encounters an error when writing the data to stdout.
362+
363+Example: --dest-filter="gzip -9" will cause remote files to be compressed.
364+Use of --dest-filter automatically enables --whole-file.
365+If your filter does not output the same number of bytes that it
366+received on input, you should use --times-only to disable size and
367+content checks on subsequent rsync runs.
368+
369 dit(bf(--protocol=NUM)) Force an older protocol version to be used. This
370 is useful for creating a batch file that is compatible with an older
371 version of rsync. For instance, if rsync 2.6.4 is being used with the
9a7eef96
WD
372--- old/sender.c
373+++ new/sender.c
7bfcb297 374@@ -43,6 +43,7 @@ extern int do_progress;
dd0d95fa
WD
375 extern int inplace;
376 extern int batch_fd;
a859733e 377 extern int write_batch;
dd0d95fa 378+extern char *source_filter;
33d38bc8 379 extern struct stats stats;
9c85142a 380 extern struct file_list *cur_flist, *first_flist, *dir_flist;
33d38bc8 381
7bfcb297 382@@ -181,6 +182,26 @@ void send_files(int f_in, int f_out)
a859733e 383 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
33d38bc8 384 int f_xfer = write_batch < 0 ? batch_fd : f_out;
fc068916 385 int ndx, j;
33d38bc8
WD
386+ char *filter_argv[MAX_FILTER_ARGS + 1];
387+ char *tmp = 0;
388+ int unlink_tmp = 0;
389+
390+ if (source_filter) {
391+ char *p;
392+ char *sep = " \t";
393+ int i;
394+ for (p = strtok(source_filter, sep), i = 0;
395+ p && i < MAX_FILTER_ARGS;
396+ p = strtok(0, sep))
397+ filter_argv[i++] = p;
398+ filter_argv[i] = NULL;
399+ if (p) {
400+ rprintf(FERROR,
401+ "Too many arguments to source-filter (> %d)\n",
402+ MAX_FILTER_ARGS);
403+ exit_cleanup(RERR_SYNTAX);
404+ }
405+ }
406
407 if (verbose > 2)
408 rprintf(FINFO, "send_files starting\n");
7bfcb297 409@@ -284,6 +305,7 @@ void send_files(int f_in, int f_out)
fc068916 410 exit_cleanup(RERR_PROTOCOL);
33d38bc8
WD
411 }
412
413+ unlink_tmp = 0;
414 fd = do_open(fname, O_RDONLY, 0);
415 if (fd == -1) {
416 if (errno == ENOENT) {
44cedd19
WD
417@@ -305,6 +327,33 @@ void send_files(int f_in, int f_out)
418 continue;
33d38bc8
WD
419 }
420
421+ if (source_filter) {
422+ int fd2;
423+ char *tmpl = "/tmp/rsync-filtered_sourceXXXXXX";
424+
425+ tmp = strdup(tmpl);
426+ fd2 = mkstemp(tmp);
427+ if (fd2 == -1) {
428+ rprintf(FERROR, "mkstemp %s failed: %s\n",
429+ tmp, strerror(errno));
430+ } else {
431+ int status;
432+ pid_t pid = run_filter_on_file(filter_argv, fd2, fd);
433+ close(fd);
434+ close(fd2);
614291be 435+ wait_process_with_flush(pid, &status);
33d38bc8
WD
436+ if (status != 0) {
437+ rprintf(FERROR,
438+ "bypassing source filter %s; exited with code: %d\n",
439+ source_filter, status);
440+ fd = do_open(fname, O_RDONLY, 0);
441+ } else {
442+ fd = do_open(tmp, O_RDONLY, 0);
443+ unlink_tmp = 1;
444+ }
445+ }
446+ }
447+
44cedd19
WD
448 /* map the local file */
449 if (do_fstat(fd, &st) != 0) {
450 io_error |= IOERR_GENERAL;
7bfcb297 451@@ -355,6 +404,8 @@ void send_files(int f_in, int f_out)
33d38bc8
WD
452 }
453 }
454 close(fd);
455+ if (unlink_tmp)
456+ unlink(tmp);
457
458 free_sums(s);
459