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

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

Use of conf file entries (iso_burning_*, media_device, media_size)
and adapatation of the rest of the code to that (including bkpinfo)

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