1 /* -*- c-file-style: "linux" -*-
4 Batch utilities for rsync.
11 char rsync_flist_file[27] = "rsync_flist.";
12 char rsync_csums_file[27] = "rsync_csums.";
13 char rsync_delta_file[27] = "rsync_delta.";
14 char rsync_argvs_file[27] = "rsync_argvs.";
16 char batch_file_ext[15];
23 struct file_list *batch_flist;
25 void create_batch_file_ext()
28 time_t elapsed_seconds;
30 /* Save run date and time to use for batch file extensions */
31 time(&elapsed_seconds);
32 timeptr = localtime(&elapsed_seconds);
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,
40 void set_batch_file_ext(char *ext)
42 strcpy(batch_file_ext, ext);
45 void write_batch_flist_file(char *buff, int bytes_to_write)
49 /* Set up file extension */
50 strcat(rsync_flist_file, batch_file_ext);
52 /* Open batch flist file for writing; create it if it doesn't exist */
54 do_open(rsync_flist_file, O_WRONLY | O_CREAT | O_TRUNC,
57 rprintf(FERROR, "Batch file %s open error: %s\n",
58 rsync_flist_file, strerror(errno));
65 /* Write buffer to batch flist file */
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));
80 void write_batch_flist_info(int flist_count, struct file_struct **fptr)
85 /* Write flist info to batch file */
87 bytes_to_write = sizeof(unsigned) +
92 (2 * sizeof(dev_t)) + sizeof(uid_t) + sizeof(gid_t);
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) {
106 write_char_bufs(fptr[i]->sum);
111 void write_char_bufs(char *buf)
113 /* Write the size of the string which will follow */
117 SIVAL(b, 0, strlen(buf));
122 write_batch_flist_file(b, sizeof(int));
124 /* Write the string if there is one */
127 write_batch_flist_file(buf, strlen(buf));
131 void write_batch_argvs_file(int orig_argc, int argc, char **argv)
137 strcat(rsync_argvs_file, batch_file_ext);
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);
144 rprintf(FERROR, "Batch file %s open error: %s\n",
145 rsync_argvs_file, strerror(errno));
150 /* Write argvs info to batch file */
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));
158 strncat(buff, argv[i], strlen(argv[i]));
161 if (i < (argc - 1)) {
162 strncat(buff, " ", 1);
165 if (!write(fdb, buff, strlen(buff))) {
166 rprintf(FERROR, "Batch file %s write error: %s\n",
167 rsync_argvs_file, strerror(errno));
174 struct file_list *create_flist_from_batch()
181 batch_flist = (struct file_list *) malloc(sizeof(batch_flist[0]));
183 out_of_memory("create_flist_from_batch");
185 batch_flist->count = 0;
186 batch_flist->malloced = 1000;
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 */
194 for (flags = read_batch_flags(); flags; flags = read_batch_flags()) {
196 int i = batch_flist->count;
198 if (i >= batch_flist->malloced) {
199 if (batch_flist->malloced < 1000)
200 batch_flist->malloced += 1000;
202 batch_flist->malloced *= 2;
204 (struct file_struct **) realloc(batch_flist->
211 if (!batch_flist->files)
212 out_of_memory("create_flist_from_batch");
214 read_batch_flist_info(&batch_flist->files[i]);
215 batch_flist->files[i]->flags = flags;
217 batch_flist->count++;
224 int read_batch_flist_file(char *buff, int len)
230 /* Set up file extension */
231 strcat(rsync_flist_file, batch_file_ext);
233 /* Open batch flist file for reading */
234 fdb = do_open(rsync_flist_file, O_RDONLY, 0);
236 rprintf(FERROR, "Batch file %s open error: %s\n",
237 rsync_flist_file, strerror(errno));
244 /* Read flist batch file */
246 bytes_read = read(fdb, buff, len);
248 if (bytes_read == -1) {
249 rprintf(FERROR, "Batch file %s read error: %s\n",
250 rsync_flist_file, strerror(errno));
254 if (bytes_read == 0) { /* EOF */
260 unsigned char read_batch_flags()
264 if (read_batch_flist_file((char *) &flags, 4)) {
271 void read_batch_flist_info(struct file_struct **fptr)
274 char char_str_len[4];
276 struct file_struct *file;
278 file = (struct file_struct *) malloc(sizeof(*file));
280 out_of_memory("read_batch_flist_info");
281 memset((char *) file, 0, sizeof(*file));
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);
300 file->basename = NULL;
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);
310 file[0].dirname = NULL;
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);
320 file[0].basedir = NULL;
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);
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);
344 void write_batch_csums_file(char *buff, int bytes_to_write)
347 static int fdb_open = 1;
350 /* Set up file extension */
351 strcat(rsync_csums_file, batch_file_ext);
353 /* Open batch csums file for writing; create it if it doesn't exist */
355 do_open(rsync_csums_file, O_WRONLY | O_CREAT | O_TRUNC,
358 rprintf(FERROR, "Batch file %s open error: %s\n",
359 rsync_csums_file, strerror(errno));
366 /* Write buffer to batch csums file */
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));
376 void close_batch_csums_file()
382 void write_batch_csum_info(int *flist_entry, int flist_count,
383 struct sum_struct *s)
387 extern int csum_length;
391 /* Write csum info to batch file */
393 write_batch_csums_file((char *) flist_entry, sizeof(int));
394 write_batch_csums_file((char *) (s ? &s->count : &int_zero),
397 for (i = 0; i < s->count; i++) {
398 write_batch_csums_file((char *) &s->sums[i].sum1,
400 if ((*flist_entry == flist_count - 1)
401 && (i == s->count - 1)) {
404 write_batch_csums_file(s->sums[i].sum2,
410 int read_batch_csums_file(char *buff, int len)
412 static int fdb_open = 1;
417 /* Set up file extension */
418 strcat(rsync_csums_file, batch_file_ext);
420 /* Open batch flist file for reading */
421 fdb = do_open(rsync_csums_file, O_RDONLY, 0);
423 rprintf(FERROR, "Batch file %s open error: %s\n",
424 rsync_csums_file, strerror(errno));
431 /* Read csums batch file */
433 bytes_read = read(fdb, buff, len);
435 if (bytes_read == -1) {
436 rprintf(FERROR, "Batch file %s read error: %s\n",
437 rsync_csums_file, strerror(errno));
445 void read_batch_csum_info(int flist_entry, struct sum_struct *s,
446 int *checksums_match)
449 int file_flist_entry;
452 char file_sum2[SUM_LENGTH];
453 extern int csum_length;
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);
465 read_batch_csums_file((char *) &file_chunk_ct,
467 *checksums_match = 1;
468 for (i = 0; i < file_chunk_ct; i++) {
470 read_batch_csums_file((char *) &file_sum1,
472 read_batch_csums_file(file_sum2, csum_length);
474 if ((s->sums[i].sum1 != file_sum1) ||
476 (s->sums[i].sum2, file_sum2,
477 csum_length) != 0)) {
478 *checksums_match = 0;
485 void write_batch_delta_file(char *buff, int bytes_to_write)
487 static int fdb_delta_open = 1;
489 if (fdb_delta_open) {
490 /* Set up file extension */
491 strcat(rsync_delta_file, batch_file_ext);
493 /* Open batch delta file for writing; create it if it doesn't exist */
495 do_open(rsync_delta_file, O_WRONLY | O_CREAT | O_TRUNC,
497 if (fdb_delta == -1) {
498 rprintf(FERROR, "Batch file %s open error: %s\n",
499 rsync_delta_file, strerror(errno));
506 /* Write buffer to batch delta file */
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));
515 void close_batch_delta_file()
521 int read_batch_delta_file(char *buff, int len)
523 static int fdb_delta_open = 1;
526 if (fdb_delta_open) {
528 /* Set up file extension */
529 strcat(rsync_delta_file, batch_file_ext);
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));
542 /* Read delta batch file */
544 bytes_read = read(fdb_delta, buff, len);
546 if (bytes_read == -1) {
547 rprintf(FERROR, "Batch file %s read error: %s\n",
548 rsync_delta_file, strerror(errno));
556 void show_flist(int index, struct file_struct **fptr)
558 /* for debugging show_flist(flist->count, flist->files * */
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",
572 if (fptr[i]->basedir)
573 rprintf(FINFO, "flist->basedir=%s\n",
578 void show_argvs(int argc, char *argv[])
580 /* for debugging * */
583 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
584 for (i = 0; i < argc; i++) {
586 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);