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

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

Some merges from stable (synchro for mem. mngt)

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