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

Last change on this file since 3892 was 3892, checked in by Bruno Cornec, 3 months ago

More compiler fixes

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