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