]>
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.13 2001/03/28 12:59:49 stelian Exp $";
45 #endif /* not linux */
51 #include <sys/types.h>
52 #include <sys/socket.h>
71 char count
[SSIZE
], filemode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
76 #define DEBUG(f) if (debug) fprintf(debug, f)
77 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
78 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
80 char *checkbuf
__P((char *, int));
81 void error
__P((int));
82 void getstring
__P((char *));
85 main(int argc
, char *argv
[])
90 unsigned long block
= 0;
94 debug
= fopen(*argv
, "w");
97 (void)setbuf(debug
, (char *)0);
102 if (read(0, &c
, 1) != 1)
111 DEBUG2("rmtd: O %s %s\n", device
, filemode
);
113 * XXX the rmt protocol does not provide a means to
114 * specify the permission bits; allow rw for everyone,
115 * as modified by the users umask
117 tape
= open(device
, atoi(filemode
), 0666);
124 DEBUG1("rmtd: C (%lu blocks)\n", block
);
125 getstring(device
); /* discard */
135 DEBUG2("rmtd: L %s %s\n", count
, pos
);
136 rval
= lseek(tape
, (off_t
)atol(count
), atoi(pos
));
144 DEBUG2("rmtd: W %s (block = %lu)\n", count
, block
);
145 record
= checkbuf(record
, n
);
146 for (i
= 0; i
< n
; i
+= cc
) {
147 cc
= read(0, &record
[i
], n
- i
);
149 DEBUG("rmtd: premature eof\n");
153 rval
= write(tape
, record
, n
);
161 DEBUG2("rmtd: R %s (block %lu)\n", count
, block
);
163 record
= checkbuf(record
, n
);
164 rval
= read(tape
, record
, n
);
167 (void)sprintf(resp
, "A%d\n", rval
);
168 (void)write(1, resp
, strlen(resp
));
169 (void)write(1, record
, rval
);
176 DEBUG2("rmtd: I %s %s\n", op
, count
);
178 mtop
.mt_op
= atoi(op
);
179 mtop
.mt_count
= atoi(count
);
180 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
182 rval
= mtop
.mt_count
;
186 case 'S': /* status */
188 { struct mtget mtget
;
189 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
191 rval
= sizeof (mtget
);
192 (void)sprintf(resp
, "A%d\n", rval
);
193 (void)write(1, resp
, strlen(resp
));
194 (void)write(1, (char *)&mtget
, sizeof (mtget
));
198 case 'V': /* version */
200 DEBUG1("rmtd: V %s\n", op
);
205 DEBUG1("rmtd: garbage command %c\n", c
);
209 DEBUG1("rmtd: A %d\n", rval
);
210 (void)sprintf(resp
, "A%d\n", rval
);
211 (void)write(1, resp
, strlen(resp
));
218 void getstring(char *bp
)
223 for (i
= 0; i
< SSIZE
; i
++) {
224 if (read(0, cp
+i
, 1) != 1)
233 checkbuf(char *record
, int size
)
236 if (size
<= maxrecsize
)
240 record
= malloc(size
);
242 DEBUG("rmtd: cannot allocate buffer space\n");
246 while (size
> 1024 &&
247 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
256 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
257 (void)snprintf(resp
, sizeof(resp
), "E%d\n%s\n", num
, strerror(num
));
258 (void)write(1, resp
, strlen(resp
));