source: MondoRescue/branches/3.1/mondo/src/common/libmondo-stream.c@ 3147

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