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

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