]>
Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Support for the batch-file options. | |
3 | * | |
4 | * Copyright (C) 1999 Weiss | |
5 | * Copyright (C) 2004 Chris Shoemaker | |
5fcf20ee | 6 | * Copyright (C) 2004-2022 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 3 of the License, or | |
11 | * (at your option) any later version. | |
0f78b815 WD |
12 | * |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
e7c67065 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 20 | */ |
6902ed17 MP |
21 | |
22 | #include "rsync.h" | |
7da17144 | 23 | #include <zlib.h> |
6902ed17 MP |
24 | #include <time.h> |
25 | ||
73f0ce69 | 26 | extern int eol_nulls; |
d3e182af | 27 | extern int recurse; |
6bf82264 | 28 | extern int xfer_dirs; |
d3e182af WD |
29 | extern int preserve_links; |
30 | extern int preserve_hard_links; | |
31 | extern int preserve_devices; | |
32 | extern int preserve_uid; | |
33 | extern int preserve_gid; | |
da01d2e8 WD |
34 | extern int preserve_acls; |
35 | extern int preserve_xattrs; | |
d3e182af | 36 | extern int always_checksum; |
e7f7064c | 37 | extern int do_compression; |
da01d2e8 WD |
38 | extern int inplace; |
39 | extern int append_mode; | |
87f2984d | 40 | extern int write_batch; |
8261047b | 41 | extern int protocol_version; |
87f2984d WD |
42 | extern int raw_argc, cooked_argc; |
43 | extern char **raw_argv, **cooked_argv; | |
2b136663 | 44 | extern char *batch_name; |
da01d2e8 WD |
45 | #ifdef ICONV_OPTION |
46 | extern char *iconv_opt; | |
47 | #endif | |
73f0ce69 | 48 | |
7b6c5c77 | 49 | extern filter_rule_list filter_list; |
e8d3168e | 50 | |
87f2984d WD |
51 | int batch_fd = -1; |
52 | int batch_sh_fd = -1; | |
a7c1fa00 WD |
53 | int batch_stream_flags; |
54 | ||
da01d2e8 WD |
55 | static int tweaked_append; |
56 | static int tweaked_append_verify; | |
57 | static int tweaked_iconv; | |
3cc185a0 | 58 | |
d3e182af | 59 | static int *flag_ptr[] = { |
e7f7064c | 60 | &recurse, /* 0 */ |
658a6369 WD |
61 | &preserve_uid, /* 1 */ |
62 | &preserve_gid, /* 2 */ | |
e7f7064c WD |
63 | &preserve_links, /* 3 */ |
64 | &preserve_devices, /* 4 */ | |
65 | &preserve_hard_links, /* 5 */ | |
66 | &always_checksum, /* 6 */ | |
67 | &xfer_dirs, /* 7 (protocol 29) */ | |
a7c1fa00 | 68 | &do_compression, /* 8 (protocol 29) */ |
da01d2e8 WD |
69 | &tweaked_iconv, /* 9 (protocol 30) */ |
70 | &preserve_acls, /* 10 (protocol 30) */ | |
71 | &preserve_xattrs, /* 11 (protocol 30) */ | |
72 | &inplace, /* 12 (protocol 30) */ | |
73 | &tweaked_append, /* 13 (protocol 30) */ | |
74 | &tweaked_append_verify, /* 14 (protocol 30) */ | |
d3e182af WD |
75 | NULL |
76 | }; | |
77 | ||
78 | static char *flag_name[] = { | |
79 | "--recurse (-r)", | |
80 | "--owner (-o)", | |
81 | "--group (-g)", | |
82 | "--links (-l)", | |
83 | "--devices (-D)", | |
84 | "--hard-links (-H)", | |
85 | "--checksum (-c)", | |
6bf82264 | 86 | "--dirs (-d)", |
e7f7064c | 87 | "--compress (-z)", |
da01d2e8 WD |
88 | "--iconv", |
89 | "--acls (-A)", | |
90 | "--xattrs (-X)", | |
91 | "--inplace", | |
92 | "--append", | |
93 | "--append-verify", | |
d3e182af WD |
94 | NULL |
95 | }; | |
96 | ||
97 | void write_stream_flags(int fd) | |
98 | { | |
99 | int i, flags; | |
100 | ||
da01d2e8 WD |
101 | tweaked_append = append_mode == 1; |
102 | tweaked_append_verify = append_mode == 2; | |
103 | #ifdef ICONV_OPTION | |
104 | tweaked_iconv = iconv_opt != NULL; | |
3cc185a0 WD |
105 | #endif |
106 | ||
d3e182af WD |
107 | /* Start the batch file with a bitmap of data-stream-affecting |
108 | * flags. */ | |
109 | for (i = 0, flags = 0; flag_ptr[i]; i++) { | |
110 | if (*flag_ptr[i]) | |
111 | flags |= 1 << i; | |
112 | } | |
113 | write_int(fd, flags); | |
114 | } | |
115 | ||
116 | void read_stream_flags(int fd) | |
117 | { | |
a7c1fa00 WD |
118 | batch_stream_flags = read_int(fd); |
119 | } | |
120 | ||
121 | void check_batch_flags(void) | |
122 | { | |
123 | int i; | |
d3e182af | 124 | |
6bf82264 | 125 | if (protocol_version < 29) |
e7f7064c | 126 | flag_ptr[7] = NULL; |
da01d2e8 WD |
127 | else if (protocol_version < 30) |
128 | flag_ptr[9] = NULL; | |
129 | tweaked_append = append_mode == 1; | |
130 | tweaked_append_verify = append_mode == 2; | |
131 | #ifdef ICONV_OPTION | |
132 | tweaked_iconv = iconv_opt != NULL; | |
133 | #endif | |
a7c1fa00 WD |
134 | for (i = 0; flag_ptr[i]; i++) { |
135 | int set = batch_stream_flags & (1 << i) ? 1 : 0; | |
d3e182af | 136 | if (*flag_ptr[i] != set) { |
da01d2e8 | 137 | if (i == 9) { |
a7c1fa00 | 138 | rprintf(FERROR, |
da01d2e8 WD |
139 | "%s specify the --iconv option to use this batch file.\n", |
140 | set ? "Please" : "Do not"); | |
141 | exit_cleanup(RERR_SYNTAX); | |
142 | } | |
951e826b | 143 | if (INFO_GTE(MISC, 1)) { |
6a48e792 WD |
144 | rprintf(FINFO, |
145 | "%sing the %s option to match the batchfile.\n", | |
146 | set ? "Sett" : "Clear", flag_name[i]); | |
147 | } | |
d3e182af WD |
148 | *flag_ptr[i] = set; |
149 | } | |
150 | } | |
e7f7064c WD |
151 | if (protocol_version < 29) { |
152 | if (recurse) | |
153 | xfer_dirs |= 1; | |
154 | else if (xfer_dirs < 2) | |
155 | xfer_dirs = 0; | |
156 | } | |
3cc185a0 | 157 | |
da01d2e8 WD |
158 | if (tweaked_append) |
159 | append_mode = 1; | |
160 | else if (tweaked_append_verify) | |
161 | append_mode = 2; | |
d3e182af WD |
162 | } |
163 | ||
87f2984d | 164 | static int write_arg(const char *arg) |
e7a69008 | 165 | { |
87f2984d WD |
166 | const char *x, *s; |
167 | int len, err = 0; | |
e7a69008 WD |
168 | |
169 | if (*arg == '-' && (x = strchr(arg, '=')) != NULL) { | |
87f2984d | 170 | err |= write(batch_sh_fd, arg, x - arg + 1) != x - arg + 1; |
e7a69008 WD |
171 | arg += x - arg + 1; |
172 | } | |
173 | ||
174 | if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) { | |
87f2984d | 175 | err |= write(batch_sh_fd, "'", 1) != 1; |
e7a69008 | 176 | for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) { |
87f2984d WD |
177 | err |= write(batch_sh_fd, s, x - s + 1) != x - s + 1; |
178 | err |= write(batch_sh_fd, "'", 1) != 1; | |
e7a69008 | 179 | } |
94112924 | 180 | len = strlen(s); |
87f2984d WD |
181 | err |= write(batch_sh_fd, s, len) != len; |
182 | err |= write(batch_sh_fd, "'", 1) != 1; | |
183 | return err; | |
e7a69008 WD |
184 | } |
185 | ||
94112924 | 186 | len = strlen(arg); |
87f2984d | 187 | err |= write(batch_sh_fd, arg, len) != len; |
94112924 | 188 | |
87f2984d WD |
189 | return err; |
190 | } | |
191 | ||
192 | /* Writes out a space and then an option (or other string) with an optional "=" + arg suffix. */ | |
193 | static int write_opt(const char *opt, const char *arg) | |
194 | { | |
195 | int len = strlen(opt); | |
196 | int err = write(batch_sh_fd, " ", 1) != 1; | |
9cb7529b | 197 | err = write(batch_sh_fd, opt, len) != len ? 1 : 0; |
87f2984d WD |
198 | if (arg) { |
199 | err |= write(batch_sh_fd, "=", 1) != 1; | |
200 | err |= write_arg(arg); | |
201 | } | |
202 | return err; | |
e7a69008 WD |
203 | } |
204 | ||
8261047b | 205 | static void write_filter_rules(int fd) |
73f0ce69 | 206 | { |
7b6c5c77 | 207 | filter_rule *ent; |
73f0ce69 WD |
208 | |
209 | write_sbuf(fd, " <<'#E#'\n"); | |
7842418b | 210 | for (ent = filter_list.head; ent; ent = ent->next) { |
8261047b | 211 | unsigned int plen; |
c8fa85b2 | 212 | char *p = get_rule_prefix(ent, "- ", 0, &plen); |
8261047b WD |
213 | write_buf(fd, p, plen); |
214 | write_sbuf(fd, ent->pattern); | |
b32d4254 | 215 | if (ent->rflags & FILTRULE_DIRECTORY) |
73f0ce69 WD |
216 | write_byte(fd, '/'); |
217 | write_byte(fd, eol_nulls ? 0 : '\n'); | |
218 | } | |
219 | if (eol_nulls) | |
220 | write_sbuf(fd, ";\n"); | |
221 | write_sbuf(fd, "#E#"); | |
222 | } | |
223 | ||
87f2984d WD |
224 | /* This sets batch_fd and (for --write-batch) batch_sh_fd. */ |
225 | void open_batch_files(void) | |
226 | { | |
227 | if (write_batch) { | |
228 | char filename[MAXPATHLEN]; | |
229 | ||
230 | stringjoin(filename, sizeof filename, batch_name, ".sh", NULL); | |
231 | ||
232 | batch_sh_fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR); | |
233 | if (batch_sh_fd < 0) { | |
234 | rsyserr(FERROR, errno, "Batch file %s open error", full_fname(filename)); | |
235 | exit_cleanup(RERR_FILESELECT); | |
236 | } | |
237 | ||
238 | batch_fd = do_open(batch_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); | |
239 | } else if (strcmp(batch_name, "-") == 0) | |
240 | batch_fd = STDIN_FILENO; | |
241 | else | |
242 | batch_fd = do_open(batch_name, O_RDONLY, S_IRUSR | S_IWUSR); | |
243 | ||
244 | if (batch_fd < 0) { | |
245 | rsyserr(FERROR, errno, "Batch file %s open error", full_fname(batch_name)); | |
246 | exit_cleanup(RERR_FILEIO); | |
247 | } | |
248 | } | |
249 | ||
e7a69008 WD |
250 | /* This routine tries to write out an equivalent --read-batch command |
251 | * given the user's --write-batch args. However, it doesn't really | |
252 | * understand most of the options, so it uses some overly simple | |
253 | * heuristics to munge the command line into something that will | |
254 | * (hopefully) work. */ | |
87f2984d | 255 | void write_batch_shell_file(void) |
6902ed17 | 256 | { |
f8683063 | 257 | int i, j, len, err = 0; |
87f2984d | 258 | char *p, *p2; |
088aac85 | 259 | |
e7a69008 | 260 | /* Write argvs info to BATCH.sh file */ |
87f2984d | 261 | err |= write_arg(raw_argv[0]); |
8261047b WD |
262 | if (filter_list.head) { |
263 | if (protocol_version >= 29) | |
87f2984d | 264 | err |= write_opt("--filter", "._-"); |
8261047b | 265 | else |
87f2984d | 266 | err |= write_opt("--exclude-from", "-"); |
8261047b | 267 | } |
87f2984d | 268 | |
f8683063 WD |
269 | /* Elide the filename args from the option list, but scan for them in reverse. */ |
270 | for (i = raw_argc-1, j = cooked_argc-1; i > 0 && j >= 0; i--) { | |
271 | if (strcmp(raw_argv[i], cooked_argv[j]) == 0) { | |
272 | raw_argv[i] = NULL; | |
273 | j--; | |
274 | } | |
275 | } | |
87f2984d WD |
276 | |
277 | for (i = 1; i < raw_argc; i++) { | |
f8683063 | 278 | if (!(p = raw_argv[i])) |
a7303a3d | 279 | continue; |
e7a69008 | 280 | if (strncmp(p, "--files-from", 12) == 0 |
e63ff70e WD |
281 | || strncmp(p, "--filter", 8) == 0 |
282 | || strncmp(p, "--include", 9) == 0 | |
283 | || strncmp(p, "--exclude", 9) == 0) { | |
e7a69008 | 284 | if (strchr(p, '=') == NULL) |
b462781f | 285 | i++; |
b9f592fb | 286 | continue; |
b462781f | 287 | } |
8261047b WD |
288 | if (strcmp(p, "-f") == 0) { |
289 | i++; | |
290 | continue; | |
291 | } | |
d630f53e | 292 | if (strncmp(p, "--write-batch", len = 13) == 0 |
87f2984d WD |
293 | || strncmp(p, "--only-write-batch", len = 18) == 0) |
294 | err |= write_opt("--read-batch", p[len] == '=' ? p + len + 1 : NULL); | |
295 | else { | |
296 | err |= write(batch_sh_fd, " ", 1) != 1; | |
297 | err |= write_arg(p); | |
94112924 | 298 | } |
1cd5beeb | 299 | } |
87f2984d WD |
300 | if (!(p = check_for_hostspec(cooked_argv[cooked_argc - 1], &p2, &i))) |
301 | p = cooked_argv[cooked_argc - 1]; | |
302 | err |= write_opt("${1:-", NULL); | |
303 | err |= write_arg(p); | |
304 | err |= write(batch_sh_fd, "}", 1) != 1; | |
7842418b | 305 | if (filter_list.head) |
87f2984d WD |
306 | write_filter_rules(batch_sh_fd); |
307 | if (write(batch_sh_fd, "\n", 1) != 1 || close(batch_sh_fd) < 0 || err) { | |
308 | rsyserr(FERROR, errno, "Batch file %s.sh write error", batch_name); | |
da01d2e8 | 309 | exit_cleanup(RERR_FILEIO); |
1cd5beeb | 310 | } |
87f2984d | 311 | batch_sh_fd = -1; |
6902ed17 | 312 | } |