2 * Support for the batch-file options.
4 * Copyright (C) 1999 Weiss
5 * Copyright (C) 2004 Chris Shoemaker
6 * Copyright (C) 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "zlib/zlib.h"
30 extern int preserve_links;
31 extern int preserve_hard_links;
32 extern int preserve_devices;
33 extern int preserve_uid;
34 extern int preserve_gid;
35 extern int always_checksum;
36 extern int do_compression;
37 extern int def_compress_level;
38 extern int protocol_version;
39 extern char *batch_name;
41 extern struct filter_list_struct filter_list;
43 static int tweaked_compress_level;
45 static int *flag_ptr[] = {
47 &preserve_uid, /* 1 */
48 &preserve_gid, /* 2 */
49 &preserve_links, /* 3 */
50 &preserve_devices, /* 4 */
51 &preserve_hard_links, /* 5 */
52 &always_checksum, /* 6 */
53 &xfer_dirs, /* 7 (protocol 29) */
54 &tweaked_compress_level,/* 8 (protocol 29) */
58 static char *flag_name[] = {
71 void write_stream_flags(int fd)
75 #if Z_DEFAULT_COMPRESSION == -1
76 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
78 #error internal logic error! Fix def_compress_level logic above and below too!
81 /* Start the batch file with a bitmap of data-stream-affecting
83 if (protocol_version < 29)
85 for (i = 0, flags = 0; flag_ptr[i]; i++) {
92 void read_stream_flags(int fd)
96 if (protocol_version < 29)
98 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
99 int set = flags & (1 << i) ? 1 : 0;
100 if (*flag_ptr[i] != set) {
103 "%sing the %s option to match the batchfile.\n",
104 set ? "Sett" : "Clear", flag_name[i]);
109 if (protocol_version < 29) {
112 else if (xfer_dirs < 2)
116 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
120 def_compress_level = tweaked_compress_level - 2;
124 static void write_arg(int fd, char *arg)
128 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
129 write(fd, arg, x - arg + 1);
133 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
135 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
136 write(fd, s, x - s + 1);
139 write(fd, s, strlen(s));
144 write(fd, arg, strlen(arg));
147 static void write_filter_rules(int fd)
149 struct filter_struct *ent;
151 write_sbuf(fd, " <<'#E#'\n");
152 for (ent = filter_list.head; ent; ent = ent->next) {
154 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
155 write_buf(fd, p, plen);
156 write_sbuf(fd, ent->pattern);
157 if (ent->match_flags & MATCHFLG_DIRECTORY)
159 write_byte(fd, eol_nulls ? 0 : '\n');
162 write_sbuf(fd, ";\n");
163 write_sbuf(fd, "#E#");
166 /* This routine tries to write out an equivalent --read-batch command
167 * given the user's --write-batch args. However, it doesn't really
168 * understand most of the options, so it uses some overly simple
169 * heuristics to munge the command line into something that will
170 * (hopefully) work. */
171 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
174 char *p, filename[MAXPATHLEN];
176 stringjoin(filename, sizeof filename,
177 batch_name, ".sh", NULL);
178 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
179 S_IRUSR | S_IWUSR | S_IEXEC);
181 rsyserr(FERROR, errno, "Batch file %s open error",
186 /* Write argvs info to BATCH.sh file */
187 write_arg(fd, argv[0]);
188 if (filter_list.head) {
189 if (protocol_version >= 29)
190 write_sbuf(fd, " --filter=._-");
192 write_sbuf(fd, " --exclude-from=-");
194 for (i = 1; i < argc - file_arg_cnt; i++) {
196 if (strncmp(p, "--files-from", 12) == 0
197 || strncmp(p, "--filter", 8) == 0
198 || strncmp(p, "--include", 9) == 0
199 || strncmp(p, "--exclude", 9) == 0) {
200 if (strchr(p, '=') == NULL)
204 if (strcmp(p, "-f") == 0) {
209 if (strncmp(p, "--write-batch", len = 13) == 0
210 || strncmp(p, "--only-write-batch", len = 18) == 0) {
211 write(fd, "--read-batch", 12);
214 write_arg(fd, p + len + 1);
219 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
221 write(fd, " ${1:-", 6);
224 if (filter_list.head)
225 write_filter_rules(fd);
226 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
227 rsyserr(FERROR, errno, "Batch file %s write error",