source: MondoRescue/trunk/mondo/mondo/common/libmondo-stream.c@ 146

Last change on this file since 146 was 146, checked in by bcornec, 18 years ago

MONDO_LOGFILE used each time now
memory management on libmondo-devices.c
some splint improvements

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