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

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

gettext fixes + last and i int removed (were forgotten)

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