Change the references to "service" to be either "section" or "module".
[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
d3d07a5e 6 * Copyright (C) 2004-2008 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;
da01d2e8
WD
38extern int inplace;
39extern int append_mode;
8261047b 40extern int protocol_version;
2b136663 41extern char *batch_name;
da01d2e8
WD
42#ifdef ICONV_OPTION
43extern char *iconv_opt;
44#endif
73f0ce69 45
7842418b 46extern struct filter_list_struct filter_list;
e8d3168e 47
a7c1fa00
WD
48int batch_stream_flags;
49
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) */
a7c1fa00 63 &do_compression, /* 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
da01d2e8
WD
96 tweaked_append = append_mode == 1;
97 tweaked_append_verify = append_mode == 2;
98#ifdef ICONV_OPTION
99 tweaked_iconv = iconv_opt != NULL;
3cc185a0
WD
100#endif
101
d3e182af
WD
102 /* Start the batch file with a bitmap of data-stream-affecting
103 * flags. */
104 for (i = 0, flags = 0; flag_ptr[i]; i++) {
105 if (*flag_ptr[i])
106 flags |= 1 << i;
107 }
108 write_int(fd, flags);
109}
110
111void read_stream_flags(int fd)
112{
a7c1fa00
WD
113 batch_stream_flags = read_int(fd);
114}
115
116void check_batch_flags(void)
117{
118 int i;
d3e182af 119
6bf82264 120 if (protocol_version < 29)
e7f7064c 121 flag_ptr[7] = NULL;
da01d2e8
WD
122 else if (protocol_version < 30)
123 flag_ptr[9] = NULL;
124 tweaked_append = append_mode == 1;
125 tweaked_append_verify = append_mode == 2;
126#ifdef ICONV_OPTION
127 tweaked_iconv = iconv_opt != NULL;
128#endif
a7c1fa00
WD
129 for (i = 0; flag_ptr[i]; i++) {
130 int set = batch_stream_flags & (1 << i) ? 1 : 0;
d3e182af 131 if (*flag_ptr[i] != set) {
da01d2e8 132 if (i == 9) {
a7c1fa00 133 rprintf(FERROR,
da01d2e8
WD
134 "%s specify the --iconv option to use this batch file.\n",
135 set ? "Please" : "Do not");
136 exit_cleanup(RERR_SYNTAX);
137 }
951e826b 138 if (INFO_GTE(MISC, 1)) {
6a48e792
WD
139 rprintf(FINFO,
140 "%sing the %s option to match the batchfile.\n",
141 set ? "Sett" : "Clear", flag_name[i]);
142 }
d3e182af
WD
143 *flag_ptr[i] = set;
144 }
145 }
e7f7064c
WD
146 if (protocol_version < 29) {
147 if (recurse)
148 xfer_dirs |= 1;
149 else if (xfer_dirs < 2)
150 xfer_dirs = 0;
151 }
3cc185a0 152
da01d2e8
WD
153 if (tweaked_append)
154 append_mode = 1;
155 else if (tweaked_append_verify)
156 append_mode = 2;
d3e182af
WD
157}
158
e7a69008
WD
159static void write_arg(int fd, char *arg)
160{
161 char *x, *s;
162
163 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
164 write(fd, arg, x - arg + 1);
165 arg += x - arg + 1;
166 }
167
168 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
169 write(fd, "'", 1);
170 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
171 write(fd, s, x - s + 1);
172 write(fd, "'", 1);
173 }
174 write(fd, s, strlen(s));
175 write(fd, "'", 1);
176 return;
177 }
178
179 write(fd, arg, strlen(arg));
180}
181
8261047b 182static void write_filter_rules(int fd)
73f0ce69 183{
7842418b 184 struct filter_struct *ent;
73f0ce69
WD
185
186 write_sbuf(fd, " <<'#E#'\n");
7842418b 187 for (ent = filter_list.head; ent; ent = ent->next) {
8261047b 188 unsigned int plen;
dd667c23 189 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
8261047b
WD
190 write_buf(fd, p, plen);
191 write_sbuf(fd, ent->pattern);
73f0ce69
WD
192 if (ent->match_flags & MATCHFLG_DIRECTORY)
193 write_byte(fd, '/');
194 write_byte(fd, eol_nulls ? 0 : '\n');
195 }
196 if (eol_nulls)
197 write_sbuf(fd, ";\n");
198 write_sbuf(fd, "#E#");
199}
200
e7a69008
WD
201/* This routine tries to write out an equivalent --read-batch command
202 * given the user's --write-batch args. However, it doesn't really
203 * understand most of the options, so it uses some overly simple
204 * heuristics to munge the command line into something that will
205 * (hopefully) work. */
206void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
6902ed17 207{
d630f53e 208 int fd, i, len;
e7a69008 209 char *p, filename[MAXPATHLEN];
088aac85 210
893c4cc0 211 stringjoin(filename, sizeof filename,
b462781f 212 batch_name, ".sh", NULL);
01966df4
WD
213 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
214 S_IRUSR | S_IWUSR | S_IEXEC);
215 if (fd < 0) {
4875d6b6 216 rsyserr(FERROR, errno, "Batch file %s open error",
45c49b52 217 filename);
da01d2e8 218 exit_cleanup(RERR_FILESELECT);
1cd5beeb 219 }
088aac85 220
e7a69008
WD
221 /* Write argvs info to BATCH.sh file */
222 write_arg(fd, argv[0]);
8261047b
WD
223 if (filter_list.head) {
224 if (protocol_version >= 29)
225 write_sbuf(fd, " --filter=._-");
226 else
227 write_sbuf(fd, " --exclude-from=-");
228 }
e7a69008
WD
229 for (i = 1; i < argc - file_arg_cnt; i++) {
230 p = argv[i];
231 if (strncmp(p, "--files-from", 12) == 0
8261047b 232 || strncmp(p, "--filter", 8) == 0
73f0ce69
WD
233 || strncmp(p, "--include", 9) == 0
234 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 235 if (strchr(p, '=') == NULL)
b462781f 236 i++;
b9f592fb 237 continue;
b462781f 238 }
8261047b
WD
239 if (strcmp(p, "-f") == 0) {
240 i++;
241 continue;
242 }
e7a69008 243 write(fd, " ", 1);
d630f53e
WD
244 if (strncmp(p, "--write-batch", len = 13) == 0
245 || strncmp(p, "--only-write-batch", len = 18) == 0) {
e7a69008 246 write(fd, "--read-batch", 12);
d630f53e 247 if (p[len] == '=') {
e7a69008 248 write(fd, "=", 1);
d630f53e 249 write_arg(fd, p + len + 1);
e7a69008 250 }
088aac85 251 } else
e7a69008 252 write_arg(fd, p);
1cd5beeb 253 }
4d3abf13 254 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
e7a69008
WD
255 p = argv[argc - 1];
256 write(fd, " ${1:-", 6);
257 write_arg(fd, p);
73f0ce69 258 write_byte(fd, '}');
7842418b 259 if (filter_list.head)
8261047b 260 write_filter_rules(fd);
73f0ce69 261 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
4875d6b6 262 rsyserr(FERROR, errno, "Batch file %s write error",
45c49b52 263 filename);
da01d2e8 264 exit_cleanup(RERR_FILEIO);
1cd5beeb 265 }
6902ed17 266}