]> git.wh0rd.org - dump.git/blobdiff - rmt/rmt.c
Fixed largefile seeks in rmt
[dump.git] / rmt / rmt.c
index 2af48e3e0cd7f8560c2946a1faeb449c5da66169..7f93f57cf32991dd70d96fa329d05ab867db7cb5 100644 (file)
--- a/rmt/rmt.c
+++ b/rmt/rmt.c
@@ -41,7 +41,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-       "$Id: rmt.c,v 1.17 2002/04/12 13:02:16 stelian Exp $";
+       "$Id: rmt.c,v 1.23 2003/02/12 11:02:31 stelian Exp $";
 #endif /* not linux */
 
 /*
@@ -49,6 +49,7 @@ static const char rcsid[] =
  */
 #include <config.h>
 #include <compatlfs.h>
+#include <rmtflags.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/mtio.h>
@@ -62,18 +63,18 @@ static const char rcsid[] =
 #include <string.h>
 #include <unistd.h>
 
-int    tape = -1;
+static int     tape = -1;
 
-char   *record;
-int    maxrecsize = -1;
+static char    *record;
+static int     maxrecsize = -1;
 
 #define        SSIZE   64
-char   device[SSIZE];
-char   count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
+static char    device[SSIZE];
+static char    count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
 
-char   resp[BUFSIZ];
+static char    resp[BUFSIZ];
 
-FILE   *debug;
+static FILE    *debug;
 #define        DEBUG(f)        if (debug) fprintf(debug, f)
 #define        DEBUG1(f,a)     if (debug) fprintf(debug, f, a)
 #define        DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
@@ -82,27 +83,37 @@ FILE        *debug;
  * Support for Sun's extended RMT protocol
  *     code originally written by Jörg Schilling <schilling@fokus.gmd.de>
  *     and relicensed by his permission from GPL to BSD for use in dump.
+ *
+ *     rmt_version is 0 for regular clients (Linux included)
+ *     rmt_version is 1 for extended clients (Sun especially). In this case
+ *             we support some extended commands (see below) and we remap
+ *             the ioctl commands to the UNIX "standard", as per:
+ *                     ftp://ftp.fokus.gmd.de/pub/unix/star/README.mtio
+ *
+ *     In order to use rmt version 1, a client must send "I-1\n0\n" 
+ *     before issuing the other I commands.
  */
-#define RMTI_VERSION    -1
-#define RMT_VERSION 1
+static int     rmt_version = 0;
+#define RMTI_VERSION   -1
+#define RMT_VERSION    1
  
 /* Extended 'i' commands */
-#define RMTI_CACHE  0
-#define RMTI_NOCACHE    1
-#define RMTI_RETEN  2
-#define RMTI_ERASE  3
-#define RMTI_EOM    4
-#define RMTI_NBSF   5
+#define RMTI_CACHE     0
+#define RMTI_NOCACHE   1
+#define RMTI_RETEN     2
+#define RMTI_ERASE     3
+#define RMTI_EOM       4
+#define RMTI_NBSF      5
  
 /* Extended 's' comands */
-#define MTS_TYPE    'T'
-#define MTS_DSREG   'D'
-#define MTS_ERREG   'E'
-#define MTS_RESID   'R'
-#define MTS_FILENO  'F'
-#define MTS_BLKNO   'B'
-#define MTS_FLAGS   'f'
-#define MTS_BF      'b'
+#define MTS_TYPE       'T'
+#define MTS_DSREG      'D'
+#define MTS_ERREG      'E'
+#define MTS_RESID      'R'
+#define MTS_FILENO     'F'
+#define MTS_BLKNO      'B'
+#define MTS_FLAGS      'f'
+#define MTS_BF         'b'
 
 char   *checkbuf __P((char *, int));
 void    error __P((int));
