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

Last change on this file since 2331 was 2331, checked in by Bruno Cornec, 15 years ago

r3342@localhost: bruno | 2009-08-14 00:46:51 +0200

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