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 int flist_extra_ndx;
40 extern char *batch_name;
42 extern struct filter_list_struct filter_list;
44 static int tweaked_preserve_uid;
45 static int tweaked_preserve_gid;
46 static int tweaked_compress_level;
48 static int *flag_ptr[] = {
50 &tweaked_preserve_uid, /* 1 */
51 &tweaked_preserve_gid, /* 2 */
52 &preserve_links, /* 3 */
53 &preserve_devices, /* 4 */
54 &preserve_hard_links, /* 5 */
55 &always_checksum, /* 6 */
56 &xfer_dirs, /* 7 (protocol 29) */
57 &tweaked_compress_level,/* 8 (protocol 29) */
61 static char *flag_name[] = {
74 void write_stream_flags(int fd)
78 tweaked_preserve_uid = preserve_uid != 0;
79 tweaked_preserve_gid = preserve_gid != 0;
80 #if Z_DEFAULT_COMPRESSION == -1
81 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
83 #error internal logic error! Fix def_compress_level logic above and below too!
86 /* Start the batch file with a bitmap of data-stream-affecting
88 if (protocol_version < 29)
90 for (i = 0, flags = 0; flag_ptr[i]; i++) {
97 void read_stream_flags(int fd)
101 if (protocol_version < 29)
103 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
104 int set = flags & (1 << i) ? 1 : 0;
105 if (*flag_ptr[i] != set) {
108 "%sing the %s option to match the batchfile.\n",
109 set ? "Sett" : "Clear", flag_name[i]);
114 if (protocol_version < 29) {
117 else if (xfer_dirs < 2)
121 if (tweaked_preserve_uid) {
123 preserve_uid = flist_extra_ndx++;
126 if (tweaked_preserve_gid) {
128 preserve_gid = flist_extra_ndx++;
131 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
135 def_compress_level = tweaked_compress_level - 2;
139 static void write_arg(int fd, char *arg)
143 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
144 write(fd, arg, x - arg + 1);
148 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
150 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
151 write(fd, s, x - s + 1);
154 write(fd, s, strlen(s));
159 write(fd, arg, strlen(arg));
162 static void write_filter_rules(int fd)
164 struct filter_struct *ent;
166 write_sbuf(fd, " <<'#E#'\n");
167 for (ent = filter_list.head; ent; ent = ent->next) {
169 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
170 write_buf(fd, p, plen);
171 write_sbuf(fd, ent->pattern);
172 if (ent->match_flags & MATCHFLG_DIRECTORY)
174 write_byte(fd, eol_nulls ? 0 : '\n');
177 write_sbuf(fd, ";\n");
178 write_sbuf(fd, "#E#");
181 /* This routine tries to write out an equivalent --read-batch command
182 * given the user's --write-batch args. However, it doesn't really
183 * understand most of the options, so it uses some overly simple
184 * heuristics to munge the command line into something that will
185 * (hopefully) work. */
186 void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
189 char *p, filename[MAXPATHLEN];
191 stringjoin(filename, sizeof filename,
192 batch_name, ".sh", NULL);
193 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
194 S_IRUSR | S_IWUSR | S_IEXEC);
196 rsyserr(FERROR, errno, "Batch file %s open error",
201 /* Write argvs info to BATCH.sh file */
202 write_arg(fd, argv[0]);
203 if (filter_list.head) {
204 if (protocol_version >= 29)
205 write_sbuf(fd, " --filter=._-");
207 write_sbuf(fd, " --exclude-from=-");
209 for (i = 1; i < argc - file_arg_cnt; i++) {
211 if (strncmp(p, "--files-from", 12) == 0
212 || strncmp(p, "--filter", 8) == 0
213 || strncmp(p, "--include", 9) == 0
214 || strncmp(p, "--exclude", 9) == 0) {
215 if (strchr(p, '=') == NULL)
219 if (strcmp(p, "-f") == 0) {
224 if (strncmp(p, "--write-batch", len = 13) == 0
225 || strncmp(p, "--only-write-batch", len = 18) == 0) {
226 write(fd, "--read-batch", 12);
229 write_arg(fd, p + len + 1);
234 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
236 write(fd, " ${1:-", 6);
239 if (filter_list.head)
240 write_filter_rules(fd);
241 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
242 rsyserr(FERROR, errno, "Batch file %s write error",