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

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

Fix a bug for allocation (pointer manipulation again - Thanks again Michel Loiseleur :-)

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