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

Last change on this file since 1933 was 1933, checked in by Bruno Cornec, 16 years ago

Attempt to fix #249 (adding START and STOP headers systematically for xattr and acl + more precise error messages

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