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

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

Continue to use configuration file data (may not compile)

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