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

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