Debian Codegraph @

ar_io.c

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