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

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

find_tape_device_and_size => mr_find_tape_device which allocates 1 string to be freed by caller

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