No empty .Rs/.Re
[netbsd-mini2440.git] / bin / pax / buf_subs.c
blob1413daa8bbc413c0405439cb90a34286e3146ea5
1 /* $NetBSD: buf_subs.c,v 1.27 2006/02/11 10:43:18 dsl Exp $ */
3 /*-
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
40 #include <sys/cdefs.h>
41 #if !defined(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.27 2006/02/11 10:43:18 dsl Exp $");
46 #endif
47 #endif /* not lint */
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"
63 * routines which implement archive and file buffering
66 #define MINFBSZ 512 /* default block size for hole detect */
67 #define MAXFLT 10 /* default media read error limit */
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.
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 */
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
93 int
94 wr_start(void)
96 buf = &(bufmem[BLKMULT]);
98 * Check to make sure the write block size meets pax specs. If the user
99 * does not specify a blocksize, we use the format default blocksize.
100 * We must be picky on writes, so we do not allow the user to create an
101 * archive that might be hard to read elsewhere. If all ok, we then
102 * open the first archive volume
104 if (!wrblksz)
105 wrblksz = frmt->bsz;
106 if (wrblksz > MAXBLK) {
107 tty_warn(1, "Write block size of %d too large, maximum is: %d",
108 wrblksz, MAXBLK);
109 return -1;
111 if (wrblksz % BLKMULT) {
112 tty_warn(1, "Write block size of %d is not a %d byte multiple",
113 wrblksz, BLKMULT);
114 return -1;
118 * we only allow wrblksz to be used with all archive operations
120 blksz = rdblksz = wrblksz;
121 if ((ar_open(arcname) < 0) && (ar_next() < 0))
122 return -1;
123 wrcnt = 0;
124 bufend = buf + wrblksz;
125 bufpt = buf;
126 return 0;
130 * rd_start()
131 * set up buffering system to read an archive
132 * Return:
133 * 0 if ok, -1 otherwise
137 rd_start(void)
140 * leave space for the header pushback (see get_arc()). If we are
141 * going to append and user specified a write block size, check it
142 * right away
144 buf = &(bufmem[BLKMULT]);
145 if ((act == APPND) && wrblksz) {
146 if (wrblksz > MAXBLK) {
147 tty_warn(1,
148 "Write block size %d too large, maximum is: %d",
149 wrblksz, MAXBLK);
150 return -1;
152 if (wrblksz % BLKMULT) {
153 tty_warn(1,
154 "Write block size %d is not a %d byte multiple",
155 wrblksz, BLKMULT);
156 return -1;
161 * open the archive
163 if ((ar_open(arcname) < 0) && (ar_next() < 0))
164 return -1;
165 bufend = buf + rdblksz;
166 bufpt = bufend;
167 rdcnt = 0;
168 return 0;
172 * cp_start()
173 * set up buffer system for copying within the file system
176 void
177 cp_start(void)
179 buf = &(bufmem[BLKMULT]);
180 rdblksz = blksz = MAXBLK;
184 * appnd_start()
185 * Set up the buffering system to append new members to an archive that
186 * was just read. The last block(s) of an archive may contain a format
187 * specific trailer. To append a new member, this trailer has to be
188 * removed from the archive. The first byte of the trailer is replaced by
189 * the start of the header of the first file added to the archive. The
190 * format specific end read function tells us how many bytes to move
191 * backwards in the archive to be positioned BEFORE the trailer. Two
192 * different positions have to be adjusted, the O.S. file offset (e.g. the
193 * position of the tape head) and the write point within the data we have
194 * stored in the read (soon to become write) buffer. We may have to move
195 * back several records (the number depends on the size of the archive
196 * record and the size of the format trailer) to read up the record where
197 * the first byte of the trailer is recorded. Trailers may span (and
198 * overlap) record boundaries.
199 * We first calculate which record has the first byte of the trailer. We
200 * move the OS file offset back to the start of this record and read it
201 * up. We set the buffer write pointer to be at this byte (the byte where
202 * the trailer starts). We then move the OS file pointer back to the
203 * start of this record so a flush of this buffer will replace the record
204 * in the archive.
205 * A major problem is rewriting this last record. For archives stored
206 * on disk files, this is trivial. However, many devices are really picky
207 * about the conditions under which they will allow a write to occur.
208 * Often devices restrict the conditions where writes can be made,
209 * so it may not be feasable to append archives stored on all types of
210 * devices.
211 * Return:
212 * 0 for success, -1 for failure
216 appnd_start(off_t skcnt)
218 int res;
219 off_t cnt;
221 if (exit_val != 0) {
222 tty_warn(0, "Cannot append to an archive that may have flaws.");
223 return -1;
226 * if the user did not specify a write blocksize, inherit the size used
227 * in the last archive volume read. (If a is set we still use rdblksz
228 * until next volume, cannot shift sizes within a single volume).
230 if (!wrblksz)
231 wrblksz = blksz = rdblksz;
232 else
233 blksz = rdblksz;
236 * make sure that this volume allows appends
238 if (ar_app_ok() < 0)
239 return -1;
242 * Calculate bytes to move back and move in front of record where we
243 * need to start writing from. Remember we have to add in any padding
244 * that might be in the buffer after the trailer in the last block. We
245 * travel skcnt + padding ROUNDED UP to blksize.
247 skcnt += bufend - bufpt;
248 if ((cnt = (skcnt/blksz) * blksz) < skcnt)
249 cnt += blksz;
250 if (ar_rev((off_t)cnt) < 0)
251 goto out;
254 * We may have gone too far if there is valid data in the block we are
255 * now in front of, read up the block and position the pointer after
256 * the valid data.
258 if ((cnt -= skcnt) > 0) {
260 * watch out for stupid tape drives. ar_rev() will set rdblksz
261 * to be real physical blocksize so we must loop until we get
262 * the old rdblksz (now in blksz). If ar_rev() fouls up the
263 * determination of the physical block size, we will fail.
265 bufpt = buf;
266 bufend = buf + blksz;
267 while (bufpt < bufend) {
268 if ((res = ar_read(bufpt, rdblksz)) <= 0)
269 goto out;
270 bufpt += res;
272 if (ar_rev((off_t)(bufpt - buf)) < 0)
273 goto out;
274 bufpt = buf + cnt;
275 bufend = buf + blksz;
276 } else {
278 * buffer is empty
280 bufend = buf + blksz;
281 bufpt = buf;
283 rdblksz = blksz;
284 rdcnt -= skcnt;
285 wrcnt = 0;
288 * At this point we are ready to write. If the device requires special
289 * handling to write at a point were previously recorded data resides,
290 * that is handled in ar_set_wr(). From now on we operate under normal
291 * ARCHIVE mode (write) conditions
293 if (ar_set_wr() < 0)
294 return -1;
295 act = ARCHIVE;
296 return 0;
298 out:
299 tty_warn(1, "Unable to rewrite archive trailer, cannot append.");
300 return -1;
304 * rd_sync()
305 * A read error occurred on this archive volume. Resync the buffer and
306 * try to reset the device (if possible) so we can continue to read. Keep
307 * trying to do this until we get a valid read, or we reach the limit on
308 * consecutive read faults (at which point we give up). The user can
309 * adjust the read error limit through a command line option.
310 * Returns:
311 * 0 on success, and -1 on failure
315 rd_sync(void)
317 int errcnt = 0;
318 int res;
321 * if the user says bail out on first fault, we are out of here...
323 if (maxflt == 0)
324 return -1;
325 if (act == APPND) {
326 tty_warn(1,
327 "Unable to append when there are archive read errors.");
328 return -1;
332 * poke at device and try to get past media error
334 if (ar_rdsync() < 0) {
335 if (ar_next() < 0)
336 return -1;
337 else
338 rdcnt = 0;
341 for (;;) {
342 if ((res = ar_read(buf, blksz)) > 0) {
344 * All right! got some data, fill that buffer
346 bufpt = buf;
347 bufend = buf + res;
348 rdcnt += res;
349 return 0;
353 * Oh well, yet another failed read...
354 * if error limit reached, ditch. otherwise poke device to move past
355 * bad media and try again. if media is badly damaged, we ask
356 * the poor (and upset user at this point) for the next archive
357 * volume. remember the goal on reads is to get the most we
358 * can extract out of the archive.
360 if ((maxflt > 0) && (++errcnt > maxflt))
361 tty_warn(0,
362 "Archive read error limit (%d) reached",maxflt);
363 else if (ar_rdsync() == 0)
364 continue;
365 if (ar_next() < 0)
366 break;
367 rdcnt = 0;
368 errcnt = 0;
370 return -1;
374 * pback()
375 * push the data used during the archive id phase back into the I/O
376 * buffer. This is required as we cannot be sure that the header does NOT
377 * overlap a block boundary (as in the case we are trying to recover a
378 * flawed archived). This was not designed to be used for any other
379 * purpose. (What software engineering, HA!)
380 * WARNING: do not even THINK of pback greater than BLKMULT, unless the
381 * pback space is increased.
384 void
385 pback(char *pt, int cnt)
387 bufpt -= cnt;
388 memcpy(bufpt, pt, cnt);
389 return;
393 * rd_skip()
394 * skip forward in the archive during an archive read. Used to get quickly
395 * past file data and padding for files the user did NOT select.
396 * Return:
397 * 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected.
401 rd_skip(off_t skcnt)
403 off_t res;
404 off_t cnt;
405 off_t skipped = 0;
408 * consume what data we have in the buffer. If we have to move forward
409 * whole records, we call the low level skip function to see if we can
410 * move within the archive without doing the expensive reads on data we
411 * do not want.
413 if (skcnt == 0)
414 return 0;
415 res = MIN((bufend - bufpt), skcnt);
416 bufpt += res;
417 skcnt -= res;
420 * if skcnt is now 0, then no additional i/o is needed
422 if (skcnt == 0)
423 return 0;
426 * We have to read more, calculate complete and partial record reads
427 * based on rdblksz. we skip over "cnt" complete records
429 res = skcnt%rdblksz;
430 cnt = (skcnt/rdblksz) * rdblksz;
433 * if the skip fails, we will have to resync. ar_fow will tell us
434 * how much it can skip over. We will have to read the rest.
436 if (ar_fow(cnt, &skipped) < 0)
437 return -1;
438 res += cnt - skipped;
439 rdcnt += skipped;
442 * what is left we have to read (which may be the whole thing if
443 * ar_fow() told us the device can only read to skip records);
445 while (res > 0L) {
446 cnt = bufend - bufpt;
448 * if the read fails, we will have to resync
450 if ((cnt <= 0) && ((cnt = buf_fill()) < 0))
451 return -1;
452 if (cnt == 0)
453 return 1;
454 cnt = MIN(cnt, res);
455 bufpt += cnt;
456 res -= cnt;
458 return 0;
462 * wr_fin()
463 * flush out any data (and pad if required) the last block. We always pad
464 * with zero (even though we do not have to). Padding with 0 makes it a
465 * lot easier to recover if the archive is damaged. zero paddding SHOULD
466 * BE a requirement....
469 void
470 wr_fin(void)
472 if (bufpt > buf) {
473 memset(bufpt, 0, bufend - bufpt);
474 bufpt = bufend;
475 (void)buf_flush(blksz);
480 * wr_rdbuf()
481 * fill the write buffer from data passed to it in a buffer (usually used
482 * by format specific write routines to pass a file header). On failure we
483 * punt. We do not allow the user to continue to write flawed archives.
484 * We assume these headers are not very large (the memory copy we use is
485 * a bit expensive).
486 * Return:
487 * 0 if buffer was filled ok, -1 o.w. (buffer flush failure)
491 wr_rdbuf(char *out, int outcnt)
493 int cnt;
496 * while there is data to copy copy into the write buffer. when the
497 * write buffer fills, flush it to the archive and continue
499 while (outcnt > 0) {
500 cnt = bufend - bufpt;
501 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))
502 return -1;
504 * only move what we have space for
506 cnt = MIN(cnt, outcnt);
507 memcpy(bufpt, out, cnt);
508 bufpt += cnt;
509 out += cnt;
510 outcnt -= cnt;
512 return 0;
516 * rd_wrbuf()
517 * copy from the read buffer into a supplied buffer a specified number of
518 * bytes. If the read buffer is empty fill it and continue to copy.
519 * usually used to obtain a file header for processing by a format
520 * specific read routine.
521 * Return
522 * number of bytes copied to the buffer, 0 indicates EOF on archive volume,
523 * -1 is a read error
527 rd_wrbuf(char *in, int cpcnt)
529 int res;
530 int cnt;
531 int incnt = cpcnt;
534 * loop until we fill the buffer with the requested number of bytes
536 while (incnt > 0) {
537 cnt = bufend - bufpt;
538 if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) {
540 * read error, return what we got (or the error if
541 * no data was copied). The caller must know that an
542 * error occurred and has the best knowledge what to
543 * do with it
545 if ((res = cpcnt - incnt) > 0)
546 return res;
547 return cnt;
551 * calculate how much data to copy based on whats left and
552 * state of buffer
554 cnt = MIN(cnt, incnt);
555 memcpy(in, bufpt, cnt);
556 bufpt += cnt;
557 incnt -= cnt;
558 in += cnt;
560 return cpcnt;
564 * wr_skip()
565 * skip forward during a write. In other words add padding to the file.
566 * we add zero filled padding as it makes flawed archives much easier to
567 * recover from. the caller tells us how many bytes of padding to add
568 * This routine was not designed to add HUGE amount of padding, just small
569 * amounts (a few 512 byte blocks at most)
570 * Return:
571 * 0 if ok, -1 if there was a buf_flush failure
575 wr_skip(off_t skcnt)
577 int cnt;
580 * loop while there is more padding to add
582 while (skcnt > 0L) {
583 cnt = bufend - bufpt;
584 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))
585 return -1;
586 cnt = MIN(cnt, skcnt);
587 memset(bufpt, 0, cnt);
588 bufpt += cnt;
589 skcnt -= cnt;
591 return 0;
595 * wr_rdfile()
596 * fill write buffer with the contents of a file. We are passed an open
597 * file descriptor to the file an the archive structure that describes the
598 * file we are storing. The variable "left" is modified to contain the
599 * number of bytes of the file we were NOT able to write to the archive.
600 * it is important that we always write EXACTLY the number of bytes that
601 * the format specific write routine told us to. The file can also get
602 * bigger, so reading to the end of file would create an improper archive,
603 * we just detect this case and warn the user. We never create a bad
604 * archive if we can avoid it. Of course trying to archive files that are
605 * active is asking for trouble. It we fail, we pass back how much we
606 * could NOT copy and let the caller deal with it.
607 * Return:
608 * 0 ok, -1 if archive write failure. a short read of the file returns a
609 * 0, but "left" is set to be greater than zero.
613 wr_rdfile(ARCHD *arcn, int ifd, off_t *left)
615 int cnt;
616 int res = 0;
617 off_t size = arcn->sb.st_size;
618 struct stat origsb, sb;
621 * by default, remember the previously obtained stat information
622 * (in arcn->sb) for comparing the mtime after reading.
623 * if Mflag is set, use the actual mtime instead.
625 origsb = arcn->sb;
626 if (Mflag && (fstat(ifd, &origsb) < 0))
627 syswarn(1, errno, "Failed stat on %s", arcn->org_name);
630 * while there are more bytes to write
632 while (size > 0L) {
633 cnt = bufend - bufpt;
634 if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) {
635 *left = size;
636 return -1;
638 cnt = MIN(cnt, size);
639 if ((res = read_with_restart(ifd, bufpt, cnt)) <= 0)
640 break;
641 size -= res;
642 bufpt += res;
646 * better check the file did not change during this operation
647 * or the file read failed.
649 if (res < 0)
650 syswarn(1, errno, "Read fault on %s", arcn->org_name);
651 else if (size != 0L)
652 tty_warn(1, "File changed size during read %s", arcn->org_name);
653 else if (fstat(ifd, &sb) < 0)
654 syswarn(1, errno, "Failed stat on %s", arcn->org_name);
655 else if (origsb.st_mtime != sb.st_mtime)
656 tty_warn(1, "File %s was modified during copy to archive",
657 arcn->org_name);
658 *left = size;
659 return 0;
663 * rd_wrfile()
664 * extract the contents of a file from the archive. If we are unable to
665 * extract the entire file (due to failure to write the file) we return
666 * the numbers of bytes we did NOT process. This way the caller knows how
667 * many bytes to skip past to find the next archive header. If the failure
668 * was due to an archive read, we will catch that when we try to skip. If
669 * the format supplies a file data crc value, we calculate the actual crc
670 * so that it can be compared to the value stored in the header
671 * NOTE:
672 * We call a special function to write the file. This function attempts to
673 * restore file holes (blocks of zeros) into the file. When files are
674 * sparse this saves space, and is a LOT faster. For non sparse files
675 * the performance hit is small. As of this writing, no archive supports
676 * information on where the file holes are.
677 * Return:
678 * 0 ok, -1 if archive read failure. if we cannot write the entire file,
679 * we return a 0 but "left" is set to be the amount unwritten
683 rd_wrfile(ARCHD *arcn, int ofd, off_t *left)
685 int cnt = 0;
686 off_t size = arcn->sb.st_size;
687 int res = 0;
688 char *fnm = arcn->name;
689 int isem = 1;
690 int rem;
691 int sz = MINFBSZ;
692 struct stat sb;
693 u_long crc = 0L;
696 * pass the blocksize of the file being written to the write routine,
697 * if the size is zero, use the default MINFBSZ
699 if (ofd < 0)
700 sz = PAXPATHLEN+1;
701 else if (fstat(ofd, &sb) == 0) {
702 if (sb.st_blksize > 0)
703 sz = (int)sb.st_blksize;
704 } else
705 syswarn(0, errno,
706 "Unable to obtain block size for file %s", fnm);
707 rem = sz;
708 *left = 0L;
711 * Copy the archive to the file the number of bytes specified. We have
712 * to assume that we want to recover file holes as none of the archive
713 * formats can record the location of file holes.
715 while (size > 0L) {
716 cnt = bufend - bufpt;
718 * if we get a read error, we do not want to skip, as we may
719 * miss a header, so we do not set left, but if we get a write
720 * error, we do want to skip over the unprocessed data.
722 if ((cnt <= 0) && ((cnt = buf_fill()) <= 0))
723 break;
724 cnt = MIN(cnt, size);
725 if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) {
726 *left = size;
727 break;
730 if (docrc) {
732 * update the actual crc value
734 cnt = res;
735 while (--cnt >= 0)
736 crc += *bufpt++ & 0xff;
737 } else
738 bufpt += res;
739 size -= res;
743 * if the last block has a file hole (all zero), we must make sure this
744 * gets updated in the file. We force the last block of zeros to be
745 * written. just closing with the file offset moved forward may not put
746 * a hole at the end of the file.
748 if (ofd >= 0 && isem && (arcn->sb.st_size > 0L))
749 file_flush(ofd, fnm, isem);
752 * if we failed from archive read, we do not want to skip
754 if ((size > 0L) && (*left == 0L))
755 return -1;
758 * some formats record a crc on file data. If so, then we compare the
759 * calculated crc to the crc stored in the archive
761 if (docrc && (size == 0L) && (arcn->crc != crc))
762 tty_warn(1,"Actual crc does not match expected crc %s",
763 arcn->name);
764 return 0;
768 * cp_file()
769 * copy the contents of one file to another. used during -rw phase of pax
770 * just as in rd_wrfile() we use a special write function to write the
771 * destination file so we can properly copy files with holes.
774 void
775 cp_file(ARCHD *arcn, int fd1, int fd2)
777 int cnt;
778 off_t cpcnt = 0L;
779 int res = 0;
780 char *fnm = arcn->name;
781 int no_hole = 0;
782 int isem = 1;
783 int rem;
784 int sz = MINFBSZ;
785 struct stat sb, origsb;
788 * check for holes in the source file. If none, we will use regular
789 * write instead of file write.
791 if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size)
792 ++no_hole;
795 * by default, remember the previously obtained stat information
796 * (in arcn->sb) for comparing the mtime after reading.
797 * if Mflag is set, use the actual mtime instead.
799 origsb = arcn->sb;
800 if (Mflag && (fstat(fd1, &origsb) < 0))
801 syswarn(1, errno, "Failed stat on %s", arcn->org_name);
804 * pass the blocksize of the file being written to the write routine,
805 * if the size is zero, use the default MINFBSZ
807 if (fstat(fd2, &sb) == 0) {
808 if (sb.st_blksize > 0)
809 sz = sb.st_blksize;
810 } else
811 syswarn(0, errno,
812 "Unable to obtain block size for file %s", fnm);
813 rem = sz;
816 * read the source file and copy to destination file until EOF
818 for(;;) {
819 if ((cnt = read_with_restart(fd1, buf, blksz)) <= 0)
820 break;
821 if (no_hole)
822 res = xwrite(fd2, buf, cnt);
823 else
824 res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm);
825 if (res != cnt)
826 break;
827 cpcnt += cnt;
831 * check to make sure the copy is valid.
833 if (res < 0)
834 syswarn(1, errno, "Failed write during copy of %s to %s",
835 arcn->org_name, arcn->name);
836 else if (cpcnt != arcn->sb.st_size)
837 tty_warn(1, "File %s changed size during copy to %s",
838 arcn->org_name, arcn->name);
839 else if (fstat(fd1, &sb) < 0)
840 syswarn(1, errno, "Failed stat of %s", arcn->org_name);
841 else if (origsb.st_mtime != sb.st_mtime)
842 tty_warn(1, "File %s was modified during copy to %s",
843 arcn->org_name, arcn->name);
846 * if the last block has a file hole (all zero), we must make sure this
847 * gets updated in the file. We force the last block of zeros to be
848 * written. just closing with the file offset moved forward may not put
849 * a hole at the end of the file.
851 if (!no_hole && isem && (arcn->sb.st_size > 0L))
852 file_flush(fd2, fnm, isem);
853 return;
857 * buf_fill()
858 * fill the read buffer with the next record (or what we can get) from
859 * the archive volume.
860 * Return:
861 * Number of bytes of data in the read buffer, -1 for read error, and
862 * 0 when finished (user specified termination in ar_next()).
866 buf_fill(void)
868 int cnt;
869 static int fini = 0;
871 if (fini)
872 return 0;
874 for(;;) {
876 * try to fill the buffer. on error the next archive volume is
877 * opened and we try again.
879 if ((cnt = ar_read(buf, blksz)) > 0) {
880 bufpt = buf;
881 bufend = buf + cnt;
882 rdcnt += cnt;
883 return cnt;
887 * errors require resync, EOF goes to next archive
888 * but in case we have not determined yet the format,
889 * this means that we have a very short file, so we
890 * are done again.
892 if (cnt < 0)
893 break;
894 if (frmt == NULL || ar_next() < 0) {
895 fini = 1;
896 return 0;
898 rdcnt = 0;
900 exit_val = 1;
901 return -1;
905 * buf_flush()
906 * force the write buffer to the archive. We are passed the number of
907 * bytes in the buffer at the point of the flush. When we change archives
908 * the record size might change. (either larger or smaller).
909 * Return:
910 * 0 if all is ok, -1 when a write error occurs.
914 buf_flush(int bufcnt)
916 int cnt;
917 int push = 0;
918 int totcnt = 0;
921 * if we have reached the user specified byte count for each archive
922 * volume, prompt for the next volume. (The non-standard -R flag).
923 * NOTE: If the wrlimit is smaller than wrcnt, we will always write
924 * at least one record. We always round limit UP to next blocksize.
926 if ((wrlimit > 0) && (wrcnt > wrlimit)) {
927 tty_warn(0,
928 "User specified archive volume byte limit reached.");
929 if (ar_next() < 0) {
930 wrcnt = 0;
931 exit_val = 1;
932 return -1;
934 wrcnt = 0;
937 * The new archive volume might have changed the size of the
938 * write blocksize. if so we figure out if we need to write
939 * (one or more times), or if there is now free space left in
940 * the buffer (it is no longer full). bufcnt has the number of
941 * bytes in the buffer, (the blocksize, at the point we were
942 * CALLED). Push has the amount of "extra" data in the buffer
943 * if the block size has shrunk from a volume change.
945 bufend = buf + blksz;
946 if (blksz > bufcnt)
947 return 0;
948 if (blksz < bufcnt)
949 push = bufcnt - blksz;
953 * We have enough data to write at least one archive block
955 for (;;) {
957 * write a block and check if it all went out ok
959 cnt = ar_write(buf, blksz);
960 if (cnt == blksz) {
962 * the write went ok
964 wrcnt += cnt;
965 totcnt += cnt;
966 if (push > 0) {
967 /* we have extra data to push to the front.
968 * check for more than 1 block of push, and if
969 * so we loop back to write again
971 memcpy(buf, bufend, push);
972 bufpt = buf + push;
973 if (push >= blksz) {
974 push -= blksz;
975 continue;
977 } else
978 bufpt = buf;
979 return totcnt;
980 } else if (cnt > 0) {
982 * Oh drat we got a partial write!
983 * if format doesnt care about alignment let it go,
984 * we warned the user in ar_write().... but this means
985 * the last record on this volume violates pax spec....
987 totcnt += cnt;
988 wrcnt += cnt;
989 bufpt = buf + cnt;
990 cnt = bufcnt - cnt;
991 memcpy(buf, bufpt, cnt);
992 bufpt = buf + cnt;
993 if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0))
994 return totcnt;
995 break;
999 * All done, go to next archive
1001 wrcnt = 0;
1002 if (ar_next() < 0)
1003 break;
1006 * The new archive volume might also have changed the block
1007 * size. if so, figure out if we have too much or too little
1008 * data for using the new block size
1010 bufend = buf + blksz;
1011 if (blksz > bufcnt)
1012 return 0;
1013 if (blksz < bufcnt)
1014 push = bufcnt - blksz;
1018 * write failed, stop pax. we must not create a bad archive!
1020 exit_val = 1;
1021 return -1;