include fixes
[unix-history] / usr / src / sbin / restore / tape.c
index 3762eb7..8104255 100644 (file)
@@ -1,7 +1,7 @@
 /* Copyright (c) 1983 Regents of the University of California */
 
 #ifndef lint
 /* Copyright (c) 1983 Regents of the University of California */
 
 #ifndef lint
-static char sccsid[] = "@(#)tape.c     3.16    (Berkeley)      83/05/14";
+static char sccsid[] = "@(#)tape.c     3.20    (Berkeley)      83/07/01";
 #endif
 
 #include "restore.h"
 #endif
 
 #include "restore.h"
@@ -10,7 +10,7 @@ static char sccsid[] = "@(#)tape.c    3.16    (Berkeley)      83/05/14";
 #include <sys/mtio.h>
 #include <sys/file.h>
 #include <setjmp.h>
 #include <sys/mtio.h>
 #include <sys/file.h>
 #include <setjmp.h>
-#include <stat.h>
+#include <sys/stat.h>
 
 static long    fssize;
 static int     mt = -1;
 
 static long    fssize;
 static int     mt = -1;
@@ -20,6 +20,7 @@ static int    bct = NTREC+1;
 static char    tbf[NTREC*TP_BSIZE];
 static union   u_spcl endoftapemark;
 static long    blksread;
 static char    tbf[NTREC*TP_BSIZE];
 static union   u_spcl endoftapemark;
 static long    blksread;
+static long    tapesread;
 static jmp_buf restart;
 static int     gettingfile = 0;        /* restart has a valid frame */
 
 static jmp_buf restart;
 static int     gettingfile = 0;        /* restart has a valid frame */
 
