Fix cast that was breaking HP/UX.
[rsync/rsync.git] / batch.c
CommitLineData
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
1cd5beeb
MP
11char rsync_flist_file[27] = "rsync_flist.";
12char rsync_csums_file[27] = "rsync_csums.";
13char rsync_delta_file[27] = "rsync_delta.";
14char rsync_argvs_file[27] = "rsync_argvs.";
6902ed17
MP
15
16char batch_file_ext[15];
1cd5beeb 17
6902ed17
MP
18int fdb;
19int fdb_delta;
1cd5beeb
MP
20int fdb_open;
21int fdb_close;
6902ed17
MP
22
23struct file_list *batch_flist;
24
25void create_batch_file_ext()
26{
1cd5beeb
MP
27 struct tm *timeptr;
28 time_t elapsed_seconds;
29
30 /* Save run date and time to use for batch file extensions */
31 time(&elapsed_seconds);
32 timeptr = localtime(&elapsed_seconds);
33
34 sprintf(batch_file_ext, "%4d%02d%02d%02d%02d%02d",
35 timeptr->tm_year + 1900, timeptr->tm_mon + 1,
36 timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min,
37 timeptr->tm_sec);
76f79ba7 38 rprintf(FINFO,"batch file extension: %s\n", batch_file_ext);
6902ed17
MP
39}
40
41void set_batch_file_ext(char *ext)
42{
1cd5beeb 43 strcpy(batch_file_ext, ext);
6902ed17
MP
44}
45
46void write_batch_flist_file(char *buff, int bytes_to_write)
47{
48
1cd5beeb
MP
49 if (fdb_open) {
50 /* Set up file extension */
51 strcat(rsync_flist_file, batch_file_ext);
52
53 /* Open batch flist file for writing; create it if it doesn't exist */
54 fdb =
55 do_open(rsync_flist_file, O_WRONLY | O_CREAT | O_TRUNC,
56 S_IREAD | S_IWRITE);
57 if (fdb == -1) {
58 rprintf(FERROR, "Batch file %s open error: %s\n",
59 rsync_flist_file, strerror(errno));
60 close(fdb);
61 exit_cleanup(1);
62 }
63 fdb_open = 0;
64 }
65
66 /* Write buffer to batch flist file */
67
68 if (write(fdb, buff, bytes_to_write) == -1) {
69 rprintf(FERROR, "Batch file %s write error: %s\n",
70 rsync_flist_file, strerror(errno));
71 close(fdb);
72 exit_cleanup(1);
73 }
74
75
76 if (fdb_close) {
77 close(fdb);
78 }
6902ed17
MP
79}
80
81void write_batch_flist_info(int flist_count, struct file_struct **fptr)
82{
1cd5beeb
MP
83 int i;
84 int bytes_to_write;
85
86 /* Write flist info to batch file */
87
76f79ba7
MP
88 bytes_to_write =
89 sizeof(unsigned) +
1cd5beeb
MP
90 sizeof(time_t) +
91 sizeof(OFF_T) +
92 sizeof(mode_t) +
6abd193f 93 sizeof(INO64_T) +
76f79ba7
MP
94 sizeof(DEV64_T) +
95 sizeof(DEV64_T) +
96 sizeof(uid_t) +
97 sizeof(gid_t);
1cd5beeb
MP
98
99 fdb_open = 1;
100 fdb_close = 0;
101
102 for (i = 0; i < flist_count; i++) {
103 write_batch_flist_file((char *) fptr[i], bytes_to_write);
104 write_char_bufs(fptr[i]->basename);
105 write_char_bufs(fptr[i]->dirname);
106 write_char_bufs(fptr[i]->basedir);
107 write_char_bufs(fptr[i]->link);
108 if (i == flist_count - 1) {
109 fdb_close = 1;
110 }
111 write_char_bufs(fptr[i]->sum);
112 }
6902ed17
MP
113
114}
115
116void write_char_bufs(char *buf)
117{
1cd5beeb 118 /* Write the size of the string which will follow */
6902ed17 119
1cd5beeb
MP
120 char b[4];
121 if (buf != NULL)
122 SIVAL(b, 0, strlen(buf));
123 else {
124 SIVAL(b, 0, 0);
125 }
6902ed17 126
1cd5beeb 127 write_batch_flist_file(b, sizeof(int));
6902ed17 128
1cd5beeb 129 /* Write the string if there is one */
6902ed17 130
1cd5beeb
MP
131 if (buf != NULL) {
132 write_batch_flist_file(buf, strlen(buf));
133 }
6902ed17
MP
134}
135
76f79ba7 136void write_batch_argvs_file(int argc, char *argv[])
6902ed17 137{
1cd5beeb
MP
138 int fdb;
139 int i;
140 char buff[256];
141
142 strcat(rsync_argvs_file, batch_file_ext);
143
144
145 /* Open batch argvs file for writing; create it if it doesn't exist */
146 fdb = do_open(rsync_argvs_file, O_WRONLY | O_CREAT | O_TRUNC,
147 S_IREAD | S_IWRITE | S_IEXEC);
148 if (fdb == -1) {
149 rprintf(FERROR, "Batch file %s open error: %s\n",
150 rsync_argvs_file, strerror(errno));
151 close(fdb);
152 exit_cleanup(1);
153 }
154 buff[0] = '\0';
155 /* Write argvs info to batch file */
156
76f79ba7
MP
157 for (i = 0; i < argc; ++i) {
158 if (i == argc - 2)
159 continue;
160 /*
161 * FIXME:
162 * I think directly manipulating argv[] is probably bogus
163 */
164 if (!strcmp(argv[i], "--write-batch")) {
165 /* Safer to change it here than script */
166 /* Change to --read-batch + ext * to get ready for remote */
167 strlcat(buff, "--read-batch ", sizeof(buff));
168 strlcat(buff, batch_file_ext, sizeof(buff));
1cd5beeb 169 } else {
76f79ba7 170 strlcat(buff, argv[i], sizeof(buff));
1cd5beeb
MP
171 }
172
173 if (i < (argc - 1)) {
76f79ba7 174 strlcat(buff, " ", sizeof(buff));
1cd5beeb
MP
175 }
176 }
76f79ba7 177 strlcat(buff, "\n", sizeof(buff));
1cd5beeb
MP
178 if (!write(fdb, buff, strlen(buff))) {
179 rprintf(FERROR, "Batch file %s write error: %s\n",
180 rsync_argvs_file, strerror(errno));
181 close(fdb);
182 exit_cleanup(1);
183 }
184 close(fdb);
6902ed17
MP
185}
186
187struct file_list *create_flist_from_batch()
188{
1cd5beeb
MP
189 unsigned char flags;
190
191 fdb_open = 1;
192 fdb_close = 0;
193
194 batch_flist = (struct file_list *) malloc(sizeof(batch_flist[0]));
195 if (!batch_flist) {
196 out_of_memory("create_flist_from_batch");
197 }
198 batch_flist->count = 0;
199 batch_flist->malloced = 1000;
200 batch_flist->files =
201 (struct file_struct **) malloc(sizeof(batch_flist->files[0]) *
202 batch_flist->malloced);
203 if (!batch_flist->files) {
204 out_of_memory("create_flist_from_batch"); /* dw -- will exit */
205 }
206
207 for (flags = read_batch_flags(); flags; flags = read_batch_flags()) {
208
209 int i = batch_flist->count;
210
211 if (i >= batch_flist->malloced) {
212 if (batch_flist->malloced < 1000)
213 batch_flist->malloced += 1000;
214 else
215 batch_flist->malloced *= 2;
216 batch_flist->files =
217 (struct file_struct **) realloc(batch_flist->
218 files,
219 sizeof
220 (batch_flist->
221 files[0]) *
222 batch_flist->
223 malloced);
224 if (!batch_flist->files)
225 out_of_memory("create_flist_from_batch");
226 }
227 read_batch_flist_info(&batch_flist->files[i]);
228 batch_flist->files[i]->flags = flags;
229
230 batch_flist->count++;
231 }
232
233 return batch_flist;
6902ed17
MP
234
235}
236
237int read_batch_flist_file(char *buff, int len)
238{
1cd5beeb
MP
239 int bytes_read;
240
241 if (fdb_open) {
242
243 /* Set up file extension */
244 strcat(rsync_flist_file, batch_file_ext);
245
246 /* Open batch flist file for reading */
247 fdb = do_open(rsync_flist_file, O_RDONLY, 0);
248 if (fdb == -1) {
249 rprintf(FERROR, "Batch file %s open error: %s\n",
250 rsync_flist_file, strerror(errno));
251 close(fdb);
252 exit_cleanup(1);
253 }
254 fdb_open = 0;
255 }
256
257 /* Read flist batch file */
258
259 bytes_read = read(fdb, buff, len);
260
261 if (bytes_read == -1) {
262 rprintf(FERROR, "Batch file %s read error: %s\n",
263 rsync_flist_file, strerror(errno));
264 close(fdb);
265 exit_cleanup(1);
266 }
267 if (bytes_read == 0) { /* EOF */
268 close(fdb);
269 }
270 return bytes_read;
6902ed17
MP
271}
272
273unsigned char read_batch_flags()
274{
1cd5beeb
MP
275 int flags;
276
277 if (read_batch_flist_file((char *) &flags, 4)) {
278 return 1;
279 } else {
280 return 0;
281 }
6902ed17
MP
282}
283
284void read_batch_flist_info(struct file_struct **fptr)
285{
1cd5beeb
MP
286 int int_str_len;
287 char char_str_len[4];
288 char buff[256];
289 struct file_struct *file;
290
291 file = (struct file_struct *) malloc(sizeof(*file));
292 if (!file)
293 out_of_memory("read_batch_flist_info");
294 memset((char *) file, 0, sizeof(*file));
295
296 (*fptr) = file;
297
298 read_batch_flist_file((char *) &file->modtime, sizeof(time_t));
299 read_batch_flist_file((char *) &file->length, sizeof(OFF_T));
300 read_batch_flist_file((char *) &file->mode, sizeof(mode_t));
6abd193f
MP
301 read_batch_flist_file((char *) &file->inode, sizeof(INO64_T));
302 read_batch_flist_file((char *) &file->dev, sizeof(DEV64_T));
76f79ba7 303 read_batch_flist_file((char *) &file->rdev, sizeof(DEV64_T));
1cd5beeb
MP
304 read_batch_flist_file((char *) &file->uid, sizeof(uid_t));
305 read_batch_flist_file((char *) &file->gid, sizeof(gid_t));
306 read_batch_flist_file(char_str_len, sizeof(char_str_len));
307 int_str_len = IVAL(char_str_len, 0);
308 if (int_str_len > 0) {
309 read_batch_flist_file(buff, int_str_len);
310 buff[int_str_len] = '\0';
311 file->basename = strdup(buff);
312 } else {
313 file->basename = NULL;
314 }
315
316 read_batch_flist_file(char_str_len, sizeof(char_str_len));
317 int_str_len = IVAL(char_str_len, 0);
318 if (int_str_len > 0) {
319 read_batch_flist_file(buff, int_str_len);
320 buff[int_str_len] = '\0';
321 file[0].dirname = strdup(buff);
322 } else {
323 file[0].dirname = NULL;
324 }
325
326 read_batch_flist_file(char_str_len, sizeof(char_str_len));
327 int_str_len = IVAL(char_str_len, 0);
328 if (int_str_len > 0) {
329 read_batch_flist_file(buff, int_str_len);
330 buff[int_str_len] = '\0';
331 file[0].basedir = strdup(buff);
332 } else {
333 file[0].basedir = NULL;
334 }
335
336 read_batch_flist_file(char_str_len, sizeof(char_str_len));
337 int_str_len = IVAL(char_str_len, 0);
338 if (int_str_len > 0) {
339 read_batch_flist_file(buff, int_str_len);
340 buff[int_str_len] = '\0';
341 file[0].link = strdup(buff);
342 } else {
343 file[0].link = NULL;
344 }
345
346 read_batch_flist_file(char_str_len, sizeof(char_str_len));
347 int_str_len = IVAL(char_str_len, 0);
348 if (int_str_len > 0) {
349 read_batch_flist_file(buff, int_str_len);
350 buff[int_str_len] = '\0';
351 file[0].sum = strdup(buff);
352 } else {
353 file[0].sum = NULL;
354 }
6902ed17
MP
355}
356
357void write_batch_csums_file(char *buff, int bytes_to_write)
358{
359
1cd5beeb
MP
360 static int fdb_open = 1;
361
362 if (fdb_open) {
363 /* Set up file extension */
364 strcat(rsync_csums_file, batch_file_ext);
365
366 /* Open batch csums file for writing; create it if it doesn't exist */
367 fdb =
368 do_open(rsync_csums_file, O_WRONLY | O_CREAT | O_TRUNC,
369 S_IREAD | S_IWRITE);
370 if (fdb == -1) {
371 rprintf(FERROR, "Batch file %s open error: %s\n",
372 rsync_csums_file, strerror(errno));
373 close(fdb);
374 exit_cleanup(1);
375 }
376 fdb_open = 0;
377 }
378
379 /* Write buffer to batch csums file */
380
381 if (write(fdb, buff, bytes_to_write) == -1) {
382 rprintf(FERROR, "Batch file %s write error: %s\n",
383 rsync_csums_file, strerror(errno));
384 close(fdb);
385 exit_cleanup(1);
386 }
6902ed17
MP
387}
388
1cd5beeb 389void close_batch_csums_file()
6902ed17 390{
1cd5beeb 391 close(fdb);
6902ed17
MP
392
393}
394
1cd5beeb
MP
395void write_batch_csum_info(int *flist_entry, int flist_count,
396 struct sum_struct *s)
6902ed17 397{
1cd5beeb 398 int i;
935b9201 399 unsigned int int_zero = 0;
1cd5beeb
MP
400 extern int csum_length;
401
402 fdb_open = 1;
403
404 /* Write csum info to batch file */
405
929e3011 406 /* FIXME: This will break if s->count is ever not exactly an int. */
1cd5beeb 407 write_batch_csums_file((char *) flist_entry, sizeof(int));
929e3011
MP
408 if (s)
409 write_batch_csums_file((char *) &s->count, sizeof(int));
410 else
411 write_batch_csums_file((char *) &int_zero, sizeof (int));
412
1cd5beeb
MP
413 if (s) {
414 for (i = 0; i < s->count; i++) {
415 write_batch_csums_file((char *) &s->sums[i].sum1,
416 sizeof(uint32));
417 if ((*flist_entry == flist_count - 1)
418 && (i == s->count - 1)) {
419 fdb_close = 1;
420 }
421 write_batch_csums_file(s->sums[i].sum2,
422 csum_length);
423 }
424 }
6902ed17
MP
425}
426
427int read_batch_csums_file(char *buff, int len)
428{
1cd5beeb
MP
429 static int fdb_open = 1;
430 int bytes_read;
431
432 if (fdb_open) {
433
434 /* Set up file extension */
435 strcat(rsync_csums_file, batch_file_ext);
436
437 /* Open batch flist file for reading */
438 fdb = do_open(rsync_csums_file, O_RDONLY, 0);
439 if (fdb == -1) {
440 rprintf(FERROR, "Batch file %s open error: %s\n",
441 rsync_csums_file, strerror(errno));
442 close(fdb);
443 exit_cleanup(1);
444 }
445 fdb_open = 0;
446 }
447
448 /* Read csums batch file */
449
450 bytes_read = read(fdb, buff, len);
451
452 if (bytes_read == -1) {
453 rprintf(FERROR, "Batch file %s read error: %s\n",
454 rsync_csums_file, strerror(errno));
455 close(fdb);
456 exit_cleanup(1);
457 }
458 return bytes_read;
6902ed17
MP
459}
460
461
1cd5beeb
MP
462void read_batch_csum_info(int flist_entry, struct sum_struct *s,
463 int *checksums_match)
6902ed17 464{
1cd5beeb
MP
465 int i;
466 int file_flist_entry;
467 int file_chunk_ct;
468 uint32 file_sum1;
469 char file_sum2[SUM_LENGTH];
470 extern int csum_length;
471
472
473 read_batch_csums_file((char *) &file_flist_entry, sizeof(int));
474 if (file_flist_entry != flist_entry) {
475 rprintf(FINFO, "file_list_entry NE flist_entry\n");
476 rprintf(FINFO, "file_flist_entry = %d flist_entry = %d\n",
477 file_flist_entry, flist_entry);
478 close(fdb);
479 exit_cleanup(1);
480
481 } else {
482 read_batch_csums_file((char *) &file_chunk_ct,
483 sizeof(int));
484 *checksums_match = 1;
485 for (i = 0; i < file_chunk_ct; i++) {
486
487 read_batch_csums_file((char *) &file_sum1,
488 sizeof(uint32));
489 read_batch_csums_file(file_sum2, csum_length);
490
491 if ((s->sums[i].sum1 != file_sum1) ||
492 (memcmp
493 (s->sums[i].sum2, file_sum2,
494 csum_length) != 0)) {
495 *checksums_match = 0;
496 }
497 } /* end for */
498 }
6902ed17
MP
499
500}
501
502void write_batch_delta_file(char *buff, int bytes_to_write)
503{
1cd5beeb
MP
504 static int fdb_delta_open = 1;
505
506 if (fdb_delta_open) {
507 /* Set up file extension */
508 strcat(rsync_delta_file, batch_file_ext);
509
510 /* Open batch delta file for writing; create it if it doesn't exist */
511 fdb_delta =
512 do_open(rsync_delta_file, O_WRONLY | O_CREAT | O_TRUNC,
513 S_IREAD | S_IWRITE);
514 if (fdb_delta == -1) {
515 rprintf(FERROR, "Batch file %s open error: %s\n",
516 rsync_delta_file, strerror(errno));
517 close(fdb_delta);
518 exit_cleanup(1);
519 }
520 fdb_delta_open = 0;
521 }
522
523 /* Write buffer to batch delta file */
524
525 if (write(fdb_delta, buff, bytes_to_write) == -1) {
526 rprintf(FERROR, "Batch file %s write error: %s\n",
527 rsync_delta_file, strerror(errno));
528 close(fdb_delta);
529 exit_cleanup(1);
530 }
6902ed17
MP
531}
532void close_batch_delta_file()
533{
1cd5beeb 534 close(fdb_delta);
6902ed17
MP
535
536}
537
538int read_batch_delta_file(char *buff, int len)
539{
1cd5beeb
MP
540 static int fdb_delta_open = 1;
541 int bytes_read;
542
543 if (fdb_delta_open) {
544
545 /* Set up file extension */
546 strcat(rsync_delta_file, batch_file_ext);
547
548 /* Open batch flist file for reading */
549 fdb_delta = do_open(rsync_delta_file, O_RDONLY, 0);
550 if (fdb_delta == -1) {
551 rprintf(FERROR, "Batch file %s open error: %s\n",
552 rsync_delta_file, strerror(errno));
553 close(fdb_delta);
554 exit_cleanup(1);
555 }
556 fdb_delta_open = 0;
557 }
558
559 /* Read delta batch file */
560
561 bytes_read = read(fdb_delta, buff, len);
562
563 if (bytes_read == -1) {
564 rprintf(FERROR, "Batch file %s read error: %s\n",
565 rsync_delta_file, strerror(errno));
566 close(fdb_delta);
567 exit_cleanup(1);
568 }
569 return bytes_read;
6902ed17
MP
570}
571
572
573void show_flist(int index, struct file_struct **fptr)
574{
1cd5beeb
MP
575 /* for debugging show_flist(flist->count, flist->files * */
576
577 int i;
578 for (i = 0; i < index; i++) {
579 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
580 rprintf(FINFO, "flist->modtime=%#lx\n",
581 (long unsigned) fptr[i]->modtime);
582 rprintf(FINFO, "flist->length=%.0f\n",
583 (double) fptr[i]->length);
584 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
585 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
586 if (fptr[i]->dirname)
587 rprintf(FINFO, "flist->dirname=%s\n",
588 fptr[i]->dirname);
589 if (fptr[i]->basedir)
590 rprintf(FINFO, "flist->basedir=%s\n",
591 fptr[i]->basedir);
592 }
6902ed17
MP
593}
594
595void show_argvs(int argc, char *argv[])
596{
1cd5beeb 597 /* for debugging * */
6902ed17 598
1cd5beeb
MP
599 int i;
600 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
601 for (i = 0; i < argc; i++) {
602 /* if (argv[i]) */
603 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
6902ed17 604
1cd5beeb 605 }
6902ed17 606}