]> git.wh0rd.org - dump.git/blobdiff - restore/tape.c
Added Helmut Jarausch patch for restoring from multiple CD + examples.
[dump.git] / restore / tape.c
index ee5c72fc35392003c72bd8e560219afc38a4ed1b..344ca3b46fadd47079622653e5fcc07f19ac47d4 100644 (file)
 
 #ifndef lint
 static const char rcsid[] =
-       "$Id: tape.c,v 1.40 2001/06/18 10:58:28 stelian Exp $";
+       "$Id: tape.c,v 1.47 2001/09/12 10:21:49 stelian Exp $";
 #endif /* not lint */
 
 #include <config.h>
 #include <compatlfs.h>
 #include <errno.h>
 #include <compaterr.h>
+#include <system.h>
 #include <setjmp.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -83,6 +84,10 @@ static const char rcsid[] =
 #include <zlib.h>
 #endif /* HAVE_ZLIB */
 
+#ifdef HAVE_BZLIB
+#include <bzlib.h>
+#endif /* HAVE_BZLIB */
+
 #include "restore.h"
 #include "extern.h"
 #include "pathnames.h"
@@ -101,7 +106,7 @@ static int  numtrec;
 static char    *tapebuf;               /* input buffer for read */
 static int     bufsize;                /* buffer size without prefix */
 static char    *tbufptr = NULL;        /* active tape buffer */
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
 static char    *comprbuf;              /* uncompress work buf */
 static size_t  comprlen;               /* size including prefix */
 #endif
@@ -147,7 +152,7 @@ static void  xtrmapskip __P((char *, size_t));
 static void     xtrskip __P((char *, size_t));
 static void     setmagtapein __P((void));
 
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
 static void    newcomprbuf __P((int));
 static void    readtape_set __P((char *));
 static void    readtape_uncompr __P((char *));
@@ -234,7 +239,7 @@ newtapebuf(long size)
        tapebufsize = size;
 }
 
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
 static void
 newcomprbuf(int size)
 {
@@ -248,7 +253,7 @@ newcomprbuf(int size)
        if (comprbuf == NULL)
                errx(1, "Cannot allocate space for decompress buffer");
 }
-#endif /* HAVE_ZLIB */
+#endif /* HAVE_ZLIB || HAVE_BZLIB */
 
 /*
  * Verify that the tape drive can be accessed and
@@ -257,10 +262,18 @@ newcomprbuf(int size)
 void
 setup(void)
 {
-       int i, j, *ip;
+       int i, j, *ip, bot_code;
        struct STAT stbuf;
 
        Vprintf(stdout, "Verify tape and initialize maps\n");
+       if (bot_script) {
+               msg("Launching %s\n", bot_script);
+               bot_code = system_command(bot_script, magtape, 1);
+               if (bot_code != 0 && bot_code != 1) {
+                       msg("Restore aborted by the beginning of tape script\n");
+                       exit(1);
+               }
+       }
 #ifdef RRESTORE
        if (host)
                mt = rmtopen(magtape, 0);
@@ -289,7 +302,7 @@ setup(void)
 
        if (zflag) {
                fprintf(stderr, "Dump tape is compressed.\n");
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
                newcomprbuf(ntrec);
 #else
                errx(1,"This restore version doesn't support decompression");
@@ -370,11 +383,12 @@ setup(void)
 void
 getvol(long nextvol)
 {
-       long newvol = 0, savecnt = 0, wantnext = 0, i;
+       long newvol = 0, wantnext = 0, i;
+       long saved_blksread = 0, saved_tpblksread = 0;
        union u_spcl tmpspcl;
 #      define tmpbuf tmpspcl.s_spcl
        char buf[TP_BSIZE];
-       int haderror = 0;
+       int haderror = 0, bot_code = 1;
 
        if (nextvol == 1) {
                tapesread = 0;
@@ -387,7 +401,8 @@ getvol(long nextvol)
                        return;
                goto gethdr;
        }
-       savecnt = blksread;
+       saved_blksread = blksread;
+       saved_tpblksread = tpblksread;
 again:
        if (pipein)
                exit(1); /* pipes do not get a second chance */
