source: MondoRescue/trunk/mondo/mondo/common/libmondo-stream.c@ 122

Last change on this file since 122 was 122, checked in by bcornec, 18 years ago

memory management going on (libmondo-verify.c, libmondo-devices.c, libmondo-string.c, libmondo-stream.c)

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