@@ -111,9 +122,9 @@ void         getstring __P((char *));
 int
 main(int argc, char *argv[])
 {
-       int rval = 0;
+       OFF_T rval = 0;
        char c;
-       int n, i, cc;
+       int n, i, cc, oflags;
        unsigned long block = 0;
 
        argc--, argv++;
@@ -136,12 +147,24 @@ top:
                getstring(device);
                getstring(filemode);
                DEBUG2("rmtd: O %s %s\n", device, filemode);
+               /*
+                * Translate extended GNU syntax into its numeric platform equivalent
+                */
+               oflags = rmtflags_toint(filemode);
+#ifdef  O_TEXT
+               /*
+                * Default to O_BINARY the client may not know that we need it.
+                */
+               if ((oflags & O_TEXT) == 0)
+                       oflags |= O_BINARY;
+#endif
+               DEBUG2("rmtd: O %s %d\n", device, oflags);
                /*
                 * XXX the rmt protocol does not provide a means to
                 * specify the permission bits; allow rw for everyone,
                 * as modified by the users umask
                 */
-               tape = OPEN(device, atoi(filemode), 0666);
+               tape = OPEN(device, oflags, 0666);
                if (tape < 0)
                        goto ioerror;
                block = 0;
@@ -160,7 +183,7 @@ top:
                getstring(count);
                getstring(pos);
                DEBUG2("rmtd: L %s %s\n", count, pos);
-               rval = LSEEK(tape, (off_t)atol(count), atoi(pos));
+               rval = LSEEK(tape, (OFF_T)atoll(count), atoi(pos));
                if (rval < 0)
                        goto ioerror;
                goto respond;
@@ -191,7 +214,7 @@ top:
                rval = read(tape, record, n);
                if (rval < 0)
                        goto ioerror;
-               (void)sprintf(resp, "A%d\n", rval);
+               (void)sprintf(resp, "A%lld\n", (long long)rval);
                (void)write(1, resp, strlen(resp));
                (void)write(1, record, rval);
                block += n >> 10;
@@ -203,13 +226,68 @@ top:
                DEBUG2("rmtd: I %s %s\n", op, count);
                if (atoi(op) == RMTI_VERSION) {
                        rval = RMT_VERSION;
-               } else { 
-                 struct mtop mtop;
-                 mtop.mt_op = atoi(op);
-                 mtop.mt_count = atoi(count);
-                 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
-                       goto ioerror;
-                 rval = mtop.mt_count;
+                       rmt_version = 1;
+               } 
+               else { 
+                       struct mtop mtop;
+                       mtop.mt_op = -1;
+                       if (rmt_version) {
+                               /* rmt version 1, assume UNIX client */
+                               switch (atoi(op)) {
+#ifdef  MTWEOF
+                                       case 0:
+                                               mtop.mt_op = MTWEOF;
+                                               break;
+#endif
+#ifdef  MTFSF
+                                       case 1:
+                                               mtop.mt_op = MTFSF;
+                                               break;
+#endif
+#ifdef  MTBSF
+                                       case 2:
+                                               mtop.mt_op = MTBSF;
+                                               break;
+#endif
+#ifdef  MTFSR
+                                       case 3:
+                                               mtop.mt_op = MTFSR;
+                                               break;
+#endif
+#ifdef  MTBSR
+                                       case 4:
+                                               mtop.mt_op = MTBSR;
+                                               break;
+#endif
+#ifdef  MTREW
+                                       case 5:
+                                               mtop.mt_op = MTREW;
+                                               break;
+#endif
+#ifdef  MTOFFL
+                                       case 6:
+                                               mtop.mt_op = MTOFFL;
+                                               break;
+#endif
+#ifdef  MTNOP
+                                       case 7:
+                                               mtop.mt_op = MTNOP;
+                                               break;
+#endif
+                               }
+                               if (mtop.mt_op == -1) {
+                                       errno = EINVAL;
+                                       goto ioerror;
+                               }
+                       }
+                       else {
+                               /* rmt version 0, assume linux client */
+                               mtop.mt_op = atoi(op);
+                       }
+                       mtop.mt_count = atoi(count);
+                       if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
+                               goto ioerror;
+                       rval = mtop.mt_count;
                }
                goto respond;
 
@@ -269,7 +347,7 @@ top:
                  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
                        goto ioerror;
                  rval = sizeof (mtget);
-                 (void)sprintf(resp, "A%d\n", rval);
+                 (void)sprintf(resp, "A%lld\n", (long long)rval);
                  (void)write(1, resp, strlen(resp));
                  (void)write(1, (char *)&mtget, sizeof (mtget));
                  goto top;
@@ -329,8 +407,8 @@ top:
                exit(3);
        }
 respond:
-       DEBUG1("rmtd: A %d\n", rval);
-       (void)sprintf(resp, "A%d\n", rval);
+       DEBUG1("rmtd: A %lld\n", (long long)rval);
+       (void)sprintf(resp, "A%lld\n", (long long)rval);
        (void)write(1, resp, strlen(resp));
        goto top;
 ioerror: