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

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

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

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