source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-stream.c@ 2324

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

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

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