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

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

Fix all remaining compiler errors

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