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

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

More cleanup

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