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

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

Fix compiler errors

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