summaryrefslogtreecommitdiff
path: root/batch.c
blob: 912bb69ec691df36e8955e65cf771d897e8dbbf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* -*- c-file-style: "linux" -*-
   
   Weiss 1/1999
   Batch utilities for rsync.

*/

#include "rsync.h"
#include <time.h>

char rsync_flist_file[27] = "rsync_flist.";
char rsync_csums_file[27] = "rsync_csums.";
char rsync_delta_file[27] = "rsync_delta.";
char rsync_argvs_file[27] = "rsync_argvs.";

char batch_file_ext[15];

int fdb;
int fdb_delta;
int fdb_open;
int fdb_close;

struct file_list *batch_flist;

void create_batch_file_ext()
{
	struct tm *timeptr;
	time_t elapsed_seconds;

	/* Save run date and time to use for batch file extensions */
	time(&elapsed_seconds);
	timeptr = localtime(&elapsed_seconds);

	sprintf(batch_file_ext, "%4d%02d%02d%02d%02d%02d",
		timeptr->tm_year + 1900, timeptr->tm_mon + 1,
		timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min,
		timeptr->tm_sec);
	rprintf(FINFO,"batch file extension: %s\n", batch_file_ext);
}

void set_batch_file_ext(char *ext)
{
	strcpy(batch_file_ext, ext);
}

void write_batch_flist_file(char *buff, int bytes_to_write)
{

	if (fdb_open) {
		/* Set up file extension */
		strcat(rsync_flist_file, batch_file_ext);

		/* Open batch flist file for writing; create it if it doesn't exist */
		fdb =
		    do_open(rsync_flist_file, O_WRONLY | O_CREAT | O_TRUNC,
			    S_IREAD | S_IWRITE);
		if (fdb == -1) {
			rprintf(FERROR, "Batch file %s open error: %s\n",
				rsync_flist_file, strerror(errno));
			close(fdb);
			exit_cleanup(1);
		}
		fdb_open = 0;
	}

	/* Write buffer to batch flist file */

	if (write(fdb, buff, bytes_to_write) == -1) {
		rprintf(FERROR, "Batch file %s write error: %s\n",
			rsync_flist_file, strerror(errno));
		close(fdb);
		exit_cleanup(1);
	}


	if (fdb_close) {
		close(fdb);
	}
}

void write_batch_flist_info(int flist_count, struct file_struct **fptr)
{
	int i;
	int bytes_to_write;

	/* Write flist info to batch file */

	bytes_to_write =
	    sizeof(unsigned) +
	    sizeof(time_t) +
	    sizeof(OFF_T) +
	    sizeof(mode_t) +
	    sizeof(INO64_T) +
	    sizeof(DEV64_T) +
	    sizeof(DEV64_T) +
	    sizeof(uid_t) +
	    sizeof(gid_t);

	fdb_open = 1;
	fdb_close = 0;

	for (i = 0; i < flist_count; i++) {
		write_batch_flist_file((char *) fptr[i], bytes_to_write);
		write_char_bufs(fptr[i]->basename);
		write_char_bufs(fptr[i]->dirname);
		write_char_bufs(fptr[i]->basedir);
		write_char_bufs(fptr[i]->link);
		if (i == flist_count - 1) {
			fdb_close = 1;
		}
		write_char_bufs(fptr[i]->sum);
	}

}

void write_char_bufs(char *buf)
{
	/* Write the size of the string which will follow  */

	char b[4];
	if (buf != NULL)
		SIVAL(b, 0, strlen(buf));
	else {
		SIVAL(b, 0, 0);
	}

	write_batch_flist_file(b, sizeof(int));

	/*  Write the string if there is one */

	if (buf != NULL) {
		write_batch_flist_file(buf, strlen(buf));
	}
}

