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