blob: 5b88b80f73ca43a1a5f0d0e370f347ae822b50f1 [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;
34extern int always_checksum;
Wayne Davisone7f70642005-03-27 05:02:49 +000035extern int do_compression;
Wayne Davison3cc185a2005-10-26 16:48:07 +000036extern int def_compress_level;
Wayne Davison82610472005-01-25 12:13:53 +000037extern int protocol_version;
Wayne Davison2b136662005-02-01 09:21:28 +000038extern char *batch_name;
Wayne Davison73f0ce62004-07-20 17:07:55 +000039
Wayne Davison78424182005-01-25 10:39:14 +000040extern struct filter_list_struct filter_list;
Martin Pool6902ed12001-08-14 02:04:47 +000041
Wayne Davison3cc185a2005-10-26 16:48:07 +000042static int tweaked_compress_level;
43
Wayne Davisond3e182a2004-07-24 16:38:49 +000044static int *flag_ptr[] = {
Wayne Davisone7f70642005-03-27 05:02:49 +000045 &recurse, /* 0 */
Wayne Davison658a6362006-12-18 06:56:23 +000046 &preserve_uid, /* 1 */
47 &preserve_gid, /* 2 */
Wayne Davisone7f70642005-03-27 05:02:49 +000048 &preserve_links, /* 3 */
49 &preserve_devices, /* 4 */
50 &preserve_hard_links, /* 5 */
51 &always_checksum, /* 6 */
52 &xfer_dirs, /* 7 (protocol 29) */
Wayne Davison3cc185a2005-10-26 16:48:07 +000053 &tweaked_compress_level,/* 8 (protocol 29) */
Wayne Davisond3e182a2004-07-24 16:38:49 +000054 NULL
55};
56
57static char *flag_name[] = {
58 "--recurse (-r)",
59 "--owner (-o)",
60 "--group (-g)",
61 "--links (-l)",
62 "--devices (-D)",
63 "--hard-links (-H)",
64 "--checksum (-c)",
Wayne Davison6bf82262005-02-02 09:40:45 +000065 "--dirs (-d)",
Wayne Davisone7f70642005-03-27 05:02:49 +000066 "--compress (-z)",
Wayne Davisond3e182a2004-07-24 16:38:49 +000067 NULL
68};
69
70void write_stream_flags(int fd)
71{
72 int i, flags;
73
Wayne Davison3cc185a2005-10-26 16:48:07 +000074#if Z_DEFAULT_COMPRESSION == -1
75 tweaked_compress_level = do_compression ? def_compress_level + 2 : 0;
76#else
77#error internal logic error! Fix def_compress_level logic above and below too!
78#endif
79
Wayne Davisond3e182a2004-07-24 16:38:49 +000080 /* Start the batch file with a bitmap of data-stream-affecting
81 * flags. */
Wayne Davisone7f70642005-03-27 05:02:49 +000082 if (protocol_version < 29)
83 flag_ptr[7] = NULL;
Wayne Davisond3e182a2004-07-24 16:38:49 +000084 for (i = 0, flags = 0; flag_ptr[i]; i++) {
85 if (*flag_ptr[i])
86 flags |= 1 << i;
87 }
88 write_int(fd, flags);
89}
90
91void read_stream_flags(int fd)
92{
93 int i, flags;
94
Wayne Davison6bf82262005-02-02 09:40:45 +000095 if (protocol_version < 29)
Wayne Davisone7f70642005-03-27 05:02:49 +000096 flag_ptr[7] = NULL;
Wayne Davisond3e182a2004-07-24 16:38:49 +000097 for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
98 int set = flags & (1 << i) ? 1 : 0;
99 if (*flag_ptr[i] != set) {
Wayne Davison6a48e792004-07-24 16:51:16 +0000100 if (verbose) {
101 rprintf(FINFO,
102 "%sing the %s option to match the batchfile.\n",
103 set ? "Sett" : "Clear", flag_name[i]);
104 }
Wayne Davisond3e182a2004-07-24 16:38:49 +0000105 *flag_ptr[i] = set;
106 }
107 }
Wayne Davisone7f70642005-03-27 05:02:49 +0000108 if (protocol_version < 29) {
109 if (recurse)
110 xfer_dirs |= 1;
111 else if (xfer_dirs < 2)
112 xfer_dirs = 0;
113 }
Wayne Davison3cc185a2005-10-26 16:48:07 +0000114
115 if (tweaked_compress_level == 0 || tweaked_compress_level == 2)
116 do_compression = 0;
117 else {
118 do_compression = 1;
119 def_compress_level = tweaked_compress_level - 2;
120 }
Wayne Davisond3e182a2004-07-24 16:38:49 +0000121}
122
Wayne Davisone7a69002004-07-19 08:27:17 +0000123static void write_arg(int fd, char *arg)
124{
125 char *x, *s;
126
127 if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
128 write(fd, arg, x - arg + 1);
129 arg += x - arg + 1;
130 }
131
132 if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
133 write(fd, "'", 1);
134 for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
135 write(fd, s, x - s + 1);
136 write(fd, "'", 1);
137 }
138 write(fd, s, strlen(s));
139 write(fd, "'", 1);
140 return;
141 }
142
143 write(fd, arg, strlen(arg));
144}
145
Wayne Davison82610472005-01-25 12:13:53 +0000146static void write_filter_rules(int fd)
Wayne Davison73f0ce62004-07-20 17:07:55 +0000147{
Wayne Davison78424182005-01-25 10:39:14 +0000148 struct filter_struct *ent;
Wayne Davison73f0ce62004-07-20 17:07:55 +0000149
150 write_sbuf(fd, " <<'#E#'\n");
Wayne Davison78424182005-01-25 10:39:14 +0000151 for (ent = filter_list.head; ent; ent = ent->next) {
Wayne Davison82610472005-01-25 12:13:53 +0000152 unsigned int plen;
Wayne Davisondd667c22005-02-04 21:12:56 +0000153 char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
Wayne Davison82610472005-01-25 12:13:53 +0000154 write_buf(fd, p, plen);
155 write_sbuf(fd, ent->pattern);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000156 if (ent->match_flags & MATCHFLG_DIRECTORY)
157 write_byte(fd, '/');
158 write_byte(fd, eol_nulls ? 0 : '\n');
159 }
160 if (eol_nulls)
161 write_sbuf(fd, ";\n");
162 write_sbuf(fd, "#E#");
163}
164
Wayne Davisone7a69002004-07-19 08:27:17 +0000165/* This routine tries to write out an equivalent --read-batch command
166 * given the user's --write-batch args. However, it doesn't really
167 * understand most of the options, so it uses some overly simple
168 * heuristics to munge the command line into something that will
169 * (hopefully) work. */
170void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)
Martin Pool6902ed12001-08-14 02:04:47 +0000171{
Wayne Davisond630f532005-04-09 18:59:44 +0000172 int fd, i, len;
Wayne Davisone7a69002004-07-19 08:27:17 +0000173 char *p, filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000174
Wayne Davison893c4cc2004-01-20 04:56:20 +0000175 stringjoin(filename, sizeof filename,
Wayne Davisonb4627812004-07-19 00:53:49 +0000176 batch_name, ".sh", NULL);
Wayne Davison01966df2004-07-14 07:20:18 +0000177 fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
178 S_IRUSR | S_IWUSR | S_IEXEC);
179 if (fd < 0) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000180 rsyserr(FERROR, errno, "Batch file %s open error",
Wayne Davison45c49b52006-01-13 21:17:09 +0000181 filename);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000182 exit_cleanup(1);
183 }
David Dykstra088aac82002-02-06 21:20:48 +0000184
Wayne Davisone7a69002004-07-19 08:27:17 +0000185 /* Write argvs info to BATCH.sh file */
186 write_arg(fd, argv[0]);
Wayne Davison82610472005-01-25 12:13:53 +0000187 if (filter_list.head) {
188 if (protocol_version >= 29)
189 write_sbuf(fd, " --filter=._-");
190 else
191 write_sbuf(fd, " --exclude-from=-");
192 }
Wayne Davisone7a69002004-07-19 08:27:17 +0000193 for (i = 1; i < argc - file_arg_cnt; i++) {
194 p = argv[i];
195 if (strncmp(p, "--files-from", 12) == 0
Wayne Davison82610472005-01-25 12:13:53 +0000196 || strncmp(p, "--filter", 8) == 0
Wayne Davison73f0ce62004-07-20 17:07:55 +0000197 || strncmp(p, "--include", 9) == 0
198 || strncmp(p, "--exclude", 9) == 0) {
Wayne Davisone7a69002004-07-19 08:27:17 +0000199 if (strchr(p, '=') == NULL)
Wayne Davisonb4627812004-07-19 00:53:49 +0000200 i++;
Wayne Davisonb9f592f2004-07-15 02:20:08 +0000201 continue;
Wayne Davisonb4627812004-07-19 00:53:49 +0000202 }
Wayne Davison82610472005-01-25 12:13:53 +0000203 if (strcmp(p, "-f") == 0) {
204 i++;
205 continue;
206 }
Wayne Davisone7a69002004-07-19 08:27:17 +0000207 write(fd, " ", 1);
Wayne Davisond630f532005-04-09 18:59:44 +0000208 if (strncmp(p, "--write-batch", len = 13) == 0
209 || strncmp(p, "--only-write-batch", len = 18) == 0) {
Wayne Davisone7a69002004-07-19 08:27:17 +0000210 write(fd, "--read-batch", 12);
Wayne Davisond630f532005-04-09 18:59:44 +0000211 if (p[len] == '=') {
Wayne Davisone7a69002004-07-19 08:27:17 +0000212 write(fd, "=", 1);
Wayne Davisond630f532005-04-09 18:59:44 +0000213 write_arg(fd, p + len + 1);
Wayne Davisone7a69002004-07-19 08:27:17 +0000214 }
David Dykstra088aac82002-02-06 21:20:48 +0000215 } else
Wayne Davisone7a69002004-07-19 08:27:17 +0000216 write_arg(fd, p);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000217 }
Wayne Davison4d3abf12005-03-01 01:15:59 +0000218 if (!(p = check_for_hostspec(argv[argc - 1], &p, &i)))
Wayne Davisone7a69002004-07-19 08:27:17 +0000219 p = argv[argc - 1];
220 write(fd, " ${1:-", 6);
221 write_arg(fd, p);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000222 write_byte(fd, '}');
Wayne Davison78424182005-01-25 10:39:14 +0000223 if (filter_list.head)
Wayne Davison82610472005-01-25 12:13:53 +0000224 write_filter_rules(fd);
Wayne Davison73f0ce62004-07-20 17:07:55 +0000225 if (write(fd, "\n", 1) != 1 || close(fd) < 0) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000226 rsyserr(FERROR, errno, "Batch file %s write error",
Wayne Davison45c49b52006-01-13 21:17:09 +0000227 filename);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000228 exit_cleanup(1);
229 }
Martin Pool6902ed12001-08-14 02:04:47 +0000230}