@@ -34,10 +35,12 @@ static int  pathlen;
 setinput(source)
        char *source;
 {
 setinput(source)
        char *source;
 {
+#ifdef RRESTORE
        char *host;
        char *host;
+#endif RRESTORE
 
        terminal = stdin;
 
        terminal = stdin;
-#ifdef RRESTOR
+#ifdef RRESTORE
        host = source;
        magtape = index(host, ':');
        if (magtape == 0) {
        host = source;
        magtape = index(host, ':');
        if (magtape == 0) {
@@ -63,7 +66,7 @@ nohost:
                pipein++;
        }
        magtape = source;
                pipein++;
        }
        magtape = source;
-#endif RRESTOR
+#endif RRESTORE
 }
 
 /*
 }
 
 /*
@@ -78,7 +81,7 @@ setup()
        extern int xtrmap(), xtrmapskip();
 
        vprintf(stdout, "Verify tape and initialize maps\n");
        extern int xtrmap(), xtrmapskip();
 
        vprintf(stdout, "Verify tape and initialize maps\n");
-#ifdef RRESTOR
+#ifdef RRESTORE
        if ((mt = rmtopen(magtape, 0)) < 0)
 #else
        if (pipein)
        if ((mt = rmtopen(magtape, 0)) < 0)
 #else
        if (pipein)
@@ -86,7 +89,7 @@ setup()
        else if ((mt = open(magtape, 0)) < 0)
 #endif
        {
        else if ((mt = open(magtape, 0)) < 0)
 #endif
        {
-               fprintf(stderr, "%s: cannot open tape\n", magtape);
+               perror(magtape);
                done(1);
        }
        volno = 1;
                done(1);
        }
        volno = 1;
@@ -119,7 +122,7 @@ setup()
        dumptime = spcl.c_ddate;
        dumpdate = spcl.c_date;
        if (stat(".", &stbuf) < 0) {
        dumptime = spcl.c_ddate;
        dumpdate = spcl.c_date;
        if (stat(".", &stbuf) < 0) {
-               fprintf(stderr, "cannot stat .\n");
+               perror("cannot stat .");
                done(1);
        }
        fssize = stbuf.st_blksize;
                done(1);
        }
        fssize = stbuf.st_blksize;
@@ -158,14 +161,23 @@ setup()
        getfile(xtrmap, xtrmapskip);
 }
 
        getfile(xtrmap, xtrmapskip);
 }
 
+/*
+ * Prompt user to load a new dump volume.
+ * "Nextvol" is the next suggested volume to use.
+ * This suggested volume is enforced when doing full
+ * or incremental restores, but can be overrridden by
+ * the user when only extracting a subset of the files.
+ */
 getvol(nextvol)
        long nextvol;
 {
        long newvol;
 getvol(nextvol)
        long nextvol;
 {
        long newvol;
-       long savecnt;
+       long savecnt, i;
        union u_spcl tmpspcl;
 #      define tmpbuf tmpspcl.s_spcl
 
        union u_spcl tmpspcl;
 #      define tmpbuf tmpspcl.s_spcl
 
+       if (nextvol == 1)
+               tapesread = 0;
        if (pipein) {
                if (nextvol != 1)
                        panic("Changing volumes on pipe input?\n");
        if (pipein) {
                if (nextvol != 1)
                        panic("Changing volumes on pipe input?\n");
@@ -182,6 +194,23 @@ again:
        else 
                newvol = 0;
        while (newvol <= 0) {
        else 
                newvol = 0;
        while (newvol <= 0) {
+               if (tapesread == 0) {
+                       fprintf(stderr, "%s%s%s%s%s",
+                           "You have not read any tapes yet.\n",
+                           "Unless you know which volume your",
+                           " file(s) are on you should start\n",
+                           "with the last volume and work",
+                           " towards towards the first.\n");
+               } else {
+                       fprintf(stderr, "You have read volumes");
+                       strcpy(tbf, ": ");
+                       for (i = 1; i < 32; i++)
+                               if (tapesread & (1 << i)) {
+                                       fprintf(stderr, "%s%d", tbf, i);
+                                       strcpy(tbf, ", ");
+                               }
+                       fprintf(stderr, "\n");
+               }
                do      {
                        fprintf(stderr, "Specify next volume #: ");
                        (void) fflush(stderr);
                do      {
                        fprintf(stderr, "Specify next volume #: ");
                        (void) fflush(stderr);
@@ -195,14 +224,16 @@ again:
                            "Volume numbers are positive numerics\n");
                }
        }
                            "Volume numbers are positive numerics\n");
                }
        }
-       if (newvol == volno)
+       if (newvol == volno) {
+               tapesread |= 1 << volno;
                return;
                return;
+       }
        closemt();
        fprintf(stderr, "Mount tape volume %d then type return ", newvol);
        (void) fflush(stderr);
        while (getc(terminal) != '\n')
                ;
        closemt();
        fprintf(stderr, "Mount tape volume %d then type return ", newvol);
        (void) fflush(stderr);
        while (getc(terminal) != '\n')
                ;
-#ifdef RRESTOR
+#ifdef RRESTORE
        if ((mt = rmtopen(magtape, 0)) == -1)
 #else
        if ((mt = open(magtape, 0)) == -1)
        if ((mt = rmtopen(magtape, 0)) == -1)
 #else
        if ((mt = open(magtape, 0)) == -1)
@@ -232,6 +263,7 @@ gethdr:
                volno = 0;
                goto again;
        }
                volno = 0;
                goto again;
        }
+       tapesread |= 1 << volno;
        blksread = savecnt;
        if (curfile.action == USING) {
                if (volno == 1)
        blksread = savecnt;
        if (curfile.action == USING) {
                if (volno == 1)
@@ -262,7 +294,7 @@ setdumpnum()
        }
        tcom.mt_op = MTFSF;
        tcom.mt_count = dumpnum - 1;
        }
        tcom.mt_op = MTFSF;
        tcom.mt_count = dumpnum - 1;
-#ifdef RRESTOR
+#ifdef RRESTORE
        rmtioctl(MTFSF, dumpnum - 1);
 #else
        if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
        rmtioctl(MTFSF, dumpnum - 1);
 #else
        if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
@@ -310,8 +342,9 @@ extractfile(name)
                        vprintf(stdout,
                            "%s: zero length symbolic link (ignored)\n", name);
                } else if (symlink(lnkbuf, name) < 0) {
                        vprintf(stdout,
                            "%s: zero length symbolic link (ignored)\n", name);
                } else if (symlink(lnkbuf, name) < 0) {
-                       fprintf(stderr, "%s: cannot create symbolic link\n",
-                           name);
+                       fprintf(stderr, "%s: ", name);
+                       (void) fflush(stderr);
+                       perror("cannot create symbolic link");
                        return (FAIL);
                } else
                        vprintf(stdout, "extract symbolic link %s\n", name);
                        return (FAIL);
                } else
                        vprintf(stdout, "extract symbolic link %s\n", name);
