source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-stream.c@ 2332

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