2 * Support for the batch-file options.
4 * Copyright (C) 1999 Weiss
5 * Copyright (C) 2004 Chris Shoemaker
6 * Copyright (C) 2004-2020 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
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.
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.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
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
;
34 extern int preserve_acls
;
35 extern int preserve_xattrs
;
36 extern int always_checksum
;
37 extern int do_compression
;
39 extern int append_mode
;
40 extern int write_batch
;
41 extern int protocol_version
;
42 extern int raw_argc
, cooked_argc
;
43 extern char **raw_argv
, **cooked_argv
;
44 extern char *batch_name
;
45 extern const char *checksum_choice
;
46 extern const char *compress_choice
;
48 extern char *iconv_opt
;
51 extern filter_rule_list filter_list
;
55 int batch_stream_flags
;
57 static int tweaked_append
;
58 static int tweaked_append_verify
;
59 static int tweaked_iconv
;
61 static int *flag_ptr
[] = {
63 &preserve_uid
, /* 1 */
64 &preserve_gid
, /* 2 */
65 &preserve_links
, /* 3 */
66 &preserve_devices
, /* 4 */
67 &preserve_hard_links
, /* 5 */
68 &always_checksum
, /* 6 */
69 &xfer_dirs
, /* 7 (protocol 29) */
70 &do_compression
, /* 8 (protocol 29) */
71 &tweaked_iconv
, /* 9 (protocol 30) */
72 &preserve_acls
, /* 10 (protocol 30) */
73 &preserve_xattrs
, /* 11 (protocol 30) */
74 &inplace
, /* 12 (protocol 30) */
75 &tweaked_append
, /* 13 (protocol 30) */
76 &tweaked_append_verify
, /* 14 (protocol 30) */
80 static char *flag_name
[] = {
99 void write_stream_flags(int fd
)
103 tweaked_append
= append_mode
== 1;
104 tweaked_append_verify
= append_mode
== 2;
106 tweaked_iconv
= iconv_opt
!= NULL
;
109 /* Start the batch file with a bitmap of data-stream-affecting
111 for (i
= 0, flags
= 0; flag_ptr
[i
]; i
++) {
115 write_int(fd
, flags
);
118 void read_stream_flags(int fd
)
120 batch_stream_flags
= read_int(fd
);
123 void check_batch_flags(void)
127 if (protocol_version
< 29)
129 else if (protocol_version
< 30)
131 tweaked_append
= append_mode
== 1;
132 tweaked_append_verify
= append_mode
== 2;
134 tweaked_iconv
= iconv_opt
!= NULL
;
136 for (i
= 0; flag_ptr
[i
]; i
++) {
137 int set
= batch_stream_flags
& (1 << i
) ? 1 : 0;
138 if (*flag_ptr
[i
] != set
) {
141 "%s specify the --iconv option to use this batch file.\n",
142 set
? "Please" : "Do not");
143 exit_cleanup(RERR_SYNTAX
);
145 if (INFO_GTE(MISC
, 1)) {
147 "%sing the %s option to match the batchfile.\n",
148 set
? "Sett" : "Clear", flag_name
[i
]);
153 if (protocol_version
< 29) {
156 else if (xfer_dirs
< 2)
162 else if (tweaked_append_verify
)
166 static int write_arg(const char *arg
)
171 if (*arg
== '-' && (x
= strchr(arg
, '=')) != NULL
) {
172 err
|= write(batch_sh_fd
, arg
, x
- arg
+ 1) != x
- arg
+ 1;
176 if (strpbrk(arg
, " \"'&;|[]()$#!*?^\\") != NULL
) {
177 err
|= write(batch_sh_fd
, "'", 1) != 1;
178 for (s
= arg
; (x
= strchr(s
, '\'')) != NULL
; s
= x
+ 1) {
179 err
|= write(batch_sh_fd
, s
, x
- s
+ 1) != x
- s
+ 1;
180 err
|= write(batch_sh_fd
, "'", 1) != 1;
183 err
|= write(batch_sh_fd
, s
, len
) != len
;
184 err
|= write(batch_sh_fd
, "'", 1) != 1;
189 err
|= write(batch_sh_fd
, arg
, len
) != len
;
194 /* Writes out a space and then an option (or other string) with an optional "=" + arg suffix. */
195 static int write_opt(const char *opt
, const char *arg
)
197 int len
= strlen(opt
);
198 int err
= write(batch_sh_fd
, " ", 1) != 1;
199 err
= write(batch_sh_fd
, opt
, len
) != len
? 1 : 0;
201 err
|= write(batch_sh_fd
, "=", 1) != 1;
202 err
|= write_arg(arg
);
207 static void write_filter_rules(int fd
)
211 write_sbuf(fd
, " <<'#E#'\n");
212 for (ent
= filter_list
.head
; ent
; ent
= ent
->next
) {
214 char *p
= get_rule_prefix(ent
, "- ", 0, &plen
);
215 write_buf(fd
, p
, plen
);
216 write_sbuf(fd
, ent
->pattern
);
217 if (ent
->rflags
& FILTRULE_DIRECTORY
)
219 write_byte(fd
, eol_nulls
? 0 : '\n');
222 write_sbuf(fd
, ";\n");
223 write_sbuf(fd
, "#E#");
226 /* This sets batch_fd and (for --write-batch) batch_sh_fd. */
227 void open_batch_files(void)
230 char filename
[MAXPATHLEN
];
232 stringjoin(filename
, sizeof filename
, batch_name
, ".sh", NULL
);
234 batch_sh_fd
= do_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
| S_IXUSR
);
235 if (batch_sh_fd
< 0) {
236 rsyserr(FERROR
, errno
, "Batch file %s open error", full_fname(filename
));
237 exit_cleanup(RERR_FILESELECT
);
240 batch_fd
= do_open(batch_name
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
241 } else if (strcmp(batch_name
, "-") == 0)
242 batch_fd
= STDIN_FILENO
;
244 batch_fd
= do_open(batch_name
, O_RDONLY
, S_IRUSR
| S_IWUSR
);
247 rsyserr(FERROR
, errno
, "Batch file %s open error", full_fname(batch_name
));
248 exit_cleanup(RERR_FILEIO
);
252 /* This routine tries to write out an equivalent --read-batch command
253 * given the user's --write-batch args. However, it doesn't really
254 * understand most of the options, so it uses some overly simple
255 * heuristics to munge the command line into something that will
256 * (hopefully) work. */
257 void write_batch_shell_file(void)
262 /* Write argvs info to BATCH.sh file */
263 err
|= write_arg(raw_argv
[0]);
264 if (filter_list
.head
) {
265 if (protocol_version
>= 29)
266 err
|= write_opt("--filter", "._-");
268 err
|= write_opt("--exclude-from", "-");
271 /* We need to make sure that any protocol-based or negotiated choices get accurately
272 * reflected in the options we save AND that we avoid any need for --read-batch to
273 * do a string-based negotation (since we don't write them into the file). */
275 err
|= write_opt("--compress-choice", compress_choice
);
276 err
|= write_opt("--checksum-choice", checksum_choice
);
278 for (i
= 1; i
< raw_argc
; i
++) {
280 if (cooked_argc
&& p
[0] == cooked_argv
[0][0] && strcmp(p
, cooked_argv
[0]) == 0) {
285 if (strncmp(p
, "--files-from", 12) == 0
286 || strncmp(p
, "--filter", 8) == 0
287 || strncmp(p
, "--include", 9) == 0
288 || strncmp(p
, "--exclude", 9) == 0) {
289 if (strchr(p
, '=') == NULL
)
293 if (strcmp(p
, "-f") == 0) {
297 if (strncmp(p
, "--write-batch", len
= 13) == 0
298 || strncmp(p
, "--only-write-batch", len
= 18) == 0)
299 err
|= write_opt("--read-batch", p
[len
] == '=' ? p
+ len
+ 1 : NULL
);
301 err
|= write(batch_sh_fd
, " ", 1) != 1;
305 if (!(p
= check_for_hostspec(cooked_argv
[cooked_argc
- 1], &p2
, &i
)))
306 p
= cooked_argv
[cooked_argc
- 1];
307 err
|= write_opt("${1:-", NULL
);
309 err
|= write(batch_sh_fd
, "}", 1) != 1;
310 if (filter_list
.head
)
311 write_filter_rules(batch_sh_fd
);
312 if (write(batch_sh_fd
, "\n", 1) != 1 || close(batch_sh_fd
) < 0 || err
) {
313 rsyserr(FERROR
, errno
, "Batch file %s.sh write error", batch_name
);
314 exit_cleanup(RERR_FILEIO
);