blob: d3f6523386f1ad21723096d34e6ec21aa17153e2 [file] [log] [blame]
Martin Pool08a740f2001-08-15 06:41:24 +00001/* -*- c-file-style: "linux" -*-
2
Martin Pool6902ed12001-08-14 02:04:47 +00003 Weiss 1/1999
Martin Pool08a740f2001-08-15 06:41:24 +00004 Batch utilities for rsync.
Martin Pool6902ed12001-08-14 02:04:47 +00005
Martin Pool1cd5bee2001-12-02 22:28:50 +00006*/
Martin Pool6902ed12001-08-14 02:04:47 +00007
8#include "rsync.h"
9#include <time.h>
10
David Dykstra088aac82002-02-06 21:20:48 +000011extern char *batch_prefix;
Martin Pool6902ed12001-08-14 02:04:47 +000012
13struct file_list *batch_flist;
14
David Dykstra088aac82002-02-06 21:20:48 +000015static char rsync_flist_file[] = ".rsync_flist";
16static char rsync_csums_file[] = ".rsync_csums";
17static char rsync_delta_file[] = ".rsync_delta";
18static char rsync_argvs_file[] = ".rsync_argvs";
Martin Pool6902ed12001-08-14 02:04:47 +000019
David Dykstra088aac82002-02-06 21:20:48 +000020static int fdb;
21static int fdb_delta;
22static int fdb_open;
23static int fdb_close;
Martin Pool6902ed12001-08-14 02:04:47 +000024
25void write_batch_flist_file(char *buff, int bytes_to_write)
26{
David Dykstra088aac82002-02-06 21:20:48 +000027 char filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +000028
Martin Pool1cd5bee2001-12-02 22:28:50 +000029 if (fdb_open) {
30 /* Set up file extension */
David Dykstra088aac82002-02-06 21:20:48 +000031 strlcpy(filename, batch_prefix, sizeof(filename));
32 strlcat(filename, rsync_flist_file, sizeof(filename));
Martin Pool6902ed12001-08-14 02:04:47 +000033
David Dykstra088aac82002-02-06 21:20:48 +000034 /*
35 * Open batch flist file for writing;
36 * create it if it doesn't exist
37 */
38 fdb = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
Martin Pool1cd5bee2001-12-02 22:28:50 +000039 S_IREAD | S_IWRITE);
40 if (fdb == -1) {
41 rprintf(FERROR, "Batch file %s open error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +000042 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +000043 close(fdb);
44 exit_cleanup(1);
45 }
46 fdb_open = 0;
47 }
Martin Pool6902ed12001-08-14 02:04:47 +000048
Martin Pool1cd5bee2001-12-02 22:28:50 +000049 /* Write buffer to batch flist file */
Martin Pool6902ed12001-08-14 02:04:47 +000050
Martin Pool1cd5bee2001-12-02 22:28:50 +000051 if (write(fdb, buff, bytes_to_write) == -1) {
52 rprintf(FERROR, "Batch file %s write error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +000053 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +000054 close(fdb);
55 exit_cleanup(1);
56 }
Martin Pool6902ed12001-08-14 02:04:47 +000057
Martin Pool1cd5bee2001-12-02 22:28:50 +000058 if (fdb_close) {
59 close(fdb);
60 }
Martin Pool6902ed12001-08-14 02:04:47 +000061}
62
63void write_batch_flist_info(int flist_count, struct file_struct **fptr)
64{
Martin Pool1cd5bee2001-12-02 22:28:50 +000065 int i;
66 int bytes_to_write;
Martin Pool6902ed12001-08-14 02:04:47 +000067
Martin Pool1cd5bee2001-12-02 22:28:50 +000068 /* Write flist info to batch file */
Martin Pool6902ed12001-08-14 02:04:47 +000069
Martin Pool76f79ba2002-01-23 05:51:06 +000070 bytes_to_write =
71 sizeof(unsigned) +
Martin Pool1cd5bee2001-12-02 22:28:50 +000072 sizeof(time_t) +
73 sizeof(OFF_T) +
74 sizeof(mode_t) +
Martin Pool6abd1932002-01-11 08:25:32 +000075 sizeof(INO64_T) +
Martin Pool76f79ba2002-01-23 05:51:06 +000076 sizeof(DEV64_T) +
77 sizeof(DEV64_T) +
78 sizeof(uid_t) +
79 sizeof(gid_t);
Martin Pool6902ed12001-08-14 02:04:47 +000080
Martin Pool1cd5bee2001-12-02 22:28:50 +000081 fdb_open = 1;
82 fdb_close = 0;
Martin Pool6902ed12001-08-14 02:04:47 +000083
Martin Pool1cd5bee2001-12-02 22:28:50 +000084 for (i = 0; i < flist_count; i++) {
85 write_batch_flist_file((char *) fptr[i], bytes_to_write);
86 write_char_bufs(fptr[i]->basename);
87 write_char_bufs(fptr[i]->dirname);
88 write_char_bufs(fptr[i]->basedir);
89 write_char_bufs(fptr[i]->link);
90 if (i == flist_count - 1) {
91 fdb_close = 1;
92 }
93 write_char_bufs(fptr[i]->sum);
94 }
Martin Pool6902ed12001-08-14 02:04:47 +000095}
96
97void write_char_bufs(char *buf)
98{
Martin Pool1cd5bee2001-12-02 22:28:50 +000099 /* Write the size of the string which will follow */
Martin Pool6902ed12001-08-14 02:04:47 +0000100
Martin Pool1cd5bee2001-12-02 22:28:50 +0000101 char b[4];
David Dykstra088aac82002-02-06 21:20:48 +0000102
103 SIVAL(b, 0, buf != NULL ? strlen(buf) : 0);
Martin Pool6902ed12001-08-14 02:04:47 +0000104
Martin Pool1cd5bee2001-12-02 22:28:50 +0000105 write_batch_flist_file(b, sizeof(int));
Martin Pool6902ed12001-08-14 02:04:47 +0000106
Martin Pool1cd5bee2001-12-02 22:28:50 +0000107 /* Write the string if there is one */
Martin Pool6902ed12001-08-14 02:04:47 +0000108
Martin Pool1cd5bee2001-12-02 22:28:50 +0000109 if (buf != NULL) {
110 write_batch_flist_file(buf, strlen(buf));
111 }
Martin Pool6902ed12001-08-14 02:04:47 +0000112}
113
Martin Pool76f79ba2002-01-23 05:51:06 +0000114void write_batch_argvs_file(int argc, char *argv[])
Martin Pool6902ed12001-08-14 02:04:47 +0000115{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000116 int fdb;
117 int i;
David Dykstra088aac82002-02-06 21:20:48 +0000118 char buff[256]; /* XXX */
119 char buff2[MAXPATHLEN + 6];
120 char filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000121
David Dykstra088aac82002-02-06 21:20:48 +0000122 /* Set up file extension */
123 strlcpy(filename, batch_prefix, sizeof(filename));
124 strlcat(filename, rsync_argvs_file, sizeof(filename));
Martin Pool6902ed12001-08-14 02:04:47 +0000125
David Dykstra088aac82002-02-06 21:20:48 +0000126 /*
127 * Open batch argvs file for writing;
128 * create it if it doesn't exist
129 */
130 fdb = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
Martin Pool1cd5bee2001-12-02 22:28:50 +0000131 S_IREAD | S_IWRITE | S_IEXEC);
132 if (fdb == -1) {
133 rprintf(FERROR, "Batch file %s open error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000134 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000135 close(fdb);
136 exit_cleanup(1);
137 }
138 buff[0] = '\0';
David Dykstra088aac82002-02-06 21:20:48 +0000139
Martin Pool1cd5bee2001-12-02 22:28:50 +0000140 /* Write argvs info to batch file */
141
Martin Pool76f79ba2002-01-23 05:51:06 +0000142 for (i = 0; i < argc; ++i) {
David Dykstra088aac82002-02-06 21:20:48 +0000143 if (i == argc - 2) /* Skip source directory on cmdline */
Martin Pool76f79ba2002-01-23 05:51:06 +0000144 continue;
145 /*
146 * FIXME:
147 * I think directly manipulating argv[] is probably bogus
148 */
David Dykstra088aac82002-02-06 21:20:48 +0000149 if (!strncmp(argv[i], "--write-batch",
150 strlen("--write-batch"))) {
Martin Pool76f79ba2002-01-23 05:51:06 +0000151 /* Safer to change it here than script */
David Dykstra088aac82002-02-06 21:20:48 +0000152 /*
153 * Change to --read-batch=prefix
154 * to get ready for remote
155 */
156 strlcat(buff, "--read-batch=", sizeof(buff));
157 strlcat(buff, batch_prefix, sizeof(buff));
158 } else
159 if (i == argc - 1) {
160 snprintf(buff2, sizeof(buff2), "${1:-%s}", argv[i]);
161 strlcat(buff, buff2, sizeof(buff));
162 }
163 else {
Martin Pool76f79ba2002-01-23 05:51:06 +0000164 strlcat(buff, argv[i], sizeof(buff));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000165 }
166
167 if (i < (argc - 1)) {
Martin Pool76f79ba2002-01-23 05:51:06 +0000168 strlcat(buff, " ", sizeof(buff));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000169 }
170 }
Martin Pool76f79ba2002-01-23 05:51:06 +0000171 strlcat(buff, "\n", sizeof(buff));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000172 if (!write(fdb, buff, strlen(buff))) {
173 rprintf(FERROR, "Batch file %s write error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000174 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000175 close(fdb);
176 exit_cleanup(1);
177 }
178 close(fdb);
Martin Pool6902ed12001-08-14 02:04:47 +0000179}
180
David Dykstra088aac82002-02-06 21:20:48 +0000181struct file_list *create_flist_from_batch(void)
Martin Pool6902ed12001-08-14 02:04:47 +0000182{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000183 unsigned char flags;
Martin Pool6902ed12001-08-14 02:04:47 +0000184
Martin Pool1cd5bee2001-12-02 22:28:50 +0000185 fdb_open = 1;
186 fdb_close = 0;
Martin Pool6902ed12001-08-14 02:04:47 +0000187
Martin Pool1cd5bee2001-12-02 22:28:50 +0000188 batch_flist = (struct file_list *) malloc(sizeof(batch_flist[0]));
189 if (!batch_flist) {
190 out_of_memory("create_flist_from_batch");
191 }
192 batch_flist->count = 0;
193 batch_flist->malloced = 1000;
194 batch_flist->files =
195 (struct file_struct **) malloc(sizeof(batch_flist->files[0]) *
196 batch_flist->malloced);
197 if (!batch_flist->files) {
David Dykstra088aac82002-02-06 21:20:48 +0000198 out_of_memory("create_flist_from_batch");
Martin Pool1cd5bee2001-12-02 22:28:50 +0000199 }
Martin Pool6902ed12001-08-14 02:04:47 +0000200
Martin Pool1cd5bee2001-12-02 22:28:50 +0000201 for (flags = read_batch_flags(); flags; flags = read_batch_flags()) {
Martin Pool6902ed12001-08-14 02:04:47 +0000202
Martin Pool1cd5bee2001-12-02 22:28:50 +0000203 int i = batch_flist->count;
Martin Pool6902ed12001-08-14 02:04:47 +0000204
Martin Pool1cd5bee2001-12-02 22:28:50 +0000205 if (i >= batch_flist->malloced) {
206 if (batch_flist->malloced < 1000)
207 batch_flist->malloced += 1000;
208 else
209 batch_flist->malloced *= 2;
210 batch_flist->files =
211 (struct file_struct **) realloc(batch_flist->
212 files,
213 sizeof
214 (batch_flist->
215 files[0]) *
216 batch_flist->
217 malloced);
218 if (!batch_flist->files)
219 out_of_memory("create_flist_from_batch");
220 }
221 read_batch_flist_info(&batch_flist->files[i]);
222 batch_flist->files[i]->flags = flags;
Martin Pool6902ed12001-08-14 02:04:47 +0000223
Martin Pool1cd5bee2001-12-02 22:28:50 +0000224 batch_flist->count++;
225 }
226
227 return batch_flist;
Martin Pool6902ed12001-08-14 02:04:47 +0000228}
229
230int read_batch_flist_file(char *buff, int len)
231{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000232 int bytes_read;
David Dykstra088aac82002-02-06 21:20:48 +0000233 char filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000234
Martin Pool1cd5bee2001-12-02 22:28:50 +0000235 if (fdb_open) {
David Dykstra088aac82002-02-06 21:20:48 +0000236 /* Set up file extension */
237 strlcpy(filename, batch_prefix, sizeof(filename));
238 strlcat(filename, rsync_flist_file, sizeof(filename));
Martin Pool6902ed12001-08-14 02:04:47 +0000239
Martin Pool1cd5bee2001-12-02 22:28:50 +0000240 /* Open batch flist file for reading */
David Dykstra088aac82002-02-06 21:20:48 +0000241 fdb = do_open(filename, O_RDONLY, 0);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000242 if (fdb == -1) {
243 rprintf(FERROR, "Batch file %s open error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000244 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000245 close(fdb);
246 exit_cleanup(1);
247 }
248 fdb_open = 0;
249 }
Martin Pool6902ed12001-08-14 02:04:47 +0000250
Martin Pool1cd5bee2001-12-02 22:28:50 +0000251 /* Read flist batch file */
Martin Pool6902ed12001-08-14 02:04:47 +0000252
David Dykstra088aac82002-02-06 21:20:48 +0000253 switch (bytes_read = read(fdb, buff, len)) {
254 case -1:
Martin Pool1cd5bee2001-12-02 22:28:50 +0000255 rprintf(FERROR, "Batch file %s read error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000256 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000257 close(fdb);
258 exit_cleanup(1);
David Dykstra088aac82002-02-06 21:20:48 +0000259 break;
260 case 0: /* EOF */
Martin Pool1cd5bee2001-12-02 22:28:50 +0000261 close(fdb);
262 }
David Dykstra088aac82002-02-06 21:20:48 +0000263
Martin Pool1cd5bee2001-12-02 22:28:50 +0000264 return bytes_read;
Martin Pool6902ed12001-08-14 02:04:47 +0000265}
266
Jos Backus1e34e4b2002-12-24 07:42:04 +0000267unsigned char read_batch_flags(void)
Martin Pool6902ed12001-08-14 02:04:47 +0000268{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000269 int flags;
Martin Pool6902ed12001-08-14 02:04:47 +0000270
Martin Pool1cd5bee2001-12-02 22:28:50 +0000271 if (read_batch_flist_file((char *) &flags, 4)) {
272 return 1;
273 } else {
274 return 0;
275 }
Martin Pool6902ed12001-08-14 02:04:47 +0000276}
277
278void read_batch_flist_info(struct file_struct **fptr)
279{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000280 int int_str_len;
281 char char_str_len[4];
282 char buff[256];
283 struct file_struct *file;
Martin Pool6902ed12001-08-14 02:04:47 +0000284
Martin Pool1cd5bee2001-12-02 22:28:50 +0000285 file = (struct file_struct *) malloc(sizeof(*file));
286 if (!file)
287 out_of_memory("read_batch_flist_info");
288 memset((char *) file, 0, sizeof(*file));
Martin Pool6902ed12001-08-14 02:04:47 +0000289
David Dykstra088aac82002-02-06 21:20:48 +0000290 *fptr = file;
Martin Pool6902ed12001-08-14 02:04:47 +0000291
David Dykstra088aac82002-02-06 21:20:48 +0000292 /*
293 * Keep these in sync with bytes_to_write assignment
294 * in write_batch_flist_info()
295 */
Martin Pool1cd5bee2001-12-02 22:28:50 +0000296 read_batch_flist_file((char *) &file->modtime, sizeof(time_t));
297 read_batch_flist_file((char *) &file->length, sizeof(OFF_T));
298 read_batch_flist_file((char *) &file->mode, sizeof(mode_t));
Martin Pool6abd1932002-01-11 08:25:32 +0000299 read_batch_flist_file((char *) &file->inode, sizeof(INO64_T));
300 read_batch_flist_file((char *) &file->dev, sizeof(DEV64_T));
Martin Pool76f79ba2002-01-23 05:51:06 +0000301 read_batch_flist_file((char *) &file->rdev, sizeof(DEV64_T));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000302 read_batch_flist_file((char *) &file->uid, sizeof(uid_t));
303 read_batch_flist_file((char *) &file->gid, sizeof(gid_t));
304 read_batch_flist_file(char_str_len, sizeof(char_str_len));
305 int_str_len = IVAL(char_str_len, 0);
306 if (int_str_len > 0) {
307 read_batch_flist_file(buff, int_str_len);
308 buff[int_str_len] = '\0';
309 file->basename = strdup(buff);
310 } else {
311 file->basename = NULL;
312 }
Martin Pool6902ed12001-08-14 02:04:47 +0000313
Martin Pool1cd5bee2001-12-02 22:28:50 +0000314 read_batch_flist_file(char_str_len, sizeof(char_str_len));
315 int_str_len = IVAL(char_str_len, 0);
316 if (int_str_len > 0) {
317 read_batch_flist_file(buff, int_str_len);
318 buff[int_str_len] = '\0';
319 file[0].dirname = strdup(buff);
320 } else {
321 file[0].dirname = NULL;
322 }
Martin Pool6902ed12001-08-14 02:04:47 +0000323
Martin Pool1cd5bee2001-12-02 22:28:50 +0000324 read_batch_flist_file(char_str_len, sizeof(char_str_len));
325 int_str_len = IVAL(char_str_len, 0);
326 if (int_str_len > 0) {
327 read_batch_flist_file(buff, int_str_len);
328 buff[int_str_len] = '\0';
329 file[0].basedir = strdup(buff);
330 } else {
331 file[0].basedir = NULL;
332 }
Martin Pool6902ed12001-08-14 02:04:47 +0000333
Martin Pool1cd5bee2001-12-02 22:28:50 +0000334 read_batch_flist_file(char_str_len, sizeof(char_str_len));
335 int_str_len = IVAL(char_str_len, 0);
336 if (int_str_len > 0) {
337 read_batch_flist_file(buff, int_str_len);
338 buff[int_str_len] = '\0';
339 file[0].link = strdup(buff);
340 } else {
341 file[0].link = NULL;
342 }
Martin Pool6902ed12001-08-14 02:04:47 +0000343
Martin Pool1cd5bee2001-12-02 22:28:50 +0000344 read_batch_flist_file(char_str_len, sizeof(char_str_len));
345 int_str_len = IVAL(char_str_len, 0);
346 if (int_str_len > 0) {
347 read_batch_flist_file(buff, int_str_len);
348 buff[int_str_len] = '\0';
349 file[0].sum = strdup(buff);
350 } else {
351 file[0].sum = NULL;
352 }
Martin Pool6902ed12001-08-14 02:04:47 +0000353}
354
Martin Pool55d9e0f2002-01-24 08:08:56 +0000355void write_batch_csums_file(void *buff, int bytes_to_write)
Martin Pool6902ed12001-08-14 02:04:47 +0000356{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000357 static int fdb_open = 1;
David Dykstra088aac82002-02-06 21:20:48 +0000358 char filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000359
Martin Pool1cd5bee2001-12-02 22:28:50 +0000360 if (fdb_open) {
361 /* Set up file extension */
David Dykstra088aac82002-02-06 21:20:48 +0000362 strlcpy(filename, batch_prefix, sizeof(filename));
363 strlcat(filename, rsync_csums_file, sizeof(filename));
Martin Pool6902ed12001-08-14 02:04:47 +0000364
David Dykstra088aac82002-02-06 21:20:48 +0000365 /*
366 * Open batch csums file for writing;
367 * create it if it doesn't exist
368 */
369 fdb = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
Martin Pool1cd5bee2001-12-02 22:28:50 +0000370 S_IREAD | S_IWRITE);
371 if (fdb == -1) {
372 rprintf(FERROR, "Batch file %s open error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000373 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000374 close(fdb);
375 exit_cleanup(1);
376 }
377 fdb_open = 0;
378 }
Martin Pool6902ed12001-08-14 02:04:47 +0000379
Martin Pool1cd5bee2001-12-02 22:28:50 +0000380 /* Write buffer to batch csums file */
Martin Pool6902ed12001-08-14 02:04:47 +0000381
Martin Pool1cd5bee2001-12-02 22:28:50 +0000382 if (write(fdb, buff, bytes_to_write) == -1) {
383 rprintf(FERROR, "Batch file %s write error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000384 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000385 close(fdb);
386 exit_cleanup(1);
387 }
Martin Pool6902ed12001-08-14 02:04:47 +0000388}
389
David Dykstra088aac82002-02-06 21:20:48 +0000390void close_batch_csums_file(void)
Martin Pool6902ed12001-08-14 02:04:47 +0000391{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000392 close(fdb);
Martin Pool6902ed12001-08-14 02:04:47 +0000393}
394
Martin Poolf8f4c862002-04-03 05:55:54 +0000395
396/**
397 * Write csum info to batch file
398 *
399 * @todo This will break if s->count is ever larger than maxint. The
400 * batch code should probably be changed to consistently use the
401 * variable-length integer routines, which is probably a compatible
402 * change.
403 **/
Martin Pool1cd5bee2001-12-02 22:28:50 +0000404void write_batch_csum_info(int *flist_entry, int flist_count,
405 struct sum_struct *s)
Martin Pool6902ed12001-08-14 02:04:47 +0000406{
Martin Pool56648712002-01-24 08:09:46 +0000407 size_t i;
Martin Pool1bc209b2002-04-03 06:55:24 +0000408 int int_count;
Martin Pool1cd5bee2001-12-02 22:28:50 +0000409 extern int csum_length;
Martin Pool6902ed12001-08-14 02:04:47 +0000410
Martin Pool1cd5bee2001-12-02 22:28:50 +0000411 fdb_open = 1;
Martin Pool6902ed12001-08-14 02:04:47 +0000412
Martin Pool55d9e0f2002-01-24 08:08:56 +0000413 write_batch_csums_file(flist_entry, sizeof(int));
Martin Pool1bc209b2002-04-03 06:55:24 +0000414 int_count = s ? (int) s->count : 0;
415 write_batch_csums_file(&int_count, sizeof int_count);
Martin Pool929e3012002-01-24 08:07:35 +0000416
Martin Pool1cd5bee2001-12-02 22:28:50 +0000417 if (s) {
418 for (i = 0; i < s->count; i++) {
Martin Pool55d9e0f2002-01-24 08:08:56 +0000419 write_batch_csums_file(&s->sums[i].sum1, sizeof(uint32));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000420 if ((*flist_entry == flist_count - 1)
421 && (i == s->count - 1)) {
422 fdb_close = 1;
423 }
David Dykstra088aac82002-02-06 21:20:48 +0000424 write_batch_csums_file(s->sums[i].sum2, csum_length);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000425 }
426 }
Martin Pool6902ed12001-08-14 02:04:47 +0000427}
428
429int read_batch_csums_file(char *buff, int len)
430{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000431 static int fdb_open = 1;
432 int bytes_read;
David Dykstra088aac82002-02-06 21:20:48 +0000433 char filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000434
Martin Pool1cd5bee2001-12-02 22:28:50 +0000435 if (fdb_open) {
David Dykstra088aac82002-02-06 21:20:48 +0000436 /* Set up file extension */
437 strlcpy(filename, batch_prefix, sizeof(filename));
438 strlcat(filename, rsync_csums_file, sizeof(filename));
Martin Pool6902ed12001-08-14 02:04:47 +0000439
Martin Pool1cd5bee2001-12-02 22:28:50 +0000440 /* Open batch flist file for reading */
David Dykstra088aac82002-02-06 21:20:48 +0000441 fdb = do_open(filename, O_RDONLY, 0);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000442 if (fdb == -1) {
443 rprintf(FERROR, "Batch file %s open error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000444 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000445 close(fdb);
446 exit_cleanup(1);
447 }
448 fdb_open = 0;
449 }
Martin Pool6902ed12001-08-14 02:04:47 +0000450
Martin Pool1cd5bee2001-12-02 22:28:50 +0000451 /* Read csums batch file */
Martin Pool6902ed12001-08-14 02:04:47 +0000452
Martin Pool1cd5bee2001-12-02 22:28:50 +0000453 bytes_read = read(fdb, buff, len);
454
455 if (bytes_read == -1) {
456 rprintf(FERROR, "Batch file %s read error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000457 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000458 close(fdb);
459 exit_cleanup(1);
460 }
David Dykstra088aac82002-02-06 21:20:48 +0000461
Martin Pool1cd5bee2001-12-02 22:28:50 +0000462 return bytes_read;
Martin Pool6902ed12001-08-14 02:04:47 +0000463}
464
Martin Pool1cd5bee2001-12-02 22:28:50 +0000465void read_batch_csum_info(int flist_entry, struct sum_struct *s,
466 int *checksums_match)
Martin Pool6902ed12001-08-14 02:04:47 +0000467{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000468 int i;
469 int file_flist_entry;
470 int file_chunk_ct;
471 uint32 file_sum1;
472 char file_sum2[SUM_LENGTH];
473 extern int csum_length;
Martin Pool6902ed12001-08-14 02:04:47 +0000474
Martin Pool1cd5bee2001-12-02 22:28:50 +0000475 read_batch_csums_file((char *) &file_flist_entry, sizeof(int));
476 if (file_flist_entry != flist_entry) {
David Dykstra088aac82002-02-06 21:20:48 +0000477 rprintf(FINFO, "file_flist_entry (%d) != flist_entry (%d)\n",
Martin Pool1cd5bee2001-12-02 22:28:50 +0000478 file_flist_entry, flist_entry);
479 close(fdb);
480 exit_cleanup(1);
481
482 } else {
483 read_batch_csums_file((char *) &file_chunk_ct,
484 sizeof(int));
485 *checksums_match = 1;
486 for (i = 0; i < file_chunk_ct; i++) {
487
488 read_batch_csums_file((char *) &file_sum1,
489 sizeof(uint32));
490 read_batch_csums_file(file_sum2, csum_length);
491
492 if ((s->sums[i].sum1 != file_sum1) ||
David Dykstra088aac82002-02-06 21:20:48 +0000493 (memcmp(s->sums[i].sum2, file_sum2, csum_length)
494 != 0)) {
Martin Pool1cd5bee2001-12-02 22:28:50 +0000495 *checksums_match = 0;
496 }
497 } /* end for */
498 }
Martin Pool6902ed12001-08-14 02:04:47 +0000499}
500
501void write_batch_delta_file(char *buff, int bytes_to_write)
502{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000503 static int fdb_delta_open = 1;
David Dykstra088aac82002-02-06 21:20:48 +0000504 char filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000505
Martin Pool1cd5bee2001-12-02 22:28:50 +0000506 if (fdb_delta_open) {
507 /* Set up file extension */
David Dykstra088aac82002-02-06 21:20:48 +0000508 strlcpy(filename, batch_prefix, sizeof(filename));
509 strlcat(filename, rsync_delta_file, sizeof(filename));
Martin Pool6902ed12001-08-14 02:04:47 +0000510
David Dykstra088aac82002-02-06 21:20:48 +0000511 /*
512 * Open batch delta file for writing;
513 * create it if it doesn't exist
514 */
515 fdb_delta = do_open(filename, O_WRONLY | O_CREAT | O_TRUNC,
Martin Pool1cd5bee2001-12-02 22:28:50 +0000516 S_IREAD | S_IWRITE);
517 if (fdb_delta == -1) {
518 rprintf(FERROR, "Batch file %s open error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000519 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000520 close(fdb_delta);
521 exit_cleanup(1);
522 }
523 fdb_delta_open = 0;
524 }
Martin Pool6902ed12001-08-14 02:04:47 +0000525
Martin Pool1cd5bee2001-12-02 22:28:50 +0000526 /* Write buffer to batch delta file */
Martin Pool6902ed12001-08-14 02:04:47 +0000527
Martin Pool1cd5bee2001-12-02 22:28:50 +0000528 if (write(fdb_delta, buff, bytes_to_write) == -1) {
529 rprintf(FERROR, "Batch file %s write error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000530 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000531 close(fdb_delta);
532 exit_cleanup(1);
533 }
Martin Pool6902ed12001-08-14 02:04:47 +0000534}
David Dykstra088aac82002-02-06 21:20:48 +0000535
536void close_batch_delta_file(void)
Martin Pool6902ed12001-08-14 02:04:47 +0000537{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000538 close(fdb_delta);
Martin Pool6902ed12001-08-14 02:04:47 +0000539}
540
541int read_batch_delta_file(char *buff, int len)
542{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000543 static int fdb_delta_open = 1;
544 int bytes_read;
David Dykstra088aac82002-02-06 21:20:48 +0000545 char filename[MAXPATHLEN];
Martin Pool6902ed12001-08-14 02:04:47 +0000546
Martin Pool1cd5bee2001-12-02 22:28:50 +0000547 if (fdb_delta_open) {
David Dykstra088aac82002-02-06 21:20:48 +0000548 /* Set up file extension */
549 strlcpy(filename, batch_prefix, sizeof(filename));
550 strlcat(filename, rsync_delta_file, sizeof(filename));
Martin Pool6902ed12001-08-14 02:04:47 +0000551
Martin Pool1cd5bee2001-12-02 22:28:50 +0000552 /* Open batch flist file for reading */
David Dykstra088aac82002-02-06 21:20:48 +0000553 fdb_delta = do_open(filename, O_RDONLY, 0);
Martin Pool1cd5bee2001-12-02 22:28:50 +0000554 if (fdb_delta == -1) {
555 rprintf(FERROR, "Batch file %s open error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000556 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000557 close(fdb_delta);
558 exit_cleanup(1);
559 }
560 fdb_delta_open = 0;
561 }
Martin Pool6902ed12001-08-14 02:04:47 +0000562
Martin Pool1cd5bee2001-12-02 22:28:50 +0000563 /* Read delta batch file */
Martin Pool6902ed12001-08-14 02:04:47 +0000564
Martin Pool1cd5bee2001-12-02 22:28:50 +0000565 bytes_read = read(fdb_delta, buff, len);
566
567 if (bytes_read == -1) {
568 rprintf(FERROR, "Batch file %s read error: %s\n",
David Dykstra088aac82002-02-06 21:20:48 +0000569 filename, strerror(errno));
Martin Pool1cd5bee2001-12-02 22:28:50 +0000570 close(fdb_delta);
571 exit_cleanup(1);
572 }
David Dykstra088aac82002-02-06 21:20:48 +0000573
Martin Pool1cd5bee2001-12-02 22:28:50 +0000574 return bytes_read;
Martin Pool6902ed12001-08-14 02:04:47 +0000575}
576
Martin Pool6902ed12001-08-14 02:04:47 +0000577void show_flist(int index, struct file_struct **fptr)
578{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000579 /* for debugging show_flist(flist->count, flist->files * */
Martin Pool6902ed12001-08-14 02:04:47 +0000580
Martin Pool1cd5bee2001-12-02 22:28:50 +0000581 int i;
582 for (i = 0; i < index; i++) {
583 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
584 rprintf(FINFO, "flist->modtime=%#lx\n",
585 (long unsigned) fptr[i]->modtime);
586 rprintf(FINFO, "flist->length=%.0f\n",
587 (double) fptr[i]->length);
588 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
589 rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
590 if (fptr[i]->dirname)
591 rprintf(FINFO, "flist->dirname=%s\n",
592 fptr[i]->dirname);
593 if (fptr[i]->basedir)
594 rprintf(FINFO, "flist->basedir=%s\n",
595 fptr[i]->basedir);
596 }
Martin Pool6902ed12001-08-14 02:04:47 +0000597}
598
599void show_argvs(int argc, char *argv[])
600{
Martin Pool1cd5bee2001-12-02 22:28:50 +0000601 /* for debugging * */
Martin Pool6902ed12001-08-14 02:04:47 +0000602
Martin Pool1cd5bee2001-12-02 22:28:50 +0000603 int i;
604 rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
605 for (i = 0; i < argc; i++) {
606 /* if (argv[i]) */
607 rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);
Martin Pool6902ed12001-08-14 02:04:47 +0000608
Martin Pool1cd5bee2001-12-02 22:28:50 +0000609 }
Martin Pool6902ed12001-08-14 02:04:47 +0000610}