void write_batch_argvs_file(int argc, char *argv[])
{
	int fdb;
	int i;
	char buff[256];

	strcat(rsync_argvs_file, batch_file_ext);


	/* Open batch argvs file for writing; create it if it doesn't exist */
	fdb = do_open(rsync_argvs_file, O_WRONLY | O_CREAT | O_TRUNC,
		      S_IREAD | S_IWRITE | S_IEXEC);
	if (fdb == -1) {
		rprintf(FERROR, "Batch file %s open error: %s\n",
			rsync_argvs_file, strerror(errno));
		close(fdb);
		exit_cleanup(1);
	}
	buff[0] = '\0';
	/* Write argvs info to batch file */

	for (i = 0; i < argc; ++i) {
		if (i == argc - 2)
		    continue;
		/*
		 * FIXME:
		 * I think directly manipulating argv[] is probably bogus
		 */
		if (!strcmp(argv[i], "--write-batch")) {
			/* Safer to change it here than script */
			/* Change to --read-batch + ext * to get ready for remote */
			strlcat(buff, "--read-batch ", sizeof(buff));
			strlcat(buff, batch_file_ext, sizeof(buff));
		} else {
			strlcat(buff, argv[i], sizeof(buff));
		}

		if (i < (argc - 1)) {
			strlcat(buff, " ", sizeof(buff));
		}
	}
	strlcat(buff, "\n", sizeof(buff));
	if (!write(fdb, buff, strlen(buff))) {
		rprintf(FERROR, "Batch file %s write error: %s\n",
			rsync_argvs_file, strerror(errno));
		close(fdb);
		exit_cleanup(1);
	}
	close(fdb);
}

struct file_list *create_flist_from_batch()
{
	unsigned char flags;

	fdb_open = 1;
	fdb_close = 0;

	batch_flist = (struct file_list *) malloc(sizeof(batch_flist[0]));
	if (!batch_flist) {
		out_of_memory("create_flist_from_batch");
	}
	batch_flist->count = 0;
	batch_flist->malloced = 1000;
	batch_flist->files =
	    (struct file_struct **) malloc(sizeof(batch_flist->files[0]) *
					   batch_flist->malloced);
	if (!batch_flist->files) {
		out_of_memory("create_flist_from_batch");	/* dw -- will exit */
	}

	for (flags = read_batch_flags(); flags; flags = read_batch_flags()) {

		int i = batch_flist->count;

		if (i >= batch_flist->malloced) {
			if (batch_flist->malloced < 1000)
				batch_flist->malloced += 1000;
			else
				batch_flist->malloced *= 2;
			batch_flist->files =
			    (struct file_struct **) realloc(batch_flist->
							    files,
							    sizeof
							    (batch_flist->
							     files[0]) *
							    batch_flist->
							    malloced);
			if (!batch_flist->files)
				out_of_memory("create_flist_from_batch");
		}
		read_batch_flist_info(&batch_flist->files[i]);
		batch_flist->files[i]->flags = flags;

		batch_flist->count++;
	}

	return batch_flist;

}

int read_batch_flist_file(char *buff, int len)
{
	int bytes_read;

	if (fdb_open) {

		/*  Set up file extension  */
		strcat(rsync_flist_file, batch_file_ext);

		/* Open batch flist file for reading */
		fdb = do_open(rsync_flist_file, O_RDONLY, 0);
		if (fdb == -1) {
			rprintf(FERROR, "Batch file %s open error: %s\n",
				rsync_flist_file, strerror(errno));
			close(fdb);
			exit_cleanup(1);
		}
		fdb_open = 0;
	}

	/* Read flist batch file */

	bytes_read = read(fdb, buff, len);

	if (bytes_read == -1) {
		rprintf(FERROR, "Batch file %s read error: %s\n",
			rsync_flist_file, strerror(errno));
		close(fdb);
		exit_cleanup(1);
	}
	if (bytes_read == 0) {	/* EOF */
		close(fdb);
	}
	return bytes_read;
}

unsigned char read_batch_flags()
{
	int flags;

	if (read_batch_flist_file((char *) &flags, 4)) {
		return 1;
	} else {
		return 0;
	}
}

