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