| 1 | /* $OpenBSD: ar_io.c,v 1.39 2009/10/27 23:59:22 deraadt Exp $ */
|
|---|
| 2 | /* $NetBSD: ar_io.c,v 1.5 1996/03/26 23:54:13 mrg Exp $ */
|
|---|
| 3 |
|
|---|
| 4 | /*-
|
|---|
| 5 | * Copyright (c) 2012
|
|---|
| 6 | * Thorsten Glaser <tg@debian.org>
|
|---|
| 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/param.h>
|
|---|
| 40 | #include <sys/time.h>
|
|---|
| 41 | #include <sys/stat.h>
|
|---|
| 42 | #include <sys/ioctl.h>
|
|---|
| 43 | #include <sys/wait.h>
|
|---|
| 44 | #include <signal.h>
|
|---|
| 45 | #include <string.h>
|
|---|
| 46 | #include <fcntl.h>
|
|---|
| 47 | #include <unistd.h>
|
|---|
| 48 | #include <stdio.h>
|
|---|
| 49 | #include <errno.h>
|
|---|
| 50 | #include <stdlib.h>
|
|---|
| 51 | #include <err.h>
|
|---|
| 52 | #include "pax.h"
|
|---|
| 53 | #include "options.h"
|
|---|
| 54 | #include "extern.h"
|
|---|
| 55 |
|
|---|
| 56 | #if HAS_TAPE
|
|---|
| 57 | #include <sys/mtio.h>
|
|---|
| 58 | #endif
|
|---|
| 59 |
|
|---|
| 60 | __RCSID("$MirOS: src/bin/pax/ar_io.c,v 1.14 2012/02/16 17:27:30 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 */
|
|---|
| 72 | int arfd = -1; /* archive file descriptor */
|
|---|
| 73 | static int artyp = ISREG; /* archive type: file/FIFO/tape */
|
|---|
| 74 | static int arvol = 1; /* archive volume number */
|
|---|
| 75 | static int lstrval = -1; /* return value from last i/o */
|
|---|
| 76 | static int io_ok; /* i/o worked on volume after resync */
|
|---|
| 77 | static int did_io; /* did i/o ever occur on volume? */
|
|---|
| 78 | static int done; /* set via tty termination */
|
|---|
| 79 | static struct stat arsb; /* stat of archive device at open */
|
|---|
| 80 | static int invld_rec; /* tape has out of spec record size */
|
|---|
| 81 | static int wr_trail = 1; /* trailer was rewritten in append */
|
|---|
| 82 | static int can_unlnk = 0; /* do we unlink null archives? */
|
|---|
| 83 | const char *arcname; /* printable name of archive */
|
|---|
| 84 | static char *arcname_alloc; /* this is so we can free(3) it */
|
|---|
| 85 | const char *compress_program; /* name of compression programme */
|
|---|
| 86 | static pid_t zpid = -1; /* pid of child process */
|
|---|
| 87 | int force_one_volume; /* 1 if we ignore volume changes */
|
|---|
| 88 |
|
|---|
| 89 | #if HAS_TAPE
|
|---|
| 90 | static int get_phys(void);
|
|---|
| 91 | #endif
|
|---|
| 92 | extern sigset_t s_mask;
|
|---|
| 93 | static void ar_start_compress(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 |
|
|---|
| 104 | int
|
|---|
| 105 | ar_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 && compress_program != NULL)
|
|---|
| 130 | ar_start_compress(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 && compress_program != NULL)
|
|---|
| 141 | ar_start_compress(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 | */
|
|---|
| 311 | void
|
|---|
| 312 | ar_close(void)
|
|---|
| 313 | {
|
|---|
| 314 | int status;
|
|---|
| 315 |
|
|---|
| 316 | if (arfd < 0) {
|
|---|
| 317 | did_io = io_ok = flcnt = 0;
|
|---|
| 318 | if (vfpart) {
|
|---|
| 319 | (void)putc('\n', listf);
|
|---|
| 320 | vfpart = 0;
|
|---|
| 321 | }
|
|---|
| 322 | return;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | /*
|
|---|
| 326 | * Close archive file. This may take a LONG while on tapes (we may be
|
|---|
| 327 | * forced to wait for the rewind to complete) so tell the user what is
|
|---|
| 328 | * going on (this avoids the user hitting control-c thinking pax is
|
|---|
| 329 | * broken).
|
|---|
| 330 | */
|
|---|
| 331 | if (vflag && (artyp == ISTAPE)) {
|
|---|
| 332 | if (vfpart)
|
|---|
| 333 | (void)putc('\n', listf);
|
|---|
| 334 | (void)fprintf(listf,
|
|---|
| 335 | "%s: Waiting for tape drive close to complete...",
|
|---|
| 336 | argv0);
|
|---|
| 337 | (void)fflush(listf);
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /*
|
|---|
| 341 | * if nothing was written to the archive (and we created it), we remove
|
|---|
| 342 | * it
|
|---|
| 343 | */
|
|---|
| 344 | if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) &&
|
|---|
| 345 | (arsb.st_size == 0)) {
|
|---|
| 346 | (void)unlink(arcname);
|
|---|
| 347 | can_unlnk = 0;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /*
|
|---|
| 351 | * for a quick extract/list, pax frequently exits before the child
|
|---|
| 352 | * process is done
|
|---|
| 353 | */
|
|---|
| 354 | if ((act == LIST || act == EXTRACT) && nflag && zpid > 0)
|
|---|
| 355 | kill(zpid, SIGINT);
|
|---|
| 356 |
|
|---|
| 357 | (void)close(arfd);
|
|---|
| 358 |
|
|---|
| 359 | /* Do not exit before child to ensure data integrity */
|
|---|
| 360 | if (zpid > 0)
|
|---|
| 361 | waitpid(zpid, &status, 0);
|
|---|
| 362 |
|
|---|
| 363 | if (vflag && (artyp == ISTAPE)) {
|
|---|
| 364 | (void)fputs("done.\n", listf);
|
|---|
| 365 | vfpart = 0;
|
|---|
| 366 | (void)fflush(listf);
|
|---|
| 367 | }
|
|---|
| 368 | arfd = -1;
|
|---|
| 369 |
|
|---|
| 370 | if (!io_ok && !did_io) {
|
|---|
| 371 | flcnt = 0;
|
|---|
| 372 | return;
|
|---|
| 373 | }
|
|---|
| 374 | did_io = io_ok = 0;
|
|---|
| 375 |
|
|---|
| 376 | /*
|
|---|
| 377 | * The volume number is only increased when the last device has data
|
|---|
| 378 | * and we have already determined the archive format.
|
|---|
| 379 | */
|
|---|
| 380 | if (frmt != NULL)
|
|---|
| 381 | ++arvol;
|
|---|
| 382 |
|
|---|
| 383 | /* Vflag can cause this to have been set */
|
|---|
| 384 | if (vfpart) {
|
|---|
| 385 | (void)putc('\n', listf);
|
|---|
| 386 | vfpart = 0;
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | /* nothing to do any more, unless vflag */
|
|---|
| 390 | if (!vflag) {
|
|---|
| 391 | flcnt = 0;
|
|---|
| 392 | return;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | /*
|
|---|
| 396 | * Print out a summary of I/O for this archive volume.
|
|---|
| 397 | */
|
|---|
| 398 |
|
|---|
| 399 | /*
|
|---|
| 400 | * If we have not determined the format yet, we just say how many bytes
|
|---|
| 401 | * we have skipped over looking for a header to id. there is no way we
|
|---|
| 402 | * could have written anything yet.
|
|---|
| 403 | */
|
|---|
| 404 | if (frmt == NULL) {
|
|---|
| 405 | (void)fprintf(listf, "%s: unknown format, %" OT_FMT
|
|---|
| 406 | " bytes skipped.\n", argv0, (ot_type)rdcnt);
|
|---|
| 407 | (void)fflush(listf);
|
|---|
| 408 | flcnt = 0;
|
|---|
| 409 | return;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | if (strcmp(NM_CPIO, argv0) == 0)
|
|---|
| 413 | (void)fprintf(listf, "%" OT_FMT " blocks\n",
|
|---|
| 414 | (ot_type)((rdcnt ? rdcnt : wrcnt) / 5120));
|
|---|
| 415 | else if (strcmp(NM_TAR, argv0) != 0)
|
|---|
| 416 | (void)fprintf(listf,
|
|---|
| 417 | "%s: %s vol %d, %lu files, %" OT_FMT " bytes read, %"
|
|---|
| 418 | OT_FMT " bytes written.\n", argv0, frmt->name, arvol-1,
|
|---|
| 419 | flcnt, (ot_type)rdcnt, (ot_type)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 | */
|
|---|
| 431 | void
|
|---|
| 432 | ar_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 |
|
|---|
| 463 | int
|
|---|
| 464 | ar_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 |
|
|---|
| 501 | int
|
|---|
| 502 | ar_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 |
|
|---|
| 525 | int
|
|---|
| 526 | ar_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 if (!frmt || !frmt->is_uar)
|
|---|
| 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 |
|
|---|
| 608 | int
|
|---|
| 609 | ar_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 |
|
|---|
| 726 | int
|
|---|
| 727 | ar_rdsync(void)
|
|---|
| 728 | {
|
|---|
| 729 | long fsbz;
|
|---|
| 730 | off_t cpos;
|
|---|
| 731 | off_t mpos;
|
|---|
| 732 | #if HAS_TAPE
|
|---|
| 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 | #if HAS_TAPE
|
|---|
| 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 |
|
|---|
| 816 | int
|
|---|
| 817 | ar_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 |
|
|---|
| 875 | int
|
|---|
| 876 | ar_rev(off_t sksz)
|
|---|
| 877 | {
|
|---|
| 878 | off_t cpos;
|
|---|
| 879 | #if HAS_TAPE
|
|---|
| 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 | #if HAS_TAPE
|
|---|
| 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 | #if HAS_TAPE
|
|---|
| 1001 | /*
|
|---|
| 1002 | * get_phys()
|
|---|
| 1003 | * Determine the physical block size on a tape drive. We need the physical
|
|---|
| 1004 | * block size so we know how many bytes we skip over when we move with
|
|---|
| 1005 | * mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
|
|---|
| 1006 | * return.
|
|---|
| 1007 | * This is one really SLOW routine...
|
|---|
| 1008 | * Return:
|
|---|
| 1009 | * physical block size if ok (ok > 0), -1 otherwise
|
|---|
| 1010 | */
|
|---|
| 1011 |
|
|---|
| 1012 | static int
|
|---|
| 1013 | get_phys(void)
|
|---|
| 1014 | {
|
|---|
| 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 | }
|
|---|
| 1114 | #endif
|
|---|
| 1115 |
|
|---|
| 1116 | /*
|
|---|
| 1117 | * ar_next()
|
|---|
| 1118 | * prompts the user for the next volume in this archive. For some devices
|
|---|
| 1119 | * we may allow the media to be changed. Otherwise a new archive is
|
|---|
| 1120 | * prompted for. By pax spec, if there is no controlling tty or an eof is
|
|---|
| 1121 | * read on tty input, we must quit pax.
|
|---|
| 1122 | * Return:
|
|---|
| 1123 | * 0 when ready to continue, -1 when all done
|
|---|
| 1124 | */
|
|---|
| 1125 |
|
|---|
| 1126 | int
|
|---|
| 1127 | ar_next(void)
|
|---|
| 1128 | {
|
|---|
| 1129 | char buf[PAXPATHLEN+2];
|
|---|
| 1130 | static int freeit = 0;
|
|---|
| 1131 | sigset_t o_mask;
|
|---|
| 1132 |
|
|---|
| 1133 | /*
|
|---|
| 1134 | * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so
|
|---|
| 1135 | * things like writing EOF etc will be done) (Watch out ar_close() can
|
|---|
| 1136 | * also be called via a signal handler, so we must prevent a race.
|
|---|
| 1137 | */
|
|---|
| 1138 | if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0)
|
|---|
| 1139 | syswarn(0, errno, "Unable to set signal mask");
|
|---|
| 1140 | ar_close();
|
|---|
| 1141 | if (sigprocmask(SIG_SETMASK, &o_mask, NULL) < 0)
|
|---|
| 1142 | syswarn(0, errno, "Unable to restore signal mask");
|
|---|
| 1143 |
|
|---|
| 1144 | if (done || !wr_trail || force_one_volume || strcmp(NM_TAR, argv0) == 0)
|
|---|
| 1145 | return(-1);
|
|---|
| 1146 |
|
|---|
| 1147 | tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0);
|
|---|
| 1148 |
|
|---|
| 1149 | /*
|
|---|
| 1150 | * if i/o is on stdin or stdout, we cannot reopen it (we do not know
|
|---|
| 1151 | * the name), the user will be forced to type it in.
|
|---|
| 1152 | */
|
|---|
| 1153 | if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG)
|
|---|
| 1154 | && (artyp != ISPIPE)) {
|
|---|
| 1155 | if (artyp == ISTAPE) {
|
|---|
| 1156 | tty_prnt("%s ready for archive tape volume: %d\n",
|
|---|
| 1157 | arcname, arvol);
|
|---|
| 1158 | tty_prnt("Load the NEXT TAPE on the tape drive");
|
|---|
| 1159 | } else {
|
|---|
| 1160 | tty_prnt("%s ready for archive volume: %d\n",
|
|---|
| 1161 | arcname, arvol);
|
|---|
| 1162 | tty_prnt("Load the NEXT STORAGE MEDIA (if required)");
|
|---|
| 1163 | }
|
|---|
| 1164 |
|
|---|
| 1165 | if ((act == ARCHIVE) || (act == APPND))
|
|---|
| 1166 | tty_prnt(" and make sure it is WRITE ENABLED.\n");
|
|---|
| 1167 | else
|
|---|
| 1168 | tty_prnt("\n");
|
|---|
| 1169 |
|
|---|
| 1170 | for (;;) {
|
|---|
| 1171 | tty_prnt("Type \"y\" to continue, \".\" to quit %s,",
|
|---|
| 1172 | argv0);
|
|---|
| 1173 | tty_prnt(" or \"s\" to switch to new device.\nIf you");
|
|---|
| 1174 | tty_prnt(" cannot change storage media, type \"s\"\n");
|
|---|
| 1175 | tty_prnt("Is the device ready and online? > ");
|
|---|
| 1176 |
|
|---|
| 1177 | if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){
|
|---|
| 1178 | done = 1;
|
|---|
| 1179 | lstrval = -1;
|
|---|
| 1180 | tty_prnt("Quitting %s!\n", argv0);
|
|---|
| 1181 | vfpart = 0;
|
|---|
| 1182 | return(-1);
|
|---|
| 1183 | }
|
|---|
| 1184 |
|
|---|
| 1185 | if ((buf[0] == '\0') || (buf[1] != '\0')) {
|
|---|
| 1186 | tty_prnt("%s unknown command, try again\n",buf);
|
|---|
| 1187 | continue;
|
|---|
| 1188 | }
|
|---|
| 1189 |
|
|---|
| 1190 | switch (buf[0]) {
|
|---|
| 1191 | case 'y':
|
|---|
| 1192 | case 'Y':
|
|---|
| 1193 | /*
|
|---|
| 1194 | * we are to continue with the same device
|
|---|
| 1195 | */
|
|---|
| 1196 | if (ar_open(arcname) >= 0)
|
|---|
| 1197 | return(0);
|
|---|
| 1198 | tty_prnt("Cannot re-open %s, try again\n",
|
|---|
| 1199 | arcname);
|
|---|
| 1200 | continue;
|
|---|
| 1201 | case 's':
|
|---|
| 1202 | case 'S':
|
|---|
| 1203 | /*
|
|---|
| 1204 | * user wants to open a different device
|
|---|
| 1205 | */
|
|---|
| 1206 | tty_prnt("Switching to a different archive\n");
|
|---|
| 1207 | break;
|
|---|
| 1208 | default:
|
|---|
| 1209 | tty_prnt("%s unknown command, try again\n",buf);
|
|---|
| 1210 | continue;
|
|---|
| 1211 | }
|
|---|
| 1212 | break;
|
|---|
| 1213 | }
|
|---|
| 1214 | } else
|
|---|
| 1215 | tty_prnt("Ready for archive volume: %d\n", arvol);
|
|---|
| 1216 |
|
|---|
| 1217 | /*
|
|---|
| 1218 | * have to go to a different archive
|
|---|
| 1219 | */
|
|---|
| 1220 | for (;;) {
|
|---|
| 1221 | tty_prnt("Input archive name or \".\" to quit %s.\n", argv0);
|
|---|
| 1222 | tty_prnt("Archive name > ");
|
|---|
| 1223 |
|
|---|
| 1224 | if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) {
|
|---|
| 1225 | done = 1;
|
|---|
| 1226 | lstrval = -1;
|
|---|
| 1227 | tty_prnt("Quitting %s!\n", argv0);
|
|---|
| 1228 | vfpart = 0;
|
|---|
| 1229 | return(-1);
|
|---|
| 1230 | }
|
|---|
| 1231 | if (buf[0] == '\0') {
|
|---|
| 1232 | tty_prnt("Empty file name, try again\n");
|
|---|
| 1233 | continue;
|
|---|
| 1234 | }
|
|---|
| 1235 | if (!strcmp(buf, "..")) {
|
|---|
| 1236 | tty_prnt("Illegal file name: .. try again\n");
|
|---|
| 1237 | continue;
|
|---|
| 1238 | }
|
|---|
| 1239 | if (strlen(buf) > PAXPATHLEN) {
|
|---|
| 1240 | tty_prnt("File name too long, try again\n");
|
|---|
| 1241 | continue;
|
|---|
| 1242 | }
|
|---|
| 1243 |
|
|---|
| 1244 | /*
|
|---|
| 1245 | * try to open new archive
|
|---|
| 1246 | */
|
|---|
| 1247 | if (ar_open(buf) >= 0) {
|
|---|
| 1248 | if (freeit) {
|
|---|
| 1249 | free(arcname_alloc);
|
|---|
| 1250 | freeit = 0;
|
|---|
| 1251 | }
|
|---|
| 1252 | if ((arcname = arcname_alloc = strdup(buf)) == NULL) {
|
|---|
| 1253 | done = 1;
|
|---|
| 1254 | lstrval = -1;
|
|---|
| 1255 | paxwarn(0, "Cannot save archive name.");
|
|---|
| 1256 | return(-1);
|
|---|
| 1257 | }
|
|---|
| 1258 | freeit = 1;
|
|---|
| 1259 | break;
|
|---|
| 1260 | }
|
|---|
| 1261 | tty_prnt("Cannot open %s, try again\n", buf);
|
|---|
| 1262 | continue;
|
|---|
| 1263 | }
|
|---|
| 1264 | return(0);
|
|---|
| 1265 | }
|
|---|
| 1266 |
|
|---|
| 1267 | /*
|
|---|
| 1268 | * ar_start_compress()
|
|---|
| 1269 | * starts the compression/decompression process as a child, using magic
|
|---|
| 1270 | * to keep the fd the same in the calling function (parent).
|
|---|
| 1271 | */
|
|---|
| 1272 | void
|
|---|
| 1273 | ar_start_compress(int fd, int wr)
|
|---|
| 1274 | {
|
|---|
| 1275 | int fds[2];
|
|---|
| 1276 | const char *compress_flags;
|
|---|
| 1277 |
|
|---|
| 1278 | if (pipe(fds) < 0)
|
|---|
| 1279 | err(1, "could not pipe");
|
|---|
| 1280 | zpid = fork();
|
|---|
| 1281 | if (zpid < 0)
|
|---|
| 1282 | err(1, "could not fork");
|
|---|
| 1283 |
|
|---|
| 1284 | /* parent */
|
|---|
| 1285 | if (zpid) {
|
|---|
| 1286 | dup2(fds[wr ? 1 : 0], fd);
|
|---|
| 1287 | close(fds[0]);
|
|---|
| 1288 | close(fds[1]);
|
|---|
| 1289 | } else {
|
|---|
| 1290 | if (wr) {
|
|---|
| 1291 | dup2(fds[0], STDIN_FILENO);
|
|---|
| 1292 | dup2(fd, STDOUT_FILENO);
|
|---|
| 1293 | compress_flags = "-c";
|
|---|
| 1294 | } else {
|
|---|
| 1295 | dup2(fds[1], STDOUT_FILENO);
|
|---|
| 1296 | dup2(fd, STDIN_FILENO);
|
|---|
| 1297 | compress_flags = "-dc";
|
|---|
| 1298 | }
|
|---|
| 1299 | close(fds[0]);
|
|---|
| 1300 | close(fds[1]);
|
|---|
| 1301 | if (execlp(compress_program, compress_program,
|
|---|
| 1302 | compress_flags, NULL) < 0)
|
|---|
| 1303 | err(1, "could not exec %s", compress_program);
|
|---|
| 1304 | /* NOTREACHED */
|
|---|
| 1305 | }
|
|---|
| 1306 | }
|
|---|