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

Last change on this file since 1924 was 1924, checked in by Bruno Cornec, 16 years ago

svn merge -r 1902:1923 $SVN_M/branches/2.2.6

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