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

Last change on this file since 3857 was 3857, checked in by Bruno Cornec, 4 months ago

find_tape_device_and_size => find_tape_device as size unused

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