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

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

Use of conf file entries (iso_burning_*, media_device, media_size)
and adapatation of the rest of the code to that (including bkpinfo)

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