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