source: MondoRescue/branches/2.2.6/mondo/src/common/libmondo-stream.c@ 1955

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