Switching to GPL 3.
[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-2007 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 version 3 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, visit the http://fsf.org website.
19 */
20
21#include "rsync.h"
22#include "zlib/zlib.h"
23#include <time.h>
24
25extern int eol_nulls;
26extern int recurse;
27extern int xfer_dirs;
28extern int preserve_links;
29extern int preserve_hard_links;
30extern int preserve_devices;
31extern int preserve_uid;
32extern int preserve_gid;
33extern int always_checksum;
34extern int do_compression;
35extern int def_compress_level;
36extern int protocol_version;
37extern char *batch_name;
38
39extern struct filter_list_struct filter_list;
40
41static int tweaked_compress_level;
42
43static int *flag_ptr[] = {
44 &recurse, /* 0 */
45 &preserve_uid, /* 1 */
46 &preserve_gid, /* 2 */
47 &preserve_links, /* 3 */
48 &preserve_devices, /* 4 */
49 &preserve_hard_links, /* 5 */
50 &always_checksum, /* 6 */
51 &xfer_dirs, /* 7 (protocol 29) */
52 &tweaked_compress_level,/* 8 (protocol 29) */
53 NULL
54};
55
56static char *flag_name[] = {
57 "--recurse (-r)",
58 "--owner (-o)",
59 "--group (-g)",
60 "--links (-l)",
61 "--devices (-D)",
62 "--hard-links (-H)",
63 "--checksum (-c)",
64 "--dirs (-d)",
65 "--compress (-z)",
66 NULL
67};
68
69void write_stream_flags(int fd)
70{
71 int i, flags;
72
73#if Z_DEFAULT_COMPRESSION == -1
74 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
75#else
76#error internal logic error! Fix def_compress_level logic above and below too!
77#endif
78
79 /* Start the batch file with a bitmap of data-stream-affecting
80 * flags. */
81 if (protocol_version < 29)
82 flag_ptr[7] = NULL;
83 for (i = 0, flags = 0; flag_ptr[i]; i++) {
84 if (*flag_ptr[i])
85 flags |= 1 << i;
86 }
87 write_int(fd, flags);
88}
89
90void read_stream_flags(int fd)
91{
92 int i, flags;
93
94 if (protocol_version < 29)
95 flag_ptr[7] = NULL;
96 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
97 int set = flags & (1 << i) ? 1 : 0;
98 if (*flag_ptr[i] != set) {
99 if (verbose) {
100 rprintf(FINFO,
101 "%sing the %s option to match the batchfile.\n",
102 set ? "Sett" : "Clear", flag_name[i]);
103 }
104 *flag_ptr[i] = set;
105 }
106 }
107 if (protocol_version < 29) {
108 if (recurse)
109 xfer_dirs |= 1;
110 else if (xfer_dirs < 2)
111 xfer_dirs = 0;
112 }
113
114 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
115 do_compression = 0;
116 else {
117 do_compression = 1;
118 def_compress_level = tweaked_compress_level - 2;
119 }
120}
121
122static void write_arg(int fd, char *arg)
123{
124 char *x, *s;
125
126 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
127 write(fd, arg, x - arg + 1);
128 arg += x - arg + 1;
129 }
130
131 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
132 write(fd, "'", 1);
133 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
134 write(fd, s, x - s + 1);
135 write(fd, "'", 1);
136 }
137 write(fd, s, strlen(s));
138 write(fd, "'", 1);
139 return;
140 }
141
142 write(fd, arg, strlen(arg));
143}
144
145static void write_filter_rules(int fd)
146{
147 struct filter_struct *ent;
148
149 write_sbuf(fd, " <<'#E#'\n");
150 for (ent = filter_list.head; ent; ent = ent->next) {
151 unsigned int plen;
152 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
153 write_buf(fd, p, plen);
154 write_sbuf(fd, ent->pattern);
155 if (ent->match_flags & MATCHFLG_DIRECTORY)
156 write_byte(fd, '/');
157 write_byte(fd, eol_nulls ? 0 : '\n');
158 }
159 if (eol_nulls)
160 write_sbuf(fd, ";\n");
161 write_sbuf(fd, "#E#");
162}
163
164/* This routine tries to write out an equivalent --read-batch command
165 * given the user's --write-batch args. However, it doesn't really
166 * understand most of the options, so it uses some overly simple
167 * heuristics to munge the command line into something that will
168 * (hopefully) work. */
169void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
170{
171 int fd, i, len;
172 char *p, filename[MAXPATHLEN];
173
174 stringjoin(filename, sizeof filename,
175 batch_name, ".sh", NULL);
176 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
177 S_IRUSR | S_IWUSR | S_IEXEC);
178 if (fd < 0) {
179 rsyserr(FERROR, errno, "Batch file %s open error",
180 filename);
181 exit_cleanup(1);
182 }
183
184 /* Write argvs info to BATCH.sh file */
185 write_arg(fd, argv[0]);
186 if (filter_list.head) {
187 if (protocol_version >= 29)
188 write_sbuf(fd, " --filter=._-");
189 else
190 write_sbuf(fd, " --exclude-from=-");
191 }
192 for (i = 1; i < argc - file_arg_cnt; i++) {
193 p = argv[i];
194 if (strncmp(p, "--files-from", 12) == 0
195 || strncmp(p, "--filter", 8) == 0
196 || strncmp(p, "--include", 9) == 0
197 || strncmp(p, "--exclude", 9) == 0) {
198 if (strchr(p, '=') == NULL)
199 i++;
200 continue;
201 }
202 if (strcmp(p, "-f") == 0) {
203 i++;
204 continue;
205 }
206 write(fd, " ", 1);
207 if (strncmp(p, "--write-batch", len = 13) == 0
208 || strncmp(p, "--only-write-batch", len = 18) == 0) {
209 write(fd, "--read-batch", 12);
210 if (p[len] == '=') {
211 write(fd, "=", 1);
212 write_arg(fd, p + len + 1);
213 }
214 } else
215 write_arg(fd, p);
216 }
217 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
218 p = argv[argc - 1];
219 write(fd, " ${1:-", 6);
220 write_arg(fd, p);
221 write_byte(fd, '}');
222 if (filter_list.head)
223 write_filter_rules(fd);
224 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
225 rsyserr(FERROR, errno, "Batch file %s write error",
226 filename);
227 exit_cleanup(1);
228 }
229}