void read_batch_flist_info(struct file_struct **fptr)
{
	int int_str_len;
	char char_str_len[4];
	char buff[256];
	struct file_struct *file;

	file = (struct file_struct *) malloc(sizeof(*file));
	if (!file)
		out_of_memory("read_batch_flist_info");
	memset((char *) file, 0, sizeof(*file));

	(*fptr) = file;

	read_batch_flist_file((char *) &file->modtime, sizeof(time_t));
	read_batch_flist_file((char *) &file->length, sizeof(OFF_T));
	read_batch_flist_file((char *) &file->mode, sizeof(mode_t));
	read_batch_flist_file((char *) &file->inode, sizeof(INO64_T));
	read_batch_flist_file((char *) &file->dev, sizeof(DEV64_T));
	read_batch_flist_file((char *) &file->rdev, sizeof(DEV64_T));
	read_batch_flist_file((char *) &file->uid, sizeof(uid_t));
	read_batch_flist_file((char *) &file->gid, sizeof(gid_t));
	read_batch_flist_file(char_str_len, sizeof(char_str_len));
	int_str_len = IVAL(char_str_len, 0);
	if (int_str_len > 0) {
		read_batch_flist_file(buff, int_str_len);
		buff[int_str_len] = '\0';
		file->basename = strdup(buff);
	} else {
		file->basename = NULL;
	}

	read_batch_flist_file(char_str_len, sizeof(char_str_len));
	int_str_len = IVAL(char_str_len, 0);
	if (int_str_len > 0) {
		read_batch_flist_file(buff, int_str_len);
		buff[int_str_len] = '\0';
		file[0].dirname = strdup(buff);
	} else {
		file[0].dirname = NULL;
	}

	read_batch_flist_file(char_str_len, sizeof(char_str_len));
	int_str_len = IVAL(char_str_len, 0);
	if (int_str_len > 0) {
		read_batch_flist_file(buff, int_str_len);
		buff[int_str_len] = '\0';
		file[0].basedir = strdup(buff);
	} else {
		file[0].basedir = NULL;
	}

	read_batch_flist_file(char_str_len, sizeof(char_str_len));
	int_str_len = IVAL(char_str_len, 0);
	if (int_str_len > 0) {
		read_batch_flist_file(buff, int_str_len);
		buff[int_str_len] = '\0';
		file[0].link = strdup(buff);
	} else {
		file[0].link = NULL;
	}

	read_batch_flist_file(char_str_len, sizeof(char_str_len));
	int_str_len = IVAL(char_str_len, 0);
	if (int_str_len > 0) {
		read_batch_flist_file(buff, int_str_len);
		buff[int_str_len] = '\0';
		file[0].sum = strdup(buff);
	} else {
		file[0].sum = NULL;
	}
}

void write_batch_csums_file(char *buff, int bytes_to_write)
{

	static int fdb_open = 1;

	if (fdb_open) {
		/* Set up file extension */
		strcat(rsync_csums_file, batch_file_ext);

		/* Open batch csums file for writing; create it if it doesn't exist */
		fdb =
		    do_open(rsync_csums_file, O_WRONLY | O_CREAT | O_TRUNC,
			    S_IREAD | S_IWRITE);
		if (fdb == -1) {
			rprintf(FERROR, "Batch file %s open error: %s\n",
				rsync_csums_file, strerror(errno));
			close(fdb);
			exit_cleanup(1);
		}
		fdb_open = 0;
	}

	/* Write buffer to batch csums file */

	if (write(fdb, buff, bytes_to_write) == -1) {
		rprintf(FERROR, "Batch file %s write error: %s\n",
			rsync_csums_file, strerror(errno));
		close(fdb);
		exit_cleanup(1);
	}
}

void close_batch_csums_file()
{
	close(fdb);

}

void write_batch_csum_info(int *flist_entry, int flist_count,
			   struct sum_struct *s)
{
	int i;
	unsigned int int_zero = 0;
	extern int csum_length;

	fdb_open = 1;

	/* Write csum info to batch file */

	/* FIXME: This will break if s->count is ever not exactly an int. */
	write_batch_csums_file((char *) flist_entry, sizeof(int));
	if (s)
		write_batch_csums_file((char *) &s->count, sizeof(int));
	else
		write_batch_csums_file((char *) &int_zero, sizeof (int));
	
	if (s) {
		for (i = 0; i < s->count; i++) {
			write_batch_csums_file((char *) &s->sums[i].sum1,
					       sizeof(uint32));
			if ((*flist_entry == flist_count - 1)
			    && (i == s->count - 1)) {
				fdb_close = 1;
			}
			write_batch_csums_file(s->sums[i].sum2,
					       csum_length);
		}
	}
}

int read_batch_csums_file(char *buff, int len)
{
	static int fdb_open = 1;
	int bytes_read;

	if (fdb_open) {

		/*  Set up file extension  */
		strcat(rsync_csums_file, batch_file_ext);

		/* Open batch flist file for reading */
		fdb = do_open(rsync_csums_file, O_RDONLY, 0);
		if (fdb == -1) {
			rprintf(FERROR, "Batch file %s open error: %s\n",
				rsync_csums_file, strerror(errno));
			close(fdb);
			exit_cleanup(1);
		}
		fdb_open = 0;
	}

	/* Read csums batch file */

	bytes_read = read(fdb, buff, len);

	if (bytes_read == -1) {
		rprintf(FERROR, "Batch file %s read error: %s\n",
			rsync_csums_file, strerror(errno));
		close(fdb);
		exit_cleanup(1);
	}
	return bytes_read;
}


