source: MondoRescue/branches/3.3/mondo/src/common/libmondo-stream.c@ 3857

Last change on this file since 3857 was 3857, checked in by Bruno Cornec, 4 months ago

find_tape_device_and_size => find_tape_device as size unused

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