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

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

Suppress lib-common-externs.h useless

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