@@ -438,7 +453,15 @@ again:
                snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
                magtape[MAXPATHLEN - 1] = '\0';
        }
-       if (!Mflag || haderror) {
+       if (bot_script && !haderror) {
+               msg("Launching %s\n", bot_script);
+               bot_code = system_command(bot_script, magtape, newvol);
+               if (bot_code != 0 && bot_code != 1) {
+                       msg("Restore aborted by the beginning of tape script\n");
+                       exit(1);
+               }
+       }
+       if (haderror || (bot_code && !Mflag)) {
                haderror = 0;
                fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
                fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
@@ -452,8 +475,11 @@ again:
                        return;
                }
                if (buf[0] != '\n') {
+                       char *pos;
                        (void) strncpy(magtape, buf, sizeof(magtape));
                        magtape[sizeof(magtape) - 1] = '\0';
+                       if ((pos = strchr(magtape, '\n'))) 
+                               magtape[pos - magtape] = '\0';
                }
        }
 #ifdef RRESTORE
@@ -474,17 +500,22 @@ gethdr:
        volno = newvol;
        setdumpnum();
        FLUSHTAPEBUF();
+       findtapeblksize();
        if (gethead(&tmpbuf) == FAIL) {
                Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
                fprintf(stderr, "tape is not dump tape\n");
                volno = 0;
                haderror = 1;
+               blksread = saved_blksread;
+               tpblksread = saved_tpblksread;
                goto again;
        }
        if (tmpbuf.c_volume != volno) {
                fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
                volno = 0;
                haderror = 1;
+               blksread = saved_blksread;
+               tpblksread = saved_tpblksread;
                goto again;
        }
        if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
@@ -493,10 +524,13 @@ gethdr:
                fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
                volno = 0;
                haderror = 1;
+               blksread = saved_blksread;
+               tpblksread = saved_tpblksread;
                goto again;
        }
        tapesread |= 1 << volno;
-       blksread = savecnt;
+       blksread = saved_blksread;
+       tpblksread = saved_tpblksread;
        /*
         * If continuing from the previous volume, skip over any
         * blocks read already at the end of the previous volume.
@@ -1265,7 +1299,7 @@ comparefile(char *name)
        /* NOTREACHED */
 }
 
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
 static void (*readtape_func)(char *) = readtape_set;
 
 /*
@@ -1298,7 +1332,7 @@ readtape_set(char *buf)
        readtape(buf);
 }
 
-#endif /* HAVE_ZLIB */
+#endif /* HAVE_ZLIB || HAVE_BZLIB */
 
 /*
  * This is the original readtape(), it's used for reading uncompressed input.
@@ -1306,7 +1340,7 @@ readtape_set(char *buf)
  * Handle read errors, and end of media.
  */
 static void
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
 readtape_uncompr(char *buf)
 #else
 readtape(char *buf)
@@ -1430,7 +1464,7 @@ getmore:
        tpblksread++;
 }
 
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
 
 /*
  * Read a compressed format block from a file or pipe and uncompress it.
@@ -1458,6 +1492,9 @@ readtape_comprfile(char *buf)
 
        /* read the block prefix */
        ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
+
+       if (Vflag && (ret == 0 || rl < PREFIXSIZE  ||  tpb->length == 0))
+               ret = 0;
        if (ret <= 0)
                goto readerr;
 
@@ -1616,6 +1653,9 @@ decompress_tapebuf(struct tapebuf *tpbin, int readsize)
        /* zflag gets set in setup() from the dump header          */
        int cresult, blocklen;        
        unsigned long worklen;
+#ifdef HAVE_BZLIB
+       unsigned int worklen2;
+#endif
        char *output = NULL,*reason = NULL, *lengtherr = NULL;              
        
        /* build a length error message */
@@ -1627,41 +1667,88 @@ decompress_tapebuf(struct tapebuf *tpbin, int readsize)
                        lengtherr = "long";
 
        worklen = comprlen;
