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

Last change on this file since 2405 was 2405, checked in by Bruno Cornec, 15 years ago

Removes some malloc_string static allocation

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