source: MondoRescue/branches/stable/mondo/src/common/libmondo-stream.c@ 1924

Last change on this file since 1924 was 1924, checked in by Bruno Cornec, 16 years ago

svn merge -r 1902:1923 $SVN_M/branches/2.2.6

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