-       cresult = Z_OK;
+       cresult = 1;
        if (tpbin->compressed) {
                /* uncompress whatever we read, if it fails, complain later */
-               cresult = uncompress(comprbuf, &worklen, tpbin->buf, blocklen);
-               output = comprbuf;
+               if (tpbin->flags == COMPRESS_ZLIB) {
+#ifndef HAVE_ZLIB
+                       errx(1,"This restore version doesn't support zlib decompression");
+#else
+                       cresult = uncompress(comprbuf, &worklen, 
+                                            tpbin->buf, blocklen);
+                       output = comprbuf;
+                       switch (cresult) {
+                               case Z_OK:
+                                       break;
+                               case Z_MEM_ERROR:
+                                       reason = "not enough memory";
+                                       break;
+                               case Z_BUF_ERROR:
+                                       reason = "buffer too small";
+                                       break;
+                               case Z_DATA_ERROR:
+                                       reason = "data error";
+                                       break;
+                               default:
+                                       reason = "unknown";
+                       }
+                       if (cresult == Z_OK)
+                               cresult = 1;
+                       else
+                               cresult = 0;
+#endif /* HAVE_ZLIB */
+               }
+               if (tpbin->flags == COMPRESS_BZLIB) {
+#ifndef HAVE_BZLIB
+                       errx(1,"This restore version doesn't support bzlib decompression");
+#else
+                       worklen2 = worklen;
+                       cresult = BZ2_bzBuffToBuffDecompress(
+                                       comprbuf, &worklen2, 
+                                       tpbin->buf, blocklen, 0, 0);
+                       worklen = worklen2;
+                       output = comprbuf;
+                       switch (cresult) {
+                               case BZ_OK:
+                                       break;
+                               case BZ_MEM_ERROR:
+                                       reason = "not enough memory";
+                                       break;
+                               case BZ_OUTBUFF_FULL:
+                                       reason = "buffer too small";
+                                       break;
+                               case BZ_DATA_ERROR:
+                               case BZ_DATA_ERROR_MAGIC:
+                               case BZ_UNEXPECTED_EOF:
+                                       reason = "data error";
+                                       break;
+                               default:
+                                       reason = "unknown";
+                       }
+                       if (cresult == BZ_OK)
+                               cresult = 1;
+                       else
+                               cresult = 0;
+#endif /* HAVE_BZLIB */
+               }
        }
        else {
                output = tpbin->buf;
                worklen = blocklen;
        }
-       switch (cresult) {
-               case Z_OK:
-                       numtrec = worklen / TP_BSIZE;
-                       if (worklen % TP_BSIZE != 0)
-                               reason = "length mismatch";
-                       break;
-               case Z_MEM_ERROR:
-                       reason = "not enough memory";
-                       break;
-               case Z_BUF_ERROR:
-                       reason = "buffer too small";
-                       break;
-               case Z_DATA_ERROR:
-                       reason = "data error";
-                       break;
-               default:
-                       reason = "unknown";
-       } /*switch */
+       if (cresult) {
+               numtrec = worklen / TP_BSIZE;
+               if (worklen % TP_BSIZE != 0)
+                       reason = "length mismatch";
+       }
        if (reason) {
                if (lengtherr)
                        fprintf(stderr, "%s compressed block: %d expected: %d\n",
                                lengtherr, readsize, tpbin->length + PREFIXSIZE);
                fprintf(stderr, "decompression error, block %ld: %s\n",
                        tpblksread+1, reason);
-               if (cresult != Z_OK)   output = NULL;
+               if (!cresult)
+                       output = NULL;
        }
        return output;
 }
@@ -1689,7 +1776,7 @@ msg_read_error(char *m)
                        break;
        }
 }
-#endif /* HAVE_ZLIB */
+#endif /* HAVE_ZLIB || HAVE_BZLIB */
 
 /*
  * Read the first block and get the blocksize from it. Test
@@ -1848,7 +1935,9 @@ setmagtapein(void) {
                        magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
        }
 
-       Vprintf(stdout,"Input is from %s\n", magtapein? "tape": "file/pipe");
+       Vprintf(stdout,"Input is from %s\n", 
+                       magtapein ? "tape" :
+                       Vflag ? "multi-volume (no tape)" : "file/pipe");
 }
 
 /*