void read_batch_csum_info(int flist_entry, struct sum_struct *s,
			  int *checksums_match)
{
	int i;
	int file_flist_entry;
	int file_chunk_ct;
	uint32 file_sum1;
	char file_sum2[SUM_LENGTH];
	extern int csum_length;


	read_batch_csums_file((char *) &file_flist_entry, sizeof(int));
	if (file_flist_entry != flist_entry) {
		rprintf(FINFO, "file_list_entry NE flist_entry\n");
		rprintf(FINFO, "file_flist_entry = %d  flist_entry = %d\n",
			file_flist_entry, flist_entry);
		close(fdb);
		exit_cleanup(1);

	} else {
		read_batch_csums_file((char *) &file_chunk_ct,
				      sizeof(int));
		*checksums_match = 1;
		for (i = 0; i < file_chunk_ct; i++) {

			read_batch_csums_file((char *) &file_sum1,
					      sizeof(uint32));
			read_batch_csums_file(file_sum2, csum_length);

			if ((s->sums[i].sum1 != file_sum1) ||
			    (memcmp
			     (s->sums[i].sum2, file_sum2,
			      csum_length) != 0)) {
				*checksums_match = 0;
			}
		}		/*  end for  */
	}

}

void write_batch_delta_file(char *buff, int bytes_to_write)
{
	static int fdb_delta_open = 1;

	if (fdb_delta_open) {
		/* Set up file extension */
		strcat(rsync_delta_file, batch_file_ext);

		/* Open batch delta file for writing; create it if it doesn't exist */
		fdb_delta =
		    do_open(rsync_delta_file, O_WRONLY | O_CREAT | O_TRUNC,
			    S_IREAD | S_IWRITE);
		if (fdb_delta == -1) {
			rprintf(FERROR, "Batch file %s open error: %s\n",
				rsync_delta_file, strerror(errno));
			close(fdb_delta);
			exit_cleanup(1);
		}
		fdb_delta_open = 0;
	}

	/* Write buffer to batch delta file */

	if (write(fdb_delta, buff, bytes_to_write) == -1) {
		rprintf(FERROR, "Batch file %s write error: %s\n",
			rsync_delta_file, strerror(errno));
		close(fdb_delta);
		exit_cleanup(1);
	}
}
void close_batch_delta_file()
{
	close(fdb_delta);

}

int read_batch_delta_file(char *buff, int len)
{
	static int fdb_delta_open = 1;
	int bytes_read;

	if (fdb_delta_open) {

		/*  Set up file extension  */
		strcat(rsync_delta_file, batch_file_ext);

		/* Open batch flist file for reading */
		fdb_delta = do_open(rsync_delta_file, O_RDONLY, 0);
		if (fdb_delta == -1) {
			rprintf(FERROR, "Batch file %s open error: %s\n",
				rsync_delta_file, strerror(errno));
			close(fdb_delta);
			exit_cleanup(1);
		}
		fdb_delta_open = 0;
	}

	/* Read delta batch file */

	bytes_read = read(fdb_delta, buff, len);

	if (bytes_read == -1) {
		rprintf(FERROR, "Batch file %s read error: %s\n",
			rsync_delta_file, strerror(errno));
		close(fdb_delta);
		exit_cleanup(1);
	}
	return bytes_read;
}


void show_flist(int index, struct file_struct **fptr)
{
	/*  for debugging    show_flist(flist->count, flist->files * */

	int i;
	for (i = 0; i < index; i++) {
		rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
		rprintf(FINFO, "flist->modtime=%#lx\n",
			(long unsigned) fptr[i]->modtime);
		rprintf(FINFO, "flist->length=%.0f\n",
			(double) fptr[i]->length);
		rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
		rprintf(FINFO, "flist->basename=%s\n", fptr[i]->basename);
		if (fptr[i]->dirname)
			rprintf(FINFO, "flist->dirname=%s\n",
				fptr[i]->dirname);
		if (fptr[i]->basedir)
			rprintf(FINFO, "flist->basedir=%s\n",
				fptr[i]->basedir);
	}
}

void show_argvs(int argc, char *argv[])
{
	/*  for debugging  * */

	int i;
	rprintf(FINFO, "BATCH.C:show_argvs,argc=%d\n", argc);
	for (i = 0; i < argc; i++) {
		/*    if (argv[i])   */
		rprintf(FINFO, "i=%d,argv[i]=%s\n", i, argv[i]);

	}
}