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

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

More improvements on tape patch (/dev/st0 has to be the default)

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