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

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

r3207@localhost: bruno | 2009-07-07 00:32:00 +0200

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