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

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

Merger memory management from trunk for libmondo-raid.c
Additional check for mr_allocstr

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