Move both calls to getpeername into a common wrapper function that
[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
406 write_batch_csums_file((char *) flist_entry, sizeof(int));
407 write_batch_csums_file((char *) (s ? &s->count : &int_zero),
408 sizeof(int));
409 if (s) {
410 for (i = 0; i < s->count; i++) {
411 write_batch_csums_file((char *) &s->sums[i].sum1,
412 sizeof(uint32));
413 if ((*flist_entry == flist_count - 1)
414 && (i == s->count - 1)) {
415 fdb_close = 1;
416 }
417 write_batch_csums_file(s->sums[i].sum2,
418 csum_length);
419 }
420 }
6902ed17
MP
421}
422
423int read_batch_csums_file(char *buff, int len)
424{
1cd5beeb
MP
425 static int fdb_open = 1;
426 int bytes_read;
427
428 if (fdb_open) {
429
430 /* Set up file extension */
431 strcat(rsync_csums_file, batch_file_ext);
432
433 /* Open batch flist file for reading */
434 fdb = do_open(rsync_csums_file, O_RDONLY, 0);
435 if (fdb == -1) {
436 rprintf(FERROR, "Batch file %s open error: %s\n",
437 rsync_csums_file, strerror(errno));
438 close(fdb);
439 exit_cleanup(1);
440 }
441 fdb_open = 0;
442 }
443
444 /* Read csums batch file */
445
446 bytes_read = read(fdb, buff, len);
447
448 if (bytes_read == -1) {
449 rprintf(FERROR, "Batch file %s read error: %s\n",
450 rsync_csums_file, strerror(errno));
451 close(fdb);
452 exit_cleanup(1);
453 }
454 return bytes_read;
6902ed17
MP
455}
456
457
1cd5beeb
MP
458void read_batch_csum_info(int flist_entry, struct sum_struct *s,
459 int *checksums_match)
6902ed17 460{
1cd5beeb
MP
461 int i;
462 int file_flist_entry;
463 int file_chunk_ct;
464 uint32 file_sum1;
465 char file_sum2[SUM_LENGTH];
466 extern int csum_length;
467
468
469 read_batch_csums_file((char *) &file_flist_entry, sizeof(int));
470 if (file_flist_entry != flist_entry) {
471 rprintf(FINFO, "file_list_entry NE flist_entry\n");
472 rprintf(FINFO, "file_flist_entry = %d flist_entry = %d\n",
473 file_flist_entry, flist_entry);
474 close(fdb);
475 exit_cleanup(1);
476
477 } else {
478 read_batch_csums_file((char *) &file_chunk_ct,
479 sizeof(int));
480 *checksums_match = 1;
481 for (i = 0; i < file_chunk_ct; i++) {
482
483 read_batch_csums_file((char *) &file_sum1,
484 sizeof(uint32));
485 read_batch_csums_file(file_sum2, csum_length);
486
487 if ((s->sums[i].sum1 != file_sum1) ||
488 (memcmp
489 (s->sums[i].sum2, file_sum2,
490 csum_length) != 0)) {
491 *checksums_match = 0;
492 }
493 } /* end for */
494 }
6902ed17
MP
495
496}
497
498void write_batch_delta_file(char *buff, int bytes_to_write)
499{
1cd5beeb
MP
500 static int fdb_delta_open = 1;
501
502 if (fdb_delta_open) {
503 /* Set up file extension */
504 strcat(rsync_delta_file, batch_file_ext);
505
506 /* Open batch delta file for writing; create it if it doesn't exist */
507 fdb_delta =
508 do_open(rsync_delta_file, O_WRONLY | O_CREAT | O_TRUNC,
509 S_IREAD | S_IWRITE);
510 if (fdb_delta == -1) {
511 rprintf(FERROR, "Batch file %s open error: %s\n",
512 rsync_delta_file, strerror(errno));
513 close(fdb_delta);
514 exit_cleanup(1);
515 }
516 fdb_delta_open = 0;
517 }
518
519 /* Write buffer to batch delta file */
520
521 if (write(fdb_delta, buff, bytes_to_write) == -1) {
522 rprintf(FERROR, "Batch file %s write error: %s\n",
523 rsync_delta_file, strerror(errno));
524 close(fdb_delta);
525 exit_cleanup(1);
526 }
6902ed17
MP
527}
528void close_batch_delta_file()
529{
1cd5beeb 530 close(fdb_delta);
6902ed17
MP
531
532}
533
534int read_batch_delta_file(char *buff, int len)
535{
1cd5beeb
MP
536 static int fdb_delta_open = 1;
537 int bytes_read;
538
539 if (fdb_delta_open) {
540
541 /* Set up file extension */
542 strcat(rsync_delta_file, batch_file_ext);
543
544 /* Open batch flist file for reading */
545 fdb_delta = do_open(rsync_delta_file, O_RDONLY, 0);
546 if (fdb_delta == -1) {
547 rprintf(FERROR, "Batch file %s open error: %s\n",
548 rsync_delta_file, strerror(errno));
549 close(fdb_delta);
550 exit_cleanup(1);
551 }
552 fdb_delta_open = 0;
553 }
554
555 /* Read delta batch file */
556
557 bytes_read = read(fdb_delta, buff, len);
558
559 if (bytes_read == -1) {
560 rprintf(FERROR, "Batch file %s read error: %s\n",
561 rsync_delta_file, strerror(errno));
562 close(fdb_delta);
563 exit_cleanup(1);
564 }
565 return bytes_read;
6902ed17
MP
566}
567
568
569void show_flist(int index, struct file_struct **fptr)
570{
1cd5beeb
MP
571 /* for debugging show_flist(flist->count, flist->files * */
572
573 int i;
574 for (i = 0; i < index; i++) {
575 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
576 rprintf(FINFO, "flist->modtime=%#lx\n",
577 (long unsigned) fptr[i]->modtime);
578 rprintf(FINFO, "flist->length=%.0f\n",
579 (double) fptr[i]->length);
580 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
581 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
582 if (fptr[i]->dirname)
583 rprintf(FINFO, "flist->dirname=%s\n",
584 fptr[i]->dirname);
585 if (fptr[i]->basedir)
586 rprintf(FINFO, "flist->basedir=%s\n",
587 fptr[i]->basedir);
588 }
6902ed17
MP
589}
590
591void show_argvs(int argc, char *argv[])
592{
1cd5beeb 593 /* for debugging * */
6902ed17 594
1cd5beeb
MP
595 int i;
596 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
597 for (i = 0; i < argc; i++) {
598 /* if (argv[i]) */
599 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
6902ed17 600
1cd5beeb 601 }
6902ed17 602}