| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 69 |
|
| 70 |
|
| 71 |
#define DMOD 0666 |
| 72 |
#define EXT_MODE O_RDONLY |
| 73 |
#define AR_MODE (O_WRONLY | O_CREAT | O_TRUNC) |
| 74 |
#define APP_MODE O_RDWR |
| 75 |
#define STDO "<STDOUT>" |
| 76 |
#define STDN "<STDIN>" |
| 77 |
int arfd = -1; |
| 78 |
static char artyp = ISREG; |
| 79 |
static int arvol = 1; |
| 80 |
static int lstrval = -1; |
| 81 |
static char io_ok; |
| 82 |
static char did_io; |
| 83 |
static char done; |
| 84 |
static struct stat arsb; |
| 85 |
static char invld_rec; |
| 86 |
static char wr_trail = 1; |
| 87 |
static char can_unlnk = 0; |
| 88 |
const char *arcname; |
| 89 |
static char *arcname_alloc = NULL; |
| 90 |
const char *compress_program; |
| 91 |
static pid_t zpid = -1; |
| 92 |
char force_one_volume; |
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
if (artyp != ISREG) |
| 211 |
can_unlnk = 0; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if (act == ARCHIVE) { |
| 216 |
blksz = rdblksz = wrblksz; |
| 217 |
lstrval = 1; |
| 218 |
return(0); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
switch (artyp) { |
| 229 |
case ISTAPE: |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
blksz = rdblksz = MAXBLK; |
| 245 |
break; |
| 246 |
case ISPIPE: |
| 247 |
case ISBLK: |
| 248 |
case ISCHR: |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 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 |
|
| 269 |
|
| 270 |
if ((act == APPND) || (artyp == ISCHR)) |
| 271 |
blksz = rdblksz; |
| 272 |
else |
| 273 |
blksz = MAXBLK; |
| 274 |
break; |
| 275 |
case ISREG: |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){ |
| 281 |
blksz = rdblksz = wrblksz; |
| 282 |
break; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT) |
| 288 |
if ((arsb.st_size % rdblksz) == 0) |
| 289 |
break; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
if (rdblksz <= 0) |
| 294 |
rdblksz = FILEBLK; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
if (act == APPND) |
| 299 |
blksz = rdblksz; |
| 300 |
else |
| 301 |
blksz = MAXBLK; |
| 302 |
break; |
| 303 |
default: |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
blksz = rdblksz = BLKMULT; |
| 308 |
break; |
| 309 |
} |
| 310 |
lstrval = 1; |
| 311 |
return(0); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 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 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 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 |
|
| 349 |
|
| 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 |
|
| 359 |
|
| 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 |
|
| 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 |
|
| 389 |
|
| 390 |
|
| 391 |
if (frmt != NULL) |
| 392 |
++arvol; |
| 393 |
|
| 394 |
|
| 395 |
if (vfpart) { |
| 396 |
SHIKATANAI write(listfd, "\n", 1); |
| 397 |
vfpart = 0; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
if (!vflag) { |
| 402 |
flcnt = 0; |
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 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 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
void |
| 440 |
ar_drain(void) |
| 441 |
{ |
| 442 |
int res; |
| 443 |
char drbuf[MAXBLK]; |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
if ((artyp != ISPIPE) || (lstrval <= 0)) |
| 451 |
return; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
while ((res = read(arfd, drbuf, sizeof(drbuf))) > 0) |
| 457 |
continue; |
| 458 |
lstrval = res; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
int |
| 472 |
ar_set_wr(void) |
| 473 |
{ |
| 474 |
off_t cpos; |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
wr_trail = 0; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
if (artyp != ISREG) |
| 486 |
return(0); |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 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 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 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 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
int |
| 534 |
ar_read(char *buf, int cnt) |
| 535 |
{ |
| 536 |
int res = 0; |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
if (lstrval <= 0) |
| 542 |
return(lstrval); |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
switch (artyp) { |
| 548 |
case ISTAPE: |
| 549 |
if ((res = read(arfd, buf, cnt)) > 0) { |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
io_ok = 1; |
| 558 |
if (res != rdblksz) { |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 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 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
if ((res = read(arfd, buf, cnt)) > 0) { |
| 588 |
io_ok = 1; |
| 589 |
return(res); |
| 590 |
} |
| 591 |
break; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 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 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
int |
| 617 |
ar_write(char *buf, int bsz) |
| 618 |
{ |
| 619 |
int res; |
| 620 |
off_t cpos; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 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 |
|
| 636 |
|
| 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 |
|
| 648 |
|
| 649 |
|
| 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 |
|
| 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 |
|
| 679 |
|
| 680 |
|
| 681 |
if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO)) |
| 682 |
res = lstrval = 0; |
| 683 |
break; |
| 684 |
case ISPIPE: |
| 685 |
default: |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
break; |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
if (res >= 0) { |
| 701 |
if (res > 0) |
| 702 |
wr_trail = 1; |
| 703 |
io_ok = 1; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 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 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 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 |
|
| 746 |
|
| 747 |
|
| 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 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 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 |
|
| 836 |
|
| 837 |
if (lstrval <= 0) |
| 838 |
return(lstrval); |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
if (artyp != ISREG) |
| 847 |
return(0); |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
if ((cpos = lseek(arfd, 0, SEEK_CUR)) >= 0) { |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 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 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 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 |
|
| 894 |
|
| 895 |
if (lstrval < 0) |
| 896 |
return(lstrval); |
| 897 |
|
| 898 |
switch (artyp) { |
| 899 |
case ISPIPE: |
| 900 |
if (sksz <= 0) |
| 901 |
break; |
| 902 |
|
| 903 |
|
| 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 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 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 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
if ((cpos -= sksz) < 0) { |
| 937 |
if (arvol > 1) { |
| 938 |
|
| 939 |
|
| 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 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
if ((phyblk = get_phys()) <= 0) { |
| 963 |
lstrval = -1; |
| 964 |
return(-1); |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
rdblksz = phyblk; |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
if (sksz <= 0) |
| 978 |
break; |
| 979 |
|
| 980 |
|
| 981 |
|
| 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 |
|
| 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 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 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 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
if (lstrval == 1) { |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 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 |
|
| 1048 |
|
| 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 |
|
| 1059 |
|
| 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 |
|
| 1074 |
|
| 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 |
|
| 1091 |
|
| 1092 |
lstrval = 1; |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
if (padsz == 0) |
| 1098 |
return(phyblk); |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
if (padsz % phyblk) { |
| 1105 |
paxwarn(1, "Tape drive unable to backspace requested amount"); |
| 1106 |
return(-1); |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 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 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
int |
| 1135 |
ar_next(void) |
| 1136 |
{ |
| 1137 |
char *buf; |
| 1138 |
sigset_t o_mask; |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 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 |
|
| 1158 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1275 |
|
| 1276 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 1327 |
} |
| 1328 |
} |