blob: b8d99a06848ecda17ecbaea268ef8a8e0cc18df7 [file] [log] [blame]
Wayne Davison0f78b812006-04-25 20:23:34 +00001/*
2 * Support for the batch-file options.
3 *
4 * Copyright (C) 1999 Weiss
5 * Copyright (C) 2004 Chris Shoemaker
Wayne Davisonba2133d2007-02-04 14:54:58 +00006 * Copyright (C) 2004-2007 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00007 *
8 * This program is free software; you can redistribute it and/or modify
Wayne Davison8e41b682007-07-10 13:55:49 +00009 * 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.
Wayne Davison0f78b812006-04-25 20:23:34 +000012 *
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 *
Wayne Davisone7c67062006-04-25 23:51:12 +000018 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000019 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000020 */
Martin Pool6902ed12001-08-14 02:04:47 +000021
22#include "rsync.h"
Wayne Davison3cc185a2005-10-26 16:48:07 +000023#include "zlib/zlib.h"
Martin Pool6902ed12001-08-14 02:04:47 +000024#include <time.h>
25
Wayne Davison73f0ce62004-07-20 17:07:55 +000026extern int eol_nulls;
Wayne Davisond3e182a2004-07-24 16:38:49 +000027extern int recurse;
Wayne Davison6bf82262005-02-02 09:40:45 +000028extern int xfer_dirs;
Wayne Davisond3e182a2004-07-24 16:38:49 +000029extern int preserve_links;
30extern int preserve_hard_links;
31extern int preserve_devices;
32extern int preserve_uid;
33extern int preserve_gid;
Wayne Davisonda01d2e2008-01-19 11:21:07 -080034extern int preserve_acls;
35extern int preserve_xattrs;
Wayne Davisond3e182a2004-07-24 16:38:49 +000036extern int always_checksum;
Wayne Davisone7f70642005-03-27 05:02:49 +000037extern int do_compression;
Wayne Davisonda01d2e2008-01-19 11:21:07 -080038extern int inplace;
39extern int append_mode;
Wayne Davison82610472005-01-25 12:13:53 +000040extern int protocol_version;
Wayne Davison2b136662005-02-01 09:21:28 +000041extern char *batch_name;
Wayne Davisonda01d2e2008-01-19 11:21:07 -080042#ifdef ICONV_OPTION
43extern char *iconv_opt;
44#endif
Wayne Davison73f0ce62004-07-20 17:07:55 +000045
Wayne Davison78424182005-01-25 10:39:14 +000046extern struct filter_list_struct filter_list;
Martin Pool6902ed12001-08-14 02:04:47 +000047
Wayne Davisona7c1fa02008-01-26 11:56:27 -080048int batch_stream_flags;
49
Wayne Davisonda01d2e2008-01-19 11:21:07 -080050static int tweaked_append;
51static int tweaked_append_verify;
52static int tweaked_iconv;
Wayne Davison3cc185a2005-10-26 16:48:07 +000053
Wayne Davisond3e182a2004-07-24 16:38:49 +000054static int *flag_ptr[] = {
Wayne Davisone7f70642005-03-27 05:02:49 +000055 &recurse, /* 0 */
Wayne Davison658a6362006-12-18 06:56:23 +000056 &preserve_uid, /* 1 */
57 &preserve_gid, /* 2 */
Wayne Davisone7f70642005-03-27 05:02:49 +000058 &preserve_links, /* 3 */
59 &preserve_devices, /* 4 */
60 &preserve_hard_links, /* 5 */
61 &always_checksum, /* 6 */
62 &xfer_dirs, /* 7 (protocol 29) */
Wayne Davisona7c1fa02008-01-26 11:56:27 -080063 &do_compression, /* 8 (protocol 29) */
Wayne Davisonda01d2e2008-01-19 11:21:07 -080064 &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) */
Wayne Davisond3e182a2004-07-24 16:38:49 +000070 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)",
Wayne Davison6bf82262005-02-02 09:40:45 +000081 "--dirs (-d)",
Wayne Davisone7f70642005-03-27 05:02:49 +000082 "--compress (-z)",
Wayne Davisonda01d2e2008-01-19 11:21:07 -080083 "--iconv",
84 "--acls (-A)",
85 "--xattrs (-X)",
86 "--inplace",
87 "--append",
88 "--append-verify",
Wayne Davisond3e182a2004-07-24 16:38:49 +000089 NULL
90};
91
92void write_stream_flags(int fd)
93{
94 int i, flags;
95
Wayne Davisonda01d2e2008-01-19 11:21:07 -080096 tweaked_append = append_mode == 1;
97 tweaked_append_verify = append_mode == 2;
98#ifdef ICONV_OPTION
99 tweaked_iconv = iconv_opt != NULL;
100#endif
Wayne Davison3cc185a2005-10-26 16:48:07 +0000101
Wayne Davisond3e182a2004-07-24 16:38:49 +0000102 /* 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{
Wayne Davisona7c1fa02008-01-26 11:56:27 -0800113 batch_stream_flags = read_int(fd);
114}
115
116void check_batch_flags(void)
117{
118 int i;
Wayne Davisond3e182a2004-07-24 16:38:49 +0000119
Wayne Davison6bf82262005-02-02 09:40:45 +0000120 if (protocol_version < 29)
Wayne Davisone7f70642005-03-27 05:02:49 +0000121 flag_ptr[7] = NULL;
Wayne Davisonda01d2e2008-01-19 11:21:07 -0800122 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
Wayne Davisona7c1fa02008-01-26 11:56:27 -0800129 for (i = 0; flag_ptr[i]; i++) {
130 int set = batch_stream_flags & (1 << i) ? 1 : 0;
Wayne Davisond3e182a2004-07-24 16:38:49 +0000131 if (*flag_ptr[i] != set) {
Wayne Davisonda01d2e2008-01-19 11:21:07 -0800132 if (i == 9) {
Wayne Davisona7c1fa02008-01-26 11:56:27 -0800133 rprintf(FERROR,
Wayne Davisonda01d2e2008-01-19 11:21:07 -0800134 "%s specify the --iconv option to use this batch file.\n",
135 set ? "Please" : "Do not");
136 exit_cleanup(RERR_SYNTAX);
137 }
Wayne Davison6a48e792004-07-24 16:51:16 +0000138 if (verbose) {
139 rprintf(FINFO,
140 "%sing the %s option to match the batchfile.\n",
141 set ? "Sett" : "Clear", flag_name[i]);
142 }
Wayne Davisond3e182a2004-07-24 16:38:49 +0000143 *flag_ptr[i] = set;
144 }
145 }
Wayne Davisone7f70642005-03-27 05:02:49 +0000146 if (protocol_version < 29) {
147 if (recurse)
148 xfer_dirs |= 1;
149 else if (xfer_dirs < 2)
150 xfer_dirs = 0;
151 }
Wayne Davison3cc185a2005-10-26 16:48:07 +0000152
Wayne Davisonda01d2e2008-01-19 11:21:07 -0800153 if (tweaked_append)
154 append_mode = 1;
155 else if (tweaked_append_verify)
156 append_mode = 2;
Wayne Davisond3e182a2004-07-24 16:38:49 +0000157}
158
Wayne Davisone7a69002004-07-19 08:27:17 +0000159static 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
Wayne Davison82610472005-01-25 12:13:53 +0000182static void write_filter_rules(int fd)
Wayne Davison73f0ce62004-07-20 17:07:55 +0000183{
Wayne Davison78424182005-01-25 10:39:14 +0000184 struct filter_struct *ent;
Wayne Davison73f0ce62004-07-20 17:07:55 +0000185
186 write_sbuf(fd, " <<'#E#'\n");
Wayne Davison78424182005-01-25 10:39:14 +0000187 for (ent = filter_list.head; ent; ent = ent->next) {
Wayne Davison82610472005-01-25 12:13:53 +0000188 unsigned int plen;
Wayne Davisondd667c22005-02-04 21:12:56 +0000189 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
Wayne Davison82610472005-01-25 12:13:53 +0000190 write_buf(fd, p, plen);
191 write_sbuf(fd, ent->pattern);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000192 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
Wayne Davisone7a69002004-07-19 08:27:17 +0000201/* 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)
Martin Pool6902ed12001-08-14 02:04:47 +0000207{
Wayne Davisond630f532005-04-09 18:59:44 +0000208 int fd, i, len;
Wayne Davisone7a69002004-07-19 08:27:17 +0000209 char *p, filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000210
Wayne Davison893c4cc2004-01-20 04:56:20 +0000211 stringjoin(filename, sizeof filename,
Wayne Davisonb4627812004-07-19 00:53:49 +0000212 batch_name, ".sh", NULL);
Wayne Davison01966df2004-07-14 07:20:18 +0000213 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
214 S_IRUSR | S_IWUSR | S_IEXEC);
215 if (fd < 0) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000216 rsyserr(FERROR, errno, "Batch file %s open error",
Wayne Davison45c49b52006-01-13 21:17:09 +0000217 filename);
Wayne Davisonda01d2e2008-01-19 11:21:07 -0800218 exit_cleanup(RERR_FILESELECT);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000219 }
David Dykstra088aac82002-02-06 21:20:48 +0000220
Wayne Davisone7a69002004-07-19 08:27:17 +0000221 /* Write argvs info to BATCH.sh file */
222 write_arg(fd, argv[0]);
Wayne Davison82610472005-01-25 12:13:53 +0000223 if (filter_list.head) {
224 if (protocol_version >= 29)
225 write_sbuf(fd, " --filter=._-");
226 else
227 write_sbuf(fd, " --exclude-from=-");
228 }
Wayne Davisone7a69002004-07-19 08:27:17 +0000229 for (i = 1; i < argc - file_arg_cnt; i++) {
230 p = argv[i];
231 if (strncmp(p, "--files-from", 12) == 0
Wayne Davison82610472005-01-25 12:13:53 +0000232 || strncmp(p, "--filter", 8) == 0
Wayne Davison73f0ce62004-07-20 17:07:55 +0000233 || strncmp(p, "--include", 9) == 0
234 || strncmp(p, "--exclude", 9) == 0) {
Wayne Davisone7a69002004-07-19 08:27:17 +0000235 if (strchr(p, '=') == NULL)
Wayne Davisonb4627812004-07-19 00:53:49 +0000236 i++;
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000237 continue;
Wayne Davisonb4627812004-07-19 00:53:49 +0000238 }
Wayne Davison82610472005-01-25 12:13:53 +0000239 if (strcmp(p, "-f") == 0) {
240 i++;
241 continue;
242 }
Wayne Davisone7a69002004-07-19 08:27:17 +0000243 write(fd, " ", 1);
Wayne Davisond630f532005-04-09 18:59:44 +0000244 if (strncmp(p, "--write-batch", len = 13) == 0
245 || strncmp(p, "--only-write-batch", len = 18) == 0) {
Wayne Davisone7a69002004-07-19 08:27:17 +0000246 write(fd, "--read-batch", 12);
Wayne Davisond630f532005-04-09 18:59:44 +0000247 if (p[len] == '=') {
Wayne Davisone7a69002004-07-19 08:27:17 +0000248 write(fd, "=", 1);
Wayne Davisond630f532005-04-09 18:59:44 +0000249 write_arg(fd, p + len + 1);
Wayne Davisone7a69002004-07-19 08:27:17 +0000250 }
David Dykstra088aac82002-02-06 21:20:48 +0000251 } else
Wayne Davisone7a69002004-07-19 08:27:17 +0000252 write_arg(fd, p);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000253 }
Wayne Davison4d3abf12005-03-01 01:15:59 +0000254 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
Wayne Davisone7a69002004-07-19 08:27:17 +0000255 p = argv[argc - 1];
256 write(fd, " ${1:-", 6);
257 write_arg(fd, p);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000258 write_byte(fd, '}');
Wayne Davison78424182005-01-25 10:39:14 +0000259 if (filter_list.head)
Wayne Davison82610472005-01-25 12:13:53 +0000260 write_filter_rules(fd);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000261 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000262 rsyserr(FERROR, errno, "Batch file %s write error",
Wayne Davison45c49b52006-01-13 21:17:09 +0000263 filename);
Wayne Davisonda01d2e2008-01-19 11:21:07 -0800264 exit_cleanup(RERR_FILEIO);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000265 }
Martin Pool6902ed12001-08-14 02:04:47 +0000266}