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, timeptr
->tm_mday
,
36 timeptr
->tm_hour
, timeptr
->tm_min
, timeptr
->tm_sec
);
39 void set_batch_file_ext(char *ext
)
41 strcpy(batch_file_ext
, ext
);
44 void write_batch_flist_file(char *buff
, int bytes_to_write
)
48 /* Set up file extension */
49 strcat(rsync_flist_file
, batch_file_ext
);
51 /* Open batch flist file for writing; create it if it doesn't exist */
52 fdb
= do_open(rsync_flist_file
, O_WRONLY
|O_CREAT
|O_TRUNC
,
55 rprintf(FERROR
, "Batch file %s open error: %s\n",
56 rsync_flist_file
, strerror(errno
));
63 /* Write buffer to batch flist file */
65 if ( write(fdb
, buff
, bytes_to_write
) == -1 ) {
66 rprintf(FERROR
, "Batch file %s write error: %s\n",
67 rsync_flist_file
, strerror(errno
));
78 void write_batch_flist_info(int flist_count
, struct file_struct
**fptr
)
83 /* Write flist info to batch file */
85 bytes_to_write
= sizeof(unsigned) +
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", rsync_argvs_file
,
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
,strlen(batch_file_ext
));
158 strncat(buff
,argv
[i
],strlen(argv
[i
]));
161 if (i
< (argc
- 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;
187 batch_flist
->files
= (struct file_struct
**)malloc(sizeof(batch_flist
->files
[0])* batch_flist
->malloced
);
188 if (!batch_flist
->files
) {
189 out_of_memory("create_flist_from_batch"); /* dw -- will exit */
192 for ( flags
=read_batch_flags() ; flags
; flags
=read_batch_flags() ) {
194 int i
= batch_flist
->count
;
196 if (i
>= batch_flist
->malloced
) {
197 if (batch_flist
->malloced
< 1000)
198 batch_flist
->malloced
+= 1000;
200 batch_flist
->malloced
*= 2;
201 batch_flist
->files
=(struct file_struct
**)realloc(batch_flist
->files
,
202 sizeof(batch_flist
->files
[0])*
203 batch_flist
->malloced
);
204 if (!batch_flist
->files
)
205 out_of_memory("create_flist_from_batch");
207 read_batch_flist_info(&batch_flist
->files
[i
]);
208 batch_flist
->files
[i
]->flags
= flags
;
210 batch_flist
->count
++;
217 int read_batch_flist_file(char *buff
, int len
)
223 /* Set up file extension */
224 strcat(rsync_flist_file
, batch_file_ext
);
226 /* Open batch flist file for reading */
227 fdb
= do_open(rsync_flist_file
, O_RDONLY
, 0);
229 rprintf(FERROR
, "Batch file %s open error: %s\n", rsync_flist_file
,
237 /* Read flist batch file */
239 bytes_read
= read(fdb
, buff
, len
);
241 if (bytes_read
== -1) {
242 rprintf(FERROR
, "Batch file %s read error: %s\n",
243 rsync_flist_file
, strerror(errno
));
247 if (bytes_read
== 0) { /* EOF */
253 unsigned char read_batch_flags()
257 if (read_batch_flist_file((char *)&flags
, 4) ) {
265 void read_batch_flist_info(struct file_struct
**fptr
)
268 char char_str_len
[4];
270 struct file_struct
*file
;
272 file
= (struct file_struct
*)malloc(sizeof(*file
));
273 if (!file
) out_of_memory("read_batch_flist_info");
274 memset((char *)file
, 0, sizeof(*file
));
278 read_batch_flist_file((char *)&file
->modtime
, sizeof(time_t));
279 read_batch_flist_file((char *)&file
->length
, sizeof(OFF_T
));
280 read_batch_flist_file((char *)&file
->mode
, sizeof(mode_t
));
281 read_batch_flist_file((char *)&file
->inode
, sizeof(INO_T
));
282 read_batch_flist_file((char *)&file
->dev
, sizeof(dev_t
));
283 read_batch_flist_file((char *)&file
->rdev
, sizeof(dev_t
));
284 read_batch_flist_file((char *)&file
->uid
, sizeof(uid_t
));
285 read_batch_flist_file((char *)&file
->gid
, sizeof(gid_t
));
286 read_batch_flist_file(char_str_len
, sizeof(char_str_len
));
287 int_str_len
= IVAL(char_str_len
,0);
288 if (int_str_len
> 0) {
289 read_batch_flist_file(buff
, int_str_len
);
290 buff
[int_str_len
] = '\0';
291 file
->basename
= strdup(buff
);
294 file
->basename
= NULL
;
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
[0].dirname
= strdup(buff
);
305 file
[0].dirname
= NULL
;
308 read_batch_flist_file(char_str_len
, sizeof(char_str_len
));
309 int_str_len
= IVAL(char_str_len
,0);
310 if (int_str_len
> 0) {
311 read_batch_flist_file(buff
, int_str_len
);
312 buff
[int_str_len
] = '\0';
313 file
[0].basedir
= strdup(buff
);
316 file
[0].basedir
= NULL
;
319 read_batch_flist_file(char_str_len
, sizeof(char_str_len
));
320 int_str_len
= IVAL(char_str_len
,0);
321 if (int_str_len
> 0) {
322 read_batch_flist_file(buff
, int_str_len
);
323 buff
[int_str_len
] = '\0';
324 file
[0].link
= strdup(buff
);
330 read_batch_flist_file(char_str_len
, sizeof(char_str_len
));
331 int_str_len
= IVAL(char_str_len
,0);
332 if (int_str_len
> 0) {
333 read_batch_flist_file(buff
, int_str_len
);
334 buff
[int_str_len
] = '\0';
335 file
[0].sum
= strdup(buff
);
342 void write_batch_csums_file(char *buff
, int bytes_to_write
)
345 static int fdb_open
= 1;
348 /* Set up file extension */
349 strcat(rsync_csums_file
, batch_file_ext
);
351 /* Open batch csums file for writing; create it if it doesn't exist */
352 fdb
= do_open(rsync_csums_file
, O_WRONLY
|O_CREAT
|O_TRUNC
,
355 rprintf(FERROR
, "Batch file %s open error: %s\n",
356 rsync_csums_file
, strerror(errno
));
363 /* Write buffer to batch csums file */
365 if ( write(fdb
, buff
, bytes_to_write
) == -1 ) {
366 rprintf(FERROR
, "Batch file %s write error: %s\n",
367 rsync_csums_file
, strerror(errno
));
373 void close_batch_csums_file()
379 void write_batch_csum_info(int *flist_entry
, int flist_count
, struct sum_struct
*s
)
383 extern int csum_length
;
387 /* Write csum info to batch file */
389 write_batch_csums_file ( (char *) flist_entry
, sizeof(int) );
390 write_batch_csums_file ( (char *) (s
?&s
->count
:&int_zero
), sizeof(int) );
392 for (i
=0; i
< s
->count
; i
++) {
393 write_batch_csums_file( (char *) &s
->sums
[i
].sum1
, sizeof(uint32
));
394 if ( (*flist_entry
== flist_count
- 1) && (i
== s
->count
- 1) ) {
397 write_batch_csums_file( s
->sums
[i
].sum2
, csum_length
);
402 int read_batch_csums_file(char *buff
, int len
)
404 static int fdb_open
= 1;
409 /* Set up file extension */
410 strcat(rsync_csums_file
, batch_file_ext
);
412 /* Open batch flist file for reading */
413 fdb
= do_open(rsync_csums_file
, O_RDONLY
, 0);
415 rprintf(FERROR
, "Batch file %s open error: %s\n", rsync_csums_file
,
423 /* Read csums batch file */
425 bytes_read
= read(fdb
, buff
, len
);
427 if (bytes_read
== -1) {
428 rprintf(FERROR
, "Batch file %s read error: %s\n",
429 rsync_csums_file
, strerror(errno
));
437 void read_batch_csum_info(int flist_entry
, struct sum_struct
*s
, int *checksums_match
)
440 int file_flist_entry
;
443 char file_sum2
[SUM_LENGTH
];
444 extern int csum_length
;
447 read_batch_csums_file((char *)&file_flist_entry
, sizeof(int));
448 if (file_flist_entry
!= flist_entry
) {
449 rprintf(FINFO
,"file_list_entry NE flist_entry\n");
450 rprintf(FINFO
,"file_flist_entry = %d flist_entry = %d\n", file_flist_entry
, flist_entry
);
456 read_batch_csums_file((char *)&file_chunk_ct
, sizeof(int));
457 *checksums_match
= 1;
458 for (i
= 0;i
< file_chunk_ct
;i
++) {
460 read_batch_csums_file((char *)&file_sum1
, sizeof(uint32
));
461 read_batch_csums_file(file_sum2
, csum_length
);
463 if ( (s
->sums
[i
].sum1
!= file_sum1
) ||
464 ( memcmp(s
->sums
[i
].sum2
,file_sum2
, csum_length
)!=0) ) {
465 *checksums_match
= 0;
472 void write_batch_delta_file(char *buff
, int bytes_to_write
)
474 static int fdb_delta_open
= 1;
476 if (fdb_delta_open
) {
477 /* Set up file extension */
478 strcat(rsync_delta_file
, batch_file_ext
);
480 /* Open batch delta file for writing; create it if it doesn't exist */
481 fdb_delta
= do_open(rsync_delta_file
, O_WRONLY
|O_CREAT
|O_TRUNC
,
483 if (fdb_delta
== -1) {
484 rprintf(FERROR
, "Batch file %s open error: %s\n",
485 rsync_delta_file
, strerror(errno
));
492 /* Write buffer to batch delta file */
494 if ( write(fdb_delta
, buff
, bytes_to_write
) == -1 ) {
495 rprintf(FERROR
, "Batch file %s write error: %s\n",
496 rsync_delta_file
, strerror(errno
));
501 void close_batch_delta_file()
507 int read_batch_delta_file(char *buff
, int len
)
509 static int fdb_delta_open
= 1;
512 if (fdb_delta_open
) {
514 /* Set up file extension */
515 strcat(rsync_delta_file
, batch_file_ext
);
517 /* Open batch flist file for reading */
518 fdb_delta
= do_open(rsync_delta_file
, O_RDONLY
, 0);
519 if (fdb_delta
== -1) {
520 rprintf(FERROR
, "Batch file %s open error: %s\n", rsync_delta_file
,
528 /* Read delta batch file */
530 bytes_read
= read(fdb_delta
, buff
, len
);
532 if (bytes_read
== -1) {
533 rprintf(FERROR
, "Batch file %s read error: %s\n",
534 rsync_delta_file
, strerror(errno
));
542 void show_flist(int index
, struct file_struct
**fptr
)
544 /* for debugging show_flist(flist->count, flist->files **/
547 for (i
=0;i
<index
;i
++) {
548 rprintf(FINFO
, "flist->flags=%#x\n",fptr
[i
]->flags
);
549 rprintf(FINFO
, "flist->modtime=%#lx\n",
550 (long unsigned) fptr
[i
]->modtime
);
551 rprintf(FINFO
, "flist->length=%.0f\n", (double) fptr
[i
]->length
);
552 rprintf(FINFO
, "flist->mode=%#o\n", (int) fptr
[i
]->mode
);
553 rprintf(FINFO
, "flist->basename=%s\n",fptr
[i
]->basename
);
554 if (fptr
[i
]->dirname
)
555 rprintf(FINFO
, "flist->dirname=%s\n",fptr
[i
]->dirname
);
556 if (fptr
[i
]->basedir
)
557 rprintf(FINFO
, "flist->basedir=%s\n",fptr
[i
]->basedir
);
561 void show_argvs(int argc
, char *argv
[])
566 rprintf(FINFO
,"BATCH.C:show_argvs,argc=%d\n",argc
);
567 for (i
=0;i
<argc
;i
++) {
569 rprintf(FINFO
,"i=%d,argv[i]=%s\n",i
,argv
[i
]);