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