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