Improved option handling for protocol 30 batch files.
[rsync/rsync.git] / batch.c
CommitLineData
0f78b815
WD
1/*
2 * Support for the batch-file options.
3 *
4 * Copyright (C) 1999 Weiss
5 * Copyright (C) 2004 Chris Shoemaker
ba2133d6 6 * Copyright (C) 2004-2007 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"
3cc185a0 23#include "zlib/zlib.h"
6902ed17
MP
24#include <time.h>
25
73f0ce69 26extern int eol_nulls;
d3e182af 27extern int recurse;
6bf82264 28extern int xfer_dirs;
d3e182af
WD
29extern int preserve_links;
30extern int preserve_hard_links;
31extern int preserve_devices;
32extern int preserve_uid;
33extern int preserve_gid;
da01d2e8
WD
34extern int preserve_acls;
35extern int preserve_xattrs;
d3e182af 36extern int always_checksum;
e7f7064c 37extern int do_compression;
3cc185a0 38extern int def_compress_level;
da01d2e8
WD
39extern int inplace;
40extern int append_mode;
8261047b 41extern int protocol_version;
2b136663 42extern char *batch_name;
da01d2e8
WD
43#ifdef ICONV_OPTION
44extern char *iconv_opt;
45#endif
73f0ce69 46
7842418b 47extern struct filter_list_struct filter_list;
e8d3168e 48
3cc185a0 49static int tweaked_compress_level;
da01d2e8
WD
50static int tweaked_append;
51static int tweaked_append_verify;
52static int tweaked_iconv;
3cc185a0 53
d3e182af 54static int *flag_ptr[] = {
e7f7064c 55 &recurse, /* 0 */
658a6369
WD
56 &preserve_uid, /* 1 */
57 &preserve_gid, /* 2 */
e7f7064c
WD
58 &preserve_links, /* 3 */
59 &preserve_devices, /* 4 */
60 &preserve_hard_links, /* 5 */
61 &always_checksum, /* 6 */
62 &xfer_dirs, /* 7 (protocol 29) */
3cc185a0 63 &tweaked_compress_level,/* 8 (protocol 29) */
da01d2e8
WD
64 &tweaked_iconv, /* 9 (protocol 30) */
65 &preserve_acls, /* 10 (protocol 30) */
66 &preserve_xattrs, /* 11 (protocol 30) */
67 &inplace, /* 12 (protocol 30) */
68 &tweaked_append, /* 13 (protocol 30) */
69 &tweaked_append_verify, /* 14 (protocol 30) */
d3e182af
WD
70 NULL
71};
72
73static char *flag_name[] = {
74 "--recurse (-r)",
75 "--owner (-o)",
76 "--group (-g)",
77 "--links (-l)",
78 "--devices (-D)",
79 "--hard-links (-H)",
80 "--checksum (-c)",
6bf82264 81 "--dirs (-d)",
e7f7064c 82 "--compress (-z)",
da01d2e8
WD
83 "--iconv",
84 "--acls (-A)",
85 "--xattrs (-X)",
86 "--inplace",
87 "--append",
88 "--append-verify",
d3e182af
WD
89 NULL
90};
91
92void write_stream_flags(int fd)
93{
94 int i, flags;
95
3cc185a0
WD
96#if Z_DEFAULT_COMPRESSION == -1
97 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
98#else
99#error internal logic error! Fix def_compress_level logic above and below too!
da01d2e8
WD
100#endif
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. */
e7f7064c
WD
109 if (protocol_version < 29)
110 flag_ptr[7] = NULL;
da01d2e8
WD
111 else if (protocol_version < 30)
112 flag_ptr[9] = NULL;
d3e182af
WD
113 for (i = 0, flags = 0; flag_ptr[i]; i++) {
114 if (*flag_ptr[i])
115 flags |= 1 << i;
116 }
117 write_int(fd, flags);
118}
119
120void read_stream_flags(int fd)
121{
122 int i, flags;
123
6bf82264 124 if (protocol_version < 29)
e7f7064c 125 flag_ptr[7] = NULL;
da01d2e8
WD
126 else if (protocol_version < 30)
127 flag_ptr[9] = NULL;
128 tweaked_append = append_mode == 1;
129 tweaked_append_verify = append_mode == 2;
130#ifdef ICONV_OPTION
131 tweaked_iconv = iconv_opt != NULL;
132#endif
d3e182af
WD
133 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
134 int set = flags & (1 << i) ? 1 : 0;
135 if (*flag_ptr[i] != set) {
da01d2e8
WD
136 if (i == 9) {
137 rprintf(FERROR,
138 "%s specify the --iconv option to use this batch file.\n",
139 set ? "Please" : "Do not");
140 exit_cleanup(RERR_SYNTAX);
141 }
6a48e792
WD
142 if (verbose) {
143 rprintf(FINFO,
144 "%sing the %s option to match the batchfile.\n",
145 set ? "Sett" : "Clear", flag_name[i]);
146 }
d3e182af
WD
147 *flag_ptr[i] = set;
148 }
149 }
e7f7064c
WD
150 if (protocol_version < 29) {
151 if (recurse)
152 xfer_dirs |= 1;
153 else if (xfer_dirs < 2)
154 xfer_dirs = 0;
155 }
3cc185a0
WD
156
157 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
158 do_compression = 0;
159 else {
160 do_compression = 1;
161 def_compress_level = tweaked_compress_level - 2;
162 }
da01d2e8
WD
163 if (tweaked_append)
164 append_mode = 1;
165 else if (tweaked_append_verify)
166 append_mode = 2;
d3e182af
WD
167}
168
e7a69008
WD
169static void write_arg(int fd, char *arg)
170{
171 char *x, *s;
172
173 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
174 write(fd, arg, x - arg + 1);
175 arg += x - arg + 1;
176 }
177
178 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
179 write(fd, "'", 1);
180 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
181 write(fd, s, x - s + 1);
182 write(fd, "'", 1);
183 }
184 write(fd, s, strlen(s));
185 write(fd, "'", 1);
186 return;
187 }
188
189 write(fd, arg, strlen(arg));
190}
191
8261047b 192static void write_filter_rules(int fd)
73f0ce69 193{
7842418b 194 struct filter_struct *ent;
73f0ce69
WD
195
196 write_sbuf(fd, " <<'#E#'\n");
7842418b 197 for (ent = filter_list.head; ent; ent = ent->next) {
8261047b 198 unsigned int plen;
dd667c23 199 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
8261047b
WD
200 write_buf(fd, p, plen);
201 write_sbuf(fd, ent->pattern);
73f0ce69
WD
202 if (ent->match_flags & MATCHFLG_DIRECTORY)
203 write_byte(fd, '/');
204 write_byte(fd, eol_nulls ? 0 : '\n');
205 }
206 if (eol_nulls)
207 write_sbuf(fd, ";\n");
208 write_sbuf(fd, "#E#");
209}
210
e7a69008
WD
211/* This routine tries to write out an equivalent --read-batch command
212 * given the user's --write-batch args. However, it doesn't really
213 * understand most of the options, so it uses some overly simple
214 * heuristics to munge the command line into something that will
215 * (hopefully) work. */
216void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
6902ed17 217{
d630f53e 218 int fd, i, len;
e7a69008 219 char *p, filename[MAXPATHLEN];
088aac85 220
893c4cc0 221 stringjoin(filename, sizeof filename,
b462781f 222 batch_name, ".sh", NULL);
01966df4
WD
223 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
224 S_IRUSR | S_IWUSR | S_IEXEC);
225 if (fd < 0) {
4875d6b6 226 rsyserr(FERROR, errno, "Batch file %s open error",
45c49b52 227 filename);
da01d2e8 228 exit_cleanup(RERR_FILESELECT);
1cd5beeb 229 }
088aac85 230
e7a69008
WD
231 /* Write argvs info to BATCH.sh file */
232 write_arg(fd, argv[0]);
8261047b
WD
233 if (filter_list.head) {
234 if (protocol_version >= 29)
235 write_sbuf(fd, " --filter=._-");
236 else
237 write_sbuf(fd, " --exclude-from=-");
238 }
e7a69008
WD
239 for (i = 1; i < argc - file_arg_cnt; i++) {
240 p = argv[i];
241 if (strncmp(p, "--files-from", 12) == 0
8261047b 242 || strncmp(p, "--filter", 8) == 0
73f0ce69
WD
243 || strncmp(p, "--include", 9) == 0
244 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 245 if (strchr(p, '=') == NULL)
b462781f 246 i++;
b9f592fb 247 continue;
b462781f 248 }
8261047b
WD
249 if (strcmp(p, "-f") == 0) {
250 i++;
251 continue;
252 }
e7a69008 253 write(fd, " ", 1);
d630f53e
WD
254 if (strncmp(p, "--write-batch", len = 13) == 0
255 || strncmp(p, "--only-write-batch", len = 18) == 0) {
e7a69008 256 write(fd, "--read-batch", 12);
d630f53e 257 if (p[len] == '=') {
e7a69008 258 write(fd, "=", 1);
d630f53e 259 write_arg(fd, p + len + 1);
e7a69008 260 }
088aac85 261 } else
e7a69008 262 write_arg(fd, p);
1cd5beeb 263 }
4d3abf13 264 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
e7a69008
WD
265 p = argv[argc - 1];
266 write(fd, " ${1:-", 6);
267 write_arg(fd, p);
73f0ce69 268 write_byte(fd, '}');
7842418b 269 if (filter_list.head)
8261047b 270 write_filter_rules(fd);
73f0ce69 271 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
4875d6b6 272 rsyserr(FERROR, errno, "Batch file %s write error",
45c49b52 273 filename);
da01d2e8 274 exit_cleanup(RERR_FILEIO);
1cd5beeb 275 }
6902ed17 276}