source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-stream.c@ 2520

Last change on this file since 2520 was 2520, checked in by Bruno Cornec, 14 years ago

r3230@localhost (orig r2264): bruno | 2009-07-12 02:04:37 +0200

r3205@localhost: bruno | 2009-07-06 19:46:07 +0200

  • Replace sprintf by mr_asprintf in libmondo-stream.c


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