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

Last change on this file since 1950 was 1950, checked in by Bruno Cornec, 16 years ago
  • Do not propose to use ILLO in mindi standalone
  • Remove temporary .img files at the end of mindi
  • Push the iso image on the tape the right way
  • Property svn:keywords set to Id
File size: 51.4 KB
Line 
1/* libmondo-stream.c
2 $Id: libmondo-stream.c 1950 2008-05-26 16:39: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 1950 2008-05-26 16:39: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(char *tapedev,
608 long internal_tape_block_size)
609{
610 char *tmp;
611 int res;
612
613 if (strncmp(tapedev, "/dev/", 5)) {
614 log_msg(1,
615 "Not using 'mt setblk'. This isn't an actual /dev entry.");
616 return (0);
617 }
618 malloc_string(tmp);
619 sprintf(tmp, "mt -f %s setblk %ld", tapedev, internal_tape_block_size);
620 res = run_program_and_log_output(tmp, 3);
621 paranoid_free(tmp);
622 return (res);
623}
624
625
626/**
627 * Handle OBDR if we were asked to do so
628 * @param tapedev The tape device to open for writing.
629 * @return 0 for success, nonzero for failure.
630 * @note This should be called ONLY from backup processes. It will OVERWRITE ANY
631 * EXISTING DATA on the tape!
632 */
633void skip_obdr(char *tapedev)
634{
635 char *command = NULL;
636 char *ntapedev = NULL;
637
638 ntapedev = get_non_rewind_dev(tapedev);
639 if (ntapedev == NULL) {
640 log_it("Not skipping OBDR");
641 }
642
643 asprintf(&command, "mt -f %s fsf 2", ntapedev);
644 run_program_and_log_output(command, 1);
645 paranoid_free(command);
646
647 paranoid_free(ntapedev);
648}
649
650int create_obdr(char *tapedev)
651{
652
653 char *ntapedev = NULL;
654 char *command = NULL;
655 int res = 0;
656
657 ntapedev = get_non_rewind_dev(tapedev);
658 if (ntapedev == NULL) {
659 log_it("Skipping OBDR");
660 return(1);
661 }
662
663 /* OBDR: First block 10 kB of zero bs = 512 */
664 asprintf(&command, "mt -f %s compression off", tapedev);
665 res = run_program_and_log_output(command, 1);
666 paranoid_free(command);
667
668 asprintf(&command, "mt -f %s rewind", tapedev);
669 res = run_program_and_log_output(command, 1);
670 paranoid_free(command);
671
672 asprintf(&command, "mt -f %s setblk 512", ntapedev);
673 res = run_program_and_log_output(command, 1);
674 paranoid_free(command);
675
676 asprintf(&command, "dd if=/dev/zero of=%s bs=512 count=20", ntapedev);
677 res = run_program_and_log_output(command, 1);
678 paranoid_free(command);
679
680 /* OBDR: then ISO boot image bs = 2048 */
681 asprintf(&command, "mt -f %s setblk 2048", ntapedev);
682 res = run_program_and_log_output(command, 1);
683 paranoid_free(command);
684
685 asprintf(&command, "dd if=%s of=%s bs=2048", MINDI_CACHE"/mondorescue.iso", ntapedev );
686 res = run_program_and_log_output(command, 1);
687 paranoid_free(command);
688
689 paranoid_free(ntapedev);
690}
691
692
693/**
694 * Open the tape device for input.
695 * @param bkpinfo The backup information structure. Fields used:
696 * - @c bkpinfo->media_device
697 * - @c bkpinfo->tmpdir
698 * @return 0 for success, nonzero for failure.
699 * @note This will also work with a cdstream for now, but don't count on this behavior.
700 */
701int openin_tape()
702{
703 /*@ buffer ***************************************************** */
704 char fname[MAX_STR_LEN];
705 char *datablock;
706 char tmp[MAX_STR_LEN];
707 char old_cwd[MAX_STR_LEN];
708 char outfname[MAX_STR_LEN];
709 /*@ int ******************************************************* */
710 int i;
711 int j;
712 int res;
713 long length, templong;
714 size_t k;
715 int retval = 0;
716 int ctrl_chr;
717
718 /*@ long long ************************************************* */
719 long long size;
720
721 /*@ pointers ************************************************** */
722 FILE *fout;
723
724 /*@ end vars *************************************************** */
725
726 assert_string_is_neither_NULL_nor_zerolength(bkpinfo->media_device);
727 if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
728 fatal_error("Cannot alloc mem for tape catalog");
729 }
730 g_tapecatalog->entries = 0;
731 g_tape_posK = 0;
732 if (g_tape_stream) {
733 log_it("FYI - I won't 'openin' the tape. It's already open.");
734 return (0);
735 }
736 if (bkpinfo->use_obdr) {
737 skip_obdr(bkpinfo->media_device);
738 }
739
740 insist_on_this_tape_number(1);
741 sprintf(outfname, "%s/tmp/all.tar.gz", bkpinfo->tmpdir);
742 make_hole_for_file(outfname);
743
744 set_tape_block_size_with_mt(bkpinfo->media_device,
745 bkpinfo->internal_tape_block_size);
746
747// start_buffer_process( bkpinfo->media_device, g_tape_fifo, FALSE);
748 log_it("Opening IN tape");
749 if (!
750 (g_tape_stream =
751 open_device_via_buffer(bkpinfo->media_device, 'r',
752 bkpinfo->internal_tape_block_size))) {
753 log_OS_error(g_tape_fifo);
754 log_to_screen("Cannot openin stream device");
755 return (1);
756 }
757 log_to_screen("Reading stream");
758 log_it("stream device = '%s'", bkpinfo->media_device);
759/* skip data disks */
760 open_evalcall_form("Skipping data disks on stream");
761 log_to_screen("Skipping data disks on stream");
762 if (!(fout = fopen(outfname, "w"))) {
763 log_OS_error(outfname);
764 log_to_screen("Cannot openout datadisk all.tar.gz file");
765 return (-1);
766 }
767 if (!(datablock = (char *) malloc(256 * 1024))) {
768 log_to_screen("Unable to malloc 256*1024");
769 exit(1);
770 }
771 for (i = 0; i < 32; i++) {
772 for (j = 0; j < 4; j++) {
773 for (length = 0, k = 0; length < 256 * 1024; length += k) {
774 k = fread(datablock + length, 1, 256 * 1024 - length,
775 g_tape_stream);
776 }
777 (void) fwrite(datablock, 1, (size_t) length, fout);
778 g_tape_posK += length / 1024;
779 }
780 if (i > 8) // otherwise, 'buffer' distorts calculations
781 {
782 templong = ((i - 8) * 4 + j) * 100 / (128 - 8 * 4);
783 update_evalcall_form((int) (templong));
784 }
785 }
786 paranoid_fclose(fout);
787 paranoid_free(datablock);
788/* find initial blocks */
789 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
790 retval += res;
791 if (ctrl_chr != BLK_START_OF_TAPE) {
792 wrong_marker(BLK_START_OF_TAPE, ctrl_chr);
793 }
794 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
795 retval += res;
796 if (ctrl_chr != BLK_START_OF_BACKUP) {
797 wrong_marker(BLK_START_OF_BACKUP, ctrl_chr);
798 }
799 close_evalcall_form();
800 log_it("Saved all.tar.gz to '%s'", outfname);
801 (void) getcwd(old_cwd, MAX_STR_LEN);
802 chdir(bkpinfo->tmpdir);
803 sprintf(tmp, "tar -zxf %s ./tmp/mondo-restore.cfg 2> /dev/null",
804 outfname);
805 paranoid_system(tmp);
806 paranoid_system("cp -f tmp/mondo-restore.cfg . 2> /dev/null");
807 chdir(old_cwd);
808 unlink(outfname);
809 return (retval);
810}
811
812
813/**
814 * Start writing to a CD stream.
815 * @param cddev The CD device to openout via cdrecord.
816 * @param speed The speed to write at.
817 * @return 0 for success, nonzero for failure.
818 * @note This should be called only from backup processes.
819 */
820int openout_cdstream(char *cddev, int speed)
821{
822 /*@ buffers ***************************************************** */
823 char command[MAX_STR_LEN * 2];
824
825 /*@ end vars *************************************************** */
826
827/* add 'dummy' if testing */
828 sprintf(command,
829 "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s",
830 cddev, speed, MONDO_LOGFILE, MONDO_LOGFILE);
831/* initialise the catalog */
832 g_current_media_number = 1;
833 if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
834 fatal_error("Cannot alloc mem for tape catalog");
835 }
836 g_tapecatalog->entries = 0;
837/* log stuff */
838 log_it("Opening OUT cdstream with the command");
839 log_it(command);
840/* log_it("Let's see what happens, shall we?"); */
841 g_tape_stream = popen(command, "w");
842 if (g_tape_stream) {
843 return (0);
844 } else {
845 log_to_screen("Failed to openout to cdstream (fifo)");
846 return (1);
847 }
848}
849
850/**
851 * Return the non-rewinding device when passed the normal one
852 * @param tapedev The tape device to open for writing.
853 * @note the caller needs to free the string returned
854 */
855char *get_non_rewind_dev(char *tapedev)
856{
857
858 char *ntapedev = NULL;
859 char *p = NULL;
860 char *q = NULL;
861
862 ntapedev = (char *)malloc(strlen(tapedev)+sizeof(char));
863 p = strrchr(tapedev,'/');
864 if (p == NULL) {
865 return(NULL);
866 }
867
868 /* Copy tapedev content up to the last / */
869 q = tapedev;
870 while (q != p) {
871 *ntapedev = *q;
872 ntapedev++;
873 q++;
874 }
875 /* Copy the '/' */
876 *ntapedev = *q;
877 ntapedev++;
878 q++;
879 /* Adds a 'n' - non-rewinding */
880 *ntapedev = 'n';
881 ntapedev++;
882 /* Copy the rest of tapedev */
883 while (*q != '\0') {
884 *ntapedev = *q;
885 ntapedev++;
886 q++;
887 }
888 *ntapedev = '\0';
889 if (mt_says_tape_exists(ntapedev)) {
890 log_it("Non-rewinding tape device is %s",ntapedev);
891 } else {
892 log_it("Unable to find non-rewinding tape device.");
893 ntapedev = NULL;
894 }
895 return(ntapedev);
896}
897
898/**
899 * Start writing to a tape device for the backup.
900 * Handle OBDR if we were asked to do so
901 * @param tapedev The tape device to open for writing.
902 * @return 0 for success, nonzero for failure.
903 * @note This should be called ONLY from backup processes. It will OVERWRITE ANY
904 * EXISTING DATA on the tape!
905 */
906int openout_tape(char *tapedev, long internal_tape_block_size)
907{
908
909 g_current_media_number = 1;
910 if (g_tape_stream) {
911 log_it("FYI - I won't 'openout' the tape. It's already open.");
912 return (0);
913 }
914 if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
915 fatal_error("Cannot alloc mem for tape catalog");
916 }
917 g_tapecatalog->entries = 0;
918 g_tape_posK = 0;
919
920 if (bkpinfo->use_obdr) {
921 create_obdr(tapedev);
922 }
923
924 set_tape_block_size_with_mt(tapedev, internal_tape_block_size);
925 log_it("Opening OUT tape");
926 if (!
927 (g_tape_stream =
928 open_device_via_buffer(tapedev, 'w', internal_tape_block_size))) {
929 log_OS_error(g_tape_fifo);
930 log_to_screen("Cannot openin stream device");
931 return (1);
932 }
933 return (0);
934}
935
936
937
938
939/**
940 * Copy a file from the opened stream (CD or tape) to @p outfile.
941 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
942 * @param outfile The file to write to.
943 * @param size The size of the file in the input stream.
944 * @return 0 for success, nonzero for failure.
945 */
946int
947read_file_from_stream_to_file(char *outfile, long long size)
948{
949
950 /*@ int ******************************************************** */
951 int res;
952
953 /*@ end vars *************************************************** */
954
955 res = read_file_from_stream_FULL(outfile, NULL, size);
956
957 return (res);
958}
959
960
961
962/**
963 * Copy a file from the currently opened stream (CD or tape) to the stream indicated
964 * by @p fout.
965 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
966 * @param fout The stream to write the file to.
967 * @param size The size of the file in bytes.
968 * @return 0 for success, nonzero for failure.
969 */
970int
971read_file_from_stream_to_stream(FILE * fout, long long size)
972{
973
974 /*@ int ******************************************************** */
975 int res;
976
977 /*@ end vars *************************************************** */
978
979 res = read_file_from_stream_FULL(NULL, fout, size);
980/* fflush(g_tape_stream);
981 fflush(fout);*/
982 return (res);
983}
984
985
986
987/**
988 * Copy a file from the currently opened stream (CD or tape) to either a
989 * @c FILE pointer indicating a currently-opened file, or a filename indicating
990 * a new file to create. This is the backbone function that read_file_from_stream_to_file()
991 * and read_file_from_stream_to_stream() are wrappers for.
992 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
993 * @param outfname If non-NULL, write to this file.
994 * @param foutstream If non-NULL, write to this stream.
995 * @param orig_size The original length of the file in bytes.
996 * @return 0 for success, nonzero for failure.
997 * @note Only @b one of @p outfname or @p foutstream may be non-NULL.
998 */
999int
1000read_file_from_stream_FULL(char *outfname, FILE * foutstream, long long orig_size)
1001{
1002 /*@ buffers ***************************************************** */
1003 char *tmp;
1004 char *datablock;
1005 char *temp_fname;
1006 char *temp_cksum;
1007 char *actual_cksum;
1008// char *pA, *pB;
1009
1010 /*@ int ********************************************************* */
1011 int retval = 0;
1012#ifdef EXTRA_TAPE_CHECKSUMS
1013 int i, ch;
1014#endif
1015 int noof_blocks;
1016 int ctrl_chr;
1017 int res;
1018 /*@ pointers **************************************************** */
1019 FILE *fout;
1020
1021 /*@ long ***************************************************** */
1022 long bytes_to_write = 0L /*,i */ ;
1023// long bytes_successfully_read_in_this_time = 0;
1024
1025 /*@ long long *************************************************** */
1026 long long temp_size, size;
1027 long bytes_read, bytes_to_read;
1028 long long total_read_from_tape_for_this_file = 0;
1029 long long where_I_was_before_tape_change = 0;
1030 /*@ unsigned int ************************************************ */
1031 /* unsigned int ch; */
1032 unsigned int crc16;
1033 unsigned int crctt;
1034
1035 bool had_to_resync = FALSE;
1036
1037 /*@ init ******************************************************* */
1038 malloc_string(tmp);
1039 malloc_string(temp_fname);
1040 malloc_string(temp_cksum);
1041 malloc_string(actual_cksum);
1042 datablock = malloc(TAPE_BLOCK_SIZE);
1043 crc16 = 0;
1044 crctt = 0;
1045 size = orig_size;
1046
1047 /*@ end vars *************************************************** */
1048
1049 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1050 if (orig_size != temp_size && orig_size != -1) {
1051 sprintf(tmp,
1052 "output file's size should be %ld K but is apparently %ld K",
1053 (long) size >> 10, (long) temp_size >> 10);
1054 log_to_screen(tmp);
1055 }
1056 if (ctrl_chr != BLK_START_FILE) {
1057 wrong_marker(BLK_START_FILE, ctrl_chr);
1058 return (1);
1059 }
1060 /* Not used
1061 sprintf(tmp, "Reading file from tape; writing to '%s'; %ld KB",
1062 outfname, (long) size >> 10);
1063 */
1064
1065 if (foutstream) {
1066 fout = foutstream;
1067 } else {
1068 fout = fopen(outfname, "w");
1069 }
1070 if (!fout) {
1071 log_OS_error(outfname);
1072 log_to_screen("Cannot openout file");
1073 return (1);
1074 }
1075 total_read_from_tape_for_this_file = 0;
1076 for (noof_blocks = 0; size > 0;
1077 noof_blocks++, size -=
1078 bytes_to_write, total_read_from_tape_for_this_file +=
1079 bytes_read) {
1080 bytes_to_write =
1081 (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
1082 bytes_to_read = TAPE_BLOCK_SIZE;
1083 bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
1084 while (bytes_read < bytes_to_read) { // next tape!
1085// crctt=crc16=0;
1086 where_I_was_before_tape_change = size;
1087 log_msg(4, "where_I_was_... = %lld",
1088 where_I_was_before_tape_change);
1089 start_to_read_from_next_tape();
1090 log_msg(4, "Started reading from next tape.");
1091 skip_incoming_files_until_we_find_this_one(temp_fname);
1092 log_msg(4, "Skipped irrelevant files OK.");
1093 for (size = orig_size; size > where_I_was_before_tape_change;
1094 size -= bytes_to_write) {
1095 bytes_read =
1096 fread(datablock, 1, bytes_to_read, g_tape_stream);
1097 }
1098 log_msg(4, "'size' is now %lld (should be %lld)", size,
1099 where_I_was_before_tape_change);
1100 log_to_screen("Successfully re-sync'd tape");
1101 had_to_resync = TRUE;
1102 bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
1103 }
1104
1105 (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
1106
1107#ifdef EXTRA_TAPE_CHECKSUMS
1108 for (i = 0; i < (int) bytes_to_write; i++) {
1109 ch = datablock[i];
1110 crc16 = updcrcr(crc16, (unsigned) ch);
1111 crctt = updcrc(crctt, (unsigned) ch);
1112 }
1113#endif
1114 }
1115 log_msg(6, "Total read from tape for this file = %lld",
1116 total_read_from_tape_for_this_file);
1117 log_msg(6, ".......................... Should be %lld", orig_size);
1118 g_tape_posK += total_read_from_tape_for_this_file / 1024;
1119 sprintf(actual_cksum, "%04x%04x", crc16, crctt);
1120 if (foutstream) { /*log_it("Finished writing to foutstream"); */
1121 } else {
1122 paranoid_fclose(fout);
1123 }
1124 res = read_header_block_from_stream(&temp_size, temp_cksum, &ctrl_chr);
1125 if (ctrl_chr != BLK_STOP_FILE) {
1126 wrong_marker(BLK_STOP_FILE, ctrl_chr);
1127// fatal_error("Bad marker"); // return(1);
1128 }
1129 if (strcmp(temp_cksum, actual_cksum)) {
1130 sprintf(tmp, "actual cksum=%s; recorded cksum=%s", actual_cksum,
1131 temp_cksum);
1132 log_to_screen(tmp);
1133 sprintf(tmp, "%s (%ld K) is corrupt on tape", temp_fname,
1134 (long) orig_size >> 10);
1135 log_to_screen(tmp);
1136 retval++;
1137 } else {
1138 sprintf(tmp, "%s is GOOD on tape", temp_fname);
1139 /* log_it(tmp); */
1140 }
1141 paranoid_free(datablock);
1142 paranoid_free(tmp);
1143 paranoid_free(temp_fname);
1144 paranoid_free(temp_cksum);
1145 paranoid_free(actual_cksum);
1146 return (retval);
1147}
1148
1149
1150
1151/**
1152 * Read a header block from the currently opened stream (CD or tape).
1153 * This block indicates the length of the following file (if it's file-related)
1154 * the filename (if it's file-related), and the block type.
1155 * @param plen Where to put the length of the file. Valid only for file-related header blocks.
1156 * @param filename Where to put the name of the file. Valid only for file-related header blocks.
1157 * @param pcontrol_char Where to put the type of block (e.g. start-file, end-file, start-tape, ...)
1158 * @return 0 for success, nonzero for failure.
1159 * @note If you read a marker (@p pcontrol_char) you're not expecting, you can call wrong_marker().
1160 */
1161int
1162read_header_block_from_stream(long long *plen, char *filename,
1163 int *pcontrol_char)
1164{
1165
1166 /*@ buffers ***************************************************** */
1167 char *tempblock;
1168
1169 /*@ int ********************************************************* */
1170 int i, retval;
1171
1172 /*@ end vars *************************************************** */
1173
1174 tempblock = (char *) malloc((size_t) TAPE_BLOCK_SIZE);
1175
1176 for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
1177 tempblock[i] = 0;
1178 }
1179 while (!(*pcontrol_char = tempblock[7000])) {
1180 g_tape_posK +=
1181 fread(tempblock, 1, (size_t) TAPE_BLOCK_SIZE,
1182 g_tape_stream) / 1024;
1183 }
1184/* memcpy((char*)plength_of_incoming_file,(char*)tempblock+7001,sizeof(long long)); */
1185/* for(*plen=0,i=7;i>=0;i--) {*plen<<=8; *plen |= tempblock[7001+i];} */
1186 memcpy((char *) plen, tempblock + 7001, sizeof(long long));
1187 if (strcmp(tempblock + 6000 + *pcontrol_char, STR_HEADER)) {
1188 log_it("Bad header block at %ld K", (long) g_tape_posK);
1189 }
1190 strcpy(filename, tempblock + 1000);
1191/* strcpy(cksum,tempblock+5555);*/
1192/* log_it( "%s (reading) fname=%s, filesize=%ld K",
1193 marker_to_string (*pcontrol_char), filename,
1194 (long) ((*plen) >> 10));
1195*/
1196 if (*pcontrol_char == BLK_ABORTED_BACKUP) {
1197 log_to_screen("I can't verify an aborted backup.");
1198 retval = 1;
1199 } else {
1200 retval = 0;
1201 }
1202 for (i = 1000; i < 1020; i++) {
1203 if (tempblock[i] < 32 || tempblock[i] > 126) {
1204 tempblock[i] = ' ';
1205 }
1206 }
1207 tempblock[i] = '\0';
1208 log_msg(6, "%s (fname=%s, size=%ld K)",
1209 marker_to_string(*pcontrol_char), tempblock + 1000,
1210 (long) (*plen) >> 10);
1211 paranoid_free(tempblock);
1212 return (retval);
1213}
1214
1215
1216
1217/**
1218 * Add specified file/slice to the internal catalog of all archives written.
1219 * This lets us restart on a new CD/tape/whatever if it runs out of room. We just
1220 * write the last [buffer size] MB from the catalog to the new tape, so we know
1221 * we have @e all archives on some CD/tape/whatever.
1222 * @param type The type of file we're cataloging (afioball, slice, something else)
1223 * @param number The fileset number or biggiefile number.
1224 * @param aux The slice number if it's a biggiefile, or any other additional info.
1225 * @param fn The original full pathname of the file we're recording.
1226 * @return The index of the record we just added.
1227 */
1228int register_in_tape_catalog(t_archtype type, int number, long aux,
1229 char *fn)
1230{
1231 int last;
1232 char fname[MAX_TAPECAT_FNAME_LEN];
1233 char *p;
1234
1235 p = strrchr(fn, '/');
1236 if (p) {
1237 p++;
1238 } else {
1239 p = fn;
1240 }
1241 strncpy(fname, p, MAX_TAPECAT_FNAME_LEN);
1242 fname[MAX_TAPECAT_FNAME_LEN] = '\0';
1243 last = g_tapecatalog->entries;
1244 if (last >= MAX_TAPECATALOG_ENTRIES) {
1245 log_it
1246 ("Warning - can't log #%d in tape catalog - too many entries already",
1247 number);
1248 return (-1);
1249 }
1250 g_tapecatalog->el[last].type = type;
1251 g_tapecatalog->el[last].number = number;
1252 g_tapecatalog->el[last].aux = aux;
1253 g_tapecatalog->el[last].tape_posK = g_tape_posK;
1254 strcpy(g_tapecatalog->el[last].fname, fname);
1255 g_tapecatalog->entries++;
1256 return (last); // returns the index of the record we've jsut added
1257}
1258
1259
1260
1261
1262/**
1263 * Decide whether we should start a new tape. This is TRUE if we've run out of tape
1264 * (got SIGPIPE) or look like we will.
1265 * @param mediasize The size of the tape in megabytes.
1266 * @param length_of_incoming_file The length of the file we're about to write, in bytes.
1267 * @bug This seems like it'll only work for media_size != autodetect, but Mondo only allows
1268 * autodetecting the size. Huh?
1269 */
1270
1271/* BERLIOS: Should be reviewed for mediasize being a off_t ??? */
1272bool
1273should_we_write_to_next_tape(long mediasize,
1274 off_t length_of_incoming_file)
1275{
1276 /*@ bool's ***************************************************** */
1277 bool we_need_a_new_tape = FALSE;
1278
1279 /*@ end vars *************************************************** */
1280
1281 if (mediasize == 0) {
1282 return (FALSE);
1283 }
1284 if (mediasize > 0 && (g_tape_posK >> 10 >= mediasize)) {
1285 log_it("mediasize = %ld", mediasize);
1286 we_need_a_new_tape = TRUE;
1287 log_to_screen("Should have started a new tape/CD already");
1288 }
1289 if ((g_tape_posK + length_of_incoming_file / 1024) >> 10 >=
1290 mediasize - (SLICE_SIZE * 4 / 1024)) {
1291 log_it("g_tape_posK = %ld\nmediasize = %ld\n", g_tape_posK,
1292 mediasize);
1293 we_need_a_new_tape = TRUE;
1294 }
1295 return (we_need_a_new_tape);
1296}
1297
1298
1299/**
1300 * Seek through the stream until we find a header block where the NAME field matches
1301 * @p the_file_I_was_reading. This is useful if you've just started reading from
1302 * a new tape and want to find the file you were reading when the tape ended.
1303 * @param the_file_I_was_reading File name to look for.
1304 * @return 0 for success, nonzero for failure.
1305 */
1306int skip_incoming_files_until_we_find_this_one(char
1307 *the_file_I_was_reading)
1308{
1309 char *pA;
1310 char *pB;
1311 int res;
1312 int ctrl_chr;
1313 char *temp_fname;
1314 char *datablock;
1315 long long temp_size, size;
1316 long bytes_to_write;
1317
1318 datablock = malloc(TAPE_BLOCK_SIZE);
1319 malloc_string(temp_fname);
1320 pB = strrchr(the_file_I_was_reading, '/');
1321 if (pB) {
1322 pB++;
1323 } else {
1324 pB = the_file_I_was_reading;
1325 }
1326 log_msg(1, "skip_incoming_..(%s)", pB);
1327 log_msg(2, "Looking for initial START_AN_AFIO_OR_SLICE");
1328 ctrl_chr = -1;
1329 while (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1330 res =
1331 read_header_block_from_stream(&temp_size, temp_fname,
1332 &ctrl_chr);
1333 if (ctrl_chr == BLK_START_AN_AFIO_OR_SLICE) {
1334 break;
1335 }
1336 log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
1337 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1338 log_msg(3, "Still trying to re-sync w/ tape");
1339 }
1340 while (ctrl_chr != BLK_START_FILE) {
1341 res =
1342 read_header_block_from_stream(&temp_size, temp_fname,
1343 &ctrl_chr);
1344 if (ctrl_chr == BLK_START_FILE) {
1345 break;
1346 }
1347 log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
1348 wrong_marker(BLK_START_FILE, ctrl_chr);
1349 log_msg(3, "Still trying to re-sync w/ tape");
1350 }
1351 pA = strrchr(temp_fname, '/');
1352 if (pA) {
1353 pA++;
1354 } else {
1355 pA = temp_fname;
1356 }
1357 pB = strrchr(the_file_I_was_reading, '/');
1358 if (pB) {
1359 pB++;
1360 } else {
1361 pB = the_file_I_was_reading;
1362 }
1363 while (strcmp(pA, pB)) {
1364 log_msg(6, "Skipping %s (it's not %s)", temp_fname,
1365 the_file_I_was_reading);
1366 for (size = temp_size; size > 0; size -= bytes_to_write) {
1367 bytes_to_write =
1368 (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
1369 // FIXME - needs error-checking and -catching
1370 fread(datablock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream);
1371 }
1372 res =
1373 read_header_block_from_stream(&temp_size, temp_fname,
1374 &ctrl_chr);
1375 if (ctrl_chr != BLK_STOP_FILE) {
1376 wrong_marker(BLK_STOP_FILE, ctrl_chr);
1377 }
1378 res =
1379 read_header_block_from_stream(&temp_size, temp_fname,
1380 &ctrl_chr);
1381 if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
1382 wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
1383 }
1384 res =
1385 read_header_block_from_stream(&temp_size, temp_fname,
1386 &ctrl_chr);
1387 if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1388 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1389 }
1390 res =
1391 read_header_block_from_stream(&temp_size, temp_fname,
1392 &ctrl_chr);
1393 if (ctrl_chr != BLK_START_FILE) {
1394 wrong_marker(BLK_START_FILE, ctrl_chr);
1395 }
1396 pA = strrchr(temp_fname, '/');
1397 if (pA) {
1398 pA++;
1399 } else {
1400 pA = temp_fname;
1401 }
1402 pB = strrchr(the_file_I_was_reading, '/');
1403 if (pB) {
1404 pB++;
1405 } else {
1406 pB = the_file_I_was_reading;
1407 }
1408 }
1409 log_msg(2, "Reading %s (it matches %s)", temp_fname,
1410 the_file_I_was_reading);
1411 paranoid_free(temp_fname);
1412 paranoid_free(datablock);
1413 return (0);
1414}
1415
1416
1417/**
1418 * Start to read from the next tape. Assumes the user has already inserted it.
1419 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
1420 * @return 0 for success, nonzero for failure.
1421 */
1422int start_to_read_from_next_tape()
1423{
1424 /*@ int ********************************************************* */
1425 int res = 0;
1426 char *sz_msg;
1427 int ctrlchr;
1428 long long temp_size;
1429 malloc_string(sz_msg);
1430 /*@ end vars *************************************************** */
1431
1432 paranoid_pclose(g_tape_stream);
1433 sync();
1434 sync();
1435 sync();
1436 log_it("Next tape requested.");
1437 insist_on_this_tape_number(g_current_media_number + 1); // will increment it, too
1438 log_it("Opening IN the next tape");
1439 if (!
1440 (g_tape_stream =
1441 open_device_via_buffer(bkpinfo->media_device, 'r',
1442 bkpinfo->internal_tape_block_size))) {
1443 log_OS_error(g_tape_fifo);
1444 log_to_screen("Cannot openin stream device");
1445 return (1);
1446 }
1447 g_tape_posK = 0;
1448 g_sigpipe = FALSE;
1449 res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
1450 if (ctrlchr != BLK_START_OF_TAPE) {
1451 wrong_marker(BLK_START_OF_TAPE, ctrlchr);
1452 }
1453 res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
1454 if (ctrlchr != BLK_START_OF_BACKUP) {
1455 wrong_marker(BLK_START_OF_BACKUP, ctrlchr);
1456 } else {
1457 log_msg(3, "Next tape opened OK. Whoopee!");
1458 }
1459 paranoid_free(sz_msg);
1460 return (res);
1461}
1462
1463
1464
1465/**
1466 * Start to write to the next tape. Assume the user has already inserted it.
1467 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
1468 * @return 0 for success, nonzero for failure.
1469 */
1470int start_to_write_to_next_tape()
1471{
1472 int res = 0;
1473 char command[MAX_STR_LEN * 2];
1474 paranoid_pclose(g_tape_stream);
1475 system("sync");
1476 system("sync");
1477 system("sync");
1478 log_it("New tape requested.");
1479 insist_on_this_tape_number(g_current_media_number + 1); // will increment g_current_media, too
1480 if (g_current_media_number > MAX_NOOF_MEDIA) {
1481 res++;
1482 log_to_screen("Too many tapes. Man, you need to use nfs!");
1483 }
1484 if (bkpinfo->backup_media_type == cdstream) {
1485 sprintf(command,
1486 "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s",
1487 bkpinfo->media_device, bkpinfo->cdrw_speed, MONDO_LOGFILE,
1488 MONDO_LOGFILE);
1489 log_it("Opening OUT to next CD with the command");
1490 log_it(command);
1491 log_it("Let's see what happens, shall we?");
1492 g_tape_stream = popen(command, "w");
1493 if (!g_tape_stream) {
1494 log_to_screen("Failed to openout to cdstream (fifo)");
1495 return (1);
1496 }
1497 } else {
1498 log_it("Opening OUT to next tape");
1499 if (!
1500 (g_tape_stream =
1501 open_device_via_buffer(bkpinfo->media_device, 'w',
1502 bkpinfo->internal_tape_block_size))) {
1503 log_OS_error(g_tape_fifo);
1504 log_to_screen("Cannot openin stream device");
1505 return (1);
1506 }
1507 }
1508 g_tape_posK = 0;
1509 g_sigpipe = FALSE;
1510 res += write_header_block_to_stream((off_t)0, "start-of-tape", BLK_START_OF_TAPE); /* just in case */
1511 res += write_header_block_to_stream((off_t)0, "start-of-backup", BLK_START_OF_BACKUP); /* just in case */
1512 return (res);
1513}
1514
1515
1516
1517
1518/**
1519 * Write a bufferfull of the most recent archives to the tape. The
1520 * rationale behind this is a bit complex. If the previous tape ended
1521 * suddenly (EOF or otherwise) some archives were probably left in the
1522 * buffer. That means that Mondo thinks they have been written, but
1523 * the external binary @c buffer has not actually written them. So to
1524 * be safe, we start the new tape by writing the last bufferfull from
1525 * the old one. This insures that all archives will be on at least
1526 * one tape. Sounds inelegant, but it works.
1527 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir is the only field used.
1528 * @return 0 for success, nonzero for failure.
1529 */
1530int write_backcatalog_to_tape()
1531{
1532 int i, last, res = 0;
1533 char *fname;
1534
1535 log_msg(2, "I am now writing back catalog to tape");
1536 malloc_string(fname);
1537 last = g_tapecatalog->entries - 1;
1538 for (i = 0; i <= last; i++) {
1539 sprintf(fname, "%s/tmpfs/backcatalog/%s", bkpinfo->tmpdir,
1540 g_tapecatalog->el[i].fname);
1541 if (!does_file_exist(fname)) {
1542 log_msg(6, "Can't write %s - it doesn't exist.", fname);
1543 } else {
1544 write_header_block_to_stream(length_of_file(fname),
1545 "start-backcatalog-afio-or-slice",
1546 BLK_START_AN_AFIO_OR_SLICE);
1547 log_msg(2, "Writing %s", fname);
1548 if (write_file_to_stream_from_file(fname)) {
1549 res++;
1550 log_msg(2, "%s failed", fname);
1551 }
1552 if (i != last) {
1553 write_header_block_to_stream((off_t)0,
1554 "stop-backcatalog-afio-or-slice",
1555 BLK_STOP_AN_AFIO_OR_SLICE);
1556 }
1557 }
1558 }
1559 paranoid_free(fname);
1560 log_msg(2, "Finished writing back catalog to tape");
1561 return (res);
1562}
1563
1564
1565
1566/**
1567 * Write all.tar.gz (produced by Mindi) to the first 32M of the first tape.
1568 * @param fname The path to all.tar.gz.
1569 * @return 0 for success, nonzero for failure.
1570 */
1571int write_data_disks_to_stream(char *fname)
1572{
1573 /*@ pointers *************************************************** */
1574 FILE *fin;
1575 char tmp[MAX_STR_LEN];
1576
1577 /*@ long ******************************************************* */
1578 long m = -1;
1579 long templong;
1580
1581 /*@ int ******************************************************** */
1582 int i, j;
1583
1584 /*@ buffers **************************************************** */
1585 char tempblock[256 * 1024];
1586
1587 /*@ end vars *************************************************** */
1588
1589 open_evalcall_form("Writing data disks to tape");
1590 log_to_screen("Writing data disks to tape");
1591 log_it("Data disks = %s", fname);
1592 if (!does_file_exist(fname)) {
1593 sprintf(tmp, "Cannot find %s", fname);
1594 log_to_screen(tmp);
1595 return (1);
1596 }
1597 if (!(fin = fopen(fname, "r"))) {
1598 log_OS_error(fname);
1599 fatal_error("Cannot openin the data disk");
1600 }
1601 for (i = 0; i < 32; i++) { /* 32MB */
1602 for (j = 0; j < 4; j++) { /* 256K x 4 = 1MB (1024K) */
1603 if (!feof(fin)) {
1604 m = (long) fread(tempblock, 1, 256 * 1024, fin);
1605 } else {
1606 m = 0;
1607 }
1608 for (; m < 256 * 1024; m++) {
1609 tempblock[m] = '\0';
1610 }
1611 g_tape_posK +=
1612 fwrite(tempblock, 1, 256 * 1024, g_tape_stream) / 1024;
1613 }
1614 if (i > g_tape_buffer_size_MB) // otherwise, 'buffer' distorts calculations
1615 {
1616 templong = ((i - 8) * 4 + j) * 100 / (128 - 8 * 4);
1617 update_evalcall_form((int) (templong));
1618 }
1619 }
1620 paranoid_fclose(fin);
1621 close_evalcall_form();
1622 return (0);
1623}
1624
1625
1626
1627
1628/**
1629 * Copy @p infile to the opened stream (CD or tape).
1630 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
1631 * @param infile The file to write to the stream.
1632 * @return 0 for success, nonzero for failure.
1633 */
1634int write_file_to_stream_from_file(char *infile)
1635{
1636 /*@ buffers **************************************************** */
1637 char tmp[MAX_STR_LEN];
1638 char datablock[TAPE_BLOCK_SIZE];
1639 char checksum[MAX_STR_LEN];
1640 char *infile_basename;
1641
1642 /*@ int ******************************************************** */
1643 int retval = 0;
1644 int noof_blocks;
1645
1646 /* unsigned int ch; */
1647 unsigned int crc16;
1648 unsigned int crctt;
1649
1650 /*@ pointers *************************************************** */
1651 FILE *fin;
1652 char *p;
1653
1654 /*@ long ******************************************************* */
1655 long bytes_to_read = 0;
1656 long i;
1657
1658 off_t filesize;
1659
1660#ifdef EXTRA_TAPE_CHECKSUMS
1661 int ch;
1662#endif
1663
1664 /*@ initialize ************************************************ */
1665 crc16 = 0;
1666 crctt = 0;
1667
1668
1669
1670 /*@ end vars *************************************************** */
1671
1672 infile_basename = strrchr(infile, '/');
1673 if (infile_basename) {
1674 infile_basename++;
1675 } else {
1676 infile_basename = infile;
1677 }
1678 filesize = length_of_file(infile);
1679 if (should_we_write_to_next_tape
1680 (bkpinfo->media_size[g_current_media_number], filesize)) {
1681 start_to_write_to_next_tape();
1682 write_backcatalog_to_tape();
1683 }
1684 p = strrchr(infile, '/');
1685 if (!p) {
1686 p = infile;
1687 } else {
1688 p++;
1689 }
1690 sprintf(tmp, "Writing file '%s' to tape (%ld KB)", p,
1691 (long) filesize >> 10);
1692 log_it(tmp);
1693 write_header_block_to_stream(filesize, infile_basename,
1694 BLK_START_FILE);
1695//go_here_to_restart_saving_of_file:
1696 if (!(fin = fopen(infile, "r"))) {
1697 log_OS_error(infile);
1698 return (1);
1699 }
1700 for (noof_blocks = 0; filesize > 0;
1701 noof_blocks++, filesize -= bytes_to_read) {
1702 if (filesize < TAPE_BLOCK_SIZE) {
1703 bytes_to_read = (long) filesize;
1704 for (i = 0; i < TAPE_BLOCK_SIZE; i++) {
1705 datablock[i] = '\0';
1706 }
1707 } else {
1708 bytes_to_read = TAPE_BLOCK_SIZE;
1709 }
1710 (void) fread(datablock, 1, (size_t) bytes_to_read, fin);
1711 g_tape_posK +=
1712 fwrite(datablock, 1, /*bytes_to_read */
1713 (size_t) TAPE_BLOCK_SIZE,
1714 g_tape_stream) / 1024;
1715 if (g_sigpipe) {
1716 iamhere("Sigpipe occurred recently. I'll start a new tape.");
1717 fclose(fin);
1718 g_sigpipe = FALSE;
1719 start_to_write_to_next_tape();
1720 write_backcatalog_to_tape(); // kinda-sorta recursive :)
1721 return (0);
1722 }
1723#ifdef EXTRA_TAPE_CHECKSUMS
1724 for (i = 0; i < bytes_to_read; i++) {
1725 ch = datablock[i];
1726 crc16 = updcrcr(crc16, (unsigned) ch);
1727 crctt = updcrc(crctt, (unsigned) ch);
1728 }
1729#endif
1730 }
1731 paranoid_fclose(fin);
1732 sprintf(checksum, "%04x%04x", crc16, crctt);
1733 /* BERLIOS: what does it do ??? */
1734 write_header_block_to_stream((off_t)g_current_media_number, checksum,
1735 BLK_STOP_FILE);
1736// log_it("File '%s' written to tape.", infile);
1737 return (retval);
1738}
1739
1740
1741
1742
1743
1744/**
1745 * Write a header block to the opened stream (CD or tape).
1746 * @param length_of_incoming_file The length to store in the header block.
1747 * Usually matters only if this is a file-related header, in which case it should
1748 * be the length of the file that will follow.
1749 * @param filename The filename to store in the header block. Usually matters
1750 * only if this is a file-related header, in which case this should be the name
1751 * if the file that will follow.
1752 * @param control_char The type of header block this is (start-file, end-file, start-tape, ...)
1753 * @return 0 for success, nonzero for failure.
1754 */
1755int
1756write_header_block_to_stream(off_t length_of_incoming_file,
1757 char *filename, int control_char)
1758{
1759 /*@ buffers **************************************************** */
1760 char tempblock[TAPE_BLOCK_SIZE];
1761 char tmp[MAX_STR_LEN];
1762 char *p;
1763
1764 /*@ int ******************************************************** */
1765 int i;
1766
1767 off_t olen;
1768
1769 /*@ end vars *************************************************** */
1770
1771
1772 olen = length_of_incoming_file;
1773 p = strrchr(filename, '/'); /* Make 'em go, "Unnnh!" Oh wait, that was _Master_ P... */
1774 if (!p) {
1775 p = filename;
1776 } else {
1777 p++;
1778 }
1779 if (!g_tape_stream) {
1780 log_to_screen
1781 ("You're not backing up to tape. Why write a tape header?");
1782 return (1);
1783 }
1784 for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
1785 tempblock[i] = 0;
1786 }
1787 sprintf(tempblock + 6000 + control_char, STR_HEADER);
1788 tempblock[7000] = control_char;
1789/* for(i=0;i<8;i++) {tempblock[7001+i]=olen&0xff; olen>>=8;} */
1790 memcpy(tempblock + 7001, (char *) &olen, sizeof(off_t));
1791/* if (length_of_incoming_file) {memcpy(tempblock+7001,(char*)&length_of_incoming_file,sizeof(long long));} */
1792 strcpy(tempblock + 1000, filename);
1793/* strcpy(tempblock+5555,cksum); */
1794 g_tape_posK +=
1795 fwrite(tempblock, 1, (size_t) TAPE_BLOCK_SIZE,
1796 g_tape_stream) / 1024;
1797 sprintf(tmp, "%s (fname=%s, size=%ld K)",
1798 marker_to_string(control_char), p,
1799 (long) length_of_incoming_file >> 10);
1800 log_msg(6, tmp);
1801/* log_tape_pos(); */
1802 return (0);
1803}
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814/**
1815 * Log (to screen) an erroneous marker, along with what it should have been.
1816 * @param should_be What we were expecting.
1817 * @param it_is What we got.
1818 */
1819void wrong_marker(int should_be, int it_is)
1820{
1821 /*@ buffer ***************************************************** */
1822 char tmp[MAX_STR_LEN];
1823
1824
1825 /*@ end vars *************************************************** */
1826 sprintf(tmp, "Wrong marker! (Should be %s, ",
1827 marker_to_string(should_be));
1828 sprintf(tmp + strlen(tmp), "is actually %s)", marker_to_string(it_is));
1829 log_to_screen(tmp);
1830}
1831
1832/* @} - end of streamGroup */
Note: See TracBrowser for help on using the repository browser.