source: freewrt/tools/paxmirabilis/src/ar_io.c@ 3784d08

freewrt_1_0 freewrt_2_0
Last change on this file since 3784d08 was 08c8e8e, checked in by Thorsten Glaser <tg@…>, 16 years ago

sync with upstream (MirBSD), which gives us

  • improvements to fgetln(3) such as memory leak fixes and better congruence with other implementations even where not mandated by the API (manual page)
  • support for Mac OSX 10.6 Snow Leopard which removed the tape functions <sys/mtio.h> *even in a server operating system*
  • gcc warning fixes

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