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