Tweaked the output a little.
[rsync/rsync.git] / batch.c
... / ...
CommitLineData
1/*
2 * Support for the batch-file options.
3 *
4 * Copyright (C) 1999 Weiss
5 * Copyright (C) 2004 Chris Shoemaker
6 * Copyright (C) 2004, 2005, 2006 Wayne Davison
7 *
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.
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 *
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.
21 */
22
23#include "rsync.h"
24#include "zlib/zlib.h"
25#include <time.h>
26
27extern int eol_nulls;
28extern int recurse;
29extern int xfer_dirs;
30extern int preserve_links;
31extern int preserve_hard_links;
32extern int preserve_devices;
33extern int preserve_uid;
34extern int preserve_gid;
35extern int always_checksum;
36extern int do_compression;
37extern int def_compress_level;
38extern int protocol_version;
39extern int flist_extra_cnt;
40extern char *batch_name;
41
42extern struct filter_list_struct filter_list;
43
44static int tweaked_preserve_uid;
45static int tweaked_preserve_gid;
46static int tweaked_compress_level;
47
48static int *flag_ptr[] = {
49 &recurse, /* 0 */
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) */
58 NULL
59};
60
61static char *flag_name[] = {
62 "--recurse (-r)",
63 "--owner (-o)",
64 "--group (-g)",
65 "--links (-l)",
66 "--devices (-D)",
67 "--hard-links (-H)",
68 "--checksum (-c)",
69 "--dirs (-d)",
70 "--compress (-z)",
71 NULL
72};
73
74void write_stream_flags(int fd)
75{
76 int i, flags;
77
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;
82#else
83#error internal logic error! Fix def_compress_level logic above and below too!
84#endif
85
86 /* Start the batch file with a bitmap of data-stream-affecting
87 * flags. */
88 if (protocol_version < 29)
89 flag_ptr[7] = NULL;
90 for (i = 0, flags = 0; flag_ptr[i]; i++) {
91 if (*flag_ptr[i])
92 flags |= 1 << i;
93 }
94 write_int(fd, flags);
95}
96
97void read_stream_flags(int fd)
98{
99 int i, flags;
100
101 if (protocol_version < 29)
102 flag_ptr[7] = NULL;
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) {
106 if (verbose) {
107 rprintf(FINFO,
108 "%sing the %s option to match the batchfile.\n",
109 set ? "Sett" : "Clear", flag_name[i]);
110 }
111 *flag_ptr[i] = set;
112 }
113 }
114 if (protocol_version < 29) {
115 if (recurse)
116 xfer_dirs |= 1;
117 else if (xfer_dirs < 2)
118 xfer_dirs = 0;
119 }
120
121 if (tweaked_preserve_uid) {
122 if (!preserve_uid)
123 preserve_uid = ++flist_extra_cnt;
124 } else
125 preserve_uid = 0;
126 if (tweaked_preserve_gid) {
127 if (!preserve_gid)
128 preserve_gid = ++flist_extra_cnt;
129 } else
130 preserve_gid = 0;
131 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
132 do_compression = 0;
133 else {
134 do_compression = 1;
135 def_compress_level = tweaked_compress_level - 2;
136 }
137}
138
139static void write_arg(int fd, char *arg)
140{
141 char *x, *s;
142
143 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
144 write(fd, arg, x - arg + 1);
145 arg += x - arg + 1;
146 }
147
148 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
149 write(fd, "'", 1);
150 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
151 write(fd, s, x - s + 1);
152 write(fd, "'", 1);
153 }
154 write(fd, s, strlen(s));
155 write(fd, "'", 1);
156 return;
157 }
158
159 write(fd, arg, strlen(arg));
160}
161
162static void write_filter_rules(int fd)
163{
164 struct filter_struct *ent;
165
166 write_sbuf(fd, " <<'#E#'\n");
167 for (ent = filter_list.head; ent; ent = ent->next) {
168 unsigned int plen;
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)
173 write_byte(fd, '/');
174 write_byte(fd, eol_nulls ? 0 : '\n');
175 }
176 if (eol_nulls)
177 write_sbuf(fd, ";\n");
178 write_sbuf(fd, "#E#");
179}
180
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. */
186void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
187{
188 int fd, i, len;
189 char *p, filename[MAXPATHLEN];
190
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);
195 if (fd < 0) {
196 rsyserr(FERROR, errno, "Batch file %s open error",
197 filename);
198 exit_cleanup(1);
199 }
200
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=._-");
206 else
207 write_sbuf(fd, " --exclude-from=-");
208 }
209 for (i = 1; i < argc - file_arg_cnt; i++) {
210 p = argv[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)
216 i++;
217 continue;
218 }
219 if (strcmp(p, "-f") == 0) {
220 i++;
221 continue;
222 }
223 write(fd, " ", 1);
224 if (strncmp(p, "--write-batch", len = 13) == 0
225 || strncmp(p, "--only-write-batch", len = 18) == 0) {
226 write(fd, "--read-batch", 12);
227 if (p[len] == '=') {
228 write(fd, "=", 1);
229 write_arg(fd, p + len + 1);
230 }
231 } else
232 write_arg(fd, p);
233 }
234 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
235 p = argv[argc - 1];
236 write(fd, " ${1:-", 6);
237 write_arg(fd, p);
238 write_byte(fd, '}');
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",
243 filename);
244 exit_cleanup(1);
245 }
246}