source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-stream.c@ 2227

Last change on this file since 2227 was 2227, checked in by Bruno Cornec, 15 years ago

Remove the iamhere function (will hopefully suppress valgrind errors at restore time)

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