source: freewrt/tools/paxmirabilis/src/ar_io.c@ fdd4f59

freewrt_1_0 freewrt_2_0
Last change on this file since fdd4f59 was bceb42b, checked in by Thorsten Glaser <tg@…>, 19 years ago

wbx@ asked me a few seconds too late to use an src/ subdirectory

git-svn-id: svn://www.freewrt.org/trunk/freewrt@204 afb5a338-a214-0410-bd46-81f09a774fd1

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