]> git.wh0rd.org - dump.git/blobdiff - restore/tape.c
noos -> popies.net
[dump.git] / restore / tape.c
index 08bb3757e7b638ece4d9f09048abb9d9072df6a5..76dded78fbef7113e3dc2740378fcc4392e59585 100644 (file)
@@ -2,8 +2,8 @@
  *     Ported to Linux's Second Extended File System as part of the
  *     dump and restore backup suit
  *     Remy Card <card@Linux.EU.Org>, 1994-1997
- *     Stelian Pop <pop@noos.fr>, 1999-2000
- *     Stelian Pop <pop@noos.fr> - Alcôve <www.alcove.fr>, 2000
+ *     Stelian Pop <stelian@popies.net>, 1999-2000
+ *     Stelian Pop <stelian@popies.net> - Alcôve <www.alcove.com>, 2000-2002
  */
 
 /*
@@ -46,7 +46,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-       "$Id: tape.c,v 1.44 2001/07/18 14:57:46 stelian Exp $";
+       "$Id: tape.c,v 1.52 2002/01/16 09:32:14 stelian Exp $";
 #endif /* not lint */
 
 #include <config.h>
@@ -84,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"
@@ -93,7 +97,7 @@ int           noresyncmesg = 0;
 #endif /* USE_QFA */
 static long    fssize = MAXBSIZE;
 static int     mt = -1;
-static int     pipein = 0;
+int            pipein = 0;
 static int     magtapein = 0;          /* input is from magtape */
 static char    magtape[MAXPATHLEN];
 static char    magtapeprefix[MAXPATHLEN];
@@ -102,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
@@ -148,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 *));
@@ -235,7 +239,7 @@ newtapebuf(long size)
        tapebufsize = size;
 }
 
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
 static void
 newcomprbuf(int size)
 {
@@ -249,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
@@ -298,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");
@@ -428,12 +432,16 @@ again:
                        fprintf(stderr, "\n");
                }
                do      {
-                       fprintf(stderr, "Specify next volume #: ");
+                       fprintf(stderr, "Specify next volume # (none if no more volumes): ");
                        (void) fflush(stderr);
                        (void) fgets(buf, TP_BSIZE, terminal);
                } while (!feof(terminal) && buf[0] == '\n');
                if (feof(terminal))
                        exit(1);
+               if (!strcmp(buf, "none\n")) {
+                       terminateinput();
+                       return;
+               }
                newvol = atoi(buf);
                if (newvol <= 0) {
                        fprintf(stderr,
@@ -496,6 +504,7 @@ 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");
@@ -1071,14 +1080,14 @@ cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
        if (STAT(tapefile, &sbuf_tape) != 0) {
                panic("Can't lstat tmp file %s: %s\n", tapefile,
                      strerror(errno));
-               compare_errors = 1;
+               do_compare_error;
        }
 
        if (sbuf_disk->st_size != sbuf_tape.st_size) {
                fprintf(stderr,
                        "%s: size changed from %ld to %ld.\n",
                        diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
-               compare_errors = 1;
+               do_compare_error;
 #ifdef COMPARE_FAIL_KEEP_FILE
                return (0);
 #else
@@ -1088,12 +1097,12 @@ cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
 
        if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
                panic("Can't open %s: %s\n", tapefile, strerror(errno));
-               compare_errors = 1;
+               do_compare_error;
        }
        if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
                close(fd_tape);
                panic("Can't open %s: %s\n", diskfile, strerror(errno));
-               compare_errors = 1;
+               do_compare_error;
        }
 
        if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
@@ -1101,7 +1110,7 @@ cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
                        diskfile);
                close(fd_tape);
                close(fd_disk);
-               compare_errors = 1;
+               do_compare_error;
 #ifdef COMPARE_FAIL_KEEP_FILE
                /* rename the file to live in /tmp */
                /* rename `tapefile' to /tmp/<basename of diskfile> */
@@ -1154,7 +1163,7 @@ comparefile(char *name)
 
        if ((r = LSTAT(name, &sb)) != 0) {
                warn("%s: does not exist (%d)", name, r);
-               compare_errors = 1;
+               do_compare_error;
                skipfile();
                return;
        }
@@ -1169,7 +1178,7 @@ comparefile(char *name)
        if (sb.st_mode != mode) {
                fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
                        name, mode & 07777, sb.st_mode & 07777);
-               compare_errors = 1;
+               do_compare_error;
        }
        switch (mode & IFMT) {
        default:
@@ -1191,7 +1200,7 @@ comparefile(char *name)
                if (!(sb.st_mode & S_IFLNK)) {
                        fprintf(stderr, "%s: is no longer a symbolic link\n",
                                name);
-                       compare_errors = 1;
+                       do_compare_error;
                        return;
                }
                lnkbuf[0] = '\0';
@@ -1201,20 +1210,20 @@ comparefile(char *name)
                        fprintf(stderr,
                                "%s: zero length symbolic link (ignored)\n",
                                name);
-                       compare_errors = 1;
+                       do_compare_error;
                        return;
                }
                if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
                        panic("readlink of %s failed: %s", name,
                              strerror(errno));
-                       compare_errors = 1;
+                       do_compare_error;
                }
                lbuf[lsize] = 0;
                if (strcmp(lbuf, lnkbuf) != 0) {
                        fprintf(stderr,
                                "%s: symbolic link changed from %s to %s.\n",
                                name, lnkbuf, lbuf);
-                       compare_errors = 1;
+                       do_compare_error;
                        return;
                }
                return;
@@ -1225,7 +1234,7 @@ comparefile(char *name)
                if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
                        fprintf(stderr, "%s: no longer a special file\n",
                                name);
-                       compare_errors = 1;
+                       do_compare_error;
                        skipfile();
                        return;
                }
@@ -1238,7 +1247,7 @@ comparefile(char *name)
                                (int)curfile.dip->di_rdev & 0xff,
                                ((int)sb.st_rdev >> 8) & 0xff,
                                (int)sb.st_rdev & 0xff);
-                       compare_errors = 1;
+                       do_compare_error;
                }
                skipfile();
                return;
@@ -1248,7 +1257,7 @@ comparefile(char *name)
                if ((ifile = OPEN(name, O_RDONLY)) < 0) {
                        panic("Can't open %s: %s\n", name, strerror(errno));
                        skipfile();
-                       compare_errors = 1;
+                       do_compare_error;
                }
                else {
                        cmperror = 0;
@@ -1262,7 +1271,7 @@ comparefile(char *name)
                                }
                        }
                        if (cmperror)
-                               compare_errors = 1;
+                               do_compare_error;
                        close(ifile);
                }
 #else
@@ -1294,7 +1303,7 @@ comparefile(char *name)
        /* NOTREACHED */
 }
 
-#ifdef HAVE_ZLIB
+#if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
 static void (*readtape_func)(char *) = readtape_set;
 
 /*
@@ -1327,7 +1336,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.
@@ -1335,7 +1344,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)
@@ -1459,7 +1468,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.
@@ -1487,6 +1496,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;
 
@@ -1645,6 +1657,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 */
@@ -1656,41 +1671,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;
 }
@@ -1718,7 +1780,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
@@ -1871,13 +1933,15 @@ setmagtapein(void) {
                /* need to know if input is really from a tape */
 #ifdef RRESTORE
                if (host)
-                       magtapein = rmtioctl(MTNOP, 1) != -1;
+                       magtapein = !lflag;
                else
 #endif
                        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");
 }
 
 /*