Commit | Line | Data |
---|---|---|
08a740ff | 1 | /* -*- c-file-style: "linux" -*- |
71020fc3 | 2 | |
6902ed17 | 3 | Weiss 1/1999 |
08a740ff | 4 | Batch utilities for rsync. |
6902ed17 | 5 | |
1cd5beeb | 6 | */ |
6902ed17 MP |
7 | |
8 | #include "rsync.h" | |
3cc185a0 | 9 | #include "zlib/zlib.h" |
6902ed17 MP |
10 | #include <time.h> |
11 | ||
2b136663 | 12 | extern int am_sender; |
73f0ce69 | 13 | extern int eol_nulls; |
d3e182af | 14 | extern int recurse; |
6bf82264 | 15 | extern int xfer_dirs; |
d3e182af WD |
16 | extern int preserve_links; |
17 | extern int preserve_hard_links; | |
18 | extern int preserve_devices; | |
19 | extern int preserve_uid; | |
20 | extern int preserve_gid; | |
21 | extern int always_checksum; | |
e7f7064c | 22 | extern int do_compression; |
3cc185a0 | 23 | extern int def_compress_level; |
8261047b | 24 | extern int protocol_version; |
2b136663 | 25 | extern char *batch_name; |
73f0ce69 | 26 | |
7842418b | 27 | extern struct filter_list_struct filter_list; |
e8d3168e | 28 | |
3cc185a0 WD |
29 | static int tweaked_compress_level; |
30 | ||
d3e182af | 31 | static int *flag_ptr[] = { |
e7f7064c WD |
32 | &recurse, /* 0 */ |
33 | &preserve_uid, /* 1 */ | |
34 | &preserve_gid, /* 2 */ | |
35 | &preserve_links, /* 3 */ | |
36 | &preserve_devices, /* 4 */ | |
37 | &preserve_hard_links, /* 5 */ | |
38 | &always_checksum, /* 6 */ | |
39 | &xfer_dirs, /* 7 (protocol 29) */ | |
3cc185a0 | 40 | &tweaked_compress_level,/* 8 (protocol 29) */ |
d3e182af WD |
41 | NULL |
42 | }; | |
43 | ||
44 | static char *flag_name[] = { | |
45 | "--recurse (-r)", | |
46 | "--owner (-o)", | |
47 | "--group (-g)", | |
48 | "--links (-l)", | |
49 | "--devices (-D)", | |
50 | "--hard-links (-H)", | |
51 | "--checksum (-c)", | |
6bf82264 | 52 | "--dirs (-d)", |
e7f7064c | 53 | "--compress (-z)", |
d3e182af WD |
54 | NULL |
55 | }; | |
56 | ||
57 | void write_stream_flags(int fd) | |
58 | { | |
59 | int i, flags; | |
60 | ||
3cc185a0 WD |
61 | #if Z_DEFAULT_COMPRESSION == -1 |
62 | tweaked_compress_level = do_compression ? def_compress_level + 2 : 0; | |
63 | #else | |
64 | #error internal logic error! Fix def_compress_level logic above and below too! | |
65 | #endif | |
66 | ||
d3e182af WD |
67 | /* Start the batch file with a bitmap of data-stream-affecting |
68 | * flags. */ | |
e7f7064c WD |
69 | if (protocol_version < 29) |
70 | flag_ptr[7] = NULL; | |
d3e182af WD |
71 | for (i = 0, flags = 0; flag_ptr[i]; i++) { |
72 | if (*flag_ptr[i]) | |
73 | flags |= 1 << i; | |
74 | } | |
75 | write_int(fd, flags); | |
76 | } | |
77 | ||
78 | void read_stream_flags(int fd) | |
79 | { | |
80 | int i, flags; | |
81 | ||
6bf82264 | 82 | if (protocol_version < 29) |
e7f7064c | 83 | flag_ptr[7] = NULL; |
d3e182af WD |
84 | for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) { |
85 | int set = flags & (1 << i) ? 1 : 0; | |
86 | if (*flag_ptr[i] != set) { | |
6a48e792 WD |
87 | if (verbose) { |
88 | rprintf(FINFO, | |
89 | "%sing the %s option to match the batchfile.\n", | |
90 | set ? "Sett" : "Clear", flag_name[i]); | |
91 | } | |
d3e182af WD |
92 | *flag_ptr[i] = set; |
93 | } | |
94 | } | |
e7f7064c WD |
95 | if (protocol_version < 29) { |
96 | if (recurse) | |
97 | xfer_dirs |= 1; | |
98 | else if (xfer_dirs < 2) | |
99 | xfer_dirs = 0; | |
100 | } | |
3cc185a0 WD |
101 | |
102 | if (tweaked_compress_level == 0 || tweaked_compress_level == 2) | |
103 | do_compression = 0; | |
104 | else { | |
105 | do_compression = 1; | |
106 | def_compress_level = tweaked_compress_level - 2; | |
107 | } | |
d3e182af WD |
108 | } |
109 | ||
e7a69008 WD |
110 | static void write_arg(int fd, char *arg) |
111 | { | |
112 | char *x, *s; | |
113 | ||
114 | if (*arg == '-' && (x = strchr(arg, '=')) != NULL) { | |
115 | write(fd, arg, x - arg + 1); | |
116 | arg += x - arg + 1; | |
117 | } | |
118 | ||
119 | if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) { | |
120 | write(fd, "'", 1); | |
121 | for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) { | |
122 | write(fd, s, x - s + 1); | |
123 | write(fd, "'", 1); | |
124 | } | |
125 | write(fd, s, strlen(s)); | |
126 | write(fd, "'", 1); | |
127 | return; | |
128 | } | |
129 | ||
130 | write(fd, arg, strlen(arg)); | |
131 | } | |
132 | ||
8261047b | 133 | static void write_filter_rules(int fd) |
73f0ce69 | 134 | { |
7842418b | 135 | struct filter_struct *ent; |
73f0ce69 WD |
136 | |
137 | write_sbuf(fd, " <<'#E#'\n"); | |
7842418b | 138 | for (ent = filter_list.head; ent; ent = ent->next) { |
8261047b | 139 | unsigned int plen; |
dd667c23 | 140 | char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen); |
8261047b WD |
141 | write_buf(fd, p, plen); |
142 | write_sbuf(fd, ent->pattern); | |
73f0ce69 WD |
143 | if (ent->match_flags & MATCHFLG_DIRECTORY) |
144 | write_byte(fd, '/'); | |
145 | write_byte(fd, eol_nulls ? 0 : '\n'); | |
146 | } | |
147 | if (eol_nulls) | |
148 | write_sbuf(fd, ";\n"); | |
149 | write_sbuf(fd, "#E#"); | |
150 | } | |
151 | ||
e7a69008 WD |
152 | /* This routine tries to write out an equivalent --read-batch command |
153 | * given the user's --write-batch args. However, it doesn't really | |
154 | * understand most of the options, so it uses some overly simple | |
155 | * heuristics to munge the command line into something that will | |
156 | * (hopefully) work. */ | |
157 | void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt) | |
6902ed17 | 158 | { |
d630f53e | 159 | int fd, i, len; |
e7a69008 | 160 | char *p, filename[MAXPATHLEN]; |
088aac85 | 161 | |
893c4cc0 | 162 | stringjoin(filename, sizeof filename, |
b462781f | 163 | batch_name, ".sh", NULL); |
01966df4 WD |
164 | fd = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC, |
165 | S_IRUSR | S_IWUSR | S_IEXEC); | |
166 | if (fd < 0) { | |
4875d6b6 | 167 | rsyserr(FERROR, errno, "Batch file %s open error", |
45c49b52 | 168 | filename); |
1cd5beeb MP |
169 | exit_cleanup(1); |
170 | } | |
088aac85 | 171 | |
e7a69008 WD |
172 | /* Write argvs info to BATCH.sh file */ |
173 | write_arg(fd, argv[0]); | |
8261047b WD |
174 | if (filter_list.head) { |
175 | if (protocol_version >= 29) | |
176 | write_sbuf(fd, " --filter=._-"); | |
177 | else | |
178 | write_sbuf(fd, " --exclude-from=-"); | |
179 | } | |
e7a69008 WD |
180 | for (i = 1; i < argc - file_arg_cnt; i++) { |
181 | p = argv[i]; | |
182 | if (strncmp(p, "--files-from", 12) == 0 | |
8261047b | 183 | || strncmp(p, "--filter", 8) == 0 |
73f0ce69 WD |
184 | || strncmp(p, "--include", 9) == 0 |
185 | || strncmp(p, "--exclude", 9) == 0) { | |
e7a69008 | 186 | if (strchr(p, '=') == NULL) |
b462781f | 187 | i++; |
b9f592fb | 188 | continue; |
b462781f | 189 | } |
8261047b WD |
190 | if (strcmp(p, "-f") == 0) { |
191 | i++; | |
192 | continue; | |
193 | } | |
e7a69008 | 194 | write(fd, " ", 1); |
d630f53e WD |
195 | if (strncmp(p, "--write-batch", len = 13) == 0 |
196 | || strncmp(p, "--only-write-batch", len = 18) == 0) { | |
e7a69008 | 197 | write(fd, "--read-batch", 12); |
d630f53e | 198 | if (p[len] == '=') { |
e7a69008 | 199 | write(fd, "=", 1); |
d630f53e | 200 | write_arg(fd, p + len + 1); |
e7a69008 | 201 | } |
088aac85 | 202 | } else |
e7a69008 | 203 | write_arg(fd, p); |
1cd5beeb | 204 | } |
4d3abf13 | 205 | if (!(p = check_for_hostspec(argv[argc - 1], &p, &i))) |
e7a69008 WD |
206 | p = argv[argc - 1]; |
207 | write(fd, " ${1:-", 6); | |
208 | write_arg(fd, p); | |
73f0ce69 | 209 | write_byte(fd, '}'); |
7842418b | 210 | if (filter_list.head) |
8261047b | 211 | write_filter_rules(fd); |
73f0ce69 | 212 | if (write(fd, "\n", 1) != 1 || close(fd) < 0) { |
4875d6b6 | 213 | rsyserr(FERROR, errno, "Batch file %s write error", |
45c49b52 | 214 | filename); |
1cd5beeb MP |
215 | exit_cleanup(1); |
216 | } | |
6902ed17 MP |
217 | } |
218 | ||
6902ed17 MP |
219 | void show_flist(int index, struct file_struct **fptr) |
220 | { | |
1cd5beeb MP |
221 | /* for debugging show_flist(flist->count, flist->files * */ |
222 | ||
223 | int i; | |
224 | for (i = 0; i < index; i++) { | |
225 | rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags); | |
226 | rprintf(FINFO, "flist->modtime=%#lx\n", | |
227 | (long unsigned) fptr[i]->modtime); | |
228 | rprintf(FINFO, "flist->length=%.0f\n", | |
229 | (double) fptr[i]->length); | |
230 | rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode); | |
4875d6b6 | 231 | rprintf(FINFO, "flist->basename=%s\n", |
45c49b52 | 232 | fptr[i]->basename); |
4875d6b6 | 233 | if (fptr[i]->dirname) { |
1cd5beeb | 234 | rprintf(FINFO, "flist->dirname=%s\n", |
45c49b52 | 235 | fptr[i]->dirname); |
4875d6b6 WD |
236 | } |
237 | if (am_sender && fptr[i]->dir.root) { | |
2b136663 | 238 | rprintf(FINFO, "flist->dir.root=%s\n", |
45c49b52 | 239 | fptr[i]->dir.root); |
4875d6b6 | 240 | } |
1cd5beeb | 241 | } |
6902ed17 MP |
242 | } |
243 | ||
4875d6b6 | 244 | /* for debugging */ |
6902ed17 MP |
245 | void show_argvs(int argc, char *argv[]) |
246 | { | |
1cd5beeb | 247 | int i; |
6902ed17 | 248 | |
4875d6b6 WD |
249 | rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc); |
250 | for (i = 0; i < argc; i++) | |
45c49b52 | 251 | rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]); |
6902ed17 | 252 | } |