source: MondoRescue/branches/2.2.6/mondo/src/common/libmondo-stream.c@ 1956

Last change on this file since 1956 was 1956, checked in by Bruno Cornec, 16 years ago

Use non-rewinding tape name earlier to pass it to mindi so that at restore time we have the right device name for OBDR

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