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" | |
9 | #include <time.h> | |
10 | ||
088aac85 | 11 | extern char *batch_prefix; |
e8d3168e WD |
12 | extern int csum_length; |
13 | extern int protocol_version; | |
14 | extern struct stats stats; | |
6902ed17 MP |
15 | |
16 | struct file_list *batch_flist; | |
17 | ||
088aac85 DD |
18 | static char rsync_flist_file[] = ".rsync_flist"; |
19 | static char rsync_csums_file[] = ".rsync_csums"; | |
20 | static char rsync_delta_file[] = ".rsync_delta"; | |
21 | static char rsync_argvs_file[] = ".rsync_argvs"; | |
6902ed17 | 22 | |
e8d3168e WD |
23 | static int f_csums = -1; |
24 | static int f_delta = -1; | |
6902ed17 | 25 | |
e8d3168e | 26 | void write_batch_flist_info(int flist_count, struct file_struct **files) |
6902ed17 | 27 | { |
088aac85 | 28 | char filename[MAXPATHLEN]; |
e8d3168e WD |
29 | int i, f, save_pv; |
30 | int64 save_written; | |
6902ed17 | 31 | |
893c4cc0 | 32 | stringjoin(filename, sizeof filename, |
71020fc3 | 33 | batch_prefix, rsync_flist_file, NULL); |
1cd5beeb | 34 | |
7e5cb909 | 35 | f = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
e8d3168e | 36 | if (f < 0) { |
d62bcc17 | 37 | rsyserr(FERROR, errno, "Batch file %s open error", filename); |
1cd5beeb MP |
38 | exit_cleanup(1); |
39 | } | |
40 | ||
e8d3168e WD |
41 | save_written = stats.total_written; |
42 | save_pv = protocol_version; | |
43 | protocol_version = PROTOCOL_VERSION; | |
44 | write_int(f, protocol_version); | |
45 | write_int(f, flist_count); | |
088aac85 | 46 | |
a5342644 | 47 | for (i = 0; i < flist_count; i++) { |
71020fc3 S |
48 | send_file_entry(files[i], f, |
49 | files[i]->flags & FLAG_TOP_DIR ? XMIT_TOP_DIR : 0); | |
a5342644 | 50 | } |
e8d3168e | 51 | send_file_entry(NULL, f, 0); |
6902ed17 | 52 | |
e8d3168e WD |
53 | protocol_version = save_pv; |
54 | stats.total_written = save_written; | |
6902ed17 | 55 | |
e8d3168e | 56 | close(f); |
6902ed17 MP |
57 | } |
58 | ||
e8d3168e | 59 | |
76f79ba7 | 60 | void write_batch_argvs_file(int argc, char *argv[]) |
6902ed17 | 61 | { |
e8d3168e | 62 | int f; |
1cd5beeb | 63 | int i; |
088aac85 DD |
64 | char buff[256]; /* XXX */ |
65 | char buff2[MAXPATHLEN + 6]; | |
66 | char filename[MAXPATHLEN]; | |
67 | ||
893c4cc0 | 68 | stringjoin(filename, sizeof filename, |
71020fc3 | 69 | batch_prefix, rsync_argvs_file, NULL); |
088aac85 | 70 | |
e8d3168e | 71 | f = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC, |
7e5cb909 | 72 | S_IRUSR | S_IWUSR | S_IEXEC); |
e8d3168e | 73 | if (f < 0) { |
d62bcc17 | 74 | rsyserr(FERROR, errno, "Batch file %s open error", filename); |
1cd5beeb MP |
75 | exit_cleanup(1); |
76 | } | |
77 | buff[0] = '\0'; | |
088aac85 | 78 | |
1cd5beeb MP |
79 | /* Write argvs info to batch file */ |
80 | ||
76f79ba7 | 81 | for (i = 0; i < argc; ++i) { |
088aac85 | 82 | if (i == argc - 2) /* Skip source directory on cmdline */ |
71020fc3 | 83 | continue; |
76f79ba7 MP |
84 | /* |
85 | * FIXME: | |
86 | * I think directly manipulating argv[] is probably bogus | |
87 | */ | |
088aac85 | 88 | if (!strncmp(argv[i], "--write-batch", |
71020fc3 | 89 | strlen("--write-batch"))) { |
76f79ba7 | 90 | /* Safer to change it here than script */ |
088aac85 DD |
91 | /* |
92 | * Change to --read-batch=prefix | |
93 | * to get ready for remote | |
94 | */ | |
71020fc3 S |
95 | strlcat(buff, "--read-batch=", sizeof buff); |
96 | strlcat(buff, batch_prefix, sizeof buff); | |
088aac85 DD |
97 | } else |
98 | if (i == argc - 1) { | |
71020fc3 S |
99 | snprintf(buff2, sizeof buff2, "${1:-%s}", argv[i]); |
100 | strlcat(buff, buff2, sizeof buff); | |
088aac85 DD |
101 | } |
102 | else { | |
71020fc3 | 103 | strlcat(buff, argv[i], sizeof buff); |
1cd5beeb MP |
104 | } |
105 | ||
106 | if (i < (argc - 1)) { | |
71020fc3 | 107 | strlcat(buff, " ", sizeof buff); |
1cd5beeb MP |
108 | } |
109 | } | |
71020fc3 | 110 | strlcat(buff, "\n", sizeof buff); |
e8d3168e | 111 | if (!write(f, buff, strlen(buff))) { |
d62bcc17 | 112 | rsyserr(FERROR, errno, "Batch file %s write error", filename); |
e8d3168e | 113 | close(f); |
1cd5beeb MP |
114 | exit_cleanup(1); |
115 | } | |
e8d3168e | 116 | close(f); |
6902ed17 MP |
117 | } |
118 | ||
088aac85 | 119 | struct file_list *create_flist_from_batch(void) |
6902ed17 | 120 | { |
088aac85 | 121 | char filename[MAXPATHLEN]; |
e8d3168e WD |
122 | unsigned short flags; |
123 | int i, f, save_pv; | |
124 | int64 save_read; | |
1cd5beeb | 125 | |
893c4cc0 | 126 | stringjoin(filename, sizeof filename, |
71020fc3 | 127 | batch_prefix, rsync_flist_file, NULL); |
1cd5beeb | 128 | |
e8d3168e WD |
129 | f = do_open(filename, O_RDONLY, 0); |
130 | if (f < 0) { | |
d62bcc17 | 131 | rsyserr(FERROR, errno, "Batch file %s open error", filename); |
1cd5beeb | 132 | exit_cleanup(1); |
1cd5beeb | 133 | } |
088aac85 | 134 | |
9935066b | 135 | batch_flist = flist_new(WITH_HLINK, "create_flist_from_batch"); |
6902ed17 | 136 | |
e8d3168e WD |
137 | save_read = stats.total_read; |
138 | save_pv = protocol_version; | |
139 | protocol_version = read_int(f); | |
1cd5beeb | 140 | |
a85906c7 S |
141 | batch_flist->count = read_int(f); |
142 | flist_expand(batch_flist); | |
1cd5beeb | 143 | |
e8d3168e | 144 | for (i = 0; (flags = read_byte(f)) != 0; i++) { |
a5342644 | 145 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
e8d3168e | 146 | flags |= read_byte(f) << 8; |
9935066b | 147 | receive_file_entry(&batch_flist->files[i], flags, batch_flist, f); |
1cd5beeb | 148 | } |
9935066b | 149 | receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */ |
1cd5beeb | 150 | |
e8d3168e WD |
151 | protocol_version = save_pv; |
152 | stats.total_read = save_read; | |
1cd5beeb | 153 | |
e8d3168e | 154 | return batch_flist; |
6902ed17 MP |
155 | } |
156 | ||
55d9e0fa | 157 | void write_batch_csums_file(void *buff, int bytes_to_write) |
6902ed17 | 158 | { |
e8d3168e | 159 | if (write(f_csums, buff, bytes_to_write) < 0) { |
d62bcc17 | 160 | rsyserr(FERROR, errno, "Batch file write error"); |
e8d3168e | 161 | close(f_csums); |
1cd5beeb MP |
162 | exit_cleanup(1); |
163 | } | |
6902ed17 MP |
164 | } |
165 | ||
088aac85 | 166 | void close_batch_csums_file(void) |
6902ed17 | 167 | { |
e8d3168e WD |
168 | close(f_csums); |
169 | f_csums = -1; | |
6902ed17 MP |
170 | } |
171 | ||
f8f4c862 MP |
172 | |
173 | /** | |
71020fc3 | 174 | * Write csum info to batch file |
f8f4c862 MP |
175 | * |
176 | * @todo This will break if s->count is ever larger than maxint. The | |
177 | * batch code should probably be changed to consistently use the | |
178 | * variable-length integer routines, which is probably a compatible | |
179 | * change. | |
180 | **/ | |
e8d3168e | 181 | void write_batch_csum_info(int *flist_entry, struct sum_struct *s) |
6902ed17 | 182 | { |
5664871e | 183 | size_t i; |
1bc209b4 | 184 | int int_count; |
03979352 WD |
185 | char filename[MAXPATHLEN]; |
186 | ||
187 | if (f_csums < 0) { | |
893c4cc0 | 188 | stringjoin(filename, sizeof filename, |
71020fc3 | 189 | batch_prefix, rsync_csums_file, NULL); |
03979352 WD |
190 | |
191 | f_csums = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC, | |
7e5cb909 | 192 | S_IRUSR | S_IWUSR); |
03979352 | 193 | if (f_csums < 0) { |
d62bcc17 WD |
194 | rsyserr(FERROR, errno, "Batch file %s open error", |
195 | filename); | |
03979352 WD |
196 | close(f_csums); |
197 | exit_cleanup(1); | |
198 | } | |
199 | } | |
1cd5beeb | 200 | |
71020fc3 | 201 | write_batch_csums_file(flist_entry, sizeof (int)); |
1bc209b4 MP |
202 | int_count = s ? (int) s->count : 0; |
203 | write_batch_csums_file(&int_count, sizeof int_count); | |
71020fc3 | 204 | |
1cd5beeb MP |
205 | if (s) { |
206 | for (i = 0; i < s->count; i++) { | |
71020fc3 S |
207 | write_batch_csums_file(&s->sums[i].sum1, |
208 | sizeof (uint32)); | |
088aac85 | 209 | write_batch_csums_file(s->sums[i].sum2, csum_length); |
1cd5beeb MP |
210 | } |
211 | } | |
6902ed17 MP |
212 | } |
213 | ||
214 | int read_batch_csums_file(char *buff, int len) | |
215 | { | |
1cd5beeb | 216 | int bytes_read; |
1cd5beeb | 217 | |
03979352 | 218 | if ((bytes_read = read(f_csums, buff, len)) < 0) { |
d62bcc17 | 219 | rsyserr(FERROR, errno, "Batch file read error"); |
e8d3168e | 220 | close(f_csums); |
1cd5beeb MP |
221 | exit_cleanup(1); |
222 | } | |
223 | return bytes_read; | |
6902ed17 MP |
224 | } |
225 | ||
1cd5beeb MP |
226 | void read_batch_csum_info(int flist_entry, struct sum_struct *s, |
227 | int *checksums_match) | |
6902ed17 | 228 | { |
1cd5beeb MP |
229 | int i; |
230 | int file_flist_entry; | |
231 | int file_chunk_ct; | |
232 | uint32 file_sum1; | |
233 | char file_sum2[SUM_LENGTH]; | |
03979352 WD |
234 | char filename[MAXPATHLEN]; |
235 | ||
236 | if (f_csums < 0) { | |
893c4cc0 | 237 | stringjoin(filename, sizeof filename, |
71020fc3 | 238 | batch_prefix, rsync_csums_file, NULL); |
03979352 WD |
239 | |
240 | f_csums = do_open(filename, O_RDONLY, 0); | |
241 | if (f_csums < 0) { | |
d62bcc17 WD |
242 | rsyserr(FERROR, errno, "Batch file %s open error", |
243 | filename); | |
03979352 WD |
244 | close(f_csums); |
245 | exit_cleanup(1); | |
246 | } | |
247 | } | |
1cd5beeb | 248 | |
71020fc3 | 249 | read_batch_csums_file((char *) &file_flist_entry, sizeof (int)); |
1cd5beeb | 250 | if (file_flist_entry != flist_entry) { |
088aac85 | 251 | rprintf(FINFO, "file_flist_entry (%d) != flist_entry (%d)\n", |
71020fc3 | 252 | file_flist_entry, flist_entry); |
e8d3168e | 253 | close(f_csums); |
1cd5beeb MP |
254 | exit_cleanup(1); |
255 | ||
256 | } else { | |
71020fc3 | 257 | read_batch_csums_file((char *) &file_chunk_ct, sizeof (int)); |
1cd5beeb MP |
258 | *checksums_match = 1; |
259 | for (i = 0; i < file_chunk_ct; i++) { | |
1cd5beeb | 260 | read_batch_csums_file((char *) &file_sum1, |
71020fc3 | 261 | sizeof (uint32)); |
1cd5beeb MP |
262 | read_batch_csums_file(file_sum2, csum_length); |
263 | ||
71020fc3 S |
264 | if ((s->sums[i].sum1 != file_sum1) |
265 | || memcmp(s->sums[i].sum2, file_sum2, csum_length)) | |
1cd5beeb | 266 | *checksums_match = 0; |
1cd5beeb MP |
267 | } /* end for */ |
268 | } | |
6902ed17 MP |
269 | } |
270 | ||
271 | void write_batch_delta_file(char *buff, int bytes_to_write) | |
272 | { | |
088aac85 | 273 | char filename[MAXPATHLEN]; |
1cd5beeb | 274 | |
e8d3168e | 275 | if (f_delta < 0) { |
893c4cc0 | 276 | stringjoin(filename, sizeof filename, |
71020fc3 | 277 | batch_prefix, rsync_delta_file, NULL); |
1cd5beeb | 278 | |
e8d3168e | 279 | f_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC, |
7e5cb909 | 280 | S_IRUSR | S_IWUSR); |
e8d3168e | 281 | if (f_delta < 0) { |
d62bcc17 WD |
282 | rsyserr(FERROR, errno, "Batch file %s open error", |
283 | filename); | |
1cd5beeb MP |
284 | exit_cleanup(1); |
285 | } | |
1cd5beeb MP |
286 | } |
287 | ||
e8d3168e | 288 | if (write(f_delta, buff, bytes_to_write) < 0) { |
d62bcc17 | 289 | rsyserr(FERROR, errno, "Batch file %s write error", filename); |
e8d3168e | 290 | close(f_delta); |
1cd5beeb MP |
291 | exit_cleanup(1); |
292 | } | |
6902ed17 | 293 | } |
088aac85 DD |
294 | |
295 | void close_batch_delta_file(void) | |
6902ed17 | 296 | { |
e8d3168e WD |
297 | close(f_delta); |
298 | f_delta = -1; | |
6902ed17 MP |
299 | } |
300 | ||
301 | int read_batch_delta_file(char *buff, int len) | |
302 | { | |
1cd5beeb | 303 | int bytes_read; |
088aac85 | 304 | char filename[MAXPATHLEN]; |
1cd5beeb | 305 | |
e8d3168e | 306 | if (f_delta < 0) { |
893c4cc0 | 307 | stringjoin(filename, sizeof filename, |
71020fc3 | 308 | batch_prefix, rsync_delta_file, NULL); |
1cd5beeb | 309 | |
e8d3168e WD |
310 | f_delta = do_open(filename, O_RDONLY, 0); |
311 | if (f_delta < 0) { | |
d62bcc17 WD |
312 | rsyserr(FERROR, errno, "Batch file %s open error", |
313 | filename); | |
e8d3168e | 314 | close(f_delta); |
1cd5beeb MP |
315 | exit_cleanup(1); |
316 | } | |
1cd5beeb MP |
317 | } |
318 | ||
e8d3168e WD |
319 | bytes_read = read(f_delta, buff, len); |
320 | if (bytes_read < 0) { | |
d62bcc17 | 321 | rsyserr(FERROR, errno, "Batch file %s read error", filename); |
e8d3168e | 322 | close(f_delta); |
1cd5beeb MP |
323 | exit_cleanup(1); |
324 | } | |
088aac85 | 325 | |
1cd5beeb | 326 | return bytes_read; |
6902ed17 MP |
327 | } |
328 | ||
6902ed17 MP |
329 | void show_flist(int index, struct file_struct **fptr) |
330 | { | |
1cd5beeb MP |
331 | /* for debugging show_flist(flist->count, flist->files * */ |
332 | ||
333 | int i; | |
334 | for (i = 0; i < index; i++) { | |
335 | rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags); | |
336 | rprintf(FINFO, "flist->modtime=%#lx\n", | |
337 | (long unsigned) fptr[i]->modtime); | |
338 | rprintf(FINFO, "flist->length=%.0f\n", | |
339 | (double) fptr[i]->length); | |
340 | rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode); | |
341 | rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename); | |
342 | if (fptr[i]->dirname) | |
343 | rprintf(FINFO, "flist->dirname=%s\n", | |
344 | fptr[i]->dirname); | |
345 | if (fptr[i]->basedir) | |
346 | rprintf(FINFO, "flist->basedir=%s\n", | |
347 | fptr[i]->basedir); | |
348 | } | |
6902ed17 MP |
349 | } |
350 | ||
351 | void show_argvs(int argc, char *argv[]) | |
352 | { | |
1cd5beeb | 353 | /* for debugging * */ |
6902ed17 | 354 | |
1cd5beeb MP |
355 | int i; |
356 | rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc); | |
357 | for (i = 0; i < argc; i++) { | |
358 | /* if (argv[i]) */ | |
359 | rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]); | |
6902ed17 | 360 | |
1cd5beeb | 361 | } |
6902ed17 | 362 | } |