source: MondoRescue/branches/3.1/mondo/src/common/libmondo-stream.c@ 3161

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