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 /* FIXME: This apparently crashes if rsync is run with
154 * just "rsync -F". I think directly manipulating
155 * argv[] is probably bogus -- what if -F is part of a
156 * run of several short options? */
157 if (!strcmp(argv[i], "-F")) { /* safer to change it here than script */
158 strncat(buff, "-f ", 3); /* chg to -f + ext to get ready for remote */
159 strncat(buff, batch_file_ext,
160 strlen(batch_file_ext));
162 strncat(buff, argv[i], strlen(argv[i]));
165 if (i < (argc - 1)) {
166 strncat(buff, " ", 1);
169 if (!write(fdb, buff, strlen(buff))) {
170 rprintf(FERROR, "Batch file %s write error: %s\n",
171 rsync_argvs_file, strerror(errno));
178 struct file_list *create_flist_from_batch()
185 batch_flist = (struct file_list *) malloc(sizeof(batch_flist[0]));
187 out_of_memory("create_flist_from_batch");
189 batch_flist->count = 0;
190 batch_flist->malloced = 1000;
192 (struct file_struct **) malloc(sizeof(batch_flist->files[0]) *
193 batch_flist->malloced);
194 if (!batch_flist->files) {
195 out_of_memory("create_flist_from_batch"); /* dw -- will exit */
198 for (flags = read_batch_flags(); flags; flags = read_batch_flags()) {
200 int i = batch_flist->count;
202 if (i >= batch_flist->malloced) {
203 if (batch_flist->malloced < 1000)
204 batch_flist->malloced += 1000;
206 batch_flist->malloced *= 2;
208 (struct file_struct **) realloc(batch_flist->
215 if (!batch_flist->files)
216 out_of_memory("create_flist_from_batch");
218 read_batch_flist_info(&batch_flist->files[i]);
219 batch_flist->files[i]->flags = flags;
221 batch_flist->count++;
228 int read_batch_flist_file(char *buff, int len)
234 /* Set up file extension */
235 strcat(rsync_flist_file, batch_file_ext);
237 /* Open batch flist file for reading */
238 fdb = do_open(rsync_flist_file, O_RDONLY, 0);
240 rprintf(FERROR, "Batch file %s open error: %s\n",
241 rsync_flist_file, strerror(errno));
248 /* Read flist batch file */
250 bytes_read = read(fdb, buff, len);
252 if (bytes_read == -1) {
253 rprintf(FERROR, "Batch file %s read error: %s\n",
254 rsync_flist_file, strerror(errno));
258 if (bytes_read == 0) { /* EOF */
264 unsigned char read_batch_flags()
268 if (read_batch_flist_file((char *) &flags, 4)) {
275 void read_batch_flist_info(struct file_struct **fptr)
278 char char_str_len[4];
280 struct file_struct *file;
282 file = (struct file_struct *) malloc(sizeof(*file));
284 out_of_memory("read_batch_flist_info");
285 memset((char *) file, 0, sizeof(*file));
289 read_batch_flist_file((char *) &file->modtime, sizeof(time_t));
290 read_batch_flist_file((char *) &file->length, sizeof(OFF_T));
291 read_batch_flist_file((char *) &file->mode, sizeof(mode_t));
292 read_batch_flist_file((char *) &file->inode, sizeof(INO_T));
293 read_batch_flist_file((char *) &file->dev, sizeof(dev_t));
294 read_batch_flist_file((char *) &file->rdev, sizeof(dev_t));
295 read_batch_flist_file((char *) &file->uid, sizeof(uid_t));
296 read_batch_flist_file((char *) &file->gid, sizeof(gid_t));
297 read_batch_flist_file(char_str_len, sizeof(char_str_len));
298 int_str_len = IVAL(char_str_len, 0);
299 if (int_str_len > 0) {
300 read_batch_flist_file(buff, int_str_len);
301 buff[int_str_len] = '\0';
302 file->basename = strdup(buff);
304 file->basename = NULL;
307 read_batch_flist_file(char_str_len, sizeof(char_str_len));
308 int_str_len = IVAL(char_str_len, 0);
309 if (int_str_len > 0) {
310 read_batch_flist_file(buff, int_str_len);
311 buff[int_str_len] = '\0';
312 file[0].dirname = strdup(buff);
314 file[0].dirname = NULL;
317 read_batch_flist_file(char_str_len, sizeof(char_str_len));
318 int_str_len = IVAL(char_str_len, 0);
319 if (int_str_len > 0) {
320 read_batch_flist_file(buff, int_str_len);
321 buff[int_str_len] = '\0';
322 file[0].basedir = strdup(buff);
324 file[0].basedir = NULL;
327 read_batch_flist_file(char_str_len, sizeof(char_str_len));
328 int_str_len = IVAL(char_str_len, 0);
329 if (int_str_len > 0) {
330 read_batch_flist_file(buff, int_str_len);
331 buff[int_str_len] = '\0';
332 file[0].link = strdup(buff);
337 read_batch_flist_file(char_str_len, sizeof(char_str_len));
338 int_str_len = IVAL(char_str_len, 0);
339 if (int_str_len > 0) {
340 read_batch_flist_file(buff, int_str_len);
341 buff[int_str_len] = '\0';
342 file[0].sum = strdup(buff);
348 void write_batch_csums_file(char *buff, int bytes_to_write)
351 static int fdb_open = 1;
354 /* Set up file extension */
355 strcat(rsync_csums_file, batch_file_ext);
357 /* Open batch csums file for writing; create it if it doesn't exist */
359 do_open(rsync_csums_file, O_WRONLY | O_CREAT | O_TRUNC,
362 rprintf(FERROR, "Batch file %s open error: %s\n",
363 rsync_csums_file, strerror(errno));
370 /* Write buffer to batch csums file */
372 if (write(fdb, buff, bytes_to_write) == -1) {
373 rprintf(FERROR, "Batch file %s write error: %s\n",
374 rsync_csums_file, strerror(errno));
380 void close_batch_csums_file()
386 void write_batch_csum_info(int *flist_entry, int flist_count,
387 struct sum_struct *s)
391 extern int csum_length;
395 /* Write csum info to batch file */
397 write_batch_csums_file((char *) flist_entry, sizeof(int));
398 write_batch_csums_file((char *) (s ? &s->count : &int_zero),
401 for (i = 0; i < s->count; i++) {
402 write_batch_csums_file((char *) &s->sums[i].sum1,
404 if ((*flist_entry == flist_count - 1)
405 && (i == s->count - 1)) {
408 write_batch_csums_file(s->sums[i].sum2,
414 int read_batch_csums_file(char *buff, int len)
416 static int fdb_open = 1;
421 /* Set up file extension */
422 strcat(rsync_csums_file, batch_file_ext);
424 /* Open batch flist file for reading */
425 fdb = do_open(rsync_csums_file, O_RDONLY, 0);
427 rprintf(FERROR, "Batch file %s open error: %s\n",
428 rsync_csums_file, strerror(errno));
435 /* Read csums batch file */
437 bytes_read = read(fdb, buff, len);
439 if (bytes_read == -1) {
440 rprintf(FERROR, "Batch file %s read error: %s\n",
441 rsync_csums_file, strerror(errno));
449 void read_batch_csum_info(int flist_entry, struct sum_struct *s,
450 int *checksums_match)
453 int file_flist_entry;
456 char file_sum2[SUM_LENGTH];
457 extern int csum_length;
460 read_batch_csums_file((char *) &file_flist_entry, sizeof(int));
461 if (file_flist_entry != flist_entry) {
462 rprintf(FINFO, "file_list_entry NE flist_entry\n");
463 rprintf(FINFO, "file_flist_entry = %d flist_entry = %d\n",
464 file_flist_entry, flist_entry);
469 read_batch_csums_file((char *) &file_chunk_ct,
471 *checksums_match = 1;
472 for (i = 0; i < file_chunk_ct; i++) {
474 read_batch_csums_file((char *) &file_sum1,
476 read_batch_csums_file(file_sum2, csum_length);
478 if ((s->sums[i].sum1 != file_sum1) ||
480 (s->sums[i].sum2, file_sum2,
481 csum_length) != 0)) {
482 *checksums_match = 0;
489 void write_batch_delta_file(char *buff, int bytes_to_write)
491 static int fdb_delta_open = 1;
493 if (fdb_delta_open) {
494 /* Set up file extension */
495 strcat(rsync_delta_file, batch_file_ext);
497 /* Open batch delta file for writing; create it if it doesn't exist */
499 do_open(rsync_delta_file, O_WRONLY | O_CREAT | O_TRUNC,
501 if (fdb_delta == -1) {
502 rprintf(FERROR, "Batch file %s open error: %s\n",
503 rsync_delta_file, strerror(errno));
510 /* Write buffer to batch delta file */
512 if (write(fdb_delta, buff, bytes_to_write) == -1) {
513 rprintf(FERROR, "Batch file %s write error: %s\n",
514 rsync_delta_file, strerror(errno));
519 void close_batch_delta_file()
525 int read_batch_delta_file(char *buff, int len)
527 static int fdb_delta_open = 1;
530 if (fdb_delta_open) {
532 /* Set up file extension */
533 strcat(rsync_delta_file, batch_file_ext);
535 /* Open batch flist file for reading */
536 fdb_delta = do_open(rsync_delta_file, O_RDONLY, 0);
537 if (fdb_delta == -1) {
538 rprintf(FERROR, "Batch file %s open error: %s\n",
539 rsync_delta_file, strerror(errno));
546 /* Read delta batch file */
548 bytes_read = read(fdb_delta, buff, len);
550 if (bytes_read == -1) {
551 rprintf(FERROR, "Batch file %s read error: %s\n",
552 rsync_delta_file, strerror(errno));
560 void show_flist(int index, struct file_struct **fptr)
562 /* for debugging show_flist(flist->count, flist->files * */
565 for (i = 0; i < index; i++) {
566 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
567 rprintf(FINFO, "flist->modtime=%#lx\n",
568 (long unsigned) fptr[i]->modtime);
569 rprintf(FINFO, "flist->length=%.0f\n",
570 (double) fptr[i]->length);
571 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
572 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
573 if (fptr[i]->dirname)
574 rprintf(FINFO, "flist->dirname=%s\n",
576 if (fptr[i]->basedir)
577 rprintf(FINFO, "flist->basedir=%s\n",
582 void show_argvs(int argc, char *argv[])
584 /* for debugging * */
587 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
588 for (i = 0; i < argc; i++) {
590 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);