]>
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@cybercable.fr>, 1999
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 copyright
[] =
44 "@(#) Copyright (c) 1983, 1993\n\
45 The Regents of the University of California. All rights reserved.\n";
50 static char sccsid
[] = "@(#)rmt.c 8.1 (Berkeley) 6/6/93";
52 static const char rcsid
[] =
53 "$Id: rmt.c,v 1.2 1999/10/11 12:53:25 stelian Exp $";
59 #include <sys/types.h>
60 #include <sys/socket.h>
77 char count
[SSIZE
], mode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
82 #define DEBUG(f) if (debug) fprintf(debug, f)
83 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
84 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
86 char *checkbuf
__P((char *, int));
87 void error
__P((int));
88 void getstring
__P((char *));
101 debug
= fopen(*argv
, "w");
104 (void)setbuf(debug
, (char *)0);
109 if (read(0, &c
, 1) != 1)
118 DEBUG2("rmtd: O %s %s\n", device
, mode
);
120 * XXX the rmt protocol does not provide a means to
121 * specify the permission bits; allow rw for everyone,
122 * as modified by the users umask
124 tape
= open(device
, atoi(mode
), 0666);
131 getstring(device
); /* discard */
140 DEBUG2("rmtd: L %s %s\n", count
, pos
);
141 rval
= lseek(tape
, (off_t
)atol(count
), atoi(pos
));
149 DEBUG1("rmtd: W %s\n", count
);
150 record
= checkbuf(record
, n
);
151 for (i
= 0; i
< n
; i
+= cc
) {
152 cc
= read(0, &record
[i
], n
- i
);
154 DEBUG("rmtd: premature eof\n");
158 rval
= write(tape
, record
, n
);
165 DEBUG1("rmtd: R %s\n", count
);
167 record
= checkbuf(record
, n
);
168 rval
= read(tape
, record
, n
);
171 (void)sprintf(resp
, "A%d\n", rval
);
172 (void)write(1, resp
, strlen(resp
));
173 (void)write(1, record
, rval
);
179 DEBUG2("rmtd: I %s %s\n", op
, count
);
181 mtop
.mt_op
= atoi(op
);
182 mtop
.mt_count
= atoi(count
);
183 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
185 rval
= mtop
.mt_count
;
189 case 'S': /* status */
191 { struct mtget mtget
;
192 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
194 rval
= sizeof (mtget
);
195 (void)sprintf(resp
, "A%d\n", rval
);
196 (void)write(1, resp
, strlen(resp
));
197 (void)write(1, (char *)&mtget
, sizeof (mtget
));
201 case 'V': /* version */
203 DEBUG1("rmtd: V %s\n", op
);
208 DEBUG1("rmtd: garbage command %c\n", c
);
212 DEBUG1("rmtd: A %d\n", rval
);
213 (void)sprintf(resp
, "A%d\n", rval
);
214 (void)write(1, resp
, strlen(resp
));
228 for (i
= 0; i
< SSIZE
; i
++) {
229 if (read(0, cp
+i
, 1) != 1)
238 checkbuf(record
, size
)
243 if (size
<= maxrecsize
)
247 record
= malloc(size
);
249 DEBUG("rmtd: cannot allocate buffer space\n");
253 while (size
> 1024 &&
254 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
264 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
265 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
266 (void)write(1, resp
, strlen(resp
));