]>
git.wh0rd.org - dump.git/blob - rmt/rmt.c
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static const char rcsid
[] =
44 "$Id: rmt.c,v 1.15 2001/05/26 11:11:16 stelian Exp $";
45 #endif /* not linux */
51 #include <compatlfs.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
72 char count
[SSIZE
], filemode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
77 #define DEBUG(f) if (debug) fprintf(debug, f)
78 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
79 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
82 * Support for Sun's extended RMT protocol
84 #define RMTI_VERSION -1
87 /* Extended 'i' commands */
89 #define RMTI_NOCACHE 1
95 /* Extended 's' comands */
100 #define MTS_FILENO 'F'
101 #define MTS_BLKNO 'B'
102 #define MTS_FLAGS 'f'
105 char *checkbuf
__P((char *, int));
106 void error
__P((int));
107 void getstring
__P((char *));
110 main(int argc
, char *argv
[])
115 unsigned long block
= 0;
119 debug
= fopen(*argv
, "w");
122 (void)setbuf(debug
, (char *)0);
127 if (read(0, &c
, 1) != 1)
136 DEBUG2("rmtd: O %s %s\n", device
, filemode
);
138 * XXX the rmt protocol does not provide a means to
139 * specify the permission bits; allow rw for everyone,
140 * as modified by the users umask
142 tape
= OPEN(device
, atoi(filemode
), 0666);
149 DEBUG1("rmtd: C (%lu blocks)\n", block
);
150 getstring(device
); /* discard */
160 DEBUG2("rmtd: L %s %s\n", count
, pos
);
161 rval
= LSEEK(tape
, (off_t
)atol(count
), atoi(pos
));
169 DEBUG2("rmtd: W %s (block = %lu)\n", count
, block
);
170 record
= checkbuf(record
, n
);
171 for (i
= 0; i
< n
; i
+= cc
) {
172 cc
= read(0, &record
[i
], n
- i
);
174 DEBUG("rmtd: premature eof\n");
178 rval
= write(tape
, record
, n
);
186 DEBUG2("rmtd: R %s (block %lu)\n", count
, block
);
188 record
= checkbuf(record
, n
);
189 rval
= read(tape
, record
, n
);
192 (void)sprintf(resp
, "A%d\n", rval
);
193 (void)write(1, resp
, strlen(resp
));
194 (void)write(1, record
, rval
);
201 DEBUG2("rmtd: I %s %s\n", op
, count
);
202 if (atoi(op
) == RMTI_VERSION
) {
206 mtop
.mt_op
= atoi(op
);
207 mtop
.mt_count
= atoi(count
);
208 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
210 rval
= mtop
.mt_count
;
219 DEBUG2 ("rmtd: i %s %s\n", op
, count
);
223 mtop
.mt_op
= MTCACHE
;
228 mtop
.mt_op
= MTNOCACHE
;
233 mtop
.mt_op
= MTRETEN
;
238 mtop
.mt_op
= MTERASE
;
255 mtop
.mt_count
= atoi (count
);
256 if (ioctl (tape
, MTIOCTOP
, (char *) &mtop
) < 0)
259 rval
= mtop
.mt_count
;
264 case 'S': /* status */
266 { struct mtget mtget
;
267 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
269 rval
= sizeof (mtget
);
270 (void)sprintf(resp
, "A%d\n", rval
);
271 (void)write(1, resp
, strlen(resp
));
272 (void)write(1, (char *)&mtget
, sizeof (mtget
));
280 if (read (0, &s
, 1) != 1)
283 if (ioctl (tape
, MTIOCGET
, (char *) &mtget
) < 0)
288 rval
= mtget
.mt_type
;
291 rval
= mtget
.mt_dsreg
;
294 rval
= mtget
.mt_erreg
;
297 rval
= mtget
.mt_resid
;
300 rval
= mtget
.mt_fileno
;
303 rval
= mtget
.mt_blkno
;
306 rval
= mtget
.mt_gstat
;
319 case 'V': /* version */
321 DEBUG1("rmtd: V %s\n", op
);
326 DEBUG1("rmtd: garbage command %c\n", c
);
330 DEBUG1("rmtd: A %d\n", rval
);
331 (void)sprintf(resp
, "A%d\n", rval
);
332 (void)write(1, resp
, strlen(resp
));
339 void getstring(char *bp
)
344 for (i
= 0; i
< SSIZE
; i
++) {
345 if (read(0, cp
+i
, 1) != 1)
354 checkbuf(char *record
, int size
)
357 if (size
<= maxrecsize
)
361 record
= malloc(size
);
363 DEBUG("rmtd: cannot allocate buffer space\n");
367 while (size
> 1024 &&
368 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
377 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
378 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
379 (void)write(1, resp
, strlen(resp
));