Tweaked the output a little.
[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
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 *
e7c67065
WD
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.
0f78b815 21 */
6902ed17
MP
22
23#include "rsync.h"
3cc185a0 24#include "zlib/zlib.h"
6902ed17
MP
25#include <time.h>
26
73f0ce69 27extern int eol_nulls;
d3e182af 28extern int recurse;
6bf82264 29extern int xfer_dirs;
d3e182af
WD
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;
e7f7064c 36extern int do_compression;
3cc185a0 37extern int def_compress_level;
8261047b 38extern int protocol_version;
f0fbf1d6 39extern int flist_extra_cnt;
2b136663 40extern char *batch_name;
73f0ce69 41
7842418b 42extern struct filter_list_struct filter_list;
e8d3168e 43
82ad07c4
WD
44static int tweaked_preserve_uid;
45static int tweaked_preserve_gid;
3cc185a0
WD
46static int tweaked_compress_level;
47
d3e182af 48static int *flag_ptr[] = {
e7f7064c 49 &recurse, /* 0 */
82ad07c4
WD
50 &tweaked_preserve_uid, /* 1 */
51 &tweaked_preserve_gid, /* 2 */
e7f7064c
WD
52 &preserve_links, /* 3 */
53 &preserve_devices, /* 4 */
54 &preserve_hard_links, /* 5 */
55 &always_checksum, /* 6 */
56 &xfer_dirs, /* 7 (protocol 29) */
3cc185a0 57 &tweaked_compress_level,/* 8 (protocol 29) */
d3e182af
WD
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)",
6bf82264 69 "--dirs (-d)",
e7f7064c 70 "--compress (-z)",
d3e182af
WD
71 NULL
72};
73
74void write_stream_flags(int fd)
75{
76 int i, flags;
77
82ad07c4
WD
78 tweaked_preserve_uid = preserve_uid != 0;
79 tweaked_preserve_gid = preserve_gid != 0;
3cc185a0
WD
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
d3e182af
WD
86 /* Start the batch file with a bitmap of data-stream-affecting
87 * flags. */
e7f7064c
WD
88 if (protocol_version < 29)
89 flag_ptr[7] = NULL;
d3e182af
WD
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
6bf82264 101 if (protocol_version < 29)
e7f7064c 102 flag_ptr[7] = NULL;
d3e182af
WD
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) {
6a48e792
WD
106 if (verbose) {
107 rprintf(FINFO,
108 "%sing the %s option to match the batchfile.\n",
109 set ? "Sett" : "Clear", flag_name[i]);
110 }
d3e182af
WD
111 *flag_ptr[i] = set;
112 }
113 }
e7f7064c
WD
114 if (protocol_version < 29) {
115 if (recurse)
116 xfer_dirs |= 1;
117 else if (xfer_dirs < 2)
118 xfer_dirs = 0;
119 }
3cc185a0 120
82ad07c4
WD
121 if (tweaked_preserve_uid) {
122 if (!preserve_uid)
f0fbf1d6 123 preserve_uid = ++flist_extra_cnt;
82ad07c4
WD
124 } else
125 preserve_uid = 0;
126 if (tweaked_preserve_gid) {
127 if (!preserve_gid)
f0fbf1d6 128 preserve_gid = ++flist_extra_cnt;
82ad07c4
WD
129 } else
130 preserve_gid = 0;
3cc185a0
WD
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 }
d3e182af
WD
137}
138
e7a69008
WD
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
8261047b 162static void write_filter_rules(int fd)
73f0ce69 163{
7842418b 164 struct filter_struct *ent;
73f0ce69
WD
165
166 write_sbuf(fd, " <<'#E#'\n");
7842418b 167 for (ent = filter_list.head; ent; ent = ent->next) {
8261047b 168 unsigned int plen;
dd667c23 169 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
8261047b
WD
170 write_buf(fd, p, plen);
171 write_sbuf(fd, ent->pattern);
73f0ce69
WD
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
e7a69008
WD
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)
6902ed17 187{
d630f53e 188 int fd, i, len;
e7a69008 189 char *p, filename[MAXPATHLEN];
088aac85 190
893c4cc0 191 stringjoin(filename, sizeof filename,
b462781f 192 batch_name, ".sh", NULL);
01966df4
WD
193 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
194 S_IRUSR | S_IWUSR | S_IEXEC);
195 if (fd < 0) {
4875d6b6 196 rsyserr(FERROR, errno, "Batch file %s open error",
45c49b52 197 filename);
1cd5beeb
MP
198 exit_cleanup(1);
199 }
088aac85 200
e7a69008
WD
201 /* Write argvs info to BATCH.sh file */
202 write_arg(fd, argv[0]);
8261047b
WD
203 if (filter_list.head) {
204 if (protocol_version >= 29)
205 write_sbuf(fd, " --filter=._-");
206 else
207 write_sbuf(fd, " --exclude-from=-");
208 }
e7a69008
WD
209 for (i = 1; i < argc - file_arg_cnt; i++) {
210 p = argv[i];
211 if (strncmp(p, "--files-from", 12) == 0
8261047b 212 || strncmp(p, "--filter", 8) == 0
73f0ce69
WD
213 || strncmp(p, "--include", 9) == 0
214 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 215 if (strchr(p, '=') == NULL)
b462781f 216 i++;
b9f592fb 217 continue;
b462781f 218 }
8261047b
WD
219 if (strcmp(p, "-f") == 0) {
220 i++;
221 continue;
222 }
e7a69008 223 write(fd, " ", 1);
d630f53e
WD
224 if (strncmp(p, "--write-batch", len = 13) == 0
225 || strncmp(p, "--only-write-batch", len = 18) == 0) {
e7a69008 226 write(fd, "--read-batch", 12);
d630f53e 227 if (p[len] == '=') {
e7a69008 228 write(fd, "=", 1);
d630f53e 229 write_arg(fd, p + len + 1);
e7a69008 230 }
088aac85 231 } else
e7a69008 232 write_arg(fd, p);
1cd5beeb 233 }
4d3abf13 234 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
e7a69008
WD
235 p = argv[argc - 1];
236 write(fd, " ${1:-", 6);
237 write_arg(fd, p);
73f0ce69 238 write_byte(fd, '}');
7842418b 239 if (filter_list.head)
8261047b 240 write_filter_rules(fd);
73f0ce69 241 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
4875d6b6 242 rsyserr(FERROR, errno, "Batch file %s write error",
45c49b52 243 filename);
1cd5beeb
MP
244 exit_cleanup(1);
245 }
6902ed17 246}