source: MondoRescue/trunk/mondo/src/common/libmondo-stream.c@ 1086

Last change on this file since 1086 was 1086, checked in by Bruno Cornec, 17 years ago

log_msg => mr_msg in trunk

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