2 * Support for the batch-file options.
4 * Copyright (C) 1999 Weiss
5 * Copyright (C) 2004 Chris Shoemaker
6 * Copyright (C) 2004-2007 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 version 3 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, visit the http://fsf.org website.
22 #include "zlib/zlib.h"
28 extern int preserve_links;
29 extern int preserve_hard_links;
30 extern int preserve_devices;
31 extern int preserve_uid;
32 extern int preserve_gid;
33 extern int always_checksum;
34 extern int do_compression;
35 extern int def_compress_level;
36 extern int protocol_version;
37 extern char *batch_name;
39 extern struct filter_list_struct filter_list;
41 static int tweaked_compress_level;
43 static int *flag_ptr[] = {
45 &preserve_uid, /* 1 */
46 &preserve_gid, /* 2 */
47 &preserve_links, /* 3 */
48 &preserve_devices, /* 4 */
49 &preserve_hard_links, /* 5 */
50 &always_checksum, /* 6 */
51 &xfer_dirs, /* 7 (protocol 29) */
52 &tweaked_compress_level,/* 8 (protocol 29) */
56 static char *flag_name[] = {
69 void write_stream_flags(int fd)
73 #if Z_DEFAULT_COMPRESSION == -1
74 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
76 #error internal logic error! Fix def_compress_level logic above and below too!
79 /* Start the batch file with a bitmap of data-stream-affecting
81 if (protocol_version < 29)
83 for (i = 0, flags = 0; flag_ptr[i]; i++) {
90 void read_stream_flags(int fd)
94 if (protocol_version < 29)
96 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
97 int set = flags & (1 << i) ? 1 : 0;
98 if (*flag_ptr[i] != set) {
101 "%sing the %s option to match the batchfile.\n",
102 set ? "Sett" : "Clear", flag_name[i]);
107 if (protocol_version < 29) {
110 else if (xfer_dirs < 2)
114 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
118 def_compress_level = tweaked_compress_level - 2;
122 static void write_arg(int fd, char *arg)
126 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
127 write(fd, arg, x - arg + 1);
131 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
133 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
134 write(fd, s, x - s + 1);
137 write(fd, s, strlen(s));
142 write(fd, arg, strlen(arg));
145 static void write_filter_rules(int fd)
147 struct filter_struct *ent;
149 write_sbuf(fd, " <<'#E#'\n");
150 for (ent = filter_list.head; ent; ent = ent->next) {
152 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
153 write_buf(fd, p, plen);
154 write_sbuf(fd, ent->pattern);
155 if (ent->match_flags & MATCHFLG_DIRECTORY)
157 write_byte(fd, eol_nulls ? 0 : '\n');
160 write_sbuf(fd, ";\n");
161 write_sbuf(fd, "#E#");
164 /* This routine tries to write out an equivalent --read-batch command
165 * given the user's --write-batch args. However, it doesn't really
166 * understand most of the options, so it uses some overly simple
167 * heuristics to munge the command line into something that will
168 * (hopefully) work. */
169 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
172 char *p, filename[MAXPATHLEN];
174 stringjoin(filename, sizeof filename,
175 batch_name, ".sh", NULL);
176 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
177 S_IRUSR | S_IWUSR | S_IEXEC);
179 rsyserr(FERROR, errno, "Batch file %s open error",
184 /* Write argvs info to BATCH.sh file */
185 write_arg(fd, argv[0]);
186 if (filter_list.head) {
187 if (protocol_version >= 29)
188 write_sbuf(fd, " --filter=._-");
190 write_sbuf(fd, " --exclude-from=-");
192 for (i = 1; i < argc - file_arg_cnt; i++) {
194 if (strncmp(p, "--files-from", 12) == 0
195 || strncmp(p, "--filter", 8) == 0
196 || strncmp(p, "--include", 9) == 0
197 || strncmp(p, "--exclude", 9) == 0) {
198 if (strchr(p, '=') == NULL)
202 if (strcmp(p, "-f") == 0) {
207 if (strncmp(p, "--write-batch", len = 13) == 0
208 || strncmp(p, "--only-write-batch", len = 18) == 0) {
209 write(fd, "--read-batch", 12);
212 write_arg(fd, p + len + 1);
217 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
219 write(fd, " ${1:-", 6);
222 if (filter_list.head)
223 write_filter_rules(fd);
224 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
225 rsyserr(FERROR, errno, "Batch file %s write error",