source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-stream.c@ 2704

Last change on this file since 2704 was 2704, checked in by Bruno Cornec, 13 years ago

r4180@localhost: bruno | 2011-01-27 10:26:41 +0100

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