* Copyright (c) 1992 Keith Muller.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Keith Muller of the University of California, San Diego.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)buf_subs.c 8.1 (Berkeley) 5/31/93";
* routines which implement archive and file buffering
#define MAXFBSZ 4096 /* max block size for hole detection */
#define MINFBSZ 512 /* min block size for hole detection */
#define MAXFLT 10 /* default media read error limit */
static char bufmem
[MAXBLK
+BLKMULT
]; /* i/o buffer + pushback id space */
static char *buf
; /* normal start of i/o buffer */
static char *bufend
; /* end or last char in i/o buffer */
static char *bufpt
; /* read/write point in i/o buffer */
int blksz
= MAXBLK
; /* block input/output size in bytes */
int wrblksz
; /* user spec output size in bytes */
int maxflt
= MAXFLT
; /* MAX consecutive media errors */
int rdblksz
; /* first read blksize (tapes only) */
off_t wrlimit
; /* # of bytes written per archive vol */
off_t wrcnt
; /* # of bytes written on current vol */
off_t rdcnt
; /* # of bytes read on current vol */
* set up the buffering system to operate in a write mode
* 0 if ok, -1 if the user specified write block size violates pax spec
buf
= &(bufmem
[BLKMULT
]);
* Check to make sure the write block size meets pax specs. If the user
* does not specify a blocksize, we use the format default blocksize.
* We must be picky on writes, so we do not allow the user to create an
* archive that might be hard to read elsewhere. If all ok, we then
* open the first archive volume
warn(1, "Write block size of %d too large, maximium is: %d",
warn(1, "Write block size of %d is not a %d byte multiple",
* we only allow wrblksz to be used with all archive operations
blksz
= rdblksz
= wrblksz
;
if ((ar_open(arcname
) < 0) && (ar_next() < 0))
* set up buffering system to read an archive
* leave space for the header pushback (see get_arc()). If we are
* going to append and user specified a write block size, check it
buf
= &(bufmem
[BLKMULT
]);
if ((act
== APPND
) && wrblksz
) {
warn(1,"Write block size %d too large, maximium is: %d",
warn(1, "Write block size %d is not a %d byte multiple",
if ((ar_open(arcname
) < 0) && (ar_next() < 0))
* set up buffer system for copying within the file system
buf
= &(bufmem
[BLKMULT
]);
rdblksz
= blksz
= MAXBLK
;
* Set up the buffering system to append new members to an archive that
* was just read. The last block(s) of an archive may contain a format
* specific trailer. To append a new member, this trailer has to be
* removed from the archive. The first byte of the trailer is replaced by
* the start of the header of the first file added to the archive. The
* format specific end read function tells us how many bytes to move
* backwards in the archive to be positioned BEFORE the trailer. Two
* different postions have to be adjusted, the O.S. file offset (e.g. the
* position of the tape head) and the write point within the data we have
* stored in the read (soon to become write) buffer. We may have to move
* back several records (the number depends on the size of the archive
* record and the size of the format trailer) to read up the record where
* the first byte of the trailer is recorded. Trailers may span (and
* overlap) record boundries.
* We first calculate which record has the first byte of the trailer. We
* move the OS file offset back to the start of this record and read it
* up. We set the buffer write pointer to be at this byte (the byte where
* the trailer starts). We then move the OS file pointer back to the
* start of this record so a flush of this buffer will replace the record
* A major problem is rewriting this last record. For archives stored
* on disk files, this is trival. However, many devices are really picky
* about the conditions under which they will allow a write to occur.
* Often devices restrict the conditions where writes can be made writes,
* so it may not be feasable to append archives stored on all types of
* 0 for success, -1 for failure
warn(0, "Cannot append to an archive that may have flaws.");
* if the user did not specify a write blocksize, inherit the size used
* in the last archive volume read. (If a is set we still use rdblksz
* until next volume, cannot shift sizes within a single volume).
wrblksz
= blksz
= rdblksz
;
* make sure that this volume allows appends
* Calculate bytes to move back and move in front of record where we
* need to start writing from. Remember we have to add in any padding
* that might be in the buffer after the trailer in the last block. We
* travel skcnt + padding ROUNDED UP to blksize.
if ((cnt
= (skcnt
/blksz
) * blksz
) < skcnt
)
if (ar_rev((off_t
)cnt
) < 0)
* We may have gone too far if there is valid data in the block we are
* now in front of, read up the block and position the pointer after
if ((cnt
-= skcnt
) > 0) {
* watch out for stupid tape drives. ar_rev() will set rdblksz
* to be real physical blocksize so we must loop until we get
* the old rdblksz (now in blksz). If ar_rev() fouls up the
* determination of the physical block size, we will fail.
if ((res
= ar_read(bufpt
, rdblksz
)) <= 0)
if (ar_rev((off_t
)(bufpt
- buf
)) < 0)
* At this point we are ready to write. If the device requires special
* handling to write at a point were previously recorded data resides,
* that is handled in ar_set_wr(). From now on we operate under normal
* ARCHIVE mode (write) conditions
warn(1, "Unable to rewrite archive trailer, cannot append.");
* A read error occurred on this archive volume. Resync the buffer and
* try to reset the device (if possible) so we can continue to read. Keep
* trying to do this until we get a valid read, or we reach the limit on
* consecutive read faults (at which point we give up). The user can
* adjust the read error limit through a command line option.
* 0 on success, and -1 on failure
* if the user says bail out on first fault, we are out of here...
warn(1, "Unable to append when there are archive read errors.");
* poke at device and try to get past media error
if ((res
= ar_read(buf
, blksz
)) > 0) {
* All right! got some data, fill that buffer
* Oh well, yet another failed read...
* if error limit reached, ditch. o.w. poke device to move past
* bad media and try again. if media is badly damaged, we ask
* the poor (and upset user at this point) for the next archive
* volume. remember the goal on reads is to get the most we
* can extract out of the archive.
if ((maxflt
> 0) && (++errcnt
> maxflt
))
warn(0,"Archive read error limit (%d) reached",maxflt
);
else if (ar_rdsync() == 0)
* push the data used during the archive id phase back into the I/O
* buffer. This is required as we cannot be sure that the header does NOT
* overlap a block boundry (as in the case we are trying to recover a
* flawed archived). This was not designed to be used for any other
* purpose. (What software engineering, HA!)
* WARNING: do not even THINK of pback greater than BLKMULT, unless the
* pback space is increased.
* skip foward in the archive during a archive read. Used to get quickly
* past file data and padding for files the user did NOT select.
* 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected.
* consume what data we have in the buffer. If we have to move foward
* whole records, we call the low level skip function to see if we can
* move within the archive without doing the expensive reads on data we
res
= MIN((bufend
- bufpt
), skcnt
);
* if skcnt is now 0, then no additional i/o is needed
* We have to read more, calculate complete and partial record reads
* based on rdblksz. we skip over "cnt" complete records
cnt
= (skcnt
/rdblksz
) * rdblksz
;
* if the skip fails, we will have to resync. ar_fow will tell us
* how much it can skip over. We will have to read the rest.
if (ar_fow(cnt
, &skipped
) < 0)
* what is left we have to read (which may be the whole thing if
* ar_fow() told us the device can only read to skip records);
* if the read fails, we will have to resync
if ((cnt
<= 0) && ((cnt
= buf_fill()) < 0))
* flush out any data (and pad if required) the last block. We always pad
* with zero (even though we do not have to). Padding with 0 makes it a
* lot easier to recover if the archive is damaged. zero paddding SHOULD
bzero(bufpt
, bufend
- bufpt
);
* fill the write buffer from data passed to it in a buffer (usually used
* by format specific write routines to pass a file header). On failure we
* punt. We do not allow the user to continue to write flawed archives.
* We assume these headers are not very large (the memory copy we use is
* 0 if buffer was filled ok, -1 o.w. (buffer flush failure)
wr_rdbuf(register char *out
, register int outcnt
)
* while there is data to copy copy into the write buffer. when the
* write buffer fills, flush it to the archive and continue
if ((cnt
<= 0) && ((cnt
= buf_flush(blksz
)) < 0))
* only move what we have space for
* copy from the read buffer into a supplied buffer a specified number of
* bytes. If the read buffer is empty fill it and continue to copy.
* usually used to obtain a file header for processing by a format
* number of bytes copied to the buffer, 0 indicates EOF on archive volume,
rd_wrbuf(register char *in
, register int cpcnt
)
register int incnt
= cpcnt
;
* loop until we fill the buffer with the requested number of bytes
if ((cnt
<= 0) && ((cnt
= buf_fill()) <= 0)) {
* read error, return what we got (or the error if
* no data was copied). The caller must know that an
* error occured and has the best knowledge what to
if ((res
= cpcnt
- incnt
) > 0)
* calculate how much data to copy based on whats left and
* skip foward during a write. In other words add padding to the file.
* we add zero filled padding as it makes flawed archives much easier to
* recover from. the caller tells us how many bytes of padding to add
* This routine was not designed to add HUGE amount of padding, just small
* amounts (a few 512 byte blocks at most)
* 0 if ok, -1 if there was a buf_flush failure
* loop while there is more padding to add
if ((cnt
<= 0) && ((cnt
= buf_flush(blksz
)) < 0))
* fill write buffer with the contents of a file. We are passed an open
* file descriptor to the file an the archive structure that describes the
* file we are storing. The variable "left" is modified to contain the
* number of bytes of the file we were NOT able to write to the archive.
* it is important that we always write EXACTLY the number of bytes that
* the format specific write routine told us to. The file can also get
* bigger, so reading to the end of file would create an improper archive,
* we just detect this case and warn the user. We never create a bad
* archive if we can avoid it. Of course trying to archive files that are
* active is asking for trouble. It we fail, we pass back how much we
* could NOT copy and let the caller deal with it.
* 0 ok, -1 if archive write failure. a short read of the file returns a
* 0, but "left" is set to be greater than zero.
wr_rdfile(ARCHD
*arcn
, int ifd
, off_t
*left
)
wr_rdfile(arcn
, ifd
, left
)
register off_t size
= arcn
->sb
.st_size
;
* while there are more bytes to write
if ((cnt
<= 0) && ((cnt
= buf_flush(blksz
)) < 0)) {
if ((res
= read(ifd
, bufpt
, cnt
)) <= 0)
* better check the file did not change during this operation
* or the file read failed.
syswarn(1, errno
, "Read fault on %s", arcn
->org_name
);
warn(1, "File changed size during read %s", arcn
->org_name
);
else if (fstat(ifd
, &sb
) < 0)
syswarn(1, errno
, "Failed stat on %s", arcn
->org_name
);
else if (arcn
->sb
.st_mtime
!= sb
.st_mtime
)
warn(1, "File %s was modified during copy to archive",
* extract the contents of a file from the archive. If we are unable to
* extract the entire file (due to failure to write the file) we return
* the numbers of bytes we did NOT process. This way the caller knows how
* many bytes to skip past to find the next archive header. If the failure
* was due to an archive read, we will catch that when we try to skip. If
* the format supplies a file data crc value, we calculate the actual crc
* so that it can be compared to the value stored in the header
* We call a special function to write the file. This function attempts to
* restore file holes (blocks of zeros) into the file. When files are
* sparse this saves space, and is a LOT faster. For non sparse files
* the performance hit is small. As of this writing, no archive supports
* information on where the file holes are.
* 0 ok, -1 if archive read failure. if we cannot write the entire file,
* we return a 0 but "left" is set to be the amount unwritten
rd_wrfile(ARCHD
*arcn
, int ofd
, off_t
*left
)
rd_wrfile(arcn
, ofd
, left
)
register off_t size
= arcn
->sb
.st_size
;
register char *fnm
= arcn
->name
;
* pass the blocksize of the file being written to the write routine,
* if an odd size, use the default MINFBSZ
if (fstat(ofd
, &sb
) == 0) {
if ((sb
.st_blksize
> 0) && (sb
.st_blksize
<= MAXFBSZ
))
syswarn(0,errno
,"Unable to obtain block size for file %s",fnm
);
* Copy the archive to the file the number of bytes specified. We have
* to assume that we want to recover file holes as none of the archive
* formats can record the location of file holes.
* if we get a read error, we do not want to skip, as we may
* miss a header, so we do not set left, but if we get a write
* error, we do want to skip over the unprocessed data.
if ((cnt
<= 0) && ((cnt
= buf_fill()) <= 0))
if ((res
= file_write(ofd
,bufpt
,cnt
,&rem
,&isem
,sz
,fnm
)) <= 0) {
* update the actual crc value
* if the last block has a file hole (all zero), we must make sure this
* gets updated in the file. We force the last block of zeros to be
* written. just closing with the file offset moved foward may not put
* a hole at the end of the file.
if (isem
&& (arcn
->sb
.st_size
> 0L))
file_flush(ofd
, fnm
, isem
);
* if we failed from archive read, we do not want to skip
if ((size
> 0L) && (*left
== 0L))
* some formats record a crc on file data. If so, then we compare the
* calculated crc to the crc stored in the archive
if (docrc
&& (size
== 0L) && (arcn
->crc
!= crc
))
warn(1,"Actual crc does not match expected crc %s",arcn
->name
);
* copy the contents of one file to another. used during -rw phase of pax
* just as in rd_wrfile() we use a special write function to write the
* destination file so we can properly copy files with holes.
cp_file(ARCHD
*arcn
, int fd1
, int fd2
)
register off_t cpcnt
= 0L;
register char *fnm
= arcn
->name
;
register int no_hole
= 0;
* check for holes in the source file. If none, we will use regular
* write instead of file write.
if (((off_t
)(arcn
->sb
.st_blocks
* BLKMULT
)) >= arcn
->sb
.st_size
)
* pass the blocksize of the file being written to the write routine,
* if an odd size, use the default MINFBSZ
if (fstat(fd2
, &sb
) == 0) {
if ((sb
.st_blksize
> 0) && (sb
.st_blksize
<= MAXFBSZ
))
syswarn(0,errno
,"Unable to obtain block size for file %s",fnm
);
* read the source file and copy to destination file until EOF
if ((cnt
= read(fd1
, buf
, blksz
)) <= 0)
res
= write(fd2
, buf
, cnt
);
res
= file_write(fd2
, buf
, cnt
, &rem
, &isem
, sz
, fnm
);
* check to make sure the copy is valid.
syswarn(1, errno
, "Failed write during copy of %s to %s",
arcn
->org_name
, arcn
->name
);
else if (cpcnt
!= arcn
->sb
.st_size
)
warn(1, "File %s changed size during copy to %s",
arcn
->org_name
, arcn
->name
);
else if (fstat(fd1
, &sb
) < 0)
syswarn(1, errno
, "Failed stat of %s", arcn
->org_name
);
else if (arcn
->sb
.st_mtime
!= sb
.st_mtime
)
warn(1, "File %s was modified during copy to %s",
arcn
->org_name
, arcn
->name
);
* if the last block has a file hole (all zero), we must make sure this
* gets updated in the file. We force the last block of zeros to be
* written. just closing with the file offset moved foward may not put
* a hole at the end of the file.
if (!no_hole
&& isem
&& (arcn
->sb
.st_size
> 0L))
file_flush(fd2
, fnm
, isem
);
* fill the read buffer with the next record (or what we can get) from
* Number of bytes of data in the read buffer, -1 for read error, and
* 0 when finished (user specified termination in ar_next()).
* try to fill the buffer. on error the next archive volume is
* opened and we try again.
if ((cnt
= ar_read(buf
, blksz
)) > 0) {
* errors require resync, EOF goes to next archive
* force the write buffer to the archive. We are passed the number of
* bytes in the buffer at the point of the flush. When we change archives
* the record size might change. (either larger or smaller).
* 0 if all is ok, -1 when a write error occurs.
buf_flush(register int bufcnt
)
* if we have reached the user specified byte count for each archive
* volume, prompt for the next volume. (The non-standrad -R flag).
* NOTE: If the wrlimit is smaller than wrcnt, we will always write
* at least one record. We always round limit UP to next blocksize.
if ((wrlimit
> 0) && (wrcnt
> wrlimit
)) {
warn(0, "User specified archive volume byte limit reached.");
* The new archive volume might have changed the size of the
* write blocksize. if so we figure out if we need to write
* (one or more times), or if there is now free space left in
* the buffer (it is no longer full). bufcnt has the number of
* bytes in the buffer, (the blocksize, at the point we were
* CALLED). Push has the amount of "extra" data in the buffer
* if the block size has shrunk from a volume change.
* We have enough data to write at least one archive block
* write a block and check if it all went out ok
cnt
= ar_write(buf
, blksz
);
/* we have extra data to push to the front.
* check for more than 1 block of push, and if
* so we loop back to write again
bcopy(bufend
, buf
, push
);
* Oh drat we got a partial write!
* if format doesnt care about alignment let it go,
* we warned the user in ar_write().... but this means
* the last record on this volume violates pax spec....
if (!frmt
->blkalgn
|| ((cnt
% frmt
->blkalgn
) == 0))
* All done, go to next archive
* The new archive volume might also have changed the block
* size. if so, figure out if we have too much or too little
* data for using the new block size
* write failed, stop pax. we must not create a bad archive!