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

Last change on this file since 3870 was 3870, checked in by Bruno Cornec, 2 months ago

Rewrite find_tape_device ans start for other find_*dev functions

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