]>
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.4 1999/10/11 13:08:11 stelian Exp $";
59 #include <sys/types.h>
60 #include <sys/socket.h>
71 #include <linux/ext2_fs.h>
72 #include <ext2fs/ext2fs.h>
73 #include <bsdcompat.h>
83 char count
[SSIZE
], filemode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
88 #define DEBUG(f) if (debug) fprintf(debug, f)
89 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
90 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
92 char *checkbuf
__P((char *, int));
93 void error
__P((int));
94 void getstring
__P((char *));
97 main(int argc
, char *argv
[])
105 debug
= fopen(*argv
, "w");
108 (void)setbuf(debug
, (char *)0);
113 if (read(0, &c
, 1) != 1)
122 DEBUG2("rmtd: O %s %s\n", device
, filemode
);
124 * XXX the rmt protocol does not provide a means to
125 * specify the permission bits; allow rw for everyone,
126 * as modified by the users umask
128 tape
= open(device
, atoi(filemode
), 0666);
135 getstring(device
); /* discard */
144 DEBUG2("rmtd: L %s %s\n", count
, pos
);
145 rval
= lseek(tape
, (off_t
)atol(count
), atoi(pos
));
153 DEBUG1("rmtd: W %s\n", count
);
154 record
= checkbuf(record
, n
);
155 for (i
= 0; i
< n
; i
+= cc
) {
156 cc
= read(0, &record
[i
], n
- i
);
158 DEBUG("rmtd: premature eof\n");
162 rval
= write(tape
, record
, n
);
169 DEBUG1("rmtd: R %s\n", count
);
171 record
= checkbuf(record
, n
);
172 rval
= read(tape
, record
, n
);
175 (void)sprintf(resp
, "A%d\n", rval
);
176 (void)write(1, resp
, strlen(resp
));
177 (void)write(1, record
, rval
);
183 DEBUG2("rmtd: I %s %s\n", op
, count
);
185 mtop
.mt_op
= atoi(op
);
186 mtop
.mt_count
= atoi(count
);
187 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
189 rval
= mtop
.mt_count
;
193 case 'S': /* status */
195 { struct mtget mtget
;
196 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
198 rval
= sizeof (mtget
);
199 (void)sprintf(resp
, "A%d\n", rval
);
200 (void)write(1, resp
, strlen(resp
));
201 (void)write(1, (char *)&mtget
, sizeof (mtget
));
205 case 'V': /* version */
207 DEBUG1("rmtd: V %s\n", op
);
212 DEBUG1("rmtd: garbage command %c\n", c
);
216 DEBUG1("rmtd: A %d\n", rval
);
217 (void)sprintf(resp
, "A%d\n", rval
);
218 (void)write(1, resp
, strlen(resp
));
225 void getstring(char *bp
)
230 for (i
= 0; i
< SSIZE
; i
++) {
231 if (read(0, cp
+i
, 1) != 1)
240 checkbuf(char *record
, int 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)
263 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
264 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
265 (void)write(1, resp
, strlen(resp
));