source: MondoRescue/branches/3.3/mondo/src/common/libmondo-stream.c@ 3860

Last change on this file since 3860 was 3860, checked in by Bruno Cornec, 3 months ago

Fix proto for write_header_block_to_stream

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