source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-stream.c@ 2227

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

Remove the iamhere function (will hopefully suppress valgrind errors at restore time)

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