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

Last change on this file since 1548 was 1548, checked in by Bruno Cornec, 17 years ago
  • Add the possibiilty to edit in interactive mode mtab and device.map for grub
  • Remove blkid cache files after restore to avoid problems in cloning mode
  • Fix what seems to appear a huge number of bugs in hack-fstab (illustration of 1 LOC = 1 bug :-)
  • Especially improve LABEL and UUID support.
  • Should fix #185
  • Exclude_path should be 4*MAX_STR_LEN everywhere. Fixed now.
  • Increasing that value will allow to having larger exclude paths.
  • Should solve bug #137 (and maybe #3 as well)
  • Adds support for fedora 7

(merge -r 1533:1547 $SVN_M/branches/2.2.5)

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