@@ -321,8 +354,9 @@ extractfile(name)
        case IFBLK:
                vprintf(stdout, "extract special file %s\n", name);
                if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
        case IFBLK:
                vprintf(stdout, "extract special file %s\n", name);
                if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
-                       fprintf(stderr, "%s: cannot create special file\n",
-                           name);
+                       fprintf(stderr, "%s: ", name);
+                       (void) fflush(stderr);
+                       perror("cannot create special file");
                        skipfile();
                        return (FAIL);
                }
                        skipfile();
                        return (FAIL);
                }
@@ -334,8 +368,10 @@ extractfile(name)
 
        case IFREG:
                vprintf(stdout, "extract file %s\n", name);
 
        case IFREG:
                vprintf(stdout, "extract file %s\n", name);
-               if ((ofile = open(name, FWRONLY|FCREATE, 0666)) < 0) {
-                       fprintf(stderr, "%s: cannot create file\n", name);
+               if ((ofile = creat(name, 0666)) < 0) {
+                       fprintf(stderr, "%s: ", name);
+                       (void) fflush(stderr);
+                       perror("cannot create file");
                        skipfile();
                        return (FAIL);
                }
                        skipfile();
                        return (FAIL);
                }
@@ -527,7 +563,7 @@ readtape(b)
                cnt = NTREC*TP_BSIZE;
                rd = 0;
        getmore:
                cnt = NTREC*TP_BSIZE;
                rd = 0;
        getmore:
-#ifdef RRESTOR
+#ifdef RRESTORE
                i = rmtread(&tbf[rd], cnt);
 #else
                i = read(mt, &tbf[rd], cnt);
                i = rmtread(&tbf[rd], cnt);
 #else
                i = read(mt, &tbf[rd], cnt);
@@ -563,13 +599,13 @@ readtape(b)
                                done(1);
                        i = NTREC*TP_BSIZE;
                        bzero(tbf, i);
                                done(1);
                        i = NTREC*TP_BSIZE;
                        bzero(tbf, i);
-#ifdef RRESTOR
+#ifdef RRESTORE
                        if (rmtseek(i, 1) < 0)
 #else
                        if (lseek(mt, i, 1) == (long)-1)
 #endif
                        {
                        if (rmtseek(i, 1) < 0)
 #else
                        if (lseek(mt, i, 1) == (long)-1)
 #endif
                        {
-                               fprintf(stderr, "continuation failed\n");
+                               perror("continuation failed");
                                done(1);
                        }
                }
                                done(1);
                        }
                }
@@ -601,7 +637,7 @@ closemt()
 {
        if (mt < 0)
                return;
 {
        if (mt < 0)
                return;
-#ifdef RRESTOR
+#ifdef RRESTORE
        rmtclose();
 #else
        (void) close(mt);
        rmtclose();
 #else
        (void) close(mt);
@@ -864,7 +900,7 @@ checksum(b)
        return(GOOD);
 }
 
        return(GOOD);
 }
 
-#ifdef RRESTOR
+#ifdef RRESTORE
 /* VARARGS1 */
 msg(cp, a1, a2, a3)
        char *cp;
 /* VARARGS1 */
 msg(cp, a1, a2, a3)
        char *cp;
@@ -872,4 +908,4 @@ msg(cp, a1, a2, a3)
 
        fprintf(stderr, cp, a1, a2, a3);
 }
 
        fprintf(stderr, cp, a1, a2, a3);
 }
-#endif RRESTOR
+#endif RRESTORE