source: MondoRescue/branches/2.2.6/mondo/src/common/libmondo-stream.c@ 1934

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

Handle part of #250

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