source: MondoRescue/branches/3.0/mondo/src/common/libmondo-stream.c@ 3060

Last change on this file since 3060 was 3060, checked in by Bruno Cornec, 11 years ago

r5035@localhost: bruno | 2012-11-09 03:17:01 +0100

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