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