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

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

svn merge -r 1938:1976 $SVN_M/branches/2.2.6

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