source: MondoRescue/branches/stable/mondo/src/common/libmondo-stream.c@ 1424

Last change on this file since 1424 was 1424, checked in by Bruno Cornec, 17 years ago

Some init added

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