]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/buf_subs.c
6e13e8f77d2fd8e306722c1cd986d85e8325cbaf
1 /* $OpenBSD: buf_subs.c,v 1.7 1997/09/01 18:29:46 deraadt Exp $ */
2 /* $NetBSD: buf_subs.c,v 1.5 1995/03/21 09:07:08 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid
[] = "@(#)buf_subs.c 8.2 (Berkeley) 4/18/94";
45 static char rcsid
[] __attribute__((__unused__
)) = "$OpenBSD: buf_subs.c,v 1.7 1997/09/01 18:29:46 deraadt Exp $";
49 #include <sys/types.h>
52 #include <sys/param.h>
62 * routines which implement archive and file buffering
65 #define MINFBSZ 512 /* default block size for hole detect */
66 #define MAXFLT 10 /* default media read error limit */
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.
73 static char bufmem
[MAXBLK
+BLKMULT
]; /* i/o buffer + pushback id space */
74 static char *buf
; /* normal start of i/o buffer */
75 static char *bufend
; /* end or last char in i/o buffer */
76 static char *bufpt
; /* read/write point in i/o buffer */
77 int blksz
= MAXBLK
; /* block input/output size in bytes */
78 int wrblksz
; /* user spec output size in bytes */
79 int maxflt
= MAXFLT
; /* MAX consecutive media errors */
80 int rdblksz
; /* first read blksize (tapes only) */
81 off_t wrlimit
; /* # of bytes written per archive vol */
82 off_t wrcnt
; /* # of bytes written on current vol */
83 off_t rdcnt
; /* # of bytes read on current vol */
87 * set up the buffering system to operate in a write mode
89 * 0 if ok, -1 if the user specified write block size violates pax spec
100 buf
= &(bufmem
[BLKMULT
]);
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
110 if (wrblksz
> MAXBLK
) {
111 paxwarn(1, "Write block size of %d too large, maximium is: %d",
115 if (wrblksz
% BLKMULT
) {
116 paxwarn(1, "Write block size of %d is not a %d byte multiple",
120 if (wrblksz
> MAXBLK_POSIX
) {
121 paxwarn(0, "Write block size of %d larger than POSIX max %d, archive may not be portable",
122 wrblksz
, MAXBLK_POSIX
);
127 * we only allow wrblksz to be used with all archive operations
129 blksz
= rdblksz
= wrblksz
;
130 if ((ar_open(arcname
) < 0) && (ar_next() < 0))
133 bufend
= buf
+ wrblksz
;
140 * set up buffering system to read an archive
142 * 0 if ok, -1 otherwise
154 * leave space for the header pushback (see get_arc()). If we are
155 * going to append and user specified a write block size, check it
158 buf
= &(bufmem
[BLKMULT
]);
159 if ((act
== APPND
) && wrblksz
) {
160 if (wrblksz
> MAXBLK
) {
161 paxwarn(1,"Write block size %d too large, maximium is: %d",
165 if (wrblksz
% BLKMULT
) {
166 paxwarn(1, "Write block size %d is not a %d byte multiple",
175 if ((ar_open(arcname
) < 0) && (ar_next() < 0))
177 bufend
= buf
+ rdblksz
;
185 * set up buffer system for copying within the file system
196 buf
= &(bufmem
[BLKMULT
]);
197 rdblksz
= blksz
= MAXBLK
;
202 * Set up the buffering system to append new members to an archive that
203 * was just read. The last block(s) of an archive may contain a format
204 * specific trailer. To append a new member, this trailer has to be
205 * removed from the archive. The first byte of the trailer is replaced by
206 * the start of the header of the first file added to the archive. The
207 * format specific end read function tells us how many bytes to move
208 * backwards in the archive to be positioned BEFORE the trailer. Two
209 * different postions have to be adjusted, the O.S. file offset (e.g. the
210 * position of the tape head) and the write point within the data we have
211 * stored in the read (soon to become write) buffer. We may have to move
212 * back several records (the number depends on the size of the archive
213 * record and the size of the format trailer) to read up the record where
214 * the first byte of the trailer is recorded. Trailers may span (and
215 * overlap) record boundries.
216 * We first calculate which record has the first byte of the trailer. We
217 * move the OS file offset back to the start of this record and read it
218 * up. We set the buffer write pointer to be at this byte (the byte where
219 * the trailer starts). We then move the OS file pointer back to the
220 * start of this record so a flush of this buffer will replace the record
222 * A major problem is rewriting this last record. For archives stored
223 * on disk files, this is trival. However, many devices are really picky
224 * about the conditions under which they will allow a write to occur.
225 * Often devices restrict the conditions where writes can be made writes,
226 * so it may not be feasable to append archives stored on all types of
229 * 0 for success, -1 for failure
234 appnd_start(off_t skcnt
)
245 paxwarn(0, "Cannot append to an archive that may have flaws.");
249 * if the user did not specify a write blocksize, inherit the size used
250 * in the last archive volume read. (If a is set we still use rdblksz
251 * until next volume, cannot shift sizes within a single volume).
254 wrblksz
= blksz
= rdblksz
;
259 * make sure that this volume allows appends
265 * Calculate bytes to move back and move in front of record where we
266 * need to start writing from. Remember we have to add in any padding
267 * that might be in the buffer after the trailer in the last block. We
268 * travel skcnt + padding ROUNDED UP to blksize.
270 skcnt
+= bufend
- bufpt
;
271 if ((cnt
= (skcnt
/blksz
) * blksz
) < skcnt
)
273 if (ar_rev((off_t
)cnt
) < 0)
277 * We may have gone too far if there is valid data in the block we are
278 * now in front of, read up the block and position the pointer after
281 if ((cnt
-= skcnt
) > 0) {
283 * watch out for stupid tape drives. ar_rev() will set rdblksz
284 * to be real physical blocksize so we must loop until we get
285 * the old rdblksz (now in blksz). If ar_rev() fouls up the
286 * determination of the physical block size, we will fail.
289 bufend
= buf
+ blksz
;
290 while (bufpt
< bufend
) {
291 if ((res
= ar_read(bufpt
, rdblksz
)) <= 0)
295 if (ar_rev((off_t
)(bufpt
- buf
)) < 0)
298 bufend
= buf
+ blksz
;
303 bufend
= buf
+ blksz
;
311 * At this point we are ready to write. If the device requires special
312 * handling to write at a point were previously recorded data resides,
313 * that is handled in ar_set_wr(). From now on we operate under normal
314 * ARCHIVE mode (write) conditions
322 paxwarn(1, "Unable to rewrite archive trailer, cannot append.");
328 * A read error occurred on this archive volume. Resync the buffer and
329 * try to reset the device (if possible) so we can continue to read. Keep
330 * trying to do this until we get a valid read, or we reach the limit on
331 * consecutive read faults (at which point we give up). The user can
332 * adjust the read error limit through a command line option.
334 * 0 on success, and -1 on failure
345 register int errcnt
= 0;
349 * if the user says bail out on first fault, we are out of here...
354 paxwarn(1, "Unable to append when there are archive read errors.");
359 * poke at device and try to get past media error
361 if (ar_rdsync() < 0) {
369 if ((res
= ar_read(buf
, blksz
)) > 0) {
371 * All right! got some data, fill that buffer
380 * Oh well, yet another failed read...
381 * if error limit reached, ditch. o.w. poke device to move past
382 * bad media and try again. if media is badly damaged, we ask
383 * the poor (and upset user at this point) for the next archive
384 * volume. remember the goal on reads is to get the most we
385 * can extract out of the archive.
387 if ((maxflt
> 0) && (++errcnt
> maxflt
))
388 paxwarn(0,"Archive read error limit (%d) reached",maxflt
);
389 else if (ar_rdsync() == 0)
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 boundry (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.
412 pback(char *pt
, int cnt
)
421 memcpy(bufpt
, pt
, cnt
);
427 * skip foward in the archive during a archive read. Used to get quickly
428 * past file data and padding for files the user did NOT select.
430 * 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected.
447 * consume what data we have in the buffer. If we have to move foward
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
454 res
= MIN((bufend
- bufpt
), skcnt
);
459 * if skcnt is now 0, then no additional i/o is needed
465 * We have to read more, calculate complete and partial record reads
466 * based on rdblksz. we skip over "cnt" complete records
469 cnt
= (skcnt
/rdblksz
) * rdblksz
;
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.
475 if (ar_fow(cnt
, &skipped
) < 0)
477 res
+= cnt
- skipped
;
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);
485 cnt
= bufend
- bufpt
;
487 * if the read fails, we will have to resync
489 if ((cnt
<= 0) && ((cnt
= buf_fill()) < 0))
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....
517 memset(bufpt
, 0, bufend
- bufpt
);
519 (void)buf_flush(blksz
);
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
531 * 0 if buffer was filled ok, -1 o.w. (buffer flush failure)
536 wr_rdbuf(register char *out
, register int outcnt
)
539 wr_rdbuf(out
, outcnt
)
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
551 cnt
= bufend
- bufpt
;
552 if ((cnt
<= 0) && ((cnt
= buf_flush(blksz
)) < 0))
555 * only move what we have space for
557 cnt
= MIN(cnt
, outcnt
);
558 memcpy(bufpt
, out
, cnt
);
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.
573 * number of bytes copied to the buffer, 0 indicates EOF on archive volume,
579 rd_wrbuf(register char *in
, register int cpcnt
)
589 register int incnt
= cpcnt
;
592 * loop until we fill the buffer with the requested number of bytes
595 cnt
= bufend
- bufpt
;
596 if ((cnt
<= 0) && ((cnt
= buf_fill()) <= 0)) {
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
603 if ((res
= cpcnt
- incnt
) > 0)
609 * calculate how much data to copy based on whats left and
612 cnt
= MIN(cnt
, incnt
);
613 memcpy(in
, bufpt
, cnt
);
623 * skip foward 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)
629 * 0 if ok, -1 if there was a buf_flush failure
644 * loop while there is more padding to add
647 cnt
= bufend
- bufpt
;
648 if ((cnt
<= 0) && ((cnt
= buf_flush(blksz
)) < 0))
650 cnt
= MIN(cnt
, skcnt
);
651 memset(bufpt
, 0, cnt
);
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.
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.
678 wr_rdfile(ARCHD
*arcn
, int ifd
, off_t
*left
)
681 wr_rdfile(arcn
, ifd
, left
)
688 register int res
= 0;
689 register off_t size
= arcn
->sb
.st_size
;
693 * while there are more bytes to write
696 cnt
= bufend
- bufpt
;
697 if ((cnt
<= 0) && ((cnt
= buf_flush(blksz
)) < 0)) {
701 cnt
= MIN(cnt
, size
);
702 if ((res
= read(ifd
, bufpt
, cnt
)) <= 0)
709 * better check the file did not change during this operation
710 * or the file read failed.
713 syswarn(1, errno
, "Read fault on %s", arcn
->org_name
);
715 paxwarn(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 paxwarn(1, "File %s was modified during copy to archive",
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
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.
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
747 rd_wrfile(ARCHD
*arcn
, int ofd
, off_t
*left
)
750 rd_wrfile(arcn
, ofd
, left
)
756 register int cnt
= 0;
757 register off_t size
= arcn
->sb
.st_size
;
758 register int res
= 0;
759 register char *fnm
= arcn
->name
;
767 * pass the blocksize of the file being written to the write routine,
768 * if the size is zero, use the default MINFBSZ
770 if (fstat(ofd
, &sb
) == 0) {
771 if (sb
.st_blksize
> 0)
772 sz
= (int)sb
.st_blksize
;
774 syswarn(0,errno
,"Unable to obtain block size for file %s",fnm
);
779 * Copy the archive to the file the number of bytes specified. We have
780 * to assume that we want to recover file holes as none of the archive
781 * formats can record the location of file holes.
784 cnt
= bufend
- bufpt
;
786 * if we get a read error, we do not want to skip, as we may
787 * miss a header, so we do not set left, but if we get a write
788 * error, we do want to skip over the unprocessed data.
790 if ((cnt
<= 0) && ((cnt
= buf_fill()) <= 0))
792 cnt
= MIN(cnt
, size
);
793 if ((res
= file_write(ofd
,bufpt
,cnt
,&rem
,&isem
,sz
,fnm
)) <= 0) {
800 * update the actual crc value
804 crc
+= *bufpt
++ & 0xff;
811 * if the last block has a file hole (all zero), we must make sure this
812 * gets updated in the file. We force the last block of zeros to be
813 * written. just closing with the file offset moved foward may not put
814 * a hole at the end of the file.
816 if (isem
&& (arcn
->sb
.st_size
> 0L))
817 file_flush(ofd
, fnm
, isem
);
820 * if we failed from archive read, we do not want to skip
822 if ((size
> 0L) && (*left
== 0L))
826 * some formats record a crc on file data. If so, then we compare the
827 * calculated crc to the crc stored in the archive
829 if (docrc
&& (size
== 0L) && (arcn
->crc
!= crc
))
830 paxwarn(1,"Actual crc does not match expected crc %s",arcn
->name
);
836 * copy the contents of one file to another. used during -rw phase of pax
837 * just as in rd_wrfile() we use a special write function to write the
838 * destination file so we can properly copy files with holes.
843 cp_file(ARCHD
*arcn
, int fd1
, int fd2
)
846 cp_file(arcn
, fd1
, fd2
)
853 register off_t cpcnt
= 0L;
854 register int res
= 0;
855 register char *fnm
= arcn
->name
;
856 register int no_hole
= 0;
863 * check for holes in the source file. If none, we will use regular
864 * write instead of file write.
866 if (((off_t
)(arcn
->sb
.st_blocks
* BLKMULT
)) >= arcn
->sb
.st_size
)
870 * pass the blocksize of the file being written to the write routine,
871 * if the size is zero, use the default MINFBSZ
873 if (fstat(fd2
, &sb
) == 0) {
874 if (sb
.st_blksize
> 0)
877 syswarn(0,errno
,"Unable to obtain block size for file %s",fnm
);
881 * read the source file and copy to destination file until EOF
884 if ((cnt
= read(fd1
, buf
, blksz
)) <= 0)
887 res
= write(fd2
, buf
, cnt
);
889 res
= file_write(fd2
, buf
, cnt
, &rem
, &isem
, sz
, fnm
);
896 * check to make sure the copy is valid.
899 syswarn(1, errno
, "Failed write during copy of %s to %s",
900 arcn
->org_name
, arcn
->name
);
901 else if (cpcnt
!= arcn
->sb
.st_size
)
902 paxwarn(1, "File %s changed size during copy to %s",
903 arcn
->org_name
, arcn
->name
);
904 else if (fstat(fd1
, &sb
) < 0)
905 syswarn(1, errno
, "Failed stat of %s", arcn
->org_name
);
906 else if (arcn
->sb
.st_mtime
!= sb
.st_mtime
)
907 paxwarn(1, "File %s was modified during copy to %s",
908 arcn
->org_name
, arcn
->name
);
911 * if the last block has a file hole (all zero), we must make sure this
912 * gets updated in the file. We force the last block of zeros to be
913 * written. just closing with the file offset moved foward may not put
914 * a hole at the end of the file.
916 if (!no_hole
&& isem
&& (arcn
->sb
.st_size
> 0L))
917 file_flush(fd2
, fnm
, isem
);
923 * fill the read buffer with the next record (or what we can get) from
924 * the archive volume.
926 * Number of bytes of data in the read buffer, -1 for read error, and
927 * 0 when finished (user specified termination in ar_next()).
946 * try to fill the buffer. on error the next archive volume is
947 * opened and we try again.
949 if ((cnt
= ar_read(buf
, blksz
)) > 0) {
957 * errors require resync, EOF goes to next archive
973 * force the write buffer to the archive. We are passed the number of
974 * bytes in the buffer at the point of the flush. When we change archives
975 * the record size might change. (either larger or smaller).
977 * 0 if all is ok, -1 when a write error occurs.
982 buf_flush(register int bufcnt
)
990 register int push
= 0;
991 register int totcnt
= 0;
994 * if we have reached the user specified byte count for each archive
995 * volume, prompt for the next volume. (The non-standrad -R flag).
996 * NOTE: If the wrlimit is smaller than wrcnt, we will always write
997 * at least one record. We always round limit UP to next blocksize.
999 if ((wrlimit
> 0) && (wrcnt
> wrlimit
)) {
1000 paxwarn(0, "User specified archive volume byte limit reached.");
1001 if (ar_next() < 0) {
1009 * The new archive volume might have changed the size of the
1010 * write blocksize. if so we figure out if we need to write
1011 * (one or more times), or if there is now free space left in
1012 * the buffer (it is no longer full). bufcnt has the number of
1013 * bytes in the buffer, (the blocksize, at the point we were
1014 * CALLED). Push has the amount of "extra" data in the buffer
1015 * if the block size has shrunk from a volume change.
1017 bufend
= buf
+ blksz
;
1021 push
= bufcnt
- blksz
;
1025 * We have enough data to write at least one archive block
1029 * write a block and check if it all went out ok
1031 cnt
= ar_write(buf
, blksz
);
1039 /* we have extra data to push to the front.
1040 * check for more than 1 block of push, and if
1041 * so we loop back to write again
1043 memcpy(buf
, bufend
, push
);
1045 if (push
>= blksz
) {
1052 } else if (cnt
> 0) {
1054 * Oh drat we got a partial write!
1055 * if format doesnt care about alignment let it go,
1056 * we warned the user in ar_write().... but this means
1057 * the last record on this volume violates pax spec....
1063 memcpy(buf
, bufpt
, cnt
);
1065 if (!frmt
->blkalgn
|| ((cnt
% frmt
->blkalgn
) == 0))
1071 * All done, go to next archive
1078 * The new archive volume might also have changed the block
1079 * size. if so, figure out if we have too much or too little
1080 * data for using the new block size
1082 bufend
= buf
+ blksz
;
1086 push
= bufcnt
- blksz
;
1090 * write failed, stop pax. we must not create a bad archive!