1 /*- 2 * Copyright (c) 1992 Keith Muller. 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Keith Muller of the University of California, San Diego. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. 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 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)buf_subs.c 8.2 (Berkeley) 4/18/94"; 41 #endif 42 #endif /* not lint */ 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/types.h> 47 #include <sys/stat.h> 48 #include <errno.h> 49 #include <unistd.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include "pax.h" 54 #include "extern.h" 55 56 /* 57 * routines which implement archive and file buffering 58 */ 59 60 #define MINFBSZ 512 /* default block size for hole detect */ 61 #define MAXFLT 10 /* default media read error limit */ 62 63 /* 64 * Need to change bufmem to dynamic allocation when the upper 65 * limit on blocking size is removed (though that will violate pax spec) 66 * MAXBLK define and tests will also need to be updated. 67 */ 68 static char bufmem[MAXBLK+BLKMULT]; /* i/o buffer + pushback id space */ 69 static char *buf; /* normal start of i/o buffer */ 70 static char *bufend; /* end or last char in i/o buffer */ 71 static char *bufpt; /* read/write point in i/o buffer */ 72 int blksz = MAXBLK; /* block input/output size in bytes */ 73 int wrblksz; /* user spec output size in bytes */ 74 int maxflt = MAXFLT; /* MAX consecutive media errors */ 75 int rdblksz; /* first read blksize (tapes only) */ 76 off_t wrlimit; /* # of bytes written per archive vol */ 77 off_t wrcnt; /* # of bytes written on current vol */ 78 off_t rdcnt; /* # of bytes read on current vol */ 79 80 /* 81 * wr_start() 82 * set up the buffering system to operate in a write mode 83 * Return: 84 * 0 if ok, -1 if the user specified write block size violates pax spec 85 */ 86 87 int 88 wr_start(void) 89 { 90 buf = &(bufmem[BLKMULT]); 91 /* 92 * Check to make sure the write block size meets pax specs. If the user 93 * does not specify a blocksize, we use the format default blocksize. 94 * We must be picky on writes, so we do not allow the user to create an 95 * archive that might be hard to read elsewhere. If all ok, we then 96 * open the first archive volume 97 */ 98 if (!wrblksz) 99 wrblksz = frmt->bsz; 100 if (wrblksz > MAXBLK) { 101 paxwarn(1, "Write block size of %d too large, maximum is: %d", 102 wrblksz, MAXBLK); 103 return(-1); 104 } 105 if (wrblksz % BLKMULT) { 106 paxwarn(1, "Write block size of %d is not a %d byte multiple", 107 wrblksz, BLKMULT); 108 return(-1); 109 } 110 if (wrblksz > MAXBLK_POSIX) { 111 paxwarn(0, "Write block size of %d larger than POSIX max %d, archive may not be portable", 112 wrblksz, MAXBLK_POSIX); 113 return(-1); 114 } 115 116 /* 117 * we only allow wrblksz to be used with all archive operations 118 */ 119 blksz = rdblksz = wrblksz; 120 if ((ar_open(arcname) < 0) && (ar_next() < 0)) 121 return(-1); 122 wrcnt = 0; 123 bufend = buf + wrblksz; 124 bufpt = buf; 125 return(0); 126 } 127 128 /* 129 * rd_start() 130 * set up buffering system to read an archive 131 * Return: 132 * 0 if ok, -1 otherwise 133 */ 134 135 int 136 rd_start(void) 137 { 138 /* 139 * leave space for the header pushback (see get_arc()). If we are 140 * going to append and user specified a write block size, check it 141 * right away 142 */ 143 buf = &(bufmem[BLKMULT]); 144 if ((act == APPND) && wrblksz) { 145 if (wrblksz > MAXBLK) { 146 paxwarn(1,"Write block size %d too large, maximum is: %d", 147 wrblksz, MAXBLK); 148 return(-1); 149 } 150 if (wrblksz % BLKMULT) { 151 paxwarn(1, "Write block size %d is not a %d byte multiple", 152 wrblksz, BLKMULT); 153 return(-1); 154 } 155 } 156 157 /* 158 * open the archive 159 */ 160 if ((ar_open(arcname) < 0) && (ar_next() < 0)) 161 return(-1); 162 bufend = buf + rdblksz; 163 bufpt = bufend; 164 rdcnt = 0; 165 return(0); 166 } 167 168 /* 169 * cp_start() 170 * set up buffer system for copying within the file system 171 */ 172 173 void 174 cp_start(void) 175 { 176 buf = &(bufmem[BLKMULT]); 177 rdblksz = blksz = MAXBLK; 178 } 179 180 /* 181 * appnd_start() 182 * Set up the buffering system to append new members to an archive that 183 * was just read. The last block(s) of an archive may contain a format 184 * specific trailer. To append a new member, this trailer has to be 185 * removed from the archive. The first byte of the trailer is replaced by 186 * the start of the header of the first file added to the archive. The 187 * format specific end read function tells us how many bytes to move 188 * backwards in the archive to be positioned BEFORE the trailer. Two 189 * different postions have to be adjusted, the O.S. file offset (e.g. the 190 * position of the tape head) and the write point within the data we have 191 * stored in the read (soon to become write) buffer. We may have to move 192 * back several records (the number depends on the size of the archive 193 * record and the size of the format trailer) to read up the record where 194 * the first byte of the trailer is recorded. Trailers may span (and 195 * overlap) record boundries. 196 * We first calculate which record has the first byte of the trailer. We 197 * move the OS file offset back to the start of this record and read it 198 * up. We set the buffer write pointer to be at this byte (the byte where 199 * the trailer starts). We then move the OS file pointer back to the 200 * start of this record so a flush of this buffer will replace the record 201 * in the archive. 202 * A major problem is rewriting this last record. For archives stored 203 * on disk files, this is trival. However, many devices are really picky 204 * about the conditions under which they will allow a write to occur. 205 * Often devices restrict the conditions where writes can be made writes, 206 * so it may not be feasable to append archives stored on all types of 207 * devices. 208 * Return: 209 * 0 for success, -1 for failure 210 */ 211 212 int 213 appnd_start(off_t skcnt) 214 { 215 int res; 216 off_t cnt; 217 218 if (exit_val != 0) { 219 paxwarn(0, "Cannot append to an archive that may have flaws."); 220 return(-1); 221 } 222 /* 223 * if the user did not specify a write blocksize, inherit the size used 224 * in the last archive volume read. (If a is set we still use rdblksz 225 * until next volume, cannot shift sizes within a single volume). 226 */ 227 if (!wrblksz) 228 wrblksz = blksz = rdblksz; 229 else 230 blksz = rdblksz; 231 232 /* 233 * make sure that this volume allows appends 234 */ 235 if (ar_app_ok() < 0) 236 return(-1); 237 238 /* 239 * Calculate bytes to move back and move in front of record where we 240 * need to start writing from. Remember we have to add in any padding 241 * that might be in the buffer after the trailer in the last block. We 242 * travel skcnt + padding ROUNDED UP to blksize. 243 */ 244 skcnt += bufend - bufpt; 245 if ((cnt = (skcnt/blksz) * blksz) < skcnt) 246 cnt += blksz; 247 if (ar_rev((off_t)cnt) < 0) 248 goto out; 249 250 /* 251 * We may have gone too far if there is valid data in the block we are 252 * now in front of, read up the block and position the pointer after 253 * the valid data. 254 */ 255 if ((cnt -= skcnt) > 0) { 256 /* 257 * watch out for stupid tape drives. ar_rev() will set rdblksz 258 * to be real physical blocksize so we must loop until we get 259 * the old rdblksz (now in blksz). If ar_rev() fouls up the 260 * determination of the physical block size, we will fail. 261 */ 262 bufpt = buf; 263 bufend = buf + blksz; 264 while (bufpt < bufend) { 265 if ((res = ar_read(bufpt, rdblksz)) <= 0) 266 goto out; 267 bufpt += res; 268 } 269 if (ar_rev((off_t)(bufpt - buf)) < 0) 270 goto out; 271 bufpt = buf + cnt; 272 bufend = buf + blksz; 273 } else { 274 /* 275 * buffer is empty 276 */ 277 bufend = buf + blksz; 278 bufpt = buf; 279 } 280 rdblksz = blksz; 281 rdcnt -= skcnt; 282 wrcnt = 0; 283 284 /* 285 * At this point we are ready to write. If the device requires special 286 * handling to write at a point were previously recorded data resides, 287 * that is handled in ar_set_wr(). From now on we operate under normal 288 * ARCHIVE mode (write) conditions 289 */ 290 if (ar_set_wr() < 0) 291 return(-1); 292 act = ARCHIVE; 293 return(0); 294 295 out: 296 paxwarn(1, "Unable to rewrite archive trailer, cannot append."); 297 return(-1); 298 } 299 300 /* 301 * rd_sync() 302 * A read error occurred on this archive volume. Resync the buffer and 303 * try to reset the device (if possible) so we can continue to read. Keep 304 * trying to do this until we get a valid read, or we reach the limit on 305 * consecutive read faults (at which point we give up). The user can 306 * adjust the read error limit through a command line option. 307 * Returns: 308 * 0 on success, and -1 on failure 309 */ 310 311 int 312 rd_sync(void) 313 { 314 int errcnt = 0; 315 int res; 316 317 /* 318 * if the user says bail out on first fault, we are out of here... 319 */ 320 if (maxflt == 0) 321 return(-1); 322 if (act == APPND) { 323 paxwarn(1, "Unable to append when there are archive read errors."); 324 return(-1); 325 } 326 327 /* 328 * poke at device and try to get past media error 329 */ 330 if (ar_rdsync() < 0) { 331 if (ar_next() < 0) 332 return(-1); 333 else 334 rdcnt = 0; 335 } 336 337 for (;;) { 338 if ((res = ar_read(buf, blksz)) > 0) { 339 /* 340 * All right! got some data, fill that buffer 341 */ 342 bufpt = buf; 343 bufend = buf + res; 344 rdcnt += res; 345 return(0); 346 } 347 348 /* 349 * Oh well, yet another failed read... 350 * if error limit reached, ditch. o.w. poke device to move past 351 * bad media and try again. if media is badly damaged, we ask 352 * the poor (and upset user at this point) for the next archive 353 * volume. remember the goal on reads is to get the most we 354 * can extract out of the archive. 355 */ 356 if ((maxflt > 0) && (++errcnt > maxflt)) 357 paxwarn(0,"Archive read error limit (%d) reached",maxflt); 358 else if (ar_rdsync() == 0) 359 continue; 360 if (ar_next() < 0) 361 break; 362 rdcnt = 0; 363 errcnt = 0; 364 } 365 return(-1); 366 } 367 368 /* 369 * pback() 370 * push the data used during the archive id phase back into the I/O 371 * buffer. This is required as we cannot be sure that the header does NOT 372 * overlap a block boundry (as in the case we are trying to recover a 373 * flawed archived). This was not designed to be used for any other 374 * purpose. (What software engineering, HA!) 375 * WARNING: do not even THINK of pback greater than BLKMULT, unless the 376 * pback space is increased. 377 */ 378 379 void 380 pback(char *pt, int cnt) 381 { 382 bufpt -= cnt; 383 memcpy(bufpt, pt, cnt); 384 return; 385 } 386 387 /* 388 * rd_skip() 389 * skip foward in the archive during a archive read. Used to get quickly 390 * past file data and padding for files the user did NOT select. 391 * Return: 392 * 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected. 393 */ 394 395 int 396 rd_skip(off_t skcnt) 397 { 398 off_t res; 399 off_t cnt; 400 off_t skipped = 0; 401 402 /* 403 * consume what data we have in the buffer. If we have to move foward 404 * whole records, we call the low level skip function to see if we can 405 * move within the archive without doing the expensive reads on data we 406 * do not want. 407 */ 408 if (skcnt == 0) 409 return(0); 410 res = MIN((bufend - bufpt), skcnt); 411 bufpt += res; 412 skcnt -= res; 413 414 /* 415 * if skcnt is now 0, then no additional i/o is needed 416 */ 417 if (skcnt == 0) 418 return(0); 419 420 /* 421 * We have to read more, calculate complete and partial record reads 422 * based on rdblksz. we skip over "cnt" complete records 423 */ 424 res = skcnt%rdblksz; 425 cnt = (skcnt/rdblksz) * rdblksz; 426 427 /* 428 * if the skip fails, we will have to resync. ar_fow will tell us 429 * how much it can skip over. We will have to read the rest. 430 */ 431 if (ar_fow(cnt, &skipped) < 0) 432 return(-1); 433 res += cnt - skipped; 434 rdcnt += skipped; 435 436 /* 437 * what is left we have to read (which may be the whole thing if 438 * ar_fow() told us the device can only read to skip records); 439 */ 440 while (res > 0L) { 441 cnt = bufend - bufpt; 442 /* 443 * if the read fails, we will have to resync 444 */ 445 if ((cnt <= 0) && ((cnt = buf_fill()) < 0)) 446 return(-1); 447 if (cnt == 0) 448 return(1); 449 cnt = MIN(cnt, res); 450 bufpt += cnt; 451 res -= cnt; 452 } 453 return(0); 454 } 455 456 /* 457 * wr_fin() 458 * flush out any data (and pad if required) the last block. We always pad 459 * with zero (even though we do not have to). Padding with 0 makes it a 460 * lot easier to recover if the archive is damaged. zero paddding SHOULD 461 * BE a requirement.... 462 */ 463 464 void 465 wr_fin(void) 466 { 467 if (bufpt > buf) { 468 memset(bufpt, 0, bufend - bufpt); 469 bufpt = bufend; 470 (void)buf_flush(blksz); 471 } 472 } 473 474 /* 475 * wr_rdbuf() 476 * fill the write buffer from data passed to it in a buffer (usually used 477 * by format specific write routines to pass a file header). On failure we 478 * punt. We do not allow the user to continue to write flawed archives. 479 * We assume these headers are not very large (the memory copy we use is 480 * a bit expensive). 481 * Return: 482 * 0 if buffer was filled ok, -1 o.w. (buffer flush failure) 483 */ 484 485 int 486 wr_rdbuf(char *out, int outcnt) 487 { 488 int cnt; 489 490 /* 491 * while there is data to copy copy into the write buffer. when the 492 * write buffer fills, flush it to the archive and continue 493 */ 494 while (outcnt > 0) { 495 cnt = bufend - bufpt; 496 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) 497 return(-1); 498 /* 499 * only move what we have space for 500 */ 501 cnt = MIN(cnt, outcnt); 502 memcpy(bufpt, out, cnt); 503 bufpt += cnt; 504 out += cnt; 505 outcnt -= cnt; 506 } 507 return(0); 508 } 509 510 /* 511 * rd_wrbuf() 512 * copy from the read buffer into a supplied buffer a specified number of 513 * bytes. If the read buffer is empty fill it and continue to copy. 514 * usually used to obtain a file header for processing by a format 515 * specific read routine. 516 * Return 517 * number of bytes copied to the buffer, 0 indicates EOF on archive volume, 518 * -1 is a read error 519 */ 520 521 int 522 rd_wrbuf(char *in, int cpcnt) 523 { 524 int res; 525 int cnt; 526 int incnt = cpcnt; 527 528 /* 529 * loop until we fill the buffer with the requested number of bytes 530 */ 531 while (incnt > 0) { 532 cnt = bufend - bufpt; 533 if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) { 534 /* 535 * read error, return what we got (or the error if 536 * no data was copied). The caller must know that an 537 * error occured and has the best knowledge what to 538 * do with it 539 */ 540 if ((res = cpcnt - incnt) > 0) 541 return(res); 542 return(cnt); 543 } 544 545 /* 546 * calculate how much data to copy based on whats left and 547 * state of buffer 548 */ 549 cnt = MIN(cnt, incnt); 550 memcpy(in, bufpt, cnt); 551 bufpt += cnt; 552 incnt -= cnt; 553 in += cnt; 554 } 555 return(cpcnt); 556 } 557 558 /* 559 * wr_skip() 560 * skip forward during a write. In other words add padding to the file. 561 * we add zero filled padding as it makes flawed archives much easier to 562 * recover from. the caller tells us how many bytes of padding to add 563 * This routine was not designed to add HUGE amount of padding, just small 564 * amounts (a few 512 byte blocks at most) 565 * Return: 566 * 0 if ok, -1 if there was a buf_flush failure 567 */ 568 569 int 570 wr_skip(off_t skcnt) 571 { 572 int cnt; 573 574 /* 575 * loop while there is more padding to add 576 */ 577 while (skcnt > 0L) { 578 cnt = bufend - bufpt; 579 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) 580 return(-1); 581 cnt = MIN(cnt, skcnt); 582 memset(bufpt, 0, cnt); 583 bufpt += cnt; 584 skcnt -= cnt; 585 } 586 return(0); 587 } 588 589 /* 590 * wr_rdfile() 591 * fill write buffer with the contents of a file. We are passed an open 592 * file descriptor to the file and the archive structure that describes the 593 * file we are storing. The variable "left" is modified to contain the 594 * number of bytes of the file we were NOT able to write to the archive. 595 * it is important that we always write EXACTLY the number of bytes that 596 * the format specific write routine told us to. The file can also get 597 * bigger, so reading to the end of file would create an improper archive, 598 * we just detect this case and warn the user. We never create a bad 599 * archive if we can avoid it. Of course trying to archive files that are 600 * active is asking for trouble. It we fail, we pass back how much we 601 * could NOT copy and let the caller deal with it. 602 * Return: 603 * 0 ok, -1 if archive write failure. a short read of the file returns a 604 * 0, but "left" is set to be greater than zero. 605 */ 606 607 int 608 wr_rdfile(ARCHD *arcn, int ifd, off_t *left) 609 { 610 int cnt; 611 int res = 0; 612 off_t size = arcn->sb.st_size; 613 struct stat sb; 614 615 /* 616 * while there are more bytes to write 617 */ 618 while (size > 0L) { 619 cnt = bufend - bufpt; 620 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) { 621 *left = size; 622 return(-1); 623 } 624 cnt = MIN(cnt, size); 625 if ((res = read(ifd, bufpt, cnt)) <= 0) 626 break; 627 size -= res; 628 bufpt += res; 629 } 630 631 /* 632 * better check the file did not change during this operation 633 * or the file read failed. 634 */ 635 if (res < 0) 636 syswarn(1, errno, "Read fault on %s", arcn->org_name); 637 else if (size != 0L) 638 paxwarn(1, "File changed size during read %s", arcn->org_name); 639 else if (fstat(ifd, &sb) < 0) 640 syswarn(1, errno, "Failed stat on %s", arcn->org_name); 641 else if (arcn->sb.st_mtime != sb.st_mtime) 642 paxwarn(1, "File %s was modified during copy to archive", 643 arcn->org_name); 644 *left = size; 645 return(0); 646 } 647 648 /* 649 * rd_wrfile() 650 * extract the contents of a file from the archive. If we are unable to 651 * extract the entire file (due to failure to write the file) we return 652 * the numbers of bytes we did NOT process. This way the caller knows how 653 * many bytes to skip past to find the next archive header. If the failure 654 * was due to an archive read, we will catch that when we try to skip. If 655 * the format supplies a file data crc value, we calculate the actual crc 656 * so that it can be compared to the value stored in the header 657 * NOTE: 658 * We call a special function to write the file. This function attempts to 659 * restore file holes (blocks of zeros) into the file. When files are 660 * sparse this saves space, and is a LOT faster. For non sparse files 661 * the performance hit is small. As of this writing, no archive supports 662 * information on where the file holes are. 663 * Return: 664 * 0 ok, -1 if archive read failure. if we cannot write the entire file, 665 * we return a 0 but "left" is set to be the amount unwritten 666 */ 667 668 int 669 rd_wrfile(ARCHD *arcn, int ofd, off_t *left) 670 { 671 int cnt = 0; 672 off_t size = arcn->sb.st_size; 673 int res = 0; 674 char *fnm = arcn->name; 675 int isem = 1; 676 int rem; 677 int sz = MINFBSZ; 678 struct stat sb; 679 u_long crc = 0L; 680 681 /* 682 * pass the blocksize of the file being written to the write routine, 683 * if the size is zero, use the default MINFBSZ 684 */ 685 if (fstat(ofd, &sb) == 0) { 686 if (sb.st_blksize > 0) 687 sz = (int)sb.st_blksize; 688 } else 689 syswarn(0,errno,"Unable to obtain block size for file %s",fnm); 690 rem = sz; 691 *left = 0L; 692 693 /* 694 * Copy the archive to the file the number of bytes specified. We have 695 * to assume that we want to recover file holes as none of the archive 696 * formats can record the location of file holes. 697 */ 698 while (size > 0L) { 699 cnt = bufend - bufpt; 700 /* 701 * if we get a read error, we do not want to skip, as we may 702 * miss a header, so we do not set left, but if we get a write 703 * error, we do want to skip over the unprocessed data. 704 */ 705 if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) 706 break; 707 cnt = MIN(cnt, size); 708 if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) { 709 *left = size; 710 break; 711 } 712 713 if (docrc) { 714 /* 715 * update the actual crc value 716 */ 717 cnt = res; 718 while (--cnt >= 0) 719 crc += *bufpt++ & 0xff; 720 } else 721 bufpt += res; 722 size -= res; 723 } 724 725 /* 726 * if the last block has a file hole (all zero), we must make sure this 727 * gets updated in the file. We force the last block of zeros to be 728 * written. just closing with the file offset moved forward may not put 729 * a hole at the end of the file. 730 */ 731 if (isem && (arcn->sb.st_size > 0L)) 732 file_flush(ofd, fnm, isem); 733 734 /* 735 * if we failed from archive read, we do not want to skip 736 */ 737 if ((size > 0L) && (*left == 0L)) 738 return(-1); 739 740 /* 741 * some formats record a crc on file data. If so, then we compare the 742 * calculated crc to the crc stored in the archive 743 */ 744 if (docrc && (size == 0L) && (arcn->crc != crc)) 745 paxwarn(1,"Actual crc does not match expected crc %s",arcn->name); 746 return(0); 747 } 748 749 /* 750 * cp_file() 751 * copy the contents of one file to another. used during -rw phase of pax 752 * just as in rd_wrfile() we use a special write function to write the 753 * destination file so we can properly copy files with holes. 754 */ 755 756 void 757 cp_file(ARCHD *arcn, int fd1, int fd2) 758 { 759 int cnt; 760 off_t cpcnt = 0L; 761 int res = 0; 762 char *fnm = arcn->name; 763 int no_hole = 0; 764 int isem = 1; 765 int rem; 766 int sz = MINFBSZ; 767 struct stat sb; 768 769 /* 770 * check for holes in the source file. If none, we will use regular 771 * write instead of file write. 772 */ 773 if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size) 774 ++no_hole; 775 776 /* 777 * pass the blocksize of the file being written to the write routine, 778 * if the size is zero, use the default MINFBSZ 779 */ 780 if (fstat(fd2, &sb) == 0) { 781 if (sb.st_blksize > 0) 782 sz = sb.st_blksize; 783 } else 784 syswarn(0,errno,"Unable to obtain block size for file %s",fnm); 785 rem = sz; 786 787 /* 788 * read the source file and copy to destination file until EOF 789 */ 790 for(;;) { 791 if ((cnt = read(fd1, buf, blksz)) <= 0) 792 break; 793 if (no_hole) 794 res = write(fd2, buf, cnt); 795 else 796 res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm); 797 if (res != cnt) 798 break; 799 cpcnt += cnt; 800 } 801 802 /* 803 * check to make sure the copy is valid. 804 */ 805 if (res < 0) 806 syswarn(1, errno, "Failed write during copy of %s to %s", 807 arcn->org_name, arcn->name); 808 else if (cpcnt != arcn->sb.st_size) 809 paxwarn(1, "File %s changed size during copy to %s", 810 arcn->org_name, arcn->name); 811 else if (fstat(fd1, &sb) < 0) 812 syswarn(1, errno, "Failed stat of %s", arcn->org_name); 813 else if (arcn->sb.st_mtime != sb.st_mtime) 814 paxwarn(1, "File %s was modified during copy to %s", 815 arcn->org_name, arcn->name); 816 817 /* 818 * if the last block has a file hole (all zero), we must make sure this 819 * gets updated in the file. We force the last block of zeros to be 820 * written. just closing with the file offset moved forward may not put 821 * a hole at the end of the file. 822 */ 823 if (!no_hole && isem && (arcn->sb.st_size > 0L)) 824 file_flush(fd2, fnm, isem); 825 return; 826 } 827 828 /* 829 * buf_fill() 830 * fill the read buffer with the next record (or what we can get) from 831 * the archive volume. 832 * Return: 833 * Number of bytes of data in the read buffer, -1 for read error, and 834 * 0 when finished (user specified termination in ar_next()). 835 */ 836 837 int 838 buf_fill(void) 839 { 840 int cnt; 841 static int fini = 0; 842 843 if (fini) 844 return(0); 845 846 for(;;) { 847 /* 848 * try to fill the buffer. on error the next archive volume is 849 * opened and we try again. 850 */ 851 if ((cnt = ar_read(buf, blksz)) > 0) { 852 bufpt = buf; 853 bufend = buf + cnt; 854 rdcnt += cnt; 855 return(cnt); 856 } 857 858 /* 859 * errors require resync, EOF goes to next archive 860 */ 861 if (cnt < 0) 862 break; 863 if (ar_next() < 0) { 864 fini = 1; 865 return(0); 866 } 867 rdcnt = 0; 868 } 869 exit_val = 1; 870 return(-1); 871 } 872 873 /* 874 * buf_flush() 875 * force the write buffer to the archive. We are passed the number of 876 * bytes in the buffer at the point of the flush. When we change archives 877 * the record size might change. (either larger or smaller). 878 * Return: 879 * 0 if all is ok, -1 when a write error occurs. 880 */ 881 882 int 883 buf_flush(int bufcnt) 884 { 885 int cnt; 886 int push = 0; 887 int totcnt = 0; 888 889 /* 890 * if we have reached the user specified byte count for each archive 891 * volume, prompt for the next volume. (The non-standrad -R flag). 892 * NOTE: If the wrlimit is smaller than wrcnt, we will always write 893 * at least one record. We always round limit UP to next blocksize. 894 */ 895 if ((wrlimit > 0) && (wrcnt > wrlimit)) { 896 paxwarn(0, "User specified archive volume byte limit reached."); 897 if (ar_next() < 0) { 898 wrcnt = 0; 899 exit_val = 1; 900 return(-1); 901 } 902 wrcnt = 0; 903 904 /* 905 * The new archive volume might have changed the size of the 906 * write blocksize. if so we figure out if we need to write 907 * (one or more times), or if there is now free space left in 908 * the buffer (it is no longer full). bufcnt has the number of 909 * bytes in the buffer, (the blocksize, at the point we were 910 * CALLED). Push has the amount of "extra" data in the buffer 911 * if the block size has shrunk from a volume change. 912 */ 913 bufend = buf + blksz; 914 if (blksz > bufcnt) 915 return(0); 916 if (blksz < bufcnt) 917 push = bufcnt - blksz; 918 } 919 920 /* 921 * We have enough data to write at least one archive block 922 */ 923 for (;;) { 924 /* 925 * write a block and check if it all went out ok 926 */ 927 cnt = ar_write(buf, blksz); 928 if (cnt == blksz) { 929 /* 930 * the write went ok 931 */ 932 wrcnt += cnt; 933 totcnt += cnt; 934 if (push > 0) { 935 /* we have extra data to push to the front. 936 * check for more than 1 block of push, and if 937 * so we loop back to write again 938 */ 939 memcpy(buf, bufend, push); 940 bufpt = buf + push; 941 if (push >= blksz) { 942 push -= blksz; 943 continue; 944 } 945 } else 946 bufpt = buf; 947 return(totcnt); 948 } else if (cnt > 0) { 949 /* 950 * Oh drat we got a partial write! 951 * if format doesnt care about alignment let it go, 952 * we warned the user in ar_write().... but this means 953 * the last record on this volume violates pax spec.... 954 */ 955 totcnt += cnt; 956 wrcnt += cnt; 957 bufpt = buf + cnt; 958 cnt = bufcnt - cnt; 959 memcpy(buf, bufpt, cnt); 960 bufpt = buf + cnt; 961 if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0)) 962 return(totcnt); 963 break; 964 } 965 966 /* 967 * All done, go to next archive 968 */ 969 wrcnt = 0; 970 if (ar_next() < 0) 971 break; 972 973 /* 974 * The new archive volume might also have changed the block 975 * size. if so, figure out if we have too much or too little 976 * data for using the new block size 977 */ 978 bufend = buf + blksz; 979 if (blksz > bufcnt) 980 return(0); 981 if (blksz < bufcnt) 982 push = bufcnt - blksz; 983 } 984 985 /* 986 * write failed, stop pax. we must not create a bad archive! 987 */ 988 exit_val = 1; 989 return(-1); 990 } 991