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

Last change on this file since 1962 was 1962, checked in by Bruno Cornec, 16 years ago
  • rewind tape for OBDR support before positioning to fsf 2
  • export TAPEDEV for post-init and pass options to mondorestore
  • Property svn:keywords set to Id
File size: 51.8 KB
RevLine 
[1]1/* libmondo-stream.c
[128]2 $Id: libmondo-stream.c 1962 2008-05-28 15:22:39Z 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 1962 2008-05-28 15:22:39Z 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");
[1962]687 asprintf(&command, "mt -f %s rewind",bkpinfo->media_device);
688 res = run_program_and_log_output(command, 1);
689 paranoid_free(command);
690
[1954]691 asprintf(&command, "mt -f %s fsf 2",bkpinfo->media_device);
[1955]692 res = run_program_and_log_output(command, 1);
[1949]693 paranoid_free(command);
694
[1954]695 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
[1955]696 return(res);
[1949]697}
698
[1954]699/**
700 * Handle OBDR if we were asked to do so
701 * @param tapedev The tape device to open for writing.
702 * @return 0 for success, nonzero for failure.
703 * @note This should be called ONLY from backup processes. It will OVERWRITE ANY
704 * EXISTING DATA on the tape!
705 */
706int create_obdr()
[1949]707{
708
709 char *command = NULL;
710 int res = 0;
711
[1955]712 log_it("Creating OBDR headers");
[1949]713 /* OBDR: First block 10 kB of zero bs = 512 */
[1954]714 asprintf(&command, "mt -f %s compression off",bkpinfo->media_device);
[1949]715 res = run_program_and_log_output(command, 1);
716 paranoid_free(command);
717
[1954]718 asprintf(&command, "mt -f %s rewind",bkpinfo->media_device);
[1955]719 res += run_program_and_log_output(command, 1);
[1949]720 paranoid_free(command);
721
[1954]722 set_tape_block_size_with_mt(512);
[1949]723
[1954]724 asprintf(&command, "dd if=/dev/zero of=%s bs=512 count=20",bkpinfo->media_device);
[1955]725 res += run_program_and_log_output(command, 1);
[1949]726 paranoid_free(command);
727
728 /* OBDR: then ISO boot image bs = 2048 */
[1954]729 set_tape_block_size_with_mt(2048);
[1949]730
[1954]731 asprintf(&command, "dd if=%s of=%s bs=2048",MINDI_CACHE"/mondorescue.iso",bkpinfo->media_device);
[1955]732 res += run_program_and_log_output(command, 1);
[1949]733 paranoid_free(command);
734
[1954]735 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
[1955]736 return(res);
[1949]737}
738
739
[1]740/**
741 * Open the tape device for input.
742 * @param bkpinfo The backup information structure. Fields used:
743 * - @c bkpinfo->media_device
744 * - @c bkpinfo->tmpdir
745 * @return 0 for success, nonzero for failure.
746 * @note This will also work with a cdstream for now, but don't count on this behavior.
747 */
[1645]748int openin_tape()
[1]749{
[128]750 /*@ buffer ***************************************************** */
751 char fname[MAX_STR_LEN];
752 char *datablock;
[1954]753 char *ntapedev = NULL;
[128]754 char tmp[MAX_STR_LEN];
755 char old_cwd[MAX_STR_LEN];
756 char outfname[MAX_STR_LEN];
757 /*@ int ******************************************************* */
758 int i;
759 int j;
[1955]760 int res = 0;
[128]761 long length, templong;
762 size_t k;
763 int retval = 0;
764 int ctrl_chr;
[1]765
[128]766 /*@ long long ************************************************* */
767 long long size;
[1]768
[128]769 /*@ pointers ************************************************** */
770 FILE *fout;
[1]771
[128]772 /*@ end vars *************************************************** */
[1]773
[128]774 assert_string_is_neither_NULL_nor_zerolength(bkpinfo->media_device);
775 if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
776 fatal_error("Cannot alloc mem for tape catalog");
777 }
778 g_tapecatalog->entries = 0;
779 g_tape_posK = 0;
780 if (g_tape_stream) {
781 log_it("FYI - I won't 'openin' the tape. It's already open.");
782 return (0);
783 }
[1954]784 ntapedev = get_non_rewind_dev(bkpinfo->media_device);
785 if ((bkpinfo->use_obdr) && (ntapedev != NULL)) {
786 strncpy(bkpinfo->media_device,ntapedev,(size_t)(MAX_STR_LEN / 4));
[1955]787 res = skip_obdr();
788 if (res != 0) {
789 log_it("Not able to skip OBDR - Restore will have to be done manually");
790 }
[1952]791 } else {
[1954]792 if (ntapedev == NULL) {
793 log_it("Not able to skip OBDR - Restore will have to be done manually");
794 }
795 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
[1948]796 }
[1954]797 paranoid_free(ntapedev);
[1948]798
[128]799 insist_on_this_tape_number(1);
800 sprintf(outfname, "%s/tmp/all.tar.gz", bkpinfo->tmpdir);
801 make_hole_for_file(outfname);
[1]802
803// start_buffer_process( bkpinfo->media_device, g_tape_fifo, FALSE);
[128]804 log_it("Opening IN tape");
805 if (!
806 (g_tape_stream =
807 open_device_via_buffer(bkpinfo->media_device, 'r',
808 bkpinfo->internal_tape_block_size))) {
809 log_OS_error(g_tape_fifo);
[541]810 log_to_screen("Cannot openin stream device");
[128]811 return (1);
812 }
[541]813 log_to_screen("Reading stream");
[128]814 log_it("stream device = '%s'", bkpinfo->media_device);
[1]815/* skip data disks */
[128]816 open_evalcall_form("Skipping data disks on stream");
[541]817 log_to_screen("Skipping data disks on stream");
[128]818 if (!(fout = fopen(outfname, "w"))) {
819 log_OS_error(outfname);
[541]820 log_to_screen("Cannot openout datadisk all.tar.gz file");
[128]821 return (-1);
[1]822 }
[128]823 if (!(datablock = (char *) malloc(256 * 1024))) {
[541]824 log_to_screen("Unable to malloc 256*1024");
[128]825 exit(1);
826 }
827 for (i = 0; i < 32; i++) {
828 for (j = 0; j < 4; j++) {
829 for (length = 0, k = 0; length < 256 * 1024; length += k) {
830 k = fread(datablock + length, 1, 256 * 1024 - length,
831 g_tape_stream);
832 }
833 (void) fwrite(datablock, 1, (size_t) length, fout);
834 g_tape_posK += length / 1024;
835 }
836 if (i > 8) // otherwise, 'buffer' distorts calculations
837 {
838 templong = ((i - 8) * 4 + j) * 100 / (128 - 8 * 4);
839 update_evalcall_form((int) (templong));
840 }
841 }
842 paranoid_fclose(fout);
843 paranoid_free(datablock);
[1]844/* find initial blocks */
[128]845 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
846 retval += res;
847 if (ctrl_chr != BLK_START_OF_TAPE) {
848 wrong_marker(BLK_START_OF_TAPE, ctrl_chr);
849 }
850 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
851 retval += res;
852 if (ctrl_chr != BLK_START_OF_BACKUP) {
853 wrong_marker(BLK_START_OF_BACKUP, ctrl_chr);
854 }
855 close_evalcall_form();
856 log_it("Saved all.tar.gz to '%s'", outfname);
857 (void) getcwd(old_cwd, MAX_STR_LEN);
858 chdir(bkpinfo->tmpdir);
[1913]859 sprintf(tmp, "tar -zxf %s ./tmp/mondo-restore.cfg 2> /dev/null",
[128]860 outfname);
861 paranoid_system(tmp);
862 paranoid_system("cp -f tmp/mondo-restore.cfg . 2> /dev/null");
863 chdir(old_cwd);
864 unlink(outfname);
865 return (retval);
[1]866}
867
868
869/**
870 * Start writing to a CD stream.
871 * @param cddev The CD device to openout via cdrecord.
872 * @param speed The speed to write at.
873 * @return 0 for success, nonzero for failure.
874 * @note This should be called only from backup processes.
875 */
[128]876int openout_cdstream(char *cddev, int speed)
[1]877{
[128]878 /*@ buffers ***************************************************** */
879 char command[MAX_STR_LEN * 2];
[1]880
[128]881 /*@ end vars *************************************************** */
[1]882
883/* add 'dummy' if testing */
[128]884 sprintf(command,
885 "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s",
886 cddev, speed, MONDO_LOGFILE, MONDO_LOGFILE);
[1]887/* initialise the catalog */
[128]888 g_current_media_number = 1;
889 if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
890 fatal_error("Cannot alloc mem for tape catalog");
891 }
892 g_tapecatalog->entries = 0;
[1]893/* log stuff */
[128]894 log_it("Opening OUT cdstream with the command");
895 log_it(command);
[1]896/* log_it("Let's see what happens, shall we?"); */
[128]897 g_tape_stream = popen(command, "w");
898 if (g_tape_stream) {
899 return (0);
900 } else {
[541]901 log_to_screen("Failed to openout to cdstream (fifo)");
[128]902 return (1);
903 }
[1]904}
905
906/**
907 * Start writing to a tape device for the backup.
[1948]908 * Handle OBDR if we were asked to do so
[1]909 * @param tapedev The tape device to open for writing.
910 * @return 0 for success, nonzero for failure.
911 * @note This should be called ONLY from backup processes. It will OVERWRITE ANY
912 * EXISTING DATA on the tape!
913 */
[1954]914int openout_tape() {
[1]915
[1954]916g_current_media_number = 1;
917if (g_tape_stream) {
918 log_it("FYI - I won't 'openout' the tape. It's already open.");
[128]919 return (0);
[1]920}
[1954]921if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
922 fatal_error("Cannot alloc mem for tape catalog");
923}
924g_tapecatalog->entries = 0;
925g_tape_posK = 0;
[1]926
[1956]927if (bkpinfo->use_obdr) {
[1954]928 create_obdr();
929} else {
930 set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
931}
932log_it("Opening OUT tape");
933if (!
934 (g_tape_stream =
[1955]935 open_device_via_buffer(bkpinfo->media_device, 'w', bkpinfo->internal_tape_block_size))) {
[1954]936 log_OS_error(g_tape_fifo);
937 log_to_screen("Cannot openin stream device");
938 return (1);
939}
940return (0);
941}
[1]942
943
944
[1954]945
[1]946/**
947 * Copy a file from the opened stream (CD or tape) to @p outfile.
948 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
949 * @param outfile The file to write to.
950 * @param size The size of the file in the input stream.
951 * @return 0 for success, nonzero for failure.
952 */
953int
[1645]954read_file_from_stream_to_file(char *outfile, long long size)
[1]955{
956
[128]957 /*@ int ******************************************************** */
958 int res;
[1]959
[128]960 /*@ end vars *************************************************** */
[1]961
[1645]962 res = read_file_from_stream_FULL(outfile, NULL, size);
[1]963
[128]964 return (res);
[1]965}
966
967
968
969/**
970 * Copy a file from the currently opened stream (CD or tape) to the stream indicated
971 * by @p fout.
972 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
973 * @param fout The stream to write the file to.
974 * @param size The size of the file in bytes.
975 * @return 0 for success, nonzero for failure.
976 */
977int
[1645]978read_file_from_stream_to_stream(FILE * fout, long long size)
[1]979{
980
[128]981 /*@ int ******************************************************** */
982 int res;
[1]983
[128]984 /*@ end vars *************************************************** */
[1]985
[1645]986 res = read_file_from_stream_FULL(NULL, fout, size);
[1]987/* fflush(g_tape_stream);
988 fflush(fout);*/
[128]989 return (res);
[1]990}
991
992
993
994/**
995 * Copy a file from the currently opened stream (CD or tape) to either a
996 * @c FILE pointer indicating a currently-opened file, or a filename indicating
997 * a new file to create. This is the backbone function that read_file_from_stream_to_file()
998 * and read_file_from_stream_to_stream() are wrappers for.
999 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
1000 * @param outfname If non-NULL, write to this file.
1001 * @param foutstream If non-NULL, write to this stream.
1002 * @param orig_size The original length of the file in bytes.
1003 * @return 0 for success, nonzero for failure.
1004 * @note Only @b one of @p outfname or @p foutstream may be non-NULL.
1005 */
1006int
[1645]1007read_file_from_stream_FULL(char *outfname, FILE * foutstream, long long orig_size)
[1]1008{
[128]1009 /*@ buffers ***************************************************** */
1010 char *tmp;
1011 char *datablock;
1012 char *temp_fname;
1013 char *temp_cksum;
1014 char *actual_cksum;
[1]1015// char *pA, *pB;
1016
[128]1017 /*@ int ********************************************************* */
1018 int retval = 0;
[1]1019#ifdef EXTRA_TAPE_CHECKSUMS
[128]1020 int i, ch;
[1]1021#endif
[128]1022 int noof_blocks;
1023 int ctrl_chr;
1024 int res;
1025 /*@ pointers **************************************************** */
1026 FILE *fout;
[1]1027
[128]1028 /*@ long ***************************************************** */
[1899]1029 long bytes_to_write = 0L /*,i */ ;
[1]1030// long bytes_successfully_read_in_this_time = 0;
1031
[128]1032 /*@ long long *************************************************** */
1033 long long temp_size, size;
1034 long bytes_read, bytes_to_read;
1035 long long total_read_from_tape_for_this_file = 0;
1036 long long where_I_was_before_tape_change = 0;
1037 /*@ unsigned int ************************************************ */
1038 /* unsigned int ch; */
1039 unsigned int crc16;
1040 unsigned int crctt;
[1]1041
[128]1042 bool had_to_resync = FALSE;
[1]1043
[128]1044 /*@ init ******************************************************* */
1045 malloc_string(tmp);
1046 malloc_string(temp_fname);
1047 malloc_string(temp_cksum);
1048 malloc_string(actual_cksum);
1049 datablock = malloc(TAPE_BLOCK_SIZE);
1050 crc16 = 0;
1051 crctt = 0;
1052 size = orig_size;
[1]1053
[128]1054 /*@ end vars *************************************************** */
[1]1055
[128]1056 res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
1057 if (orig_size != temp_size && orig_size != -1) {
1058 sprintf(tmp,
1059 "output file's size should be %ld K but is apparently %ld K",
1060 (long) size >> 10, (long) temp_size >> 10);
1061 log_to_screen(tmp);
1062 }
1063 if (ctrl_chr != BLK_START_FILE) {
1064 wrong_marker(BLK_START_FILE, ctrl_chr);
1065 return (1);
1066 }
[1881]1067 /* Not used
[128]1068 sprintf(tmp, "Reading file from tape; writing to '%s'; %ld KB",
1069 outfname, (long) size >> 10);
[1881]1070 */
[1]1071
[128]1072 if (foutstream) {
1073 fout = foutstream;
1074 } else {
1075 fout = fopen(outfname, "w");
[1]1076 }
[128]1077 if (!fout) {
1078 log_OS_error(outfname);
[541]1079 log_to_screen("Cannot openout file");
[128]1080 return (1);
1081 }
1082 total_read_from_tape_for_this_file = 0;
1083 for (noof_blocks = 0; size > 0;
1084 noof_blocks++, size -=
1085 bytes_to_write, total_read_from_tape_for_this_file +=
1086 bytes_read) {
1087 bytes_to_write =
1088 (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
1089 bytes_to_read = TAPE_BLOCK_SIZE;
1090 bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
1091 while (bytes_read < bytes_to_read) { // next tape!
1092// crctt=crc16=0;
1093 where_I_was_before_tape_change = size;
1094 log_msg(4, "where_I_was_... = %lld",
1095 where_I_was_before_tape_change);
[1645]1096 start_to_read_from_next_tape();
[128]1097 log_msg(4, "Started reading from next tape.");
1098 skip_incoming_files_until_we_find_this_one(temp_fname);
1099 log_msg(4, "Skipped irrelevant files OK.");
1100 for (size = orig_size; size > where_I_was_before_tape_change;
1101 size -= bytes_to_write) {
1102 bytes_read =
1103 fread(datablock, 1, bytes_to_read, g_tape_stream);
1104 }
1105 log_msg(4, "'size' is now %lld (should be %lld)", size,
1106 where_I_was_before_tape_change);
1107 log_to_screen("Successfully re-sync'd tape");
1108 had_to_resync = TRUE;
1109 bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
1110 }
[1]1111
[128]1112 (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]1113
1114#ifdef EXTRA_TAPE_CHECKSUMS
[128]1115 for (i = 0; i < (int) bytes_to_write; i++) {
1116 ch = datablock[i];
1117 crc16 = updcrcr(crc16, (unsigned) ch);
1118 crctt = updcrc(crctt, (unsigned) ch);
1119 }
[1]1120#endif
[128]1121 }
1122 log_msg(6, "Total read from tape for this file = %lld",
1123 total_read_from_tape_for_this_file);
1124 log_msg(6, ".......................... Should be %lld", orig_size);
1125 g_tape_posK += total_read_from_tape_for_this_file / 1024;
1126 sprintf(actual_cksum, "%04x%04x", crc16, crctt);
1127 if (foutstream) { /*log_it("Finished writing to foutstream"); */
1128 } else {
1129 paranoid_fclose(fout);
1130 }
1131 res = read_header_block_from_stream(&temp_size, temp_cksum, &ctrl_chr);
1132 if (ctrl_chr != BLK_STOP_FILE) {
1133 wrong_marker(BLK_STOP_FILE, ctrl_chr);
[1]1134// fatal_error("Bad marker"); // return(1);
[128]1135 }
1136 if (strcmp(temp_cksum, actual_cksum)) {
[541]1137 sprintf(tmp, "actual cksum=%s; recorded cksum=%s", actual_cksum,
[128]1138 temp_cksum);
1139 log_to_screen(tmp);
[541]1140 sprintf(tmp, "%s (%ld K) is corrupt on tape", temp_fname,
[128]1141 (long) orig_size >> 10);
1142 log_to_screen(tmp);
1143 retval++;
1144 } else {
1145 sprintf(tmp, "%s is GOOD on tape", temp_fname);
1146 /* log_it(tmp); */
1147 }
1148 paranoid_free(datablock);
1149 paranoid_free(tmp);
1150 paranoid_free(temp_fname);
1151 paranoid_free(temp_cksum);
1152 paranoid_free(actual_cksum);
1153 return (retval);
[1]1154}
1155
1156
1157
1158/**
1159 * Read a header block from the currently opened stream (CD or tape).
1160 * This block indicates the length of the following file (if it's file-related)
1161 * the filename (if it's file-related), and the block type.
1162 * @param plen Where to put the length of the file. Valid only for file-related header blocks.
1163 * @param filename Where to put the name of the file. Valid only for file-related header blocks.
1164 * @param pcontrol_char Where to put the type of block (e.g. start-file, end-file, start-tape, ...)
1165 * @return 0 for success, nonzero for failure.
1166 * @note If you read a marker (@p pcontrol_char) you're not expecting, you can call wrong_marker().
1167 */
1168int
[128]1169read_header_block_from_stream(long long *plen, char *filename,
1170 int *pcontrol_char)
[1]1171{
1172
[128]1173 /*@ buffers ***************************************************** */
1174 char *tempblock;
[1]1175
[128]1176 /*@ int ********************************************************* */
1177 int i, retval;
[1]1178
[128]1179 /*@ end vars *************************************************** */
[1]1180
[128]1181 tempblock = (char *) malloc((size_t) TAPE_BLOCK_SIZE);
[1]1182
[128]1183 for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
1184 tempblock[i] = 0;
1185 }
1186 while (!(*pcontrol_char = tempblock[7000])) {
1187 g_tape_posK +=
1188 fread(tempblock, 1, (size_t) TAPE_BLOCK_SIZE,
1189 g_tape_stream) / 1024;
1190 }
[1]1191/* memcpy((char*)plength_of_incoming_file,(char*)tempblock+7001,sizeof(long long)); */
1192/* for(*plen=0,i=7;i>=0;i--) {*plen<<=8; *plen |= tempblock[7001+i];} */
[128]1193 memcpy((char *) plen, tempblock + 7001, sizeof(long long));
[1934]1194 if (strcmp(tempblock + 6000 + *pcontrol_char, STR_HEADER)) {
[128]1195 log_it("Bad header block at %ld K", (long) g_tape_posK);
1196 }
1197 strcpy(filename, tempblock + 1000);
[1]1198/* strcpy(cksum,tempblock+5555);*/
1199/* log_it( "%s (reading) fname=%s, filesize=%ld K",
1200 marker_to_string (*pcontrol_char), filename,
1201 (long) ((*plen) >> 10));
1202*/
[128]1203 if (*pcontrol_char == BLK_ABORTED_BACKUP) {
1204 log_to_screen("I can't verify an aborted backup.");
1205 retval = 1;
1206 } else {
1207 retval = 0;
1208 }
1209 for (i = 1000; i < 1020; i++) {
1210 if (tempblock[i] < 32 || tempblock[i] > 126) {
1211 tempblock[i] = ' ';
1212 }
1213 }
1214 tempblock[i] = '\0';
1215 log_msg(6, "%s (fname=%s, size=%ld K)",
1216 marker_to_string(*pcontrol_char), tempblock + 1000,
1217 (long) (*plen) >> 10);
1218 paranoid_free(tempblock);
1219 return (retval);
[1]1220}
1221
1222
1223
1224/**
1225 * Add specified file/slice to the internal catalog of all archives written.
1226 * This lets us restart on a new CD/tape/whatever if it runs out of room. We just
1227 * write the last [buffer size] MB from the catalog to the new tape, so we know
1228 * we have @e all archives on some CD/tape/whatever.
1229 * @param type The type of file we're cataloging (afioball, slice, something else)
1230 * @param number The fileset number or biggiefile number.
1231 * @param aux The slice number if it's a biggiefile, or any other additional info.
1232 * @param fn The original full pathname of the file we're recording.
1233 * @return The index of the record we just added.
1234 */
[128]1235int register_in_tape_catalog(t_archtype type, int number, long aux,
1236 char *fn)
[1]1237{
[128]1238 int last;
1239 char fname[MAX_TAPECAT_FNAME_LEN];
1240 char *p;
[1]1241
[128]1242 p = strrchr(fn, '/');
1243 if (p) {
1244 p++;
1245 } else {
1246 p = fn;
1247 }
1248 strncpy(fname, p, MAX_TAPECAT_FNAME_LEN);
1249 fname[MAX_TAPECAT_FNAME_LEN] = '\0';
1250 last = g_tapecatalog->entries;
1251 if (last >= MAX_TAPECATALOG_ENTRIES) {
1252 log_it
1253 ("Warning - can't log #%d in tape catalog - too many entries already",
1254 number);
1255 return (-1);
1256 }
1257 g_tapecatalog->el[last].type = type;
1258 g_tapecatalog->el[last].number = number;
1259 g_tapecatalog->el[last].aux = aux;
1260 g_tapecatalog->el[last].tape_posK = g_tape_posK;
1261 strcpy(g_tapecatalog->el[last].fname, fname);
1262 g_tapecatalog->entries++;
1263 return (last); // returns the index of the record we've jsut added
[1]1264}
1265
1266
1267
1268
1269/**
1270 * Decide whether we should start a new tape. This is TRUE if we've run out of tape
1271 * (got SIGPIPE) or look like we will.
1272 * @param mediasize The size of the tape in megabytes.
1273 * @param length_of_incoming_file The length of the file we're about to write, in bytes.
1274 * @bug This seems like it'll only work for media_size != autodetect, but Mondo only allows
1275 * autodetecting the size. Huh?
1276 */
[684]1277
1278/* BERLIOS: Should be reviewed for mediasize being a off_t ??? */
[1]1279bool
[128]1280should_we_write_to_next_tape(long mediasize,
[684]1281 off_t length_of_incoming_file)
[1]1282{
[128]1283 /*@ bool's ***************************************************** */
1284 bool we_need_a_new_tape = FALSE;
[1]1285
[128]1286 /*@ end vars *************************************************** */
[1]1287
[128]1288 if (mediasize == 0) {
1289 return (FALSE);
1290 }
1291 if (mediasize > 0 && (g_tape_posK >> 10 >= mediasize)) {
1292 log_it("mediasize = %ld", mediasize);
1293 we_need_a_new_tape = TRUE;
[541]1294 log_to_screen("Should have started a new tape/CD already");
[128]1295 }
1296 if ((g_tape_posK + length_of_incoming_file / 1024) >> 10 >=
1297 mediasize - (SLICE_SIZE * 4 / 1024)) {
1298 log_it("g_tape_posK = %ld\nmediasize = %ld\n", g_tape_posK,
1299 mediasize);
1300 we_need_a_new_tape = TRUE;
1301 }
1302 return (we_need_a_new_tape);
[1]1303}
1304
1305
1306/**
1307 * Seek through the stream until we find a header block where the NAME field matches
1308 * @p the_file_I_was_reading. This is useful if you've just started reading from
1309 * a new tape and want to find the file you were reading when the tape ended.
1310 * @param the_file_I_was_reading File name to look for.
1311 * @return 0 for success, nonzero for failure.
1312 */
[128]1313int skip_incoming_files_until_we_find_this_one(char
1314 *the_file_I_was_reading)
[1]1315{
[128]1316 char *pA;
1317 char *pB;
1318 int res;
1319 int ctrl_chr;
1320 char *temp_fname;
1321 char *datablock;
1322 long long temp_size, size;
1323 long bytes_to_write;
[1]1324
[128]1325 datablock = malloc(TAPE_BLOCK_SIZE);
1326 malloc_string(temp_fname);
1327 pB = strrchr(the_file_I_was_reading, '/');
1328 if (pB) {
1329 pB++;
1330 } else {
1331 pB = the_file_I_was_reading;
[1]1332 }
[128]1333 log_msg(1, "skip_incoming_..(%s)", pB);
1334 log_msg(2, "Looking for initial START_AN_AFIO_OR_SLICE");
1335 ctrl_chr = -1;
1336 while (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1337 res =
1338 read_header_block_from_stream(&temp_size, temp_fname,
1339 &ctrl_chr);
1340 if (ctrl_chr == BLK_START_AN_AFIO_OR_SLICE) {
1341 break;
1342 }
1343 log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
1344 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1345 log_msg(3, "Still trying to re-sync w/ tape");
1346 }
1347 while (ctrl_chr != BLK_START_FILE) {
1348 res =
1349 read_header_block_from_stream(&temp_size, temp_fname,
1350 &ctrl_chr);
1351 if (ctrl_chr == BLK_START_FILE) {
1352 break;
1353 }
1354 log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
1355 wrong_marker(BLK_START_FILE, ctrl_chr);
1356 log_msg(3, "Still trying to re-sync w/ tape");
1357 }
1358 pA = strrchr(temp_fname, '/');
1359 if (pA) {
1360 pA++;
1361 } else {
1362 pA = temp_fname;
1363 }
1364 pB = strrchr(the_file_I_was_reading, '/');
1365 if (pB) {
1366 pB++;
1367 } else {
1368 pB = the_file_I_was_reading;
1369 }
1370 while (strcmp(pA, pB)) {
1371 log_msg(6, "Skipping %s (it's not %s)", temp_fname,
1372 the_file_I_was_reading);
1373 for (size = temp_size; size > 0; size -= bytes_to_write) {
1374 bytes_to_write =
1375 (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
1376 // FIXME - needs error-checking and -catching
1377 fread(datablock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream);
1378 }
1379 res =
1380 read_header_block_from_stream(&temp_size, temp_fname,
1381 &ctrl_chr);
1382 if (ctrl_chr != BLK_STOP_FILE) {
1383 wrong_marker(BLK_STOP_FILE, ctrl_chr);
1384 }
1385 res =
1386 read_header_block_from_stream(&temp_size, temp_fname,
1387 &ctrl_chr);
1388 if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
1389 wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
1390 }
1391 res =
1392 read_header_block_from_stream(&temp_size, temp_fname,
1393 &ctrl_chr);
1394 if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1395 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1396 }
1397 res =
1398 read_header_block_from_stream(&temp_size, temp_fname,
1399 &ctrl_chr);
1400 if (ctrl_chr != BLK_START_FILE) {
1401 wrong_marker(BLK_START_FILE, ctrl_chr);
1402 }
1403 pA = strrchr(temp_fname, '/');
1404 if (pA) {
1405 pA++;
1406 } else {
1407 pA = temp_fname;
1408 }
1409 pB = strrchr(the_file_I_was_reading, '/');
1410 if (pB) {
1411 pB++;
1412 } else {
1413 pB = the_file_I_was_reading;
1414 }
1415 }
1416 log_msg(2, "Reading %s (it matches %s)", temp_fname,
1417 the_file_I_was_reading);
1418 paranoid_free(temp_fname);
1419 paranoid_free(datablock);
1420 return (0);
[1]1421}
1422
1423
1424/**
1425 * Start to read from the next tape. Assumes the user has already inserted it.
1426 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
1427 * @return 0 for success, nonzero for failure.
1428 */
[1645]1429int start_to_read_from_next_tape()
[1]1430{
[128]1431 /*@ int ********************************************************* */
1432 int res = 0;
1433 char *sz_msg;
1434 int ctrlchr;
1435 long long temp_size;
1436 malloc_string(sz_msg);
1437 /*@ end vars *************************************************** */
[1]1438
[128]1439 paranoid_pclose(g_tape_stream);
[1934]1440 sync();
1441 sync();
1442 sync();
[128]1443 log_it("Next tape requested.");
1444 insist_on_this_tape_number(g_current_media_number + 1); // will increment it, too
1445 log_it("Opening IN the next tape");
1446 if (!
1447 (g_tape_stream =
1448 open_device_via_buffer(bkpinfo->media_device, 'r',
1449 bkpinfo->internal_tape_block_size))) {
1450 log_OS_error(g_tape_fifo);
[541]1451 log_to_screen("Cannot openin stream device");
[128]1452 return (1);
1453 }
1454 g_tape_posK = 0;
1455 g_sigpipe = FALSE;
1456 res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
1457 if (ctrlchr != BLK_START_OF_TAPE) {
1458 wrong_marker(BLK_START_OF_TAPE, ctrlchr);
1459 }
1460 res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
1461 if (ctrlchr != BLK_START_OF_BACKUP) {
1462 wrong_marker(BLK_START_OF_BACKUP, ctrlchr);
1463 } else {
1464 log_msg(3, "Next tape opened OK. Whoopee!");
1465 }
1466 paranoid_free(sz_msg);
1467 return (res);
[1]1468}
1469
1470
1471
1472/**
1473 * Start to write to the next tape. Assume the user has already inserted it.
1474 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
1475 * @return 0 for success, nonzero for failure.
1476 */
[1645]1477int start_to_write_to_next_tape()
[1]1478{
[128]1479 int res = 0;
1480 char command[MAX_STR_LEN * 2];
1481 paranoid_pclose(g_tape_stream);
1482 system("sync");
1483 system("sync");
1484 system("sync");
1485 log_it("New tape requested.");
1486 insist_on_this_tape_number(g_current_media_number + 1); // will increment g_current_media, too
1487 if (g_current_media_number > MAX_NOOF_MEDIA) {
1488 res++;
[541]1489 log_to_screen("Too many tapes. Man, you need to use nfs!");
[1]1490 }
[128]1491 if (bkpinfo->backup_media_type == cdstream) {
1492 sprintf(command,
1493 "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s",
1494 bkpinfo->media_device, bkpinfo->cdrw_speed, MONDO_LOGFILE,
1495 MONDO_LOGFILE);
1496 log_it("Opening OUT to next CD with the command");
1497 log_it(command);
1498 log_it("Let's see what happens, shall we?");
1499 g_tape_stream = popen(command, "w");
1500 if (!g_tape_stream) {
[541]1501 log_to_screen("Failed to openout to cdstream (fifo)");
[128]1502 return (1);
1503 }
1504 } else {
1505 log_it("Opening OUT to next tape");
1506 if (!
1507 (g_tape_stream =
1508 open_device_via_buffer(bkpinfo->media_device, 'w',
1509 bkpinfo->internal_tape_block_size))) {
1510 log_OS_error(g_tape_fifo);
[541]1511 log_to_screen("Cannot openin stream device");
[128]1512 return (1);
1513 }
[1]1514 }
[128]1515 g_tape_posK = 0;
1516 g_sigpipe = FALSE;
[684]1517 res += write_header_block_to_stream((off_t)0, "start-of-tape", BLK_START_OF_TAPE); /* just in case */
1518 res += write_header_block_to_stream((off_t)0, "start-of-backup", BLK_START_OF_BACKUP); /* just in case */
[128]1519 return (res);
[1]1520}
1521
1522
1523
1524
1525/**
1526 * Write a bufferfull of the most recent archives to the tape. The
1527 * rationale behind this is a bit complex. If the previous tape ended
1528 * suddenly (EOF or otherwise) some archives were probably left in the
1529 * buffer. That means that Mondo thinks they have been written, but
1530 * the external binary @c buffer has not actually written them. So to
1531 * be safe, we start the new tape by writing the last bufferfull from
1532 * the old one. This insures that all archives will be on at least
1533 * one tape. Sounds inelegant, but it works.
1534 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir is the only field used.
1535 * @return 0 for success, nonzero for failure.
1536 */
[1645]1537int write_backcatalog_to_tape()
[1]1538{
[128]1539 int i, last, res = 0;
1540 char *fname;
[1]1541
[128]1542 log_msg(2, "I am now writing back catalog to tape");
1543 malloc_string(fname);
1544 last = g_tapecatalog->entries - 1;
1545 for (i = 0; i <= last; i++) {
1546 sprintf(fname, "%s/tmpfs/backcatalog/%s", bkpinfo->tmpdir,
1547 g_tapecatalog->el[i].fname);
1548 if (!does_file_exist(fname)) {
1549 log_msg(6, "Can't write %s - it doesn't exist.", fname);
1550 } else {
1551 write_header_block_to_stream(length_of_file(fname),
1552 "start-backcatalog-afio-or-slice",
1553 BLK_START_AN_AFIO_OR_SLICE);
1554 log_msg(2, "Writing %s", fname);
[1645]1555 if (write_file_to_stream_from_file(fname)) {
[128]1556 res++;
1557 log_msg(2, "%s failed", fname);
1558 }
1559 if (i != last) {
[684]1560 write_header_block_to_stream((off_t)0,
[128]1561 "stop-backcatalog-afio-or-slice",
1562 BLK_STOP_AN_AFIO_OR_SLICE);
1563 }
1564 }
1565 }
1566 paranoid_free(fname);
1567 log_msg(2, "Finished writing back catalog to tape");
1568 return (res);
[1]1569}
1570
1571
1572
1573/**
1574 * Write all.tar.gz (produced by Mindi) to the first 32M of the first tape.
1575 * @param fname The path to all.tar.gz.
1576 * @return 0 for success, nonzero for failure.
1577 */
[128]1578int write_data_disks_to_stream(char *fname)
[1]1579{
[128]1580 /*@ pointers *************************************************** */
1581 FILE *fin;
1582 char tmp[MAX_STR_LEN];
[1]1583
[128]1584 /*@ long ******************************************************* */
1585 long m = -1;
1586 long templong;
[1]1587
[128]1588 /*@ int ******************************************************** */
1589 int i, j;
[1]1590
[128]1591 /*@ buffers **************************************************** */
1592 char tempblock[256 * 1024];
[1]1593
[128]1594 /*@ end vars *************************************************** */
[1]1595
[541]1596 open_evalcall_form("Writing data disks to tape");
1597 log_to_screen("Writing data disks to tape");
[128]1598 log_it("Data disks = %s", fname);
1599 if (!does_file_exist(fname)) {
[541]1600 sprintf(tmp, "Cannot find %s", fname);
[128]1601 log_to_screen(tmp);
1602 return (1);
[1]1603 }
[128]1604 if (!(fin = fopen(fname, "r"))) {
1605 log_OS_error(fname);
1606 fatal_error("Cannot openin the data disk");
1607 }
1608 for (i = 0; i < 32; i++) { /* 32MB */
1609 for (j = 0; j < 4; j++) { /* 256K x 4 = 1MB (1024K) */
1610 if (!feof(fin)) {
1611 m = (long) fread(tempblock, 1, 256 * 1024, fin);
1612 } else {
1613 m = 0;
1614 }
1615 for (; m < 256 * 1024; m++) {
1616 tempblock[m] = '\0';
1617 }
1618 g_tape_posK +=
1619 fwrite(tempblock, 1, 256 * 1024, g_tape_stream) / 1024;
1620 }
1621 if (i > g_tape_buffer_size_MB) // otherwise, 'buffer' distorts calculations
1622 {
1623 templong = ((i - 8) * 4 + j) * 100 / (128 - 8 * 4);
1624 update_evalcall_form((int) (templong));
1625 }
1626 }
1627 paranoid_fclose(fin);
1628 close_evalcall_form();
1629 return (0);
[1]1630}
1631
1632
1633
1634
1635/**
1636 * Copy @p infile to the opened stream (CD or tape).
1637 * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
1638 * @param infile The file to write to the stream.
1639 * @return 0 for success, nonzero for failure.
1640 */
[1645]1641int write_file_to_stream_from_file(char *infile)
[1]1642{
[128]1643 /*@ buffers **************************************************** */
1644 char tmp[MAX_STR_LEN];
1645 char datablock[TAPE_BLOCK_SIZE];
1646 char checksum[MAX_STR_LEN];
1647 char *infile_basename;
[1]1648
[128]1649 /*@ int ******************************************************** */
1650 int retval = 0;
1651 int noof_blocks;
[1]1652
[128]1653 /* unsigned int ch; */
1654 unsigned int crc16;
1655 unsigned int crctt;
[1]1656
[128]1657 /*@ pointers *************************************************** */
1658 FILE *fin;
1659 char *p;
[1]1660
[128]1661 /*@ long ******************************************************* */
1662 long bytes_to_read = 0;
1663 long i;
[1]1664
[684]1665 off_t filesize;
[1]1666
1667#ifdef EXTRA_TAPE_CHECKSUMS
[128]1668 int ch;
[1]1669#endif
1670
[128]1671 /*@ initialize ************************************************ */
1672 crc16 = 0;
1673 crctt = 0;
[1]1674
1675
1676
[128]1677 /*@ end vars *************************************************** */
[1]1678
[128]1679 infile_basename = strrchr(infile, '/');
1680 if (infile_basename) {
1681 infile_basename++;
1682 } else {
1683 infile_basename = infile;
1684 }
1685 filesize = length_of_file(infile);
1686 if (should_we_write_to_next_tape
1687 (bkpinfo->media_size[g_current_media_number], filesize)) {
[1645]1688 start_to_write_to_next_tape();
1689 write_backcatalog_to_tape();
[128]1690 }
1691 p = strrchr(infile, '/');
1692 if (!p) {
1693 p = infile;
1694 } else {
1695 p++;
1696 }
1697 sprintf(tmp, "Writing file '%s' to tape (%ld KB)", p,
1698 (long) filesize >> 10);
1699 log_it(tmp);
1700 write_header_block_to_stream(filesize, infile_basename,
1701 BLK_START_FILE);
[1]1702//go_here_to_restart_saving_of_file:
[128]1703 if (!(fin = fopen(infile, "r"))) {
1704 log_OS_error(infile);
1705 return (1);
[1]1706 }
[128]1707 for (noof_blocks = 0; filesize > 0;
1708 noof_blocks++, filesize -= bytes_to_read) {
1709 if (filesize < TAPE_BLOCK_SIZE) {
1710 bytes_to_read = (long) filesize;
1711 for (i = 0; i < TAPE_BLOCK_SIZE; i++) {
1712 datablock[i] = '\0';
1713 }
1714 } else {
1715 bytes_to_read = TAPE_BLOCK_SIZE;
1716 }
1717 (void) fread(datablock, 1, (size_t) bytes_to_read, fin);
1718 g_tape_posK +=
1719 fwrite(datablock, 1, /*bytes_to_read */
1720 (size_t) TAPE_BLOCK_SIZE,
1721 g_tape_stream) / 1024;
1722 if (g_sigpipe) {
1723 iamhere("Sigpipe occurred recently. I'll start a new tape.");
1724 fclose(fin);
1725 g_sigpipe = FALSE;
[1645]1726 start_to_write_to_next_tape();
1727 write_backcatalog_to_tape(); // kinda-sorta recursive :)
[128]1728 return (0);
1729 }
[1]1730#ifdef EXTRA_TAPE_CHECKSUMS
[128]1731 for (i = 0; i < bytes_to_read; i++) {
1732 ch = datablock[i];
1733 crc16 = updcrcr(crc16, (unsigned) ch);
1734 crctt = updcrc(crctt, (unsigned) ch);
1735 }
[1]1736#endif
[128]1737 }
1738 paranoid_fclose(fin);
1739 sprintf(checksum, "%04x%04x", crc16, crctt);
[684]1740 /* BERLIOS: what does it do ??? */
1741 write_header_block_to_stream((off_t)g_current_media_number, checksum,
[128]1742 BLK_STOP_FILE);
[1]1743// log_it("File '%s' written to tape.", infile);
[128]1744 return (retval);
[1]1745}
1746
1747
1748
1749
1750
1751/**
1752 * Write a header block to the opened stream (CD or tape).
1753 * @param length_of_incoming_file The length to store in the header block.
1754 * Usually matters only if this is a file-related header, in which case it should
1755 * be the length of the file that will follow.
1756 * @param filename The filename to store in the header block. Usually matters
1757 * only if this is a file-related header, in which case this should be the name
1758 * if the file that will follow.
1759 * @param control_char The type of header block this is (start-file, end-file, start-tape, ...)
1760 * @return 0 for success, nonzero for failure.
1761 */
1762int
[684]1763write_header_block_to_stream(off_t length_of_incoming_file,
[128]1764 char *filename, int control_char)
[1]1765{
[128]1766 /*@ buffers **************************************************** */
1767 char tempblock[TAPE_BLOCK_SIZE];
1768 char tmp[MAX_STR_LEN];
1769 char *p;
[1]1770
[128]1771 /*@ int ******************************************************** */
1772 int i;
[1]1773
[684]1774 off_t olen;
[1]1775
[128]1776 /*@ end vars *************************************************** */
[1]1777
1778
[128]1779 olen = length_of_incoming_file;
1780 p = strrchr(filename, '/'); /* Make 'em go, "Unnnh!" Oh wait, that was _Master_ P... */
1781 if (!p) {
1782 p = filename;
1783 } else {
1784 p++;
1785 }
1786 if (!g_tape_stream) {
1787 log_to_screen
[541]1788 ("You're not backing up to tape. Why write a tape header?");
[128]1789 return (1);
1790 }
1791 for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
1792 tempblock[i] = 0;
1793 }
[1934]1794 sprintf(tempblock + 6000 + control_char, STR_HEADER);
[128]1795 tempblock[7000] = control_char;
[1]1796/* for(i=0;i<8;i++) {tempblock[7001+i]=olen&0xff; olen>>=8;} */
[684]1797 memcpy(tempblock + 7001, (char *) &olen, sizeof(off_t));
[1]1798/* if (length_of_incoming_file) {memcpy(tempblock+7001,(char*)&length_of_incoming_file,sizeof(long long));} */
[128]1799 strcpy(tempblock + 1000, filename);
[1]1800/* strcpy(tempblock+5555,cksum); */
[128]1801 g_tape_posK +=
1802 fwrite(tempblock, 1, (size_t) TAPE_BLOCK_SIZE,
1803 g_tape_stream) / 1024;
1804 sprintf(tmp, "%s (fname=%s, size=%ld K)",
1805 marker_to_string(control_char), p,
1806 (long) length_of_incoming_file >> 10);
1807 log_msg(6, tmp);
[1]1808/* log_tape_pos(); */
[128]1809 return (0);
[1]1810}
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821/**
1822 * Log (to screen) an erroneous marker, along with what it should have been.
1823 * @param should_be What we were expecting.
1824 * @param it_is What we got.
1825 */
[128]1826void wrong_marker(int should_be, int it_is)
[1]1827{
[128]1828 /*@ buffer ***************************************************** */
1829 char tmp[MAX_STR_LEN];
[1]1830
1831
[128]1832 /*@ end vars *************************************************** */
[541]1833 sprintf(tmp, "Wrong marker! (Should be %s, ",
[128]1834 marker_to_string(should_be));
[541]1835 sprintf(tmp + strlen(tmp), "is actually %s)", marker_to_string(it_is));
[128]1836 log_to_screen(tmp);
[1]1837}
1838
1839/* @} - end of streamGroup */
Note: See TracBrowser for help on using the repository browser.