source: MondoRescue/branches/3.3/mondo/src/common/libmondo-stream.c@ 3860

Last change on this file since 3860 was 3860, checked in by Bruno Cornec, 2 months ago

Fix proto for write_header_block_to_stream

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