ar_io.c revision 1.5 1 1.5 mrg /* $NetBSD: ar_io.c,v 1.5 1996/03/26 23:54:13 mrg Exp $ */
2 1.4 cgd
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1992 Keith Muller.
5 1.1 jtc * Copyright (c) 1992, 1993
6 1.1 jtc * The Regents of the University of California. All rights reserved.
7 1.1 jtc *
8 1.1 jtc * This code is derived from software contributed to Berkeley by
9 1.1 jtc * Keith Muller of the University of California, San Diego.
10 1.1 jtc *
11 1.1 jtc * Redistribution and use in source and binary forms, with or without
12 1.1 jtc * modification, are permitted provided that the following conditions
13 1.1 jtc * are met:
14 1.1 jtc * 1. Redistributions of source code must retain the above copyright
15 1.1 jtc * notice, this list of conditions and the following disclaimer.
16 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jtc * notice, this list of conditions and the following disclaimer in the
18 1.1 jtc * documentation and/or other materials provided with the distribution.
19 1.1 jtc * 3. All advertising materials mentioning features or use of this software
20 1.1 jtc * must display the following acknowledgement:
21 1.1 jtc * This product includes software developed by the University of
22 1.1 jtc * California, Berkeley and its contributors.
23 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
24 1.1 jtc * may be used to endorse or promote products derived from this software
25 1.1 jtc * without specific prior written permission.
26 1.1 jtc *
27 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 jtc * SUCH DAMAGE.
38 1.1 jtc */
39 1.1 jtc
40 1.1 jtc #ifndef lint
41 1.4 cgd #if 0
42 1.4 cgd static char sccsid[] = "@(#)ar_io.c 8.2 (Berkeley) 4/18/94";
43 1.4 cgd #else
44 1.5 mrg static char rcsid[] = "$NetBSD: ar_io.c,v 1.5 1996/03/26 23:54:13 mrg Exp $";
45 1.4 cgd #endif
46 1.1 jtc #endif /* not lint */
47 1.1 jtc
48 1.1 jtc #include <sys/types.h>
49 1.1 jtc #include <sys/time.h>
50 1.1 jtc #include <sys/stat.h>
51 1.1 jtc #include <sys/ioctl.h>
52 1.1 jtc #include <sys/mtio.h>
53 1.1 jtc #include <sys/param.h>
54 1.1 jtc #include <signal.h>
55 1.1 jtc #include <string.h>
56 1.1 jtc #include <fcntl.h>
57 1.1 jtc #include <unistd.h>
58 1.1 jtc #include <stdio.h>
59 1.1 jtc #include <ctype.h>
60 1.1 jtc #include <errno.h>
61 1.1 jtc #include <stdlib.h>
62 1.1 jtc #include "pax.h"
63 1.1 jtc #include "extern.h"
64 1.1 jtc
65 1.1 jtc /*
66 1.1 jtc * Routines which deal directly with the archive I/O device/file.
67 1.1 jtc */
68 1.1 jtc
69 1.1 jtc #define DMOD 0666 /* default mode of created archives */
70 1.1 jtc #define EXT_MODE O_RDONLY /* open mode for list/extract */
71 1.1 jtc #define AR_MODE (O_WRONLY | O_CREAT | O_TRUNC) /* mode for archive */
72 1.1 jtc #define APP_MODE O_RDWR /* mode for append */
73 1.1 jtc #define STDO "<STDOUT>" /* psuedo name for stdout */
74 1.1 jtc #define STDN "<STDIN>" /* psuedo name for stdin */
75 1.1 jtc static int arfd = -1; /* archive file descriptor */
76 1.1 jtc static int artyp = ISREG; /* archive type: file/FIFO/tape */
77 1.1 jtc static int arvol = 1; /* archive volume number */
78 1.1 jtc static int lstrval = -1; /* return value from last i/o */
79 1.1 jtc static int io_ok; /* i/o worked on volume after resync */
80 1.1 jtc static int did_io; /* did i/o ever occur on volume? */
81 1.1 jtc static int done; /* set via tty termination */
82 1.1 jtc static struct stat arsb; /* stat of archive device at open */
83 1.1 jtc static int invld_rec; /* tape has out of spec record size */
84 1.1 jtc static int wr_trail = 1; /* trailer was rewritten in append */
85 1.1 jtc static int can_unlnk = 0; /* do we unlink null archives? */
86 1.1 jtc char *arcname; /* printable name of archive */
87 1.5 mrg char *gzip_program; /* name of gzip program */
88 1.1 jtc
89 1.1 jtc static int get_phys __P((void));
90 1.1 jtc extern sigset_t s_mask;
91 1.5 mrg static void ar_start_gzip __P((int));
92 1.1 jtc
93 1.1 jtc /*
94 1.1 jtc * ar_open()
95 1.1 jtc * Opens the next archive volume. Determines the type of the device and
96 1.1 jtc * sets up block sizes as required by the archive device and the format.
97 1.1 jtc * Note: we may be called with name == NULL on the first open only.
98 1.1 jtc * Return:
99 1.1 jtc * -1 on failure, 0 otherwise
100 1.1 jtc */
101 1.1 jtc
102 1.1 jtc #if __STDC__
103 1.1 jtc int
104 1.1 jtc ar_open(char *name)
105 1.1 jtc #else
106 1.1 jtc int
107 1.1 jtc ar_open(name)
108 1.1 jtc char *name;
109 1.1 jtc #endif
110 1.1 jtc {
111 1.1 jtc struct mtget mb;
112 1.1 jtc
113 1.1 jtc if (arfd != -1)
114 1.1 jtc (void)close(arfd);
115 1.1 jtc arfd = -1;
116 1.1 jtc can_unlnk = did_io = io_ok = invld_rec = 0;
117 1.1 jtc artyp = ISREG;
118 1.1 jtc flcnt = 0;
119 1.1 jtc
120 1.1 jtc /*
121 1.1 jtc * open based on overall operation mode
122 1.1 jtc */
123 1.1 jtc switch (act) {
124 1.1 jtc case LIST:
125 1.1 jtc case EXTRACT:
126 1.1 jtc if (name == NULL) {
127 1.1 jtc arfd = STDIN_FILENO;
128 1.1 jtc arcname = STDN;
129 1.1 jtc } else if ((arfd = open(name, EXT_MODE, DMOD)) < 0)
130 1.1 jtc syswarn(0, errno, "Failed open to read on %s", name);
131 1.5 mrg if (zflag)
132 1.5 mrg ar_start_gzip(arfd);
133 1.1 jtc break;
134 1.1 jtc case ARCHIVE:
135 1.1 jtc if (name == NULL) {
136 1.1 jtc arfd = STDOUT_FILENO;
137 1.1 jtc arcname = STDO;
138 1.1 jtc } else if ((arfd = open(name, AR_MODE, DMOD)) < 0)
139 1.1 jtc syswarn(0, errno, "Failed open to write on %s", name);
140 1.1 jtc else
141 1.1 jtc can_unlnk = 1;
142 1.5 mrg if (zflag)
143 1.5 mrg ar_start_gzip(arfd);
144 1.1 jtc break;
145 1.1 jtc case APPND:
146 1.5 mrg if (zflag)
147 1.5 mrg err(1, "can not gzip while appending");
148 1.1 jtc if (name == NULL) {
149 1.1 jtc arfd = STDOUT_FILENO;
150 1.1 jtc arcname = STDO;
151 1.1 jtc } else if ((arfd = open(name, APP_MODE, DMOD)) < 0)
152 1.1 jtc syswarn(0, errno, "Failed open to read/write on %s",
153 1.1 jtc name);
154 1.1 jtc break;
155 1.1 jtc case COPY:
156 1.1 jtc /*
157 1.1 jtc * arfd not used in COPY mode
158 1.1 jtc */
159 1.1 jtc arcname = "<NONE>";
160 1.1 jtc lstrval = 1;
161 1.1 jtc return(0);
162 1.1 jtc }
163 1.1 jtc if (arfd < 0)
164 1.1 jtc return(-1);
165 1.1 jtc
166 1.1 jtc /*
167 1.1 jtc * set up is based on device type
168 1.1 jtc */
169 1.1 jtc if (fstat(arfd, &arsb) < 0) {
170 1.1 jtc syswarn(0, errno, "Failed stat on %s", arcname);
171 1.1 jtc (void)close(arfd);
172 1.1 jtc arfd = -1;
173 1.1 jtc can_unlnk = 0;
174 1.1 jtc return(-1);
175 1.1 jtc }
176 1.1 jtc if (S_ISDIR(arsb.st_mode)) {
177 1.1 jtc warn(0, "Cannot write an archive on top of a directory %s",
178 1.1 jtc arcname);
179 1.1 jtc (void)close(arfd);
180 1.1 jtc arfd = -1;
181 1.1 jtc can_unlnk = 0;
182 1.1 jtc return(-1);
183 1.1 jtc }
184 1.1 jtc
185 1.1 jtc if (S_ISCHR(arsb.st_mode))
186 1.1 jtc artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE;
187 1.1 jtc else if (S_ISBLK(arsb.st_mode))
188 1.1 jtc artyp = ISBLK;
189 1.1 jtc else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE))
190 1.1 jtc artyp = ISPIPE;
191 1.1 jtc else
192 1.1 jtc artyp = ISREG;
193 1.1 jtc
194 1.1 jtc /*
195 1.1 jtc * make sure we beyond any doubt that we only can unlink regular files
196 1.1 jtc * we created
197 1.1 jtc */
198 1.1 jtc if (artyp != ISREG)
199 1.1 jtc can_unlnk = 0;
200 1.1 jtc /*
201 1.1 jtc * if we are writing, we are done
202 1.1 jtc */
203 1.1 jtc if (act == ARCHIVE) {
204 1.1 jtc blksz = rdblksz = wrblksz;
205 1.1 jtc lstrval = 1;
206 1.1 jtc return(0);
207 1.1 jtc }
208 1.1 jtc
209 1.1 jtc /*
210 1.1 jtc * set default blksz on read. APPNDs writes rdblksz on the last volume
211 1.1 jtc * On all new archive volumes, we shift to wrblksz (if the user
212 1.1 jtc * specified one, otherwize we will continue to use rdblksz). We
213 1.1 jtc * must to set blocksize based on what kind of device the archive is
214 1.1 jtc * stored.
215 1.1 jtc */
216 1.1 jtc switch(artyp) {
217 1.1 jtc case ISTAPE:
218 1.1 jtc /*
219 1.1 jtc * Tape drives come in at least two flavors. Those that support
220 1.1 jtc * variable sized records and those that have fixed sized
221 1.1 jtc * records. They must be treated differently. For tape drives
222 1.1 jtc * that support variable sized records, we must make large
223 1.1 jtc * reads to make sure we get the entire record, otherwise we
224 1.1 jtc * will just get the first part of the record (up to size we
225 1.1 jtc * asked). Tapes with fixed sized records may or may not return
226 1.1 jtc * multiple records in a single read. We really do not care
227 1.1 jtc * what the physical record size is UNLESS we are going to
228 1.1 jtc * append. (We will need the physical block size to rewrite
229 1.1 jtc * the trailer). Only when we are appending do we go to the
230 1.1 jtc * effort to figure out the true PHYSICAL record size.
231 1.1 jtc */
232 1.1 jtc blksz = rdblksz = MAXBLK;
233 1.1 jtc break;
234 1.1 jtc case ISPIPE:
235 1.1 jtc case ISBLK:
236 1.1 jtc case ISCHR:
237 1.1 jtc /*
238 1.1 jtc * Blocksize is not a major issue with these devices (but must
239 1.1 jtc * be kept a multiple of 512). If the user specified a write
240 1.1 jtc * block size, we use that to read. Under append, we must
241 1.1 jtc * always keep blksz == rdblksz. Otherwise we go ahead and use
242 1.1 jtc * the device optimal blocksize as (and if) returned by stat
243 1.1 jtc * and if it is within pax specs.
244 1.1 jtc */
245 1.1 jtc if ((act == APPND) && wrblksz) {
246 1.1 jtc blksz = rdblksz = wrblksz;
247 1.1 jtc break;
248 1.1 jtc }
249 1.1 jtc
250 1.1 jtc if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) &&
251 1.1 jtc ((arsb.st_blksize % BLKMULT) == 0))
252 1.1 jtc rdblksz = arsb.st_blksize;
253 1.1 jtc else
254 1.1 jtc rdblksz = DEVBLK;
255 1.1 jtc /*
256 1.1 jtc * For performance go for large reads when we can without harm
257 1.1 jtc */
258 1.1 jtc if ((act == APPND) || (artyp == ISCHR))
259 1.1 jtc blksz = rdblksz;
260 1.1 jtc else
261 1.1 jtc blksz = MAXBLK;
262 1.1 jtc break;
263 1.1 jtc case ISREG:
264 1.1 jtc /*
265 1.1 jtc * if the user specified wrblksz works, use it. Under appends
266 1.1 jtc * we must always keep blksz == rdblksz
267 1.1 jtc */
268 1.1 jtc if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){
269 1.1 jtc blksz = rdblksz = wrblksz;
270 1.1 jtc break;
271 1.1 jtc }
272 1.1 jtc /*
273 1.1 jtc * See if we can find the blocking factor from the file size
274 1.1 jtc */
275 1.1 jtc for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT)
276 1.1 jtc if ((arsb.st_size % rdblksz) == 0)
277 1.1 jtc break;
278 1.1 jtc /*
279 1.1 jtc * When we cannont find a match, we may have a flawed archive.
280 1.1 jtc */
281 1.1 jtc if (rdblksz <= 0)
282 1.1 jtc rdblksz = FILEBLK;
283 1.1 jtc /*
284 1.1 jtc * for performance go for large reads when we can
285 1.1 jtc */
286 1.1 jtc if (act == APPND)
287 1.1 jtc blksz = rdblksz;
288 1.1 jtc else
289 1.1 jtc blksz = MAXBLK;
290 1.1 jtc break;
291 1.1 jtc default:
292 1.1 jtc /*
293 1.1 jtc * should never happen, worse case, slow...
294 1.1 jtc */
295 1.1 jtc blksz = rdblksz = BLKMULT;
296 1.1 jtc break;
297 1.1 jtc }
298 1.1 jtc lstrval = 1;
299 1.1 jtc return(0);
300 1.1 jtc }
301 1.1 jtc
302 1.1 jtc /*
303 1.1 jtc * ar_close()
304 1.1 jtc * closes archive device, increments volume number, and prints i/o summary
305 1.1 jtc */
306 1.1 jtc #if __STDC__
307 1.1 jtc void
308 1.1 jtc ar_close(void)
309 1.1 jtc #else
310 1.1 jtc void
311 1.1 jtc ar_close()
312 1.1 jtc #endif
313 1.1 jtc {
314 1.1 jtc FILE *outf;
315 1.1 jtc
316 1.1 jtc if (arfd < 0) {
317 1.1 jtc did_io = io_ok = flcnt = 0;
318 1.1 jtc return;
319 1.1 jtc }
320 1.1 jtc
321 1.1 jtc if (act == LIST)
322 1.1 jtc outf = stdout;
323 1.1 jtc else
324 1.1 jtc outf = stderr;
325 1.1 jtc
326 1.1 jtc /*
327 1.1 jtc * Close archive file. This may take a LONG while on tapes (we may be
328 1.1 jtc * forced to wait for the rewind to complete) so tell the user what is
329 1.1 jtc * going on (this avoids the user hitting control-c thinking pax is
330 1.1 jtc * broken).
331 1.1 jtc */
332 1.1 jtc if (vflag && (artyp == ISTAPE)) {
333 1.1 jtc if (vfpart)
334 1.1 jtc (void)putc('\n', outf);
335 1.1 jtc (void)fprintf(outf,
336 1.1 jtc "%s: Waiting for tape drive close to complete...",
337 1.1 jtc argv0);
338 1.1 jtc (void)fflush(outf);
339 1.1 jtc }
340 1.1 jtc
341 1.1 jtc /*
342 1.1 jtc * if nothing was written to the archive (and we created it), we remove
343 1.1 jtc * it
344 1.1 jtc */
345 1.1 jtc if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) &&
346 1.1 jtc (arsb.st_size == 0)) {
347 1.1 jtc (void)unlink(arcname);
348 1.1 jtc can_unlnk = 0;
349 1.1 jtc }
350 1.1 jtc
351 1.1 jtc (void)close(arfd);
352 1.1 jtc
353 1.1 jtc if (vflag && (artyp == ISTAPE)) {
354 1.1 jtc (void)fputs("done.\n", outf);
355 1.1 jtc vfpart = 0;
356 1.1 jtc (void)fflush(outf);
357 1.1 jtc }
358 1.1 jtc arfd = -1;
359 1.1 jtc
360 1.1 jtc if (!io_ok && !did_io) {
361 1.1 jtc flcnt = 0;
362 1.1 jtc return;
363 1.1 jtc }
364 1.1 jtc did_io = io_ok = 0;
365 1.1 jtc
366 1.1 jtc /*
367 1.1 jtc * The volume number is only increased when the last device has data
368 1.1 jtc * and we have already determined the archive format.
369 1.1 jtc */
370 1.1 jtc if (frmt != NULL)
371 1.1 jtc ++arvol;
372 1.1 jtc
373 1.1 jtc if (!vflag) {
374 1.1 jtc flcnt = 0;
375 1.1 jtc return;
376 1.1 jtc }
377 1.1 jtc
378 1.1 jtc /*
379 1.1 jtc * Print out a summary of I/O for this archive volume.
380 1.1 jtc */
381 1.1 jtc if (vfpart) {
382 1.1 jtc (void)putc('\n', outf);
383 1.1 jtc vfpart = 0;
384 1.1 jtc }
385 1.1 jtc
386 1.1 jtc /*
387 1.1 jtc * If we have not determined the format yet, we just say how many bytes
388 1.1 jtc * we have skipped over looking for a header to id. there is no way we
389 1.1 jtc * could have written anything yet.
390 1.1 jtc */
391 1.1 jtc if (frmt == NULL) {
392 1.1 jtc # ifdef NET2_STAT
393 1.1 jtc (void)fprintf(outf, "%s: unknown format, %lu bytes skipped.\n",
394 1.1 jtc # else
395 1.1 jtc (void)fprintf(outf, "%s: unknown format, %qu bytes skipped.\n",
396 1.1 jtc # endif
397 1.1 jtc argv0, rdcnt);
398 1.1 jtc (void)fflush(outf);
399 1.1 jtc flcnt = 0;
400 1.1 jtc return;
401 1.1 jtc }
402 1.1 jtc
403 1.1 jtc (void)fprintf(outf,
404 1.1 jtc # ifdef NET2_STAT
405 1.1 jtc "%s: %s vol %d, %lu files, %lu bytes read, %lu bytes written.\n",
406 1.1 jtc # else
407 1.1 jtc "%s: %s vol %d, %lu files, %qu bytes read, %qu bytes written.\n",
408 1.1 jtc # endif
409 1.1 jtc argv0, frmt->name, arvol-1, flcnt, rdcnt, wrcnt);
410 1.1 jtc (void)fflush(outf);
411 1.1 jtc flcnt = 0;
412 1.1 jtc }
413 1.1 jtc
414 1.1 jtc /*
415 1.1 jtc * ar_drain()
416 1.1 jtc * drain any archive format independent padding from an archive read
417 1.1 jtc * from a socket or a pipe. This is to prevent the process on the
418 1.1 jtc * other side of the pipe from getting a SIGPIPE (pax will stop
419 1.1 jtc * reading an archive once a format dependent trailer is detected).
420 1.1 jtc */
421 1.1 jtc #if __STDC__
422 1.1 jtc void
423 1.1 jtc ar_drain(void)
424 1.1 jtc #else
425 1.1 jtc void
426 1.1 jtc ar_drain()
427 1.1 jtc #endif
428 1.1 jtc {
429 1.1 jtc register int res;
430 1.1 jtc char drbuf[MAXBLK];
431 1.1 jtc
432 1.1 jtc /*
433 1.1 jtc * we only drain from a pipe/socket. Other devices can be closed
434 1.1 jtc * without reading up to end of file. We sure hope that pipe is closed
435 1.1 jtc * on the other side so we will get an EOF.
436 1.1 jtc */
437 1.1 jtc if ((artyp != ISPIPE) || (lstrval <= 0))
438 1.1 jtc return;
439 1.1 jtc
440 1.1 jtc /*
441 1.1 jtc * keep reading until pipe is drained
442 1.1 jtc */
443 1.1 jtc while ((res = read(arfd, drbuf, sizeof(drbuf))) > 0)
444 1.1 jtc ;
445 1.1 jtc lstrval = res;
446 1.1 jtc }
447 1.1 jtc
448 1.1 jtc /*
449 1.1 jtc * ar_set_wr()
450 1.1 jtc * Set up device right before switching from read to write in an append.
451 1.1 jtc * device dependent code (if required) to do this should be added here.
452 1.1 jtc * For all archive devices we are already positioned at the place we want
453 1.1 jtc * to start writing when this routine is called.
454 1.1 jtc * Return:
455 1.1 jtc * 0 if all ready to write, -1 otherwise
456 1.1 jtc */
457 1.1 jtc
458 1.1 jtc #if __STDC__
459 1.1 jtc int
460 1.1 jtc ar_set_wr(void)
461 1.1 jtc #else
462 1.1 jtc int
463 1.1 jtc ar_set_wr()
464 1.1 jtc #endif
465 1.1 jtc {
466 1.1 jtc off_t cpos;
467 1.1 jtc
468 1.1 jtc /*
469 1.1 jtc * we must make sure the trailer is rewritten on append, ar_next()
470 1.1 jtc * will stop us if the archive containing the trailer was not written
471 1.1 jtc */
472 1.1 jtc wr_trail = 0;
473 1.1 jtc
474 1.1 jtc /*
475 1.1 jtc * Add any device dependent code as required here
476 1.1 jtc */
477 1.1 jtc if (artyp != ISREG)
478 1.1 jtc return(0);
479 1.1 jtc /*
480 1.1 jtc * Ok we have an archive in a regular file. If we were rewriting a
481 1.1 jtc * file, we must get rid of all the stuff after the current offset
482 1.1 jtc * (it was not written by pax).
483 1.1 jtc */
484 1.1 jtc if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) ||
485 1.1 jtc (ftruncate(arfd, cpos) < 0)) {
486 1.1 jtc syswarn(1, errno, "Unable to truncate archive file");
487 1.1 jtc return(-1);
488 1.1 jtc }
489 1.1 jtc return(0);
490 1.1 jtc }
491 1.1 jtc
492 1.1 jtc /*
493 1.1 jtc * ar_app_ok()
494 1.1 jtc * check if the last volume in the archive allows appends. We cannot check
495 1.1 jtc * this until we are ready to write since there is no spec that says all
496 1.1 jtc * volumes in a single archive have to be of the same type...
497 1.1 jtc * Return:
498 1.1 jtc * 0 if we can append, -1 otherwise.
499 1.1 jtc */
500 1.1 jtc
501 1.1 jtc #if __STDC__
502 1.1 jtc int
503 1.1 jtc ar_app_ok(void)
504 1.1 jtc #else
505 1.1 jtc int
506 1.1 jtc ar_app_ok()
507 1.1 jtc #endif
508 1.1 jtc {
509 1.1 jtc if (artyp == ISPIPE) {
510 1.1 jtc warn(1, "Cannot append to an archive obtained from a pipe.");
511 1.1 jtc return(-1);
512 1.1 jtc }
513 1.1 jtc
514 1.1 jtc if (!invld_rec)
515 1.1 jtc return(0);
516 1.1 jtc warn(1,"Cannot append, device record size %d does not support %s spec",
517 1.1 jtc rdblksz, argv0);
518 1.1 jtc return(-1);
519 1.1 jtc }
520 1.1 jtc
521 1.1 jtc /*
522 1.1 jtc * ar_read()
523 1.1 jtc * read up to a specified number of bytes from the archive into the
524 1.1 jtc * supplied buffer. When dealing with tapes we may not always be able to
525 1.1 jtc * read what we want.
526 1.1 jtc * Return:
527 1.1 jtc * Number of bytes in buffer. 0 for end of file, -1 for a read error.
528 1.1 jtc */
529 1.1 jtc
530 1.1 jtc #if __STDC__
531 1.1 jtc int
532 1.1 jtc ar_read(register char *buf, register int cnt)
533 1.1 jtc #else
534 1.1 jtc int
535 1.1 jtc ar_read(buf, cnt)
536 1.1 jtc register char *buf;
537 1.1 jtc register int cnt;
538 1.1 jtc #endif
539 1.1 jtc {
540 1.1 jtc register int res = 0;
541 1.1 jtc
542 1.1 jtc /*
543 1.1 jtc * if last i/o was in error, no more reads until reset or new volume
544 1.1 jtc */
545 1.1 jtc if (lstrval <= 0)
546 1.1 jtc return(lstrval);
547 1.1 jtc
548 1.1 jtc /*
549 1.1 jtc * how we read must be based on device type
550 1.1 jtc */
551 1.1 jtc switch (artyp) {
552 1.1 jtc case ISTAPE:
553 1.1 jtc if ((res = read(arfd, buf, cnt)) > 0) {
554 1.1 jtc /*
555 1.1 jtc * CAUTION: tape systems may not always return the same
556 1.1 jtc * sized records so we leave blksz == MAXBLK. The
557 1.1 jtc * physical record size that a tape drive supports is
558 1.1 jtc * very hard to determine in a uniform and portable
559 1.1 jtc * manner.
560 1.1 jtc */
561 1.1 jtc io_ok = 1;
562 1.1 jtc if (res != rdblksz) {
563 1.1 jtc /*
564 1.1 jtc * Record size changed. If this is happens on
565 1.1 jtc * any record after the first, we probably have
566 1.1 jtc * a tape drive which has a fixed record size
567 1.1 jtc * we are getting multiple records in a single
568 1.1 jtc * read). Watch out for record blocking that
569 1.1 jtc * violates pax spec (must be a multiple of
570 1.1 jtc * BLKMULT).
571 1.1 jtc */
572 1.1 jtc rdblksz = res;
573 1.1 jtc if (rdblksz % BLKMULT)
574 1.1 jtc invld_rec = 1;
575 1.1 jtc }
576 1.1 jtc return(res);
577 1.1 jtc }
578 1.1 jtc break;
579 1.1 jtc case ISREG:
580 1.1 jtc case ISBLK:
581 1.1 jtc case ISCHR:
582 1.1 jtc case ISPIPE:
583 1.1 jtc default:
584 1.1 jtc /*
585 1.1 jtc * Files are so easy to deal with. These other things cannot
586 1.1 jtc * be trusted at all. So when we are dealing with character
587 1.1 jtc * devices and pipes we just take what they have ready for us
588 1.1 jtc * and return. Trying to do anything else with them runs the
589 1.1 jtc * risk of failure.
590 1.1 jtc */
591 1.1 jtc if ((res = read(arfd, buf, cnt)) > 0) {
592 1.1 jtc io_ok = 1;
593 1.1 jtc return(res);
594 1.1 jtc }
595 1.1 jtc break;
596 1.1 jtc }
597 1.1 jtc
598 1.1 jtc /*
599 1.1 jtc * We are in trouble at this point, something is broken...
600 1.1 jtc */
601 1.1 jtc lstrval = res;
602 1.1 jtc if (res < 0)
603 1.1 jtc syswarn(1, errno, "Failed read on archive volume %d", arvol);
604 1.1 jtc else
605 1.1 jtc warn(0, "End of archive volume %d reached", arvol);
606 1.1 jtc return(res);
607 1.1 jtc }
608 1.1 jtc
609 1.1 jtc /*
610 1.1 jtc * ar_write()
611 1.1 jtc * Write a specified number of bytes in supplied buffer to the archive
612 1.1 jtc * device so it appears as a single "block". Deals with errors and tries
613 1.1 jtc * to recover when faced with short writes.
614 1.1 jtc * Return:
615 1.1 jtc * Number of bytes written. 0 indicates end of volume reached and with no
616 1.1 jtc * flaws (as best that can be detected). A -1 indicates an unrecoverable
617 1.1 jtc * error in the archive occured.
618 1.1 jtc */
619 1.1 jtc
620 1.1 jtc #if __STDC__
621 1.1 jtc int
622 1.1 jtc ar_write(register char *buf, register int bsz)
623 1.1 jtc #else
624 1.1 jtc int
625 1.1 jtc ar_write(buf, bsz)
626 1.1 jtc register char *buf;
627 1.1 jtc register int bsz;
628 1.1 jtc #endif
629 1.1 jtc {
630 1.1 jtc register int res;
631 1.1 jtc off_t cpos;
632 1.1 jtc
633 1.1 jtc /*
634 1.1 jtc * do not allow pax to create a "bad" archive. Once a write fails on
635 1.1 jtc * an archive volume prevent further writes to it.
636 1.1 jtc */
637 1.1 jtc if (lstrval <= 0)
638 1.1 jtc return(lstrval);
639 1.1 jtc
640 1.1 jtc if ((res = write(arfd, buf, bsz)) == bsz) {
641 1.1 jtc wr_trail = 1;
642 1.1 jtc io_ok = 1;
643 1.1 jtc return(bsz);
644 1.1 jtc }
645 1.1 jtc /*
646 1.1 jtc * write broke, see what we can do with it. We try to send any partial
647 1.1 jtc * writes that may violate pax spec to the next archive volume.
648 1.1 jtc */
649 1.1 jtc if (res < 0)
650 1.1 jtc lstrval = res;
651 1.1 jtc else
652 1.1 jtc lstrval = 0;
653 1.1 jtc
654 1.1 jtc switch (artyp) {
655 1.1 jtc case ISREG:
656 1.1 jtc if ((res > 0) && (res % BLKMULT)) {
657 1.1 jtc /*
658 1.1 jtc * try to fix up partial writes which are not BLKMULT
659 1.1 jtc * in size by forcing the runt record to next archive
660 1.1 jtc * volume
661 1.1 jtc */
662 1.1 jtc if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
663 1.1 jtc break;
664 1.1 jtc cpos -= (off_t)res;
665 1.1 jtc if (ftruncate(arfd, cpos) < 0)
666 1.1 jtc break;
667 1.1 jtc res = lstrval = 0;
668 1.1 jtc break;
669 1.1 jtc }
670 1.1 jtc if (res >= 0)
671 1.1 jtc break;
672 1.1 jtc /*
673 1.1 jtc * if file is out of space, handle it like a return of 0
674 1.1 jtc */
675 1.1 jtc if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT))
676 1.1 jtc res = lstrval = 0;
677 1.1 jtc break;
678 1.1 jtc case ISTAPE:
679 1.1 jtc case ISCHR:
680 1.1 jtc case ISBLK:
681 1.1 jtc if (res >= 0)
682 1.1 jtc break;
683 1.1 jtc if (errno == EACCES) {
684 1.1 jtc warn(0, "Write failed, archive is write protected.");
685 1.1 jtc res = lstrval = 0;
686 1.1 jtc return(0);
687 1.1 jtc }
688 1.1 jtc /*
689 1.1 jtc * see if we reached the end of media, if so force a change to
690 1.1 jtc * the next volume
691 1.1 jtc */
692 1.1 jtc if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO))
693 1.1 jtc res = lstrval = 0;
694 1.1 jtc break;
695 1.1 jtc case ISPIPE:
696 1.1 jtc default:
697 1.1 jtc /*
698 1.1 jtc * we cannot fix errors to these devices
699 1.1 jtc */
700 1.1 jtc break;
701 1.1 jtc }
702 1.1 jtc
703 1.1 jtc /*
704 1.1 jtc * Better tell the user the bad news...
705 1.1 jtc * if this is a block aligned archive format, we may have a bad archive
706 1.1 jtc * if the format wants the header to start at a BLKMULT boundry. While
707 1.1 jtc * we can deal with the mis-aligned data, it violates spec and other
708 1.1 jtc * archive readers will likely fail. if the format is not block
709 1.1 jtc * aligned, the user may be lucky (and the archive is ok).
710 1.1 jtc */
711 1.1 jtc if (res >= 0) {
712 1.1 jtc if (res > 0)
713 1.1 jtc wr_trail = 1;
714 1.1 jtc io_ok = 1;
715 1.1 jtc }
716 1.1 jtc
717 1.1 jtc /*
718 1.1 jtc * If we were trying to rewrite the trailer and it didn't work, we
719 1.1 jtc * must quit right away.
720 1.1 jtc */
721 1.1 jtc if (!wr_trail && (res <= 0)) {
722 1.1 jtc warn(1,"Unable to append, trailer re-write failed. Quitting.");
723 1.1 jtc return(res);
724 1.1 jtc }
725 1.1 jtc
726 1.1 jtc if (res == 0)
727 1.1 jtc warn(0, "End of archive volume %d reached", arvol);
728 1.1 jtc else if (res < 0)
729 1.1 jtc syswarn(1, errno, "Failed write to archive volume: %d", arvol);
730 1.1 jtc else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0))
731 1.1 jtc warn(0,"WARNING: partial archive write. Archive MAY BE FLAWED");
732 1.1 jtc else
733 1.1 jtc warn(1,"WARNING: partial archive write. Archive IS FLAWED");
734 1.1 jtc return(res);
735 1.1 jtc }
736 1.1 jtc
737 1.1 jtc /*
738 1.1 jtc * ar_rdsync()
739 1.1 jtc * Try to move past a bad spot on a flawed archive as needed to continue
740 1.1 jtc * I/O. Clears error flags to allow I/O to continue.
741 1.1 jtc * Return:
742 1.1 jtc * 0 when ok to try i/o again, -1 otherwise.
743 1.1 jtc */
744 1.1 jtc
745 1.1 jtc #if __STDC__
746 1.1 jtc int
747 1.1 jtc ar_rdsync(void)
748 1.1 jtc #else
749 1.1 jtc int
750 1.1 jtc ar_rdsync()
751 1.1 jtc #endif
752 1.1 jtc {
753 1.1 jtc long fsbz;
754 1.1 jtc off_t cpos;
755 1.1 jtc off_t mpos;
756 1.1 jtc struct mtop mb;
757 1.1 jtc
758 1.1 jtc /*
759 1.1 jtc * Fail resync attempts at user request (done) or this is going to be
760 1.1 jtc * an update/append to a existing archive. if last i/o hit media end,
761 1.1 jtc * we need to go to the next volume not try a resync
762 1.1 jtc */
763 1.1 jtc if ((done > 0) || (lstrval == 0))
764 1.1 jtc return(-1);
765 1.1 jtc
766 1.1 jtc if ((act == APPND) || (act == ARCHIVE)) {
767 1.1 jtc warn(1, "Cannot allow updates to an archive with flaws.");
768 1.1 jtc return(-1);
769 1.1 jtc }
770 1.1 jtc if (io_ok)
771 1.1 jtc did_io = 1;
772 1.1 jtc
773 1.1 jtc switch(artyp) {
774 1.1 jtc case ISTAPE:
775 1.1 jtc /*
776 1.1 jtc * if the last i/o was a successful data transfer, we assume
777 1.1 jtc * the fault is just a bad record on the tape that we are now
778 1.1 jtc * past. If we did not get any data since the last resync try
779 1.1 jtc * to move the tape foward one PHYSICAL record past any
780 1.1 jtc * damaged tape section. Some tape drives are stubborn and need
781 1.1 jtc * to be pushed.
782 1.1 jtc */
783 1.1 jtc if (io_ok) {
784 1.1 jtc io_ok = 0;
785 1.1 jtc lstrval = 1;
786 1.1 jtc break;
787 1.1 jtc }
788 1.1 jtc mb.mt_op = MTFSR;
789 1.1 jtc mb.mt_count = 1;
790 1.1 jtc if (ioctl(arfd, MTIOCTOP, &mb) < 0)
791 1.1 jtc break;
792 1.1 jtc lstrval = 1;
793 1.1 jtc break;
794 1.1 jtc case ISREG:
795 1.1 jtc case ISCHR:
796 1.1 jtc case ISBLK:
797 1.1 jtc /*
798 1.1 jtc * try to step over the bad part of the device.
799 1.1 jtc */
800 1.1 jtc io_ok = 0;
801 1.1 jtc if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG))
802 1.1 jtc fsbz = BLKMULT;
803 1.1 jtc if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
804 1.1 jtc break;
805 1.1 jtc mpos = fsbz - (cpos % (off_t)fsbz);
806 1.1 jtc if (lseek(arfd, mpos, SEEK_CUR) < 0)
807 1.1 jtc break;
808 1.1 jtc lstrval = 1;
809 1.1 jtc break;
810 1.1 jtc case ISPIPE:
811 1.1 jtc default:
812 1.1 jtc /*
813 1.1 jtc * cannot recover on these archive device types
814 1.1 jtc */
815 1.1 jtc io_ok = 0;
816 1.1 jtc break;
817 1.1 jtc }
818 1.1 jtc if (lstrval <= 0) {
819 1.1 jtc warn(1, "Unable to recover from an archive read failure.");
820 1.1 jtc return(-1);
821 1.1 jtc }
822 1.1 jtc warn(0, "Attempting to recover from an archive read failure.");
823 1.1 jtc return(0);
824 1.1 jtc }
825 1.1 jtc
826 1.1 jtc /*
827 1.1 jtc * ar_fow()
828 1.1 jtc * Move the I/O position within the archive foward the specified number of
829 1.1 jtc * bytes as supported by the device. If we cannot move the requested
830 1.1 jtc * number of bytes, return the actual number of bytes moved in skipped.
831 1.1 jtc * Return:
832 1.1 jtc * 0 if moved the requested distance, -1 on complete failure, 1 on
833 1.1 jtc * partial move (the amount moved is in skipped)
834 1.1 jtc */
835 1.1 jtc
836 1.1 jtc #if __STDC__
837 1.1 jtc int
838 1.1 jtc ar_fow(off_t sksz, off_t *skipped)
839 1.1 jtc #else
840 1.1 jtc int
841 1.1 jtc ar_fow(sksz, skipped)
842 1.1 jtc off_t sksz;
843 1.1 jtc off_t *skipped;
844 1.1 jtc #endif
845 1.1 jtc {
846 1.1 jtc off_t cpos;
847 1.1 jtc off_t mpos;
848 1.1 jtc
849 1.1 jtc *skipped = 0;
850 1.1 jtc if (sksz <= 0)
851 1.1 jtc return(0);
852 1.1 jtc
853 1.1 jtc /*
854 1.1 jtc * we cannot move foward at EOF or error
855 1.1 jtc */
856 1.1 jtc if (lstrval <= 0)
857 1.1 jtc return(lstrval);
858 1.1 jtc
859 1.1 jtc /*
860 1.1 jtc * Safer to read forward on devices where it is hard to find the end of
861 1.1 jtc * the media without reading to it. With tapes we cannot be sure of the
862 1.1 jtc * number of physical blocks to skip (we do not know physical block
863 1.1 jtc * size at this point), so we must only read foward on tapes!
864 1.1 jtc */
865 1.1 jtc if (artyp != ISREG)
866 1.1 jtc return(0);
867 1.1 jtc
868 1.1 jtc /*
869 1.1 jtc * figure out where we are in the archive
870 1.1 jtc */
871 1.1 jtc if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) {
872 1.1 jtc /*
873 1.1 jtc * we can be asked to move farther than there are bytes in this
874 1.1 jtc * volume, if so, just go to file end and let normal buf_fill()
875 1.1 jtc * deal with the end of file (it will go to next volume by
876 1.1 jtc * itself)
877 1.1 jtc */
878 1.1 jtc if ((mpos = cpos + sksz) > arsb.st_size) {
879 1.1 jtc *skipped = arsb.st_size - cpos;
880 1.1 jtc mpos = arsb.st_size;
881 1.1 jtc } else
882 1.1 jtc *skipped = sksz;
883 1.1 jtc if (lseek(arfd, mpos, SEEK_SET) >= 0)
884 1.1 jtc return(0);
885 1.1 jtc }
886 1.1 jtc syswarn(1, errno, "Foward positioning operation on archive failed");
887 1.1 jtc lstrval = -1;
888 1.1 jtc return(-1);
889 1.1 jtc }
890 1.1 jtc
891 1.1 jtc /*
892 1.1 jtc * ar_rev()
893 1.1 jtc * move the i/o position within the archive backwards the specified byte
894 1.1 jtc * count as supported by the device. With tapes drives we RESET rdblksz to
895 1.1 jtc * the PHYSICAL blocksize.
896 1.1 jtc * NOTE: We should only be called to move backwards so we can rewrite the
897 1.1 jtc * last records (the trailer) of an archive (APPEND).
898 1.1 jtc * Return:
899 1.1 jtc * 0 if moved the requested distance, -1 on complete failure
900 1.1 jtc */
901 1.1 jtc
902 1.1 jtc #if __STDC__
903 1.1 jtc int
904 1.1 jtc ar_rev(off_t sksz)
905 1.1 jtc #else
906 1.1 jtc int
907 1.1 jtc ar_rev(sksz)
908 1.1 jtc off_t sksz;
909 1.1 jtc #endif
910 1.1 jtc {
911 1.1 jtc off_t cpos;
912 1.1 jtc struct mtop mb;
913 1.1 jtc register int phyblk;
914 1.1 jtc
915 1.1 jtc /*
916 1.1 jtc * make sure we do not have try to reverse on a flawed archive
917 1.1 jtc */
918 1.1 jtc if (lstrval < 0)
919 1.1 jtc return(lstrval);
920 1.1 jtc
921 1.1 jtc switch(artyp) {
922 1.1 jtc case ISPIPE:
923 1.1 jtc if (sksz <= 0)
924 1.1 jtc break;
925 1.1 jtc /*
926 1.1 jtc * cannot go backwards on these critters
927 1.1 jtc */
928 1.1 jtc warn(1, "Reverse positioning on pipes is not supported.");
929 1.1 jtc lstrval = -1;
930 1.1 jtc return(-1);
931 1.1 jtc case ISREG:
932 1.1 jtc case ISBLK:
933 1.1 jtc case ISCHR:
934 1.1 jtc default:
935 1.1 jtc if (sksz <= 0)
936 1.1 jtc break;
937 1.1 jtc
938 1.1 jtc /*
939 1.1 jtc * For things other than files, backwards movement has a very
940 1.1 jtc * high probability of failure as we really do not know the
941 1.1 jtc * true attributes of the device we are talking to (the device
942 1.1 jtc * may not even have the ability to lseek() in any direction).
943 1.1 jtc * First we figure out where we are in the archive.
944 1.1 jtc */
945 1.1 jtc if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) {
946 1.1 jtc syswarn(1, errno,
947 1.1 jtc "Unable to obtain current archive byte offset");
948 1.1 jtc lstrval = -1;
949 1.1 jtc return(-1);
950 1.1 jtc }
951 1.1 jtc
952 1.1 jtc /*
953 1.1 jtc * we may try to go backwards past the start when the archive
954 1.1 jtc * is only a single record. If this hapens and we are on a
955 1.1 jtc * multi volume archive, we need to go to the end of the
956 1.1 jtc * previous volume and continue our movement backwards from
957 1.1 jtc * there.
958 1.1 jtc */
959 1.1 jtc if ((cpos -= sksz) < (off_t)0L) {
960 1.1 jtc if (arvol > 1) {
961 1.1 jtc /*
962 1.1 jtc * this should never happen
963 1.1 jtc */
964 1.1 jtc warn(1,"Reverse position on previous volume.");
965 1.1 jtc lstrval = -1;
966 1.1 jtc return(-1);
967 1.1 jtc }
968 1.1 jtc cpos = (off_t)0L;
969 1.1 jtc }
970 1.1 jtc if (lseek(arfd, cpos, SEEK_SET) < 0) {
971 1.1 jtc syswarn(1, errno, "Unable to seek archive backwards");
972 1.1 jtc lstrval = -1;
973 1.1 jtc return(-1);
974 1.1 jtc }
975 1.1 jtc break;
976 1.1 jtc case ISTAPE:
977 1.1 jtc /*
978 1.1 jtc * Calculate and move the proper number of PHYSICAL tape
979 1.1 jtc * blocks. If the sksz is not an even multiple of the physical
980 1.1 jtc * tape size, we cannot do the move (this should never happen).
981 1.1 jtc * (We also cannot handler trailers spread over two vols).
982 1.1 jtc * get_phys() also makes sure we are in front of the filemark.
983 1.1 jtc */
984 1.1 jtc if ((phyblk = get_phys()) <= 0) {
985 1.1 jtc lstrval = -1;
986 1.1 jtc return(-1);
987 1.1 jtc }
988 1.1 jtc
989 1.1 jtc /*
990 1.1 jtc * make sure future tape reads only go by physical tape block
991 1.1 jtc * size (set rdblksz to the real size).
992 1.1 jtc */
993 1.1 jtc rdblksz = phyblk;
994 1.1 jtc
995 1.1 jtc /*
996 1.1 jtc * if no movement is required, just return (we must be after
997 1.1 jtc * get_phys() so the physical blocksize is properly set)
998 1.1 jtc */
999 1.1 jtc if (sksz <= 0)
1000 1.1 jtc break;
1001 1.1 jtc
1002 1.1 jtc /*
1003 1.1 jtc * ok we have to move. Make sure the tape drive can do it.
1004 1.1 jtc */
1005 1.1 jtc if (sksz % phyblk) {
1006 1.1 jtc warn(1,
1007 1.1 jtc "Tape drive unable to backspace requested amount");
1008 1.1 jtc lstrval = -1;
1009 1.1 jtc return(-1);
1010 1.1 jtc }
1011 1.1 jtc
1012 1.1 jtc /*
1013 1.1 jtc * move backwards the requested number of bytes
1014 1.1 jtc */
1015 1.1 jtc mb.mt_op = MTBSR;
1016 1.1 jtc mb.mt_count = sksz/phyblk;
1017 1.1 jtc if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1018 1.1 jtc syswarn(1,errno, "Unable to backspace tape %d blocks.",
1019 1.1 jtc mb.mt_count);
1020 1.1 jtc lstrval = -1;
1021 1.1 jtc return(-1);
1022 1.1 jtc }
1023 1.1 jtc break;
1024 1.1 jtc }
1025 1.1 jtc lstrval = 1;
1026 1.1 jtc return(0);
1027 1.1 jtc }
1028 1.1 jtc
1029 1.1 jtc /*
1030 1.1 jtc * get_phys()
1031 1.1 jtc * Determine the physical block size on a tape drive. We need the physical
1032 1.1 jtc * block size so we know how many bytes we skip over when we move with
1033 1.1 jtc * mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
1034 1.1 jtc * return.
1035 1.1 jtc * This is one really SLOW routine...
1036 1.1 jtc * Return:
1037 1.1 jtc * physical block size if ok (ok > 0), -1 otherwise
1038 1.1 jtc */
1039 1.1 jtc
1040 1.1 jtc #if __STDC__
1041 1.1 jtc static int
1042 1.1 jtc get_phys(void)
1043 1.1 jtc #else
1044 1.1 jtc static int
1045 1.1 jtc get_phys()
1046 1.1 jtc #endif
1047 1.1 jtc {
1048 1.1 jtc register int padsz = 0;
1049 1.1 jtc register int res;
1050 1.1 jtc register int phyblk;
1051 1.1 jtc struct mtop mb;
1052 1.1 jtc char scbuf[MAXBLK];
1053 1.1 jtc
1054 1.1 jtc /*
1055 1.1 jtc * move to the file mark, and then back up one record and read it.
1056 1.1 jtc * this should tell us the physical record size the tape is using.
1057 1.1 jtc */
1058 1.1 jtc if (lstrval == 1) {
1059 1.1 jtc /*
1060 1.1 jtc * we know we are at file mark when we get back a 0 from
1061 1.1 jtc * read()
1062 1.1 jtc */
1063 1.1 jtc while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0)
1064 1.1 jtc padsz += res;
1065 1.1 jtc if (res < 0) {
1066 1.1 jtc syswarn(1, errno, "Unable to locate tape filemark.");
1067 1.1 jtc return(-1);
1068 1.1 jtc }
1069 1.1 jtc }
1070 1.1 jtc
1071 1.1 jtc /*
1072 1.1 jtc * move backwards over the file mark so we are at the end of the
1073 1.1 jtc * last record.
1074 1.1 jtc */
1075 1.1 jtc mb.mt_op = MTBSF;
1076 1.1 jtc mb.mt_count = 1;
1077 1.1 jtc if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1078 1.1 jtc syswarn(1, errno, "Unable to backspace over tape filemark.");
1079 1.1 jtc return(-1);
1080 1.1 jtc }
1081 1.1 jtc
1082 1.1 jtc /*
1083 1.1 jtc * move backwards so we are in front of the last record and read it to
1084 1.1 jtc * get physical tape blocksize.
1085 1.1 jtc */
1086 1.1 jtc mb.mt_op = MTBSR;
1087 1.1 jtc mb.mt_count = 1;
1088 1.1 jtc if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1089 1.1 jtc syswarn(1, errno, "Unable to backspace over last tape block.");
1090 1.1 jtc return(-1);
1091 1.1 jtc }
1092 1.1 jtc if ((phyblk = read(arfd, scbuf, sizeof(scbuf))) <= 0) {
1093 1.1 jtc syswarn(1, errno, "Cannot determine archive tape blocksize.");
1094 1.1 jtc return(-1);
1095 1.1 jtc }
1096 1.1 jtc
1097 1.1 jtc /*
1098 1.1 jtc * read foward to the file mark, then back up in front of the filemark
1099 1.1 jtc * (this is a bit paranoid, but should be safe to do).
1100 1.1 jtc */
1101 1.1 jtc while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0)
1102 1.1 jtc ;
1103 1.1 jtc if (res < 0) {
1104 1.1 jtc syswarn(1, errno, "Unable to locate tape filemark.");
1105 1.1 jtc return(-1);
1106 1.1 jtc }
1107 1.1 jtc mb.mt_op = MTBSF;
1108 1.1 jtc mb.mt_count = 1;
1109 1.1 jtc if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1110 1.1 jtc syswarn(1, errno, "Unable to backspace over tape filemark.");
1111 1.1 jtc return(-1);
1112 1.1 jtc }
1113 1.1 jtc
1114 1.1 jtc /*
1115 1.1 jtc * set lstrval so we know that the filemark has not been seen
1116 1.1 jtc */
1117 1.1 jtc lstrval = 1;
1118 1.1 jtc
1119 1.1 jtc /*
1120 1.1 jtc * return if there was no padding
1121 1.1 jtc */
1122 1.1 jtc if (padsz == 0)
1123 1.1 jtc return(phyblk);
1124 1.1 jtc
1125 1.1 jtc /*
1126 1.1 jtc * make sure we can move backwards over the padding. (this should
1127 1.1 jtc * never fail).
1128 1.1 jtc */
1129 1.1 jtc if (padsz % phyblk) {
1130 1.1 jtc warn(1, "Tape drive unable to backspace requested amount");
1131 1.1 jtc return(-1);
1132 1.1 jtc }
1133 1.1 jtc
1134 1.1 jtc /*
1135 1.1 jtc * move backwards over the padding so the head is where it was when
1136 1.1 jtc * we were first called (if required).
1137 1.1 jtc */
1138 1.1 jtc mb.mt_op = MTBSR;
1139 1.1 jtc mb.mt_count = padsz/phyblk;
1140 1.1 jtc if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
1141 1.1 jtc syswarn(1,errno,"Unable to backspace tape over %d pad blocks",
1142 1.1 jtc mb.mt_count);
1143 1.1 jtc return(-1);
1144 1.1 jtc }
1145 1.1 jtc return(phyblk);
1146 1.1 jtc }
1147 1.1 jtc
1148 1.1 jtc /*
1149 1.1 jtc * ar_next()
1150 1.1 jtc * prompts the user for the next volume in this archive. For some devices
1151 1.1 jtc * we may allow the media to be changed. Otherwise a new archive is
1152 1.1 jtc * prompted for. By pax spec, if there is no controlling tty or an eof is
1153 1.1 jtc * read on tty input, we must quit pax.
1154 1.1 jtc * Return:
1155 1.1 jtc * 0 when ready to continue, -1 when all done
1156 1.1 jtc */
1157 1.1 jtc
1158 1.1 jtc #if __STDC__
1159 1.1 jtc int
1160 1.1 jtc ar_next(void)
1161 1.1 jtc #else
1162 1.1 jtc int
1163 1.1 jtc ar_next()
1164 1.1 jtc #endif
1165 1.1 jtc {
1166 1.1 jtc char buf[PAXPATHLEN+2];
1167 1.1 jtc static int freeit = 0;
1168 1.1 jtc sigset_t o_mask;
1169 1.1 jtc
1170 1.1 jtc /*
1171 1.1 jtc * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so
1172 1.1 jtc * things like writing EOF etc will be done) (Watch out ar_close() can
1173 1.1 jtc * also be called via a signal handler, so we must prevent a race.
1174 1.1 jtc */
1175 1.1 jtc if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0)
1176 1.1 jtc syswarn(0, errno, "Unable to set signal mask");
1177 1.1 jtc ar_close();
1178 1.1 jtc if (sigprocmask(SIG_SETMASK, &o_mask, (sigset_t *)NULL) < 0)
1179 1.1 jtc syswarn(0, errno, "Unable to restore signal mask");
1180 1.1 jtc
1181 1.1 jtc if (done || !wr_trail)
1182 1.1 jtc return(-1);
1183 1.1 jtc
1184 1.1 jtc tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0);
1185 1.1 jtc
1186 1.1 jtc /*
1187 1.1 jtc * if i/o is on stdin or stdout, we cannot reopen it (we do not know
1188 1.1 jtc * the name), the user will be forced to type it in.
1189 1.1 jtc */
1190 1.1 jtc if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG)
1191 1.1 jtc && (artyp != ISPIPE)) {
1192 1.1 jtc if (artyp == ISTAPE) {
1193 1.1 jtc tty_prnt("%s ready for archive tape volume: %d\n",
1194 1.1 jtc arcname, arvol);
1195 1.1 jtc tty_prnt("Load the NEXT TAPE on the tape drive");
1196 1.1 jtc } else {
1197 1.1 jtc tty_prnt("%s ready for archive volume: %d\n",
1198 1.1 jtc arcname, arvol);
1199 1.1 jtc tty_prnt("Load the NEXT STORAGE MEDIA (if required)");
1200 1.1 jtc }
1201 1.1 jtc
1202 1.1 jtc if ((act == ARCHIVE) || (act == APPND))
1203 1.1 jtc tty_prnt(" and make sure it is WRITE ENABLED.\n");
1204 1.1 jtc else
1205 1.1 jtc tty_prnt("\n");
1206 1.1 jtc
1207 1.1 jtc for(;;) {
1208 1.1 jtc tty_prnt("Type \"y\" to continue, \".\" to quit %s,",
1209 1.1 jtc argv0);
1210 1.1 jtc tty_prnt(" or \"s\" to switch to new device.\nIf you");
1211 1.1 jtc tty_prnt(" cannot change storage media, type \"s\"\n");
1212 1.1 jtc tty_prnt("Is the device ready and online? > ");
1213 1.1 jtc
1214 1.1 jtc if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){
1215 1.1 jtc done = 1;
1216 1.1 jtc lstrval = -1;
1217 1.1 jtc tty_prnt("Quitting %s!\n", argv0);
1218 1.1 jtc vfpart = 0;
1219 1.1 jtc return(-1);
1220 1.1 jtc }
1221 1.1 jtc
1222 1.1 jtc if ((buf[0] == '\0') || (buf[1] != '\0')) {
1223 1.1 jtc tty_prnt("%s unknown command, try again\n",buf);
1224 1.1 jtc continue;
1225 1.1 jtc }
1226 1.1 jtc
1227 1.1 jtc switch (buf[0]) {
1228 1.1 jtc case 'y':
1229 1.1 jtc case 'Y':
1230 1.1 jtc /*
1231 1.1 jtc * we are to continue with the same device
1232 1.1 jtc */
1233 1.1 jtc if (ar_open(arcname) >= 0)
1234 1.1 jtc return(0);
1235 1.1 jtc tty_prnt("Cannot re-open %s, try again\n",
1236 1.1 jtc arcname);
1237 1.1 jtc continue;
1238 1.1 jtc case 's':
1239 1.1 jtc case 'S':
1240 1.1 jtc /*
1241 1.1 jtc * user wants to open a different device
1242 1.1 jtc */
1243 1.1 jtc tty_prnt("Switching to a different archive\n");
1244 1.1 jtc break;
1245 1.1 jtc default:
1246 1.1 jtc tty_prnt("%s unknown command, try again\n",buf);
1247 1.1 jtc continue;
1248 1.1 jtc }
1249 1.1 jtc break;
1250 1.1 jtc }
1251 1.1 jtc } else
1252 1.1 jtc tty_prnt("Ready for archive volume: %d\n", arvol);
1253 1.1 jtc
1254 1.1 jtc /*
1255 1.1 jtc * have to go to a different archive
1256 1.1 jtc */
1257 1.1 jtc for (;;) {
1258 1.1 jtc tty_prnt("Input archive name or \".\" to quit %s.\n", argv0);
1259 1.1 jtc tty_prnt("Archive name > ");
1260 1.1 jtc
1261 1.1 jtc if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) {
1262 1.1 jtc done = 1;
1263 1.1 jtc lstrval = -1;
1264 1.1 jtc tty_prnt("Quitting %s!\n", argv0);
1265 1.1 jtc vfpart = 0;
1266 1.1 jtc return(-1);
1267 1.1 jtc }
1268 1.1 jtc if (buf[0] == '\0') {
1269 1.1 jtc tty_prnt("Empty file name, try again\n");
1270 1.1 jtc continue;
1271 1.1 jtc }
1272 1.1 jtc if (!strcmp(buf, "..")) {
1273 1.1 jtc tty_prnt("Illegal file name: .. try again\n");
1274 1.1 jtc continue;
1275 1.1 jtc }
1276 1.1 jtc if (strlen(buf) > PAXPATHLEN) {
1277 1.1 jtc tty_prnt("File name too long, try again\n");
1278 1.1 jtc continue;
1279 1.1 jtc }
1280 1.1 jtc
1281 1.1 jtc /*
1282 1.1 jtc * try to open new archive
1283 1.1 jtc */
1284 1.1 jtc if (ar_open(buf) >= 0) {
1285 1.1 jtc if (freeit) {
1286 1.1 jtc (void)free(arcname);
1287 1.1 jtc freeit = 0;
1288 1.1 jtc }
1289 1.1 jtc if ((arcname = strdup(buf)) == NULL) {
1290 1.1 jtc done = 1;
1291 1.1 jtc lstrval = -1;
1292 1.1 jtc warn(0, "Cannot save archive name.");
1293 1.1 jtc return(-1);
1294 1.1 jtc }
1295 1.1 jtc freeit = 1;
1296 1.1 jtc break;
1297 1.1 jtc }
1298 1.1 jtc tty_prnt("Cannot open %s, try again\n", buf);
1299 1.1 jtc continue;
1300 1.1 jtc }
1301 1.1 jtc return(0);
1302 1.5 mrg }
1303 1.5 mrg
1304 1.5 mrg /*
1305 1.5 mrg * ar_start_gzip()
1306 1.5 mrg * starts the gzip compression/decompression process as a child, using magic
1307 1.5 mrg * to keep the fd the same in the calling function (parent).
1308 1.5 mrg */
1309 1.5 mrg void
1310 1.5 mrg #ifdef __STDC__
1311 1.5 mrg ar_start_gzip(int fd)
1312 1.5 mrg #else
1313 1.5 mrg ar_start_gzip(fd)
1314 1.5 mrg int fd;
1315 1.5 mrg #endif
1316 1.5 mrg {
1317 1.5 mrg pid_t pid;
1318 1.5 mrg int fds[2];
1319 1.5 mrg char *gzip_flags;
1320 1.5 mrg
1321 1.5 mrg if (pipe(fds) < 0)
1322 1.5 mrg err(1, "could not pipe");
1323 1.5 mrg pid = fork();
1324 1.5 mrg if (pid < 0)
1325 1.5 mrg err(1, "could not fork");
1326 1.5 mrg
1327 1.5 mrg /* parent */
1328 1.5 mrg if (pid) {
1329 1.5 mrg switch (act) {
1330 1.5 mrg case ARCHIVE:
1331 1.5 mrg dup2(fds[1], fd);
1332 1.5 mrg break;
1333 1.5 mrg case LIST:
1334 1.5 mrg case EXTRACT:
1335 1.5 mrg dup2(fds[0], fd);
1336 1.5 mrg break;
1337 1.5 mrg default:
1338 1.5 mrg errx(1, "ar_start_gzip: impossible");
1339 1.5 mrg }
1340 1.5 mrg close(fds[0]);
1341 1.5 mrg close(fds[1]);
1342 1.5 mrg } else {
1343 1.5 mrg switch (act) {
1344 1.5 mrg case ARCHIVE:
1345 1.5 mrg dup2(fds[0], STDIN_FILENO);
1346 1.5 mrg dup2(fd, STDOUT_FILENO);
1347 1.5 mrg gzip_flags = "-c";
1348 1.5 mrg break;
1349 1.5 mrg case LIST:
1350 1.5 mrg case EXTRACT:
1351 1.5 mrg dup2(fds[1], STDOUT_FILENO);
1352 1.5 mrg dup2(fd, STDIN_FILENO);
1353 1.5 mrg gzip_flags = "-dc";
1354 1.5 mrg break;
1355 1.5 mrg default:
1356 1.5 mrg errx(1, "ar_start_gzip: impossible");
1357 1.5 mrg }
1358 1.5 mrg close(fds[0]);
1359 1.5 mrg close(fds[1]);
1360 1.5 mrg if (execlp(gzip_program, gzip_program, gzip_flags, NULL) < 0)
1361 1.5 mrg err(1, "could not exec");
1362 1.5 mrg /* NOTREACHED */
1363 1.5 mrg }
1364 1.1 jtc }
1365