Martin gave his approval to use GPLv3 with this code.
[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
4fd842f9 9 * it under the terms of the GNU General Public License version 3 as
ba2133d6 10 * published by the Free Software Foundation.
0f78b815
WD
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 *
e7c67065 17 * You should have received a copy of the GNU General Public License along
4fd842f9 18 * with this program; if not, visit the http://fsf.org website.
0f78b815 19 */
6902ed17
MP
20
21#include "rsync.h"
3cc185a0 22#include "zlib/zlib.h"
6902ed17
MP
23#include <time.h>
24
73f0ce69 25extern int eol_nulls;
d3e182af 26extern int recurse;
6bf82264 27extern int xfer_dirs;
d3e182af
WD
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;
e7f7064c 34extern int do_compression;
3cc185a0 35extern int def_compress_level;
8261047b 36extern int protocol_version;
2b136663 37extern char *batch_name;
73f0ce69 38
7842418b 39extern struct filter_list_struct filter_list;
e8d3168e 40
3cc185a0
WD
41static int tweaked_compress_level;
42
d3e182af 43static int *flag_ptr[] = {
e7f7064c 44 &recurse, /* 0 */
658a6369
WD
45 &preserve_uid, /* 1 */
46 &preserve_gid, /* 2 */
e7f7064c
WD
47 &preserve_links, /* 3 */
48 &preserve_devices, /* 4 */
49 &preserve_hard_links, /* 5 */
50 &always_checksum, /* 6 */
51 &xfer_dirs, /* 7 (protocol 29) */
3cc185a0 52 &tweaked_compress_level,/* 8 (protocol 29) */
d3e182af
WD
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)",
6bf82264 64 "--dirs (-d)",
e7f7064c 65 "--compress (-z)",
d3e182af
WD
66 NULL
67};
68
69void write_stream_flags(int fd)
70{
71 int i, flags;
72
3cc185a0
WD
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
d3e182af
WD
79 /* Start the batch file with a bitmap of data-stream-affecting
80 * flags. */
e7f7064c
WD
81 if (protocol_version < 29)
82 flag_ptr[7] = NULL;
d3e182af
WD
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
6bf82264 94 if (protocol_version < 29)
e7f7064c 95 flag_ptr[7] = NULL;
d3e182af
WD
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) {
6a48e792
WD
99 if (verbose) {
100 rprintf(FINFO,
101 "%sing the %s option to match the batchfile.\n",
102 set ? "Sett" : "Clear", flag_name[i]);
103 }
d3e182af
WD
104 *flag_ptr[i] = set;
105 }
106 }
e7f7064c
WD
107 if (protocol_version < 29) {
108 if (recurse)
109 xfer_dirs |= 1;
110 else if (xfer_dirs < 2)
111 xfer_dirs = 0;
112 }
3cc185a0
WD
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 }
d3e182af
WD
120}
121
e7a69008
WD
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
8261047b 145static void write_filter_rules(int fd)
73f0ce69 146{
7842418b 147 struct filter_struct *ent;
73f0ce69
WD
148
149 write_sbuf(fd, " <<'#E#'\n");
7842418b 150 for (ent = filter_list.head; ent; ent = ent->next) {
8261047b 151 unsigned int plen;
dd667c23 152 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
8261047b
WD
153 write_buf(fd, p, plen);
154 write_sbuf(fd, ent->pattern);
73f0ce69
WD
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
e7a69008
WD
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)
6902ed17 170{
d630f53e 171 int fd, i, len;
e7a69008 172 char *p, filename[MAXPATHLEN];
088aac85 173
893c4cc0 174 stringjoin(filename, sizeof filename,
b462781f 175 batch_name, ".sh", NULL);
01966df4
WD
176 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
177 S_IRUSR | S_IWUSR | S_IEXEC);
178 if (fd < 0) {
4875d6b6 179 rsyserr(FERROR, errno, "Batch file %s open error",
45c49b52 180 filename);
1cd5beeb
MP
181 exit_cleanup(1);
182 }
088aac85 183
e7a69008
WD
184 /* Write argvs info to BATCH.sh file */
185 write_arg(fd, argv[0]);
8261047b
WD
186 if (filter_list.head) {
187 if (protocol_version >= 29)
188 write_sbuf(fd, " --filter=._-");
189 else
190 write_sbuf(fd, " --exclude-from=-");
191 }
e7a69008
WD
192 for (i = 1; i < argc - file_arg_cnt; i++) {
193 p = argv[i];
194 if (strncmp(p, "--files-from", 12) == 0
8261047b 195 || strncmp(p, "--filter", 8) == 0
73f0ce69
WD
196 || strncmp(p, "--include", 9) == 0
197 || strncmp(p, "--exclude", 9) == 0) {
e7a69008 198 if (strchr(p, '=') == NULL)
b462781f 199 i++;
b9f592fb 200 continue;
b462781f 201 }
8261047b
WD
202 if (strcmp(p, "-f") == 0) {
203 i++;
204 continue;
205 }
e7a69008 206 write(fd, " ", 1);
d630f53e
WD
207 if (strncmp(p, "--write-batch", len = 13) == 0
208 || strncmp(p, "--only-write-batch", len = 18) == 0) {
e7a69008 209 write(fd, "--read-batch", 12);
d630f53e 210 if (p[len] == '=') {
e7a69008 211 write(fd, "=", 1);
d630f53e 212 write_arg(fd, p + len + 1);
e7a69008 213 }
088aac85 214 } else
e7a69008 215 write_arg(fd, p);
1cd5beeb 216 }
4d3abf13 217 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
e7a69008
WD
218 p = argv[argc - 1];
219 write(fd, " ${1:-", 6);
220 write_arg(fd, p);
73f0ce69 221 write_byte(fd, '}');
7842418b 222 if (filter_list.head)
8261047b 223 write_filter_rules(fd);
73f0ce69 224 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
4875d6b6 225 rsyserr(FERROR, errno, "Batch file %s write error",
45c49b52 226 filename);
1cd5beeb
MP
227 exit_cleanup(1);
228 }
6902ed17 229}