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