source: MondoRescue/branches/3.3/mondo/src/common/libmondo-stream.c@ 3877

Last change on this file since 3877 was 3877, checked in by Bruno Cornec, 4 months ago

More compiler fixes

  • Property svn:keywords set to Id
File size: 48.5 KB
Line 
1/* libmondo-stream.c
2 $Id: libmondo-stream.c 3877 2024-03-08 02:59:07Z bruno $
3
4...tools for talking to tapes, Monitas streams, etc.
5
6*/
7
8
9/**
10 * @file
11 * Functions for writing data to/reading data from streams (tape, CD stream, etc.)
12 */
13
14#include "my-stuff.h"
15#include "mr_mem.h"
16#include "mr_file.h"
17#include "mondostructures.h"
18
19#include "libmondo-string-EXT.h"
20#include "libmondo-files-EXT.h"
21#include "libmondo-gui-EXT.h"
22#include "libmondo-fork-EXT.h"
23#include "libmondo-tools-EXT.h"
24#include "libmondo-fifo-EXT.h"
25
26#define EXTRA_TAPE_CHECKSUMS
27#define STR_HEADER "Mondolicious, baby"
28
29/*@unused@*/
30//static char cvsid[] = "$Id: libmondo-stream.c 3877 2024-03-08 02:59:07Z bruno $";
31extern bool g_sigpipe;
32extern int g_tape_buffer_size_MB;
33
34extern char *g_getfacl;
35extern char *g_getfattr;
36extern char *MONDO_LOGFILE;
37
38/* Reference to global bkpinfo */
39extern struct s_bkpinfo *bkpinfo;
40
41/**
42 * @addtogroup globalGroup
43 * @{
44 */
45/**
46 * The file pointer for the opened tape/CD stream.
47 * Opened and closed by openin_tape(), openin_cdstream(),
48 * openout_tape(), openout_cdstream(), closein_tape(), closein_cdstream(),
49 * closeout_tape(), and closeout_cdstream().
50 */
51FILE *g_tape_stream = NULL;
52
53
54/**
55 * The position (in kilobytes) where we are on the tape.
56 */
57long long g_tape_posK = 0;
58
59/**
60 * The current media number we're using. This value is 1-based.
61 */
62int g_current_media_number = -1;
63
64/**
65 * The tape catalog that keeps track of files written to tape.
66 */
67struct s_tapecatalog *g_tapecatalog;
68
69/* @} - end of globalGroup */
70
71int write_backcatalog_to_tape();
72
73
74
75
76
77/**
78 * @addtogroup streamGroup
79 * @{
80 */
81/**
82 * Close the global output file descriptor which Mondo has used to read
83 * from the CD stream.
84 * @param bkpinfo The backup information structure. Passed directly to closein_tape().
85 * @return 0 for success, nonzero for failure.
86 * @note This should be called by restore processes only.
87 */
88int closein_cdstream()
89{
90 return (closein_tape());
91}
92
93/**
94 * Write a header block to the opened stream (CD or tape).
95 * @param length_of_incoming_file The length to store in the header block.
96 * Usually matters only if this is a file-related header, in which case it should
97 * be the length of the file that will follow.
98 * @param filename The filename to store in the header block. Usually matters
99 * only if this is a file-related header, in which case this should be the name
100 * if the file that will follow.
101 * @param control_char The type of header block this is (start-file, end-file, start-tape, ...)
102 * @return 0 for success, nonzero for failure.
103 */
104int write_header_block_to_stream(off_t length_of_incoming_file, const char *filename, int control_char)
105{
106 /*@ buffers **************************************************** */
107 char tempblock[TAPE_BLOCK_SIZE];
108 char *p;
109 char *tmp = NULL;
110
111 /*@ int ******************************************************** */
112 int i;
113
114 off_t olen;
115
116 /*@ end vars *************************************************** */
117
118 olen = length_of_incoming_file;
119 p = strrchr(filename, '/'); /* Make 'em go, "Unnnh!" Oh wait, that was _Master_ P... */
120 if (!p) {
121 p = filename;
122 } else {
123 p++;
124 }
125 if (!g_tape_stream) {
126 log_to_screen("You're not backing up to tape. Why write a tape header?");
127 return (1);
128 }
129 for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
130 tempblock[i] = 0;
131 }
132 sprintf(tempblock + 6000 + control_char, STR_HEADER);
133 tempblock[7000] = control_char;
134 if (length_of_incoming_file > 0) {
135 memcpy(tempblock + 7001, (char *) &olen, sizeof(off_t));
136 }
137 strcpy(tempblock + 1000, filename);
138 g_tape_posK += fwrite(tempblock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream) / 1024;
139 tmp = marker_to_string(control_char);
140 log_msg(6, "%s (fname=%s, size=%ld K)", tmp, p, (long) length_of_incoming_file >> 10);
141 mr_free(tmp);
142 return (0);
143}
144
145
146/**
147 * Close the global output file descriptor which Mondo has used to read
148 * from buffer or dd, which read from the tape.
149 * @param bkpinfo The backup information structure. Unused.
150 * @return 0 for success, nonzero for failure.
151 * @note This should be called by restore processes only.
152 * @note This function also works for cdstreams for now, but don't count on this behavior.
153 * @bug @p bkpinfo parameter is unused.
154 */
155int closein_tape()
156{
157 /*@ int's ******************************************************* */
158 int retval = 0;
159 int res = 0;
160 int ctrl_chr = '\0';
161
162 /*@ buffers ***************************************************** */
163 char fname[MAX_STR_LEN];
164
165 /*@ long long's ************************************************* */
166 long long size;
167 char *blk;
168 int i;
169 int j;
170
171 blk = (char *) malloc(256 * 1024);
172
173 log_it("closein_tape() -- entering");
174 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
175 retval += res;
176 if (ctrl_chr != BLK_END_OF_BACKUP) {
177 wrong_marker(BLK_END_OF_BACKUP, ctrl_chr);
178 }
179 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
180 retval += res;
181 if (ctrl_chr != BLK_END_OF_TAPE) {
182 wrong_marker(BLK_END_OF_TAPE, ctrl_chr);
183 }
184 for (i = 0; i < 8 && !feof(g_tape_stream); i++) {
185 j = fread(blk, 1, 256 * 1024, g_tape_stream);
186 if (j) {
187 // FIXME
188 }
189 }
190 sleep(1);
191 sync();
192 sleep(1);
193 paranoid_pclose(g_tape_stream);
194 log_it("closein_tape() -- leaving");
195 if (!bkpinfo->please_dont_eject) {
196 eject_device(bkpinfo->media_device);
197 }
198 paranoid_free(blk);
199 paranoid_free(g_tapecatalog);
200 return (retval);
201}
202
203
204
205/**
206 * Start to write to the next tape. Assume the user has already inserted it.
207 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
208 * @return 0 for success, nonzero for failure.
209 */
210int start_to_write_to_next_tape()
211{
212 int res = 0;
213 char *command = NULL;
214
215 if (bkpinfo->media_device == NULL) {
216 log_it("Unable to open out from NULL device");
217 return (1);
218 }
219
220 paranoid_pclose(g_tape_stream);
221 sync();
222 sync();
223 sync();
224 log_it("New tape requested.");
225 insist_on_this_tape_number(g_current_media_number + 1); // will increment g_current_media, too
226 if (bkpinfo->backup_media_type == cdstream) {
227 mr_asprintf(command, "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s", bkpinfo->media_device, bkpinfo->cdrw_speed, MONDO_LOGFILE, MONDO_LOGFILE);
228 log_it("Opening OUT to next CD with the command");
229 log_it(command);
230 log_it("Let's see what happens, shall we?");
231 g_tape_stream = popen(command, "w");
232 mr_free(command);
233
234 if (!g_tape_stream) {
235 log_to_screen("Failed to openout to cdstream (fifo)");
236 return (1);
237 }
238 } else {
239 log_it("Opening OUT to next tape");
240 if (!(g_tape_stream = open_device_via_buffer(bkpinfo->media_device, 'w', bkpinfo->internal_tape_block_size))) {
241 log_OS_error(g_tape_fifo);
242 log_to_screen("Cannot openin stream device");
243 return (1);
244 }
245 }
246 g_tape_posK = 0;
247 g_sigpipe = FALSE;
248 res += write_header_block_to_stream((off_t)0, "start-of-tape", BLK_START_OF_TAPE); /* just in case */
249 res += write_header_block_to_stream((off_t)0, "start-of-backup", BLK_START_OF_BACKUP); /* just in case */
250 return (res);
251}
252
253
254
255/**
256 * Decide whether we should start a new tape. This is TRUE if we've run out of tape
257 * (got SIGPIPE) or look like we will.
258 * @param mediasize The size of the tape in megabytes.
259 * @param length_of_incoming_file The length of the file we're about to write, in bytes.
260 * @bug This seems like it'll only work for media_size != autodetect, but Mondo only allows
261 * autodetecting the size. Huh?
262 */
263
264/* TODO: Should be reviewed for mediasize being a off_t ??? */
265bool
266should_we_write_to_next_tape(long mediasize,
267 off_t length_of_incoming_file)
268{
269 /*@ bool's ***************************************************** */
270 bool we_need_a_new_tape = FALSE;
271
272 /*@ end vars *************************************************** */
273
274 if (mediasize == 0) {
275 return (FALSE);
276 }
277 if (mediasize > 0 && (g_tape_posK >> 10 >= mediasize)) {
278 log_it("mediasize = %ld", mediasize);
279 we_need_a_new_tape = TRUE;
280 log_to_screen("Should have started a new tape/CD already");
281 }
282 if ((g_tape_posK + length_of_incoming_file / 1024) >> 10 >=
283 mediasize - (SLICE_SIZE * 4 / 1024)) {
284 log_it("g_tape_posK = %ld\nmediasize = %ld\n", g_tape_posK,
285 mediasize);
286 we_need_a_new_tape = TRUE;
287 }
288 return (we_need_a_new_tape);
289}
290
291
292
293
294
295/**
296 * Close the global output file descriptor which Mondo has been using to write
297 * to the tape device (via buffer or dd).
298 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
299 * @return 0 for success, nonzero for failure.
300 * @note This should be called by backup processes only.
301 */
302int closeout_tape()
303{
304 /*@ int's ******************************************************* */
305 int retval = 0;
306// int res = 0;
307// int ctrl_chr = '\0';
308
309 /*@ buffers ***************************************************** */
310// char fname[MAX_STR_LEN];
311
312 /*@ long long's ************************************************* */
313// long long size;
314 int i;
315 char *blk;
316
317 blk = (char *) malloc(256 * 1024);
318
319 sleep(1);
320 sync();
321 sleep(1);
322 log_it("closeout_tape() -- entering");
323 retval +=
324 write_header_block_to_stream((off_t)0, "end-of-backup",
325 BLK_END_OF_BACKUP);
326 retval += write_header_block_to_stream((off_t)0, "end-of-tape", BLK_END_OF_TAPE); /* just in case */
327/* write 1MB of crap */
328 for (i = 0; i < 256 * 1024; i++) {
329 blk[i] = (int) (random() & 0xFF);
330 }
331 for (i = 0; i < 4 * 8; i++) {
332 if (fwrite(blk, 1, 256 * 1024, g_tape_stream)) {
333 //FIXME
334 }
335 if (should_we_write_to_next_tape
336 (bkpinfo->media_size, (off_t)256 * 1024)) {
337 start_to_write_to_next_tape();
338 }
339 }
340/* write 1MB of zeroes */
341/*
342 for (i = 0; i < 256*1024; i++)
343 {
344 blk[i] = 0;
345 }
346 for (i = 0; i < 4; i++)
347 {
348 fwrite (blk, 1, 256*1024, g_tape_stream);
349 if (should_we_write_to_next_tape (bkpinfo->media_size, 256*1024))
350 {
351 start_to_write_to_next_tape ();
352 }
353 }
354*/
355 sleep(2);
356 paranoid_pclose(g_tape_stream);
357 log_it("closeout_tape() -- leaving");
358 for (i = 0; i < g_tapecatalog->entries; i++) {
359 log_it("i=%d type=%s num=%d aux=%ld posK=%lld", i,
360 (g_tapecatalog->el[i].type ==
361 fileset) ? "fileset" : "bigslice",
362 g_tapecatalog->el[i].number, g_tapecatalog->el[i].aux,
363 g_tapecatalog->el[i].tape_posK);
364 }
365 paranoid_free(blk);
366 paranoid_free(g_tapecatalog);
367 return (retval);
368}
369
370
371
372bool mt_says_tape_exists(char *dev)
373{
374 char *command = NULL;
375 int res;
376
377 mr_asprintf(command, "mt -f %s status", dev);
378 res = run_program_and_log_output(command, 1);
379 mr_free(command);
380
381 if (res) {
382 return (FALSE);
383 } else {
384 return (TRUE);
385 }
386}
387
388
389
390/**
391 * Determine the name of the tape device. Tries the SCSI tape for
392 * this platform, then "/dev/st0", then "/dev/ht0" and "/dev/osst0".
393 * @return the value of the tape device or NULL if not found. Allocated dynamically must be freed by caller.
394 */
395char *find_tape_device(void) {
396 char *tmp = NULL;
397 char *dev = NULL;
398 char *command = NULL;
399
400 log_to_screen("I am looking for your tape streamer. Please wait.");
401
402 if (bkpinfo->media_device != NULL) {
403 log_msg(3, "Been there, done that. Returning %s", bkpinfo->media_device);
404 return (bkpinfo->media_device);
405 }
406
407 tmp = find_home_of_exe("lsscsi");
408 if (tmp != NULL) {
409 mr_asprintf(command, "%s | grep ' tape' | awk '{print $NF}' | head -1", tmp);
410 dev = call_program_and_get_last_line_of_output(command);
411 mr_free(command);
412 }
413 mr_free(tmp);
414
415 if ((dev != NULL) && does_file_exist(dev)) {
416 if (mt_says_tape_exists(dev)) {
417 log_it("find_tape_device found %s automatically", dev);
418 }
419 } else {
420 /* trying something else then */
421 mr_asprintf(dev, "%s", VANILLA_SCSI_TAPE);
422 if (!mt_says_tape_exists(dev)) {
423 mr_asprintf(dev, "%s", ALT_TAPE);
424 if (!mt_says_tape_exists(dev)) {
425 mr_asprintf(dev, "%s", "/dev/osst0");
426 if (!mt_says_tape_exists(dev)) {
427 log_it("Unable to find a tape device on this system");
428 }
429 }
430 }
431 if (dev != NULL) {
432 log_it("find_tape_device found %s manually", dev);
433 }
434 }
435
436 if ((dev != NULL) && does_file_exist(dev)) {
437 log_it("find_tape_device returns %s", dev);
438 } else {
439 mr_free(dev);
440 log_it("find_tape_device found no tape on your system returning NULL");
441 }
442 return (dev);
443}
444
445
446/**
447 * Copy a file from the opened stream (CD or tape) to @p outfile.
448 * @param outfile The file to write to.
449 * @param size The size of the file in the input stream.
450 * @return 0 for success, nonzero for failure.
451 */
452int
453read_file_from_stream_to_file(char *outfile, long long size)
454{
455
456 /*@ int ******************************************************** */
457 int res;
458
459 /*@ end vars *************************************************** */
460
461 res = read_file_from_stream_FULL(outfile, NULL, size);
462
463 return (res);
464}
465
466
467
468
469int read_EXAT_files_from_tape(long long *ptmp_size, char *tmp_fname,
470 int *pctrl_chr, char *xattr_fname,
471 char *acl_fname)
472{
473 int res = 0;
474 char fname_buf[PATH_MAX];
475 char *fname = (char *)fname_buf; /* Should NOT be NULL */
476 char *tmp = NULL;
477
478 // xattr
479 if (g_getfattr) {
480 res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
481 if (*pctrl_chr != BLK_START_EXAT_FILE) {
482 wrong_marker(BLK_START_EXAT_FILE, *pctrl_chr);
483 }
484 if (strstr(fname, "xattr") == NULL) {
485 mr_asprintf(tmp,"Wrong order expected xattr, got %s, sunshine.", fname);
486 fatal_error(tmp);
487 }
488 read_file_from_stream_to_file(xattr_fname, *ptmp_size);
489 res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
490 if (*pctrl_chr != BLK_STOP_EXAT_FILE) {
491 wrong_marker(BLK_STOP_EXAT_FILE, *pctrl_chr);
492 }
493 log_msg(1, "Got xattr");
494 res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
495 if (*pctrl_chr != BLK_STOP_EXTENDED_ATTRIBUTES) {
496 wrong_marker(BLK_STOP_EXTENDED_ATTRIBUTES, *pctrl_chr);
497 }
498 res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
499 if (*pctrl_chr == BLK_START_AN_AFIO_OR_SLICE) {
500 log_msg(1, "No acl attributes found, skipping to afio files");
501 return(0);
502 } else {
503 if (*pctrl_chr != BLK_START_EXTENDED_ATTRIBUTES) {
504 wrong_marker(BLK_START_EXTENDED_ATTRIBUTES, *pctrl_chr);
505 }
506 }
507 }
508 // acl
509 if (g_getfacl) {
510 res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
511 if (*pctrl_chr != BLK_START_EXAT_FILE) {
512 wrong_marker(BLK_START_EXAT_FILE, *pctrl_chr);
513 }
514 if (strstr(fname, "acl") == NULL) {
515 mr_asprintf(tmp,"Wrong order expected acl, got %s, sunshine.", fname);
516 fatal_error(tmp);
517 }
518 read_file_from_stream_to_file(acl_fname, *ptmp_size);
519 res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
520 if (*pctrl_chr != BLK_STOP_EXAT_FILE) {
521 wrong_marker(BLK_STOP_EXAT_FILE, *pctrl_chr);
522 }
523 res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
524 if (*pctrl_chr != BLK_STOP_EXTENDED_ATTRIBUTES) {
525 wrong_marker(BLK_STOP_EXTENDED_ATTRIBUTES, *pctrl_chr);
526 }
527 log_msg(1, "Got acl");
528 }
529 // tarball itself
530 res = read_header_block_from_stream(ptmp_size, tmp_fname, pctrl_chr);
531 log_msg(1, "End of extended attributes, now looking for afioball");
532 return (res);
533}
534
535
536/**
537 * Copy @p infile to the opened stream (CD or tape).
538 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
539 * @param infile The file to write to the stream.
540 * @return 0 for success, nonzero for failure.
541 */
542int write_file_to_stream_from_file(char *infile)
543{
544 /*@ buffers **************************************************** */
545 char datablock[TAPE_BLOCK_SIZE];
546 char *checksum = NULL;
547 char *infile_basename;
548
549 /*@ int ******************************************************** */
550 int retval = 0;
551 int noof_blocks;
552
553 /* unsigned int ch; */
554 unsigned int crc16;
555 unsigned int crctt;
556
557 /*@ pointers *************************************************** */
558 FILE *fin;
559 char *p;
560
561 /*@ long ******************************************************* */
562 long bytes_to_read = 0;
563 long i;
564
565 off_t filesize;
566
567#ifdef EXTRA_TAPE_CHECKSUMS
568 int ch;
569#endif
570
571 /*@ initialize ************************************************ */
572 crc16 = 0;
573 crctt = 0;
574
575
576
577 /*@ end vars *************************************************** */
578
579 infile_basename = strrchr(infile, '/');
580 if (infile_basename) {
581 infile_basename++;
582 } else {
583 infile_basename = infile;
584 }
585 filesize = length_of_file(infile);
586 if (should_we_write_to_next_tape(bkpinfo->media_size, filesize)) {
587 start_to_write_to_next_tape();
588 write_backcatalog_to_tape();
589 }
590 p = strrchr(infile, '/');
591 if (!p) {
592 p = infile;
593 } else {
594 p++;
595 }
596 log_it("Writing file '%s' to tape (%ld KB)", p, (long) filesize >> 10);
597 write_header_block_to_stream(filesize, infile_basename, BLK_START_FILE);
598//go_here_to_restart_saving_of_file:
599 if (!(fin = fopen(infile, "r"))) {
600 log_OS_error(infile);
601 return (1);
602 }
603 for (noof_blocks = 0; filesize > 0;
604 noof_blocks++, filesize -= bytes_to_read) {
605 if (filesize < TAPE_BLOCK_SIZE) {
606 bytes_to_read = (long) filesize;
607 for (i = 0; i < TAPE_BLOCK_SIZE; i++) {
608 datablock[i] = '\0';
609 }
610 } else {
611 bytes_to_read = TAPE_BLOCK_SIZE;
612 }
613 if (fread(datablock, 1, (size_t) bytes_to_read, fin)) {
614 // FIXME
615 }
616 g_tape_posK +=
617 fwrite(datablock, 1, /*bytes_to_read */
618 (size_t) TAPE_BLOCK_SIZE,
619 g_tape_stream) / 1024;
620 if (g_sigpipe) {
621 log_it("Sigpipe occurred recently. I'll start a new tape.");
622 fclose(fin);
623 g_sigpipe = FALSE;
624 start_to_write_to_next_tape();
625 write_backcatalog_to_tape(); // kinda-sorta recursive :)
626 return (0);
627 }
628#ifdef EXTRA_TAPE_CHECKSUMS
629 for (i = 0; i < bytes_to_read; i++) {
630 ch = datablock[i];
631 crc16 = updcrcr(crc16, (unsigned) ch);
632 crctt = updcrc(crctt, (unsigned) ch);
633 }
634#endif
635 }
636 paranoid_fclose(fin);
637 mr_asprintf(checksum, "%04x%04x", crc16, crctt);
638 /* TODO: what does it do ??? */
639 write_header_block_to_stream((off_t)g_current_media_number, checksum, BLK_STOP_FILE);
640 mr_free(checksum);
641
642// log_it("File '%s' written to tape.", infile);
643 return (retval);
644}
645
646
647
648
649
650
651
652
653int write_EXAT_files_to_tape(char *xattr_fname,
654 char *acl_fname)
655{
656 int res = 0;
657 if (g_getfattr) {
658 // xattr
659 write_header_block_to_stream(length_of_file(xattr_fname), xattr_fname, BLK_START_EXTENDED_ATTRIBUTES);
660 write_header_block_to_stream(length_of_file(xattr_fname), xattr_fname, BLK_START_EXAT_FILE);
661 write_file_to_stream_from_file(xattr_fname);
662 write_header_block_to_stream((off_t)-1, xattr_fname, BLK_STOP_EXAT_FILE);
663 write_header_block_to_stream(length_of_file(xattr_fname), xattr_fname, BLK_STOP_EXTENDED_ATTRIBUTES);
664 }
665 if (g_getfacl) {
666 // acl
667 write_header_block_to_stream(length_of_file(acl_fname), acl_fname, BLK_START_EXTENDED_ATTRIBUTES);
668 write_header_block_to_stream(length_of_file(acl_fname), acl_fname, BLK_START_EXAT_FILE);
669 write_file_to_stream_from_file(acl_fname);
670 write_header_block_to_stream((off_t)-1, acl_fname, BLK_STOP_EXAT_FILE);
671 write_header_block_to_stream(length_of_file(acl_fname), acl_fname, BLK_STOP_EXTENDED_ATTRIBUTES);
672 }
673 return (res);
674}
675
676
677
678
679/**
680 * Tell the user to insert tape @p tapeno and wait for it to settle (5 seconds).
681 * @param tapeno The tape number to insist on.
682 * @bug There is currently no way to actually verify that the user has actually
683 * inserted the right tape.
684 */
685void insist_on_this_tape_number(int tapeno)
686{
687 int i;
688 char *tmp = NULL;
689
690 log_it("Insisting on tape #%d", tapeno);
691 if (g_current_media_number != tapeno) {
692 mr_asprintf(tmp, "When the tape drive goes quiet, please insert volume %d in this series.", tapeno);
693 popup_and_OK(tmp);
694 mr_free(tmp);
695 open_evalcall_form("Waiting while the tape drive settles");
696 } else {
697 open_evalcall_form("Waiting while the tape drive rewinds");
698 }
699
700 for (i = 0; i <= 100; i += 2) {
701 usleep(100000);
702 update_evalcall_form(i);
703 }
704 close_evalcall_form();
705 log_it("I assume user has inserted it. They _say_ they have...");
706 g_current_media_number = tapeno;
707
708 // log_it("g_current_media_number = %d", g_current_media_number);
709 log_it("OK, I've finished insisting. On with the revelry.");
710}
711
712
713
714
715/**
716 * Debugging aid - log the offset we're at on the tape (reading or writing).
717 */
718void log_tape_pos(void)
719{
720 /*@ buffers ***************************************************** */
721
722
723 /*@ end vars *************************************************** */
724
725 log_it("Tape position -- %ld KB (%ld MB)", (long) g_tape_posK,
726 (long) g_tape_posK >> 10);
727}
728
729
730
731
732/**
733 * Add a file to a collection of recently archived filesets/slices.
734 * The type is determined by the filename: if it contains ".afio." it is
735 * assumed to be a fileset, otherwise if it contains "slice" it's a slice,
736 * otherwise we generate a fatal_error().
737 * @param td The current @c bkpinfo->tempdir (file will be placed in <tt>td</tt>/tmpfs/backcatalog/).
738 * @param latest_fname The file to place in the collection.
739 * @return 0, always.
740 * @bug Return value is redundant.
741 * @bug The detection won't work for uncompressed afioballs (they end in ".afio", no dot afterwards). // Not true. They end in '.' -Hugo
742 */
743int maintain_collection_of_recent_archives(char *td, char *latest_fname)
744{
745 long long final_alleged_writeK, final_projected_certain_writeK,
746 cposK, bufsize_K;
747 int last, curr, i;
748 char *command = NULL;
749 char *tmpdir = NULL;
750 char *old_fname = NULL;
751 char *p;
752 char suffix[16];
753
754 bufsize_K = (long long) (1024LL * (1 + g_tape_buffer_size_MB));
755 if ((p = strrchr(latest_fname, '.'))) {
756 strcpy(suffix, ++p);
757 } else {
758 suffix[0] = '\0';
759 }
760 mr_asprintf(tmpdir, "%s/tmpfs/backcatalog", td);
761 mkdir(tmpdir, 0x700);
762 mr_asprintf(command, "cp -f %s %s", latest_fname, tmpdir);
763 if (run_program_and_log_output(command, 6)) {
764 log_it("Warning - failed to copy %s to backcatalog at %s",
765 latest_fname, tmpdir);
766 }
767 mr_free(command);
768
769 last = g_tapecatalog->entries - 1;
770 if (last <= 0) {
771 log_it("Too early to start deleting from collection.");
772 mr_free(tmpdir);
773 return (0);
774 }
775 final_alleged_writeK = g_tapecatalog->el[last].tape_posK;
776 final_projected_certain_writeK = final_alleged_writeK - bufsize_K;
777 for (curr = last; curr >= 0; curr--) {
778 cposK = g_tapecatalog->el[curr].tape_posK;
779 if (cposK < final_projected_certain_writeK) {
780 break;
781 }
782 }
783 if (curr < 0) {
784 log_it("Not far enough into tape to start deleting old archives from collection.");
785 mr_free(tmpdir);
786 return (0);
787 }
788
789 for (i = curr - 1; i >= 0 && curr - i < 10; i--) {
790 mr_asprintf(old_fname, "%s/%s", tmpdir, g_tapecatalog->el[i].fname);
791 unlink(old_fname);
792 mr_free(old_fname);
793 }
794 mr_free(tmpdir);
795 return (0);
796}
797
798
799
800/**
801 * Open the tape device for input.
802 * @param bkpinfo The backup information structure. Fields used:
803 * - @c bkpinfo->media_device
804 * - @c bkpinfo->tmpdir
805 * @return 0 for success, nonzero for failure.
806 * @note This will also work with a cdstream for now, but don't count on this behavior.
807 */
808int openin_tape()
809{
810 /*@ buffer ***************************************************** */
811 char fname[MAX_STR_LEN];
812 char *datablock;
813 char *tmp = NULL;
814 char *old_pwd = NULL;
815 char *outfname = NULL;
816 /*@ int ******************************************************* */
817 int i;
818 int j;
819 int res = 0;
820 long length, templong;
821 size_t k;
822 int retval = 0;
823 int ctrl_chr;
824
825 /*@ long long ************************************************* */
826 long long size;
827
828 /*@ pointers ************************************************** */
829 FILE *fout;
830
831 /*@ end vars *************************************************** */
832
833 g_tapecatalog = mr_malloc(sizeof(struct s_tapecatalog));
834 g_tapecatalog->entries = 0;
835 g_tape_posK = 0;
836 if (g_tape_stream) {
837 log_it("FYI - I won't 'openin' the tape. It's already open.");
838 return (0);
839 }
840
841 // mondoarchive should have configured everything to give the right non-rew device
842 if ((bkpinfo->use_obdr) && (bkpinfo->media_device != NULL)) {
843 res = skip_obdr();
844 if (res != 0) {
845 log_it("Not able to skip OBDR - Restore will have to be done manually");
846 }
847 } else {
848 if (bkpinfo->media_device == NULL) {
849 log_it("Not able to skip OBDR - Restore will have to be done manually");
850 return (1);
851 }
852 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
853 }
854
855 insist_on_this_tape_number(1);
856 mr_asprintf(outfname, "%s/tmp/all.tar.gz", bkpinfo->tmpdir);
857 make_hole_for_file(outfname);
858
859 log_it("Opening IN tape");
860 if (!(g_tape_stream = open_device_via_buffer(bkpinfo->media_device, 'r', bkpinfo->internal_tape_block_size))) {
861 log_OS_error(g_tape_fifo);
862 log_to_screen("Cannot openin stream device");
863 mr_free(outfname);
864 return (1);
865 }
866 log_to_screen("Reading stream");
867 log_it("stream device = '%s'", bkpinfo->media_device);
868 /* skip data disks */
869 open_evalcall_form("Skipping data disks on stream");
870 log_to_screen("Skipping data disks on stream");
871 if (!(fout = fopen(outfname, "w"))) {
872 log_OS_error(outfname);
873 log_to_screen("Cannot openout datadisk all.tar.gz file");
874 mr_free(outfname);
875 return (-1);
876 }
877 if (!(datablock = (char *) malloc(256 * 1024))) {
878 log_to_screen("Unable to malloc 256*1024");
879 mr_free(outfname);
880 finish(1);
881 }
882 for (i = 0; i < 32; i++) {
883 for (j = 0; j < 4; j++) {
884 for (length = 0, k = 0; length < 256 * 1024; length += k) {
885 k = fread(datablock + length, 1, 256 * 1024 - length,
886 g_tape_stream);
887 }
888 if (fwrite(datablock, 1, (size_t) length, fout)) {
889 // FIXME
890 }
891 g_tape_posK += length / 1024;
892 }
893 if (i > 8) // otherwise, 'buffer' distorts calculations
894 {
895 templong = ((i - 8) * 4 + j) * 100 / (128 - 8 * 4);
896 update_evalcall_form((int) (templong));
897 }
898 }
899 paranoid_fclose(fout);
900 paranoid_free(datablock);
901
902/* find initial blocks */
903 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
904 retval += res;
905 if (ctrl_chr != BLK_START_OF_TAPE) {
906 wrong_marker(BLK_START_OF_TAPE, ctrl_chr);
907 }
908 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
909 retval += res;
910 if (ctrl_chr != BLK_START_OF_BACKUP) {
911 wrong_marker(BLK_START_OF_BACKUP, ctrl_chr);
912 }
913 close_evalcall_form();
914 log_it("Saved all.tar.gz to '%s'", outfname);
915 old_pwd = mr_getcwd();
916 if (chdir(bkpinfo->tmpdir)) {
917 // FIXME
918 }
919 mr_asprintf(tmp, "tar -zxf %s ./tmp/mondorestore.cfg 2> /dev/null", outfname);
920 paranoid_system(tmp);
921 mr_free(tmp);
922
923 paranoid_system("cp -f tmp/mondorestore.cfg . 2> /dev/null");
924 if (chdir(old_pwd)) {
925 // FIXME
926 }
927 mr_free(old_pwd);
928 unlink(outfname);
929 mr_free(outfname);
930 return (retval);
931}
932
933
934
935/**
936 * Open the CD stream for input.
937 * @param bkpinfo The backup information structure. Passed to openin_tape().
938 * @return 0 for success, nonzero for failure.
939 * @note Equivalent to openin_tape() for now, but don't count on this behavior.
940 */
941int openin_cdstream()
942{
943 return (openin_tape());
944}
945
946/**
947 * FIFO used to read/write to the tape device.
948 * @bug This seems obsolete now that we call an external @c buffer program. Please look onto this.
949 */
950char g_tape_fifo[MAX_STR_LEN];
951
952
953
954int set_tape_block_size_with_mt(long internal_tape_block_size)
955{
956 char *tmp = NULL;
957 int res;
958
959 if (bkpinfo->media_device == NULL) {
960 return(1);
961 }
962
963 if (strncmp(bkpinfo->media_device, "/dev/", 5)) {
964 log_msg(1, "Not using 'mt setblk'. This isn't an actual /dev entry.");
965 return (0);
966 }
967 mr_asprintf(tmp, "mt -f %s setblk %ld", bkpinfo->media_device, internal_tape_block_size);
968 res = run_program_and_log_output(tmp, 3);
969 mr_free(tmp);
970 return (res);
971}
972
973/**
974 * Return the non-rewinding device when passed the normal one
975 * @param tapedev The tape device to open for writing.
976 * @note the caller needs to free the string returned
977 */
978char *get_non_rewind_dev(char *tapedev)
979{
980
981 char *ntapedev = NULL;
982 char *p = NULL;
983 char *q = NULL;
984 char *r = NULL;
985
986 ntapedev = (char *)mr_malloc(strlen(tapedev)+2*sizeof(char));
987 p = strrchr(tapedev,'/');
988 if (p == NULL) {
989 log_it("Didn't find a '/' in %s",tapedev);
990 return(NULL);
991 }
992
993 /* Copy tapedev content up to the last / */
994 q = tapedev;
995 r = ntapedev;
996 while (q != p) {
997 *r = *q;
998 r++;
999 q++;
1000 }
1001 /* Copy the '/' */
1002 *r = *q;
1003 r++;
1004 q++;
1005 /* Adds a 'n' - non-rewinding */
1006 *r = 'n';
1007 r++;
1008 /* Copy the rest of tapedev */
1009 while (*q != '\0') {
1010 *r = *q;
1011 r++;
1012 q++;
1013 }
1014 *r = '\0';
1015 if (mt_says_tape_exists(ntapedev)) {
1016 log_it("Non-rewinding tape device is %s",ntapedev);
1017 } else {
1018 log_it("Unable to find non-rewinding tape device.");
1019 ntapedev = NULL;
1020 }
1021 return(ntapedev);
1022}
1023
1024
1025
1026/**
1027 * Handle OBDR if we were asked to do so
1028 * @param tapedev The tape device to open for reading.
1029 */
1030int skip_obdr(void)
1031{
1032 char *command = NULL;
1033 int res = 0;
1034
1035 if (bkpinfo->media_device == NULL) {
1036 return(1);
1037 }
1038
1039 log_it("Skipping OBDR headers");
1040 mr_asprintf(command, "mt -f %s rewind",bkpinfo->media_device);
1041 res = run_program_and_log_output(command, 1);
1042 paranoid_free(command);
1043
1044 mr_asprintf(command, "mt -f %s fsf 2",bkpinfo->media_device);
1045 res = run_program_and_log_output(command, 1);
1046 paranoid_free(command);
1047
1048 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
1049 return(res);
1050}
1051
1052/**
1053 * Handle OBDR if we were asked to do so
1054 * @param tapedev The tape device to open for writing.
1055 * @return 0 for success, nonzero for failure.
1056 * @note This should be called ONLY from backup processes. It will OVERWRITE ANY
1057 * EXISTING DATA on the tape!
1058 */
1059int create_obdr(void)
1060{
1061
1062 char *command = NULL;
1063 int res = 0;
1064
1065 if (bkpinfo->media_device == NULL) {
1066 return(1);
1067 }
1068
1069 log_it("Creating OBDR headers");
1070 /* OBDR: First block 10 kB of zero bs = 512 */
1071 mr_asprintf(command, "mt -f %s compression off",bkpinfo->media_device);
1072 res = run_program_and_log_output(command, 1);
1073 paranoid_free(command);
1074
1075 mr_asprintf(command, "mt -f %s rewind",bkpinfo->media_device);
1076 res += run_program_and_log_output(command, 1);
1077 paranoid_free(command);
1078
1079 set_tape_block_size_with_mt(512);
1080
1081 mr_asprintf(command, "dd if=/dev/zero of=%s bs=512 count=20",bkpinfo->media_device);
1082 res += run_program_and_log_output(command, 1);
1083 paranoid_free(command);
1084
1085 /* OBDR: then ISO boot image bs = 2048 */
1086 set_tape_block_size_with_mt(2048);
1087
1088 mr_asprintf(command, "dd if=%s of=%s bs=2048",MINDI_CACHE"/mindi.iso",bkpinfo->media_device);
1089 res += run_program_and_log_output(command, 1);
1090 paranoid_free(command);
1091
1092 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
1093
1094 /* restore compression mode on */
1095 mr_asprintf(command, "mt -f %s compression on",bkpinfo->media_device);
1096 res = run_program_and_log_output(command, 1);
1097 paranoid_free(command);
1098
1099 return(res);
1100}
1101
1102
1103/**
1104 * Start writing to a CD stream.
1105 * @param cddev The CD device to openout via cdrecord.
1106 * @param speed The speed to write at.
1107 * @return 0 for success, nonzero for failure.
1108 * @note This should be called only from backup processes.
1109 */
1110int openout_cdstream(char *cddev, int speed)
1111{
1112 /*@ buffers ***************************************************** */
1113 char *command = NULL;
1114
1115 /*@ end vars *************************************************** */
1116
1117 if (cddev == NULL) {
1118 log_to_screen("Failed to openout NULL cddev");
1119 return (1);
1120 }
1121 /* add 'dummy' if testing */
1122 mr_asprintf(command, "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s", cddev, speed, MONDO_LOGFILE, MONDO_LOGFILE);
1123 /* initialise the catalog */
1124 g_current_media_number = 1;
1125 if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
1126 fatal_error("Cannot alloc mem for tape catalog");
1127 }
1128 g_tapecatalog->entries = 0;
1129 /* log stuff */
1130 log_it("Opening OUT cdstream with the command");
1131 log_it(command);
1132 /* log_it("Let's see what happens, shall we?"); */
1133 g_tape_stream = popen(command, "w");
1134 mr_free(command);
1135
1136 if (g_tape_stream) {
1137 return (0);
1138 } else {
1139 log_to_screen("Failed to openout to cdstream (fifo)");
1140 return (1);
1141 }
1142}
1143
1144
1145/**
1146 * Start writing to a tape device for the backup.
1147 * Handle OBDR if we were asked to do so
1148 * @param tapedev The tape device to open for writing.
1149 * @return 0 for success, nonzero for failure.
1150 * @note This should be called ONLY from backup processes. It will OVERWRITE ANY
1151 * EXISTING DATA on the tape!
1152 */
1153int openout_tape() {
1154
1155g_current_media_number = 1;
1156if (g_tape_stream) {
1157 log_it("FYI - I won't 'openout' the tape. It's already open.");
1158 return (0);
1159}
1160if (bkpinfo->media_device == NULL) {
1161 log_it("Unable to openout NULL device");
1162 return(1);
1163}
1164g_tapecatalog = mr_malloc(sizeof(struct s_tapecatalog));
1165g_tapecatalog->entries = 0;
1166g_tape_posK = 0;
1167
1168if (bkpinfo->use_obdr) {
1169 create_obdr();
1170} else {
1171 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
1172}
1173log_it("Opening OUT tape");
1174if (!(g_tape_stream = open_device_via_buffer(bkpinfo->media_device, 'w', bkpinfo->internal_tape_block_size))) {
1175 log_OS_error(g_tape_fifo);
1176 log_to_screen("Cannot openin stream device");
1177 return (1);
1178}
1179return (0);
1180}
1181
1182
1183
1184/**
1185 * Copy a file from the currently opened stream (CD or tape) to either a
1186 * @c FILE pointer indicating a currently-opened file, or a filename indicating
1187 * a new file to create. This is the backbone function that read_file_from_stream_to_file()
1188 * and read_file_from_stream_to_stream() are wrappers for.
1189 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
1190 * @param outfname If non-NULL, write to this file.
1191 * @param foutstream If non-NULL, write to this stream.
1192 * @param orig_size The original length of the file in bytes.
1193 * @return 0 for success, nonzero for failure.
1194 * @note Only @b one of @p outfname or @p foutstream may be non-NULL.
1195 */
1196int
1197read_file_from_stream_FULL(char *outfname, FILE * foutstream, long long orig_size)
1198{
1199 /*@ buffers ***************************************************** */
1200 char *datablock;
1201 char *temp_fname;
1202 char *temp_cksum;
1203 char *actual_cksum = NULL;
1204
1205 /*@ int ********************************************************* */
1206 int retval = 0;
1207#ifdef EXTRA_TAPE_CHECKSUMS
1208 int i, ch;
1209#endif
1210 int noof_blocks;
1211 int ctrl_chr;
1212 int res;
1213 /*@ pointers **************************************************** */
1214 FILE *fout;
1215
1216 /*@ long ***************************************************** */
1217 long bytes_to_write = 0L /*,i */ ;
1218// long bytes_successfully_read_in_this_time = 0;
1219
1220 /*@ long long *************************************************** */
1221 long long temp_size, size;
1222 long bytes_read, bytes_to_read;
1223 long long total_read_from_tape_for_this_file = 0;
1224 long long where_I_was_before_tape_change = 0;
1225 /*@ unsigned int ************************************************ */
1226 /* unsigned int ch; */
1227 unsigned int crc16;
1228 unsigned int crctt;
1229
1230 /*@ init ******************************************************* */
1231 malloc_string(temp_fname);
1232 malloc_string(temp_cksum);
1233 datablock = malloc(TAPE_BLOCK_SIZE);
1234 crc16 = 0;
1235 crctt = 0;
1236 size = orig_size;
1237
1238 /*@ end vars *************************************************** */
1239
1240 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1241 if (res) {
1242 // FIXME
1243 }
1244 if (orig_size != temp_size && orig_size != -1) {
1245 log_to_screen("output file's size should be %ld K but is apparently %ld K", (long) size >> 10, (long) temp_size >> 10);
1246 }
1247 if (ctrl_chr != BLK_START_FILE) {
1248 wrong_marker(BLK_START_FILE, ctrl_chr);
1249 return (1);
1250 }
1251
1252 if (foutstream) {
1253 fout = foutstream;
1254 } else {
1255 fout = fopen(outfname, "w");
1256 }
1257 if (!fout) {
1258 log_OS_error(outfname);
1259 log_to_screen("Cannot openout file");
1260 return (1);
1261 }
1262 total_read_from_tape_for_this_file = 0;
1263 for (noof_blocks = 0; size > 0;
1264 noof_blocks++, size -=
1265 bytes_to_write, total_read_from_tape_for_this_file +=
1266 bytes_read) {
1267 bytes_to_write =
1268 (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
1269 bytes_to_read = TAPE_BLOCK_SIZE;
1270 bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
1271 while (bytes_read < bytes_to_read) { // next tape!
1272// crctt=crc16=0;
1273 where_I_was_before_tape_change = size;
1274 log_msg(4, "where_I_was_... = %lld",
1275 where_I_was_before_tape_change);
1276 start_to_read_from_next_tape();
1277 log_msg(4, "Started reading from next tape.");
1278 skip_incoming_files_until_we_find_this_one(temp_fname);
1279 log_msg(4, "Skipped irrelevant files OK.");
1280 for (size = orig_size; size > where_I_was_before_tape_change;
1281 size -= bytes_to_write) {
1282 bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
1283 }
1284 log_msg(4, "'size' is now %lld (should be %lld)", size,
1285 where_I_was_before_tape_change);
1286 log_to_screen("Successfully re-sync'd tape");
1287 bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
1288 }
1289
1290 if (fwrite(datablock, 1, (size_t) bytes_to_write, fout)) { // for blocking reasons, bytes_successfully_read_in isn't necessarily the same as bytes_to_write
1291 // FIXME
1292 }
1293
1294#ifdef EXTRA_TAPE_CHECKSUMS
1295 for (i = 0; i < (int) bytes_to_write; i++) {
1296 ch = datablock[i];
1297 crc16 = updcrcr(crc16, (unsigned) ch);
1298 crctt = updcrc(crctt, (unsigned) ch);
1299 }
1300#endif
1301 }
1302 log_msg(6, "Total read from tape for this file = %lld",
1303 total_read_from_tape_for_this_file);
1304 log_msg(6, ".......................... Should be %lld", orig_size);
1305 g_tape_posK += total_read_from_tape_for_this_file / 1024;
1306 mr_asprintf(actual_cksum, "%04x%04x", crc16, crctt);
1307 if (foutstream) { /*log_it("Finished writing to foutstream"); */
1308 } else {
1309 paranoid_fclose(fout);
1310 }
1311 res = read_header_block_from_stream(&temp_size, temp_cksum, &ctrl_chr);
1312 if (ctrl_chr != BLK_STOP_FILE) {
1313 wrong_marker(BLK_STOP_FILE, ctrl_chr);
1314 }
1315 if (strcmp(temp_cksum, actual_cksum)) {
1316 log_to_screen("actual cksum=%s; recorded cksum=%s", actual_cksum, temp_cksum);
1317 log_to_screen("%s (%ld K) is corrupt on tape", temp_fname, (long) orig_size >> 10);
1318 retval++;
1319 }
1320 mr_free(actual_cksum);
1321
1322 paranoid_free(datablock);
1323 paranoid_free(temp_fname);
1324 paranoid_free(temp_cksum);
1325 return (retval);
1326}
1327
1328
1329
1330/**
1331 * Copy a file from the currently opened stream (CD or tape) to the stream indicated
1332 * by @p fout.
1333 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
1334 * @param fout The stream to write the file to.
1335 * @param size The size of the file in bytes.
1336 * @return 0 for success, nonzero for failure.
1337 */
1338int
1339read_file_from_stream_to_stream(FILE * fout, long long size)
1340{
1341
1342 /*@ int ******************************************************** */
1343 int res;
1344
1345 /*@ end vars *************************************************** */
1346
1347 res = read_file_from_stream_FULL(NULL, fout, size);
1348/* fflush(g_tape_stream);
1349 fflush(fout);*/
1350 return (res);
1351}
1352
1353
1354/**
1355 * Seek through the stream until we find a header block where the NAME field matches
1356 * @p the_file_I_was_reading. This is useful if you've just started reading from
1357 * a new tape and want to find the file you were reading when the tape ended.
1358 * @param the_file_I_was_reading File name to look for.
1359 * @return 0 for success, nonzero for failure.
1360 */
1361int skip_incoming_files_until_we_find_this_one(char
1362 *the_file_I_was_reading)
1363{
1364 char *pA;
1365 char *pB;
1366 int res;
1367 int ctrl_chr;
1368 char *temp_fname;
1369 char *datablock;
1370 long long temp_size, size;
1371 long bytes_to_write;
1372
1373 datablock = malloc(TAPE_BLOCK_SIZE);
1374 malloc_string(temp_fname);
1375 pB = strrchr(the_file_I_was_reading, '/');
1376 if (pB) {
1377 pB++;
1378 } else {
1379 pB = the_file_I_was_reading;
1380 }
1381 log_msg(1, "skip_incoming_..(%s)", pB);
1382 log_msg(2, "Looking for initial START_AN_AFIO_OR_SLICE");
1383 ctrl_chr = -1;
1384 while (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1385 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1386 if (res) {
1387 // FIXME
1388 }
1389 if (ctrl_chr == BLK_START_AN_AFIO_OR_SLICE) {
1390 break;
1391 }
1392 log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
1393 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1394 log_msg(3, "Still trying to re-sync w/ tape");
1395 }
1396 while (ctrl_chr != BLK_START_FILE) {
1397 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1398 if (res) {
1399 //FIXME
1400 }
1401 if (ctrl_chr == BLK_START_FILE) {
1402 break;
1403 }
1404 log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
1405 wrong_marker(BLK_START_FILE, ctrl_chr);
1406 log_msg(3, "Still trying to re-sync w/ tape");
1407 }
1408 pA = strrchr(temp_fname, '/');
1409 if (pA) {
1410 pA++;
1411 } else {
1412 pA = temp_fname;
1413 }
1414 pB = strrchr(the_file_I_was_reading, '/');
1415 if (pB) {
1416 pB++;
1417 } else {
1418 pB = the_file_I_was_reading;
1419 }
1420 while (strcmp(pA, pB)) {
1421 log_msg(6, "Skipping %s (it's not %s)", temp_fname, the_file_I_was_reading);
1422 for (size = temp_size; size > 0; size -= bytes_to_write) {
1423 bytes_to_write =
1424 (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
1425 if (fread(datablock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream)) {
1426 // FIXME - needs error-checking and -catching
1427 }
1428 }
1429 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1430 if (ctrl_chr != BLK_STOP_FILE) {
1431 wrong_marker(BLK_STOP_FILE, ctrl_chr);
1432 }
1433 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1434 if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
1435 wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
1436 }
1437 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1438 if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1439 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1440 }
1441 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1442 if (ctrl_chr != BLK_START_FILE) {
1443 wrong_marker(BLK_START_FILE, ctrl_chr);
1444 }
1445 pA = strrchr(temp_fname, '/');
1446 if (pA) {
1447 pA++;
1448 } else {
1449 pA = temp_fname;
1450 }
1451 pB = strrchr(the_file_I_was_reading, '/');
1452 if (pB) {
1453 pB++;
1454 } else {
1455 pB = the_file_I_was_reading;
1456 }
1457 }
1458 log_msg(2, "Reading %s (it matches %s)", temp_fname, the_file_I_was_reading);
1459 paranoid_free(temp_fname);
1460 paranoid_free(datablock);
1461 return (0);
1462}
1463
1464
1465/**
1466 * Start to read from the next tape. Assumes the user has already inserted it.
1467 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
1468 * @return 0 for success, nonzero for failure.
1469 */
1470int start_to_read_from_next_tape()
1471{
1472 /*@ int ********************************************************* */
1473 int res = 0;
1474 char *sz_msg;
1475 int ctrlchr;
1476 long long temp_size;
1477 malloc_string(sz_msg);
1478 /*@ end vars *************************************************** */
1479
1480 if (bkpinfo->media_device == NULL) {
1481 log_it("Unable to open in from NULL device");
1482 return (1);
1483 }
1484
1485 paranoid_pclose(g_tape_stream);
1486 sync();
1487 sync();
1488 sync();
1489 log_it("Next tape requested.");
1490 insist_on_this_tape_number(g_current_media_number + 1); // will increment it, too
1491 log_it("Opening IN the next tape");
1492 if (!(g_tape_stream = open_device_via_buffer(bkpinfo->media_device, 'r', bkpinfo->internal_tape_block_size))) {
1493 log_OS_error(g_tape_fifo);
1494 log_to_screen("Cannot openin stream device");
1495 return (1);
1496 }
1497 g_tape_posK = 0;
1498 g_sigpipe = FALSE;
1499 res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
1500 if (ctrlchr != BLK_START_OF_TAPE) {
1501 wrong_marker(BLK_START_OF_TAPE, ctrlchr);
1502 }
1503 res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
1504 if (ctrlchr != BLK_START_OF_BACKUP) {
1505 wrong_marker(BLK_START_OF_BACKUP, ctrlchr);
1506 } else {
1507 log_msg(3, "Next tape opened OK. Whoopee!");
1508 }
1509 paranoid_free(sz_msg);
1510 return (res);
1511}
1512
1513
1514
1515/**
1516 * Read a header block from the currently opened stream (CD or tape).
1517 * This block indicates the length of the following file (if it's file-related)
1518 * the filename (if it's file-related), and the block type.
1519 * @param plen Where to put the length of the file. Valid only for file-related header blocks.
1520 * @param filename Where to put the name of the file. Valid only for file-related header blocks.
1521 * @param pcontrol_char Where to put the type of block (e.g. start-file, end-file, start-tape, ...)
1522 * @return 0 for success, nonzero for failure.
1523 * @note If you read a marker (@p pcontrol_char) you're not expecting, you can call wrong_marker().
1524 */
1525int
1526read_header_block_from_stream(long long *plen, char *filename,
1527 int *pcontrol_char)
1528{
1529
1530 /*@ buffers ***************************************************** */
1531 char *tempblock;
1532 char *tmp = NULL;
1533
1534 /*@ int ********************************************************* */
1535 int i, retval;
1536
1537 /*@ end vars *************************************************** */
1538
1539 tempblock = (char *) malloc((size_t) TAPE_BLOCK_SIZE);
1540
1541 for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
1542 tempblock[i] = 0;
1543 }
1544 while (!(*pcontrol_char = tempblock[7000])) {
1545 g_tape_posK += fread(tempblock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream) / 1024;
1546 }
1547 memcpy((char *) plen, tempblock + 7001, sizeof(long long));
1548 if (strcmp(tempblock + 6000 + *pcontrol_char, STR_HEADER)) {
1549 log_it("Bad header block at %ld K", (long) g_tape_posK);
1550 }
1551 strcpy(filename, tempblock + 1000);
1552 if (*pcontrol_char == BLK_ABORTED_BACKUP) {
1553 log_to_screen("I can't verify an aborted backup.");
1554 retval = 1;
1555 } else {
1556 retval = 0;
1557 }
1558 for (i = 1000; i < 1020; i++) {
1559 if (tempblock[i] < 32 || tempblock[i] > 126) {
1560 tempblock[i] = ' ';
1561 }
1562 }
1563 tempblock[i] = '\0';
1564 tmp = marker_to_string(*pcontrol_char);
1565 log_msg(6, "%s (fname=%s, size=%ld K)", tmp, tempblock + 1000, (long) (*plen) >> 10);
1566 mr_free(tmp);
1567 paranoid_free(tempblock);
1568 return (retval);
1569}
1570
1571
1572
1573/**
1574 * Add specified file/slice to the internal catalog of all archives written.
1575 * This lets us restart on a new CD/tape/whatever if it runs out of room. We just
1576 * write the last [buffer size] MB from the catalog to the new tape, so we know
1577 * we have @e all archives on some CD/tape/whatever.
1578 * @param type The type of file we're cataloging (afioball, slice, something else)
1579 * @param number The fileset number or biggiefile number.
1580 * @param aux The slice number if it's a biggiefile, or any other additional info.
1581 * @param fn The original full pathname of the file we're recording.
1582 * @return The index of the record we just added.
1583 */
1584int register_in_tape_catalog(t_archtype type, int number, long aux,
1585 char *fn)
1586{
1587 int last;
1588 char fname[MAX_TAPECAT_FNAME_LEN+1];
1589 char *p;
1590
1591 p = strrchr(fn, '/');
1592 if (p) {
1593 p++;
1594 } else {
1595 p = fn;
1596 }
1597 strncpy(fname, p, MAX_TAPECAT_FNAME_LEN);
1598 fname[MAX_TAPECAT_FNAME_LEN] = '\0';
1599 last = g_tapecatalog->entries;
1600 if (last >= MAX_TAPECATALOG_ENTRIES) {
1601 log_it
1602 ("Warning - can't log #%d in tape catalog - too many entries already",
1603 number);
1604 return (-1);
1605 }
1606 g_tapecatalog->el[last].type = type;
1607 g_tapecatalog->el[last].number = number;
1608 g_tapecatalog->el[last].aux = aux;
1609 g_tapecatalog->el[last].tape_posK = g_tape_posK;
1610 strcpy(g_tapecatalog->el[last].fname, fname);
1611 g_tapecatalog->entries++;
1612 return (last); // returns the index of the record we've jsut added
1613}
1614
1615
1616
1617
1618/**
1619 * Write a bufferfull of the most recent archives to the tape. The
1620 * rationale behind this is a bit complex. If the previous tape ended
1621 * suddenly (EOF or otherwise) some archives were probably left in the
1622 * buffer. That means that Mondo thinks they have been written, but
1623 * the external binary @c buffer has not actually written them. So to
1624 * be safe, we start the new tape by writing the last bufferfull from
1625 * the old one. This insures that all archives will be on at least
1626 * one tape. Sounds inelegant, but it works.
1627 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir is the only field used.
1628 * @return 0 for success, nonzero for failure.
1629 */
1630int write_backcatalog_to_tape()
1631{
1632 int i, last, res = 0;
1633 char *fname = NULL;
1634
1635 log_msg(2, "I am now writing back catalog to tape");
1636 last = g_tapecatalog->entries - 1;
1637 for (i = 0; i <= last; i++) {
1638 mr_asprintf(fname, "%s/tmpfs/backcatalog/%s", bkpinfo->tmpdir, g_tapecatalog->el[i].fname);
1639 if (!does_file_exist(fname)) {
1640 log_msg(6, "Can't write %s - it doesn't exist.", fname);
1641 } else {
1642 write_header_block_to_stream(length_of_file(fname), "start-backcatalog-afio-or-slice", BLK_START_AN_AFIO_OR_SLICE);
1643 log_msg(2, "Writing %s", fname);
1644 if (write_file_to_stream_from_file(fname)) {
1645 res++;
1646 log_msg(2, "%s failed", fname);
1647 }
1648 if (i != last) {
1649 write_header_block_to_stream((off_t)0, "stop-backcatalog-afio-or-slice", BLK_STOP_AN_AFIO_OR_SLICE);
1650 }
1651 }
1652 mr_free(fname);
1653 }
1654 log_msg(2, "Finished writing back catalog to tape");
1655 return (res);
1656}
1657
1658
1659
1660/**
1661 * Write all.tar.gz (produced by Mindi) to the first 32M of the first tape.
1662 * @param fname The path to all.tar.gz.
1663 * @return 0 for success, nonzero for failure.
1664 */
1665int write_data_disks_to_stream(char *fname)
1666{
1667 /*@ pointers *************************************************** */
1668 FILE *fin;
1669
1670 /*@ long ******************************************************* */
1671 long m = -1;
1672 long templong;
1673
1674 /*@ int ******************************************************** */
1675 int i, j;
1676
1677 /*@ buffers **************************************************** */
1678 char tempblock[256 * 1024];
1679
1680 /*@ end vars *************************************************** */
1681
1682 open_evalcall_form("Writing data disks to tape");
1683 log_to_screen("Writing data disks to tape");
1684 log_it("Data disks = %s", fname);
1685 if (!does_file_exist(fname)) {
1686 log_to_screen("Cannot find %s", fname);
1687 return (1);
1688 }
1689 if (!(fin = fopen(fname, "r"))) {
1690 log_OS_error(fname);
1691 fatal_error("Cannot openin the data disk");
1692 }
1693 for (i = 0; i < 32; i++) { /* 32MB */
1694 for (j = 0; j < 4; j++) { /* 256K x 4 = 1MB (1024K) */
1695 if (!feof(fin)) {
1696 m = (long) fread(tempblock, 1, 256 * 1024, fin);
1697 } else {
1698 m = 0;
1699 }
1700 for (; m < 256 * 1024; m++) {
1701 tempblock[m] = '\0';
1702 }
1703 g_tape_posK +=
1704 fwrite(tempblock, 1, 256 * 1024, g_tape_stream) / 1024;
1705 }
1706 if (i > g_tape_buffer_size_MB) // otherwise, 'buffer' distorts calculations
1707 {
1708 templong = ((i - 8) * 4 + j) * 100 / (128 - 8 * 4);
1709 update_evalcall_form((int) (templong));
1710 }
1711 }
1712 paranoid_fclose(fin);
1713 close_evalcall_form();
1714 return (0);
1715}
1716
1717
1718
1719
1720
1721
1722
1723
1724/**
1725 * Log (to screen) an erroneous marker, along with what it should have been.
1726 * @param should_be What we were expecting.
1727 * @param it_is What we got.
1728 */
1729void wrong_marker(int should_be, int it_is)
1730{
1731 char *tmp1 = NULL;
1732 char *tmp2 = NULL;
1733 tmp1 = marker_to_string(should_be);
1734 tmp2 = marker_to_string(it_is);
1735 log_to_screen("Wrong marker! (Should be %s, is actually %s)", tmp1, tmp2);
1736 mr_free(tmp1);
1737 mr_free(tmp2);
1738}
1739
1740/* @} - end of streamGroup */
Note: See TracBrowser for help on using the repository browser.