8000 minix/commands/pax/ar_io.c at R3.1.6 · flcs/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1298 lines (1198 loc) · 32 KB

ar_io.c

File metadata and controls

1298 lines (1198 loc) · 32 KB
if (!invld_rec)
return(0);
paxwarn(1,"Cannot append, device record size %d does not support %s spec",
rdblksz, argv0);
return(-1);
}
/*
* ar_read()
* read up to a specified number of bytes from the archive into the
* supplied buffer. When dealing with tapes we may not always be able to
* read what we want.
* Return:
* Number of bytes in buffer. 0 for end of file, -1 for a read error.
*/
int
ar_read(char *buf, int cnt)
{
int res = 0;
/*
* if last i/o was in error, no more reads until reset or new volume
*/
if (lstrval <= 0)
return(lstrval);
/*
* how we read must be based on device type
*/
switch (artyp) {
case ISTAPE:
if ((res = read(arfd, buf, cnt)) > 0) {
/*
* CAUTION: tape systems may not always return the same
* sized records so we leave blksz == MAXBLK. The
* physical record size that a tape drive supports is
* very hard to determine in a uniform and portable
* manner.
*/
io_ok = 1;
if (res != rdblksz) {
/*
* Record size changed. If this is happens on
* any record after the first, we probably have
* a tape drive which has a fixed record size
* we are getting multiple records in a single
* read). Watch out for record blocking that
* violates pax spec (must be a multiple of
* BLKMULT).
*/
rdblksz = res;
if (rdblksz % BLKMULT)
invld_rec = 1;
}
return(res);
}
break;
case ISREG:
case ISBLK:
case ISCHR:
case ISPIPE:
default:
/*
* Files are so easy to deal with. These other things cannot
* be trusted at all. So when we are dealing with character
* devices and pipes we just take what they have ready for us
* and return. Trying to do anything else with them runs the
* risk of failure.
*/
if ((res = read(arfd, buf, cnt)) > 0) {
io_ok = 1;
return(res);
}
break;
}
/*
* We are in trouble at this point, something is broken...
*/
lstrval = res;
if (res < 0)
syswarn(1, errno, "Failed read on archive volume %d", arvol);
else
paxwarn(0, "End of archive volume %d reached", arvol);
return(res);
}
/*
* ar_write()
* Write a specified number of bytes in supplied buffer to the archive
* device so it appears as a single "block". Deals with errors and tries
* to recover when faced with short writes.
* Return:
* Number of bytes written. 0 indicates end of volume reached and with no
* flaws (as best that can be detected). A -1 indicates an unrecoverable
* error in the archive occured.
*/
int
ar_write(char *buf, int bsz)
{
int res;
off_t cpos;
/*
* do not allow pax to create a "bad" archive. Once a write fails on
* an archive volume prevent further writes to it.
*/
if (lstrval <= 0)
return(lstrval);
if ((res = write(arfd, buf, bsz)) == bsz) {
wr_trail = 1;
io_ok = 1;
return(bsz);
}
/*
* write broke, see what we can do with it. We try to send any partial
* writes that may violate pax spec to the next archive volume.
*/
if (res < 0)
lstrval = res;
else
lstrval = 0;
switch (artyp) {
case ISREG:
if ((res > 0) && (res % BLKMULT)) {
/*
* try to fix up partial writes which are not BLKMULT
* in size by forcing the runt record to next archive
* volume
*/
if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
break;
cpos -= (off_t)res;
if (ftruncate(arfd, cpos) < 0)
break;
res = lstrval = 0;
break;
}
if (res >= 0)
break;
/*
* if file is out of space, handle it like a return of 0
*/
if ((errno == ENOSPC) || (errno == EFBIG)
#ifdef EDQUOT
|| (errno == EDQUOT)
#endif
)
res = lstrval = 0;
break;
case ISTAPE:
case ISCHR:
case ISBLK:
if (res >= 0)
break;
if (errno == EACCES) {
paxwarn(0, "Write failed, archive is write protected.");
res = lstrval = 0;
return(0);
}
/*
* see if we reached the end of media, if so force a change to
* the next volume
*/
if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO))
res = lstrval = 0;
break;
case ISPIPE:
default:
/*
* we cannot fix errors to these devices
*/
break;
}
/*
* Better tell the user the bad news...
* if this is a block aligned archive format, we may have a bad archive
* if the format wants the header to start at a BLKMULT boundary. While
* we can deal with the mis-aligned data, it violates spec and other
* archive readers will likely fail. if the format is not block
* aligned, the user may be lucky (and the archive is ok).
*/
if (res >= 0) {
if (res > 0)
wr_trail = 1;
io_ok = 1;
}
/*
* If we were trying to rewrite the trailer and it didn't work, we
* must quit right away.
*/
if (!wr_trail && (res <= 0)) {
paxwarn(1,"Unable to append, trailer re-write failed. Quitting.");
return(res);
}
if (res == 0)
paxwarn(0, "End of archive volume %d reached", arvol);
else if (res < 0)
syswarn(1, errno, "Failed write to archive volume: %d", arvol);
else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0))
paxwarn(0,"WARNING: partial archive write. Archive MAY BE FLAWED");
else
paxwarn(1,"WARNING: partial archive write. Archive IS FLAWED");
return(res);
}
/*
* ar_rdsync()
* Try to move past a bad spot on a flawed archive as needed to continue
* I/O. Clears error flags to allow I/O to continue.
* Return:
* 0 when ok to try i/o again, -1 otherwise.
*/
int
ar_rdsync(void)
{
long fsbz;
off_t cpos;
off_t mpos;
struct mtop mb;
/*
* Fail resync attempts at user request (done) or this is going to be
* an update/append to an existing archive. If last i/o hit media end,
* we need to go to the next volume not try a resync.
*/
if ((done > 0) || (lstrval == 0))
return(-1);
if ((act == APPND) || (act == ARCHIVE)) {
paxwarn(1, "Cannot allow updates to an archive with flaws.");
return(-1);
}
if (io_ok)
did_io = 1;
switch(artyp) {
case ISTAPE:
/*
* if the last i/o was a successful data transfer, we assume
* the fault is just a bad record on the tape that we are now
* past. If we did not get any data since the last resync try
* to move the tape forward one PHYSICAL record past any
* damaged tape section. Some tape drives are stubborn and need
* to be pushed.
*/
if (io_ok) {
io_ok = 0;
lstrval = 1;
break;
}
mb.mt_op = MTFSR;
mb.mt_count = 1;
if (ioctl(arfd, MTIOCTOP, &mb) < 0)
break;
lstrval = 1;
break;
case ISREG:
case ISCHR:
case ISBLK:
/*
* try to step over the bad part of the device.
*/
io_ok = 0;
#if 0
/* no blksize on minix */
if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG))
#endif
fsbz = BLKMULT;
if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
break;
mpos = fsbz - (cpos % (off_t)fsbz);
if (lseek(arfd, mpos, SEEK_CUR) < 0)
break;
lstrval = 1;
break;
case ISPIPE:
default:
/*
* cannot recover on these archive device types
*/
io_ok = 0;
break;
}
if (lstrval <= 0) {
paxwarn(1, "Unable to recover from an archive read failure.");
return(-1);
}
paxwarn(0, "Attempting to recover from an archive read failure.");
return(0);
}
/*
* ar_fow()
* Move the I/O position within the archive foward the specified number of
* bytes as supported by the device. If we cannot move the requested
* number of bytes, return the actual number of bytes moved in skipped.
* Return:
* 0 if moved the requested distance, -1 on complete failure, 1 on
* partial move (the amount moved is in skipped)
*/
int
ar_fow(off_t sksz, off_t *skipped)
{
off_t cpos;
off_t mpos;
*skipped = 0;
if (sksz <= 0)
return(0);
/*
* we cannot move foward at EOF or error
*/
if (lstrval <= 0)
return(lstrval);
/*
* Safer to read forward on devices where it is hard to find the end of
* the media without reading to it. With tapes we cannot be sure of the
* number of physical blocks to skip (we do not know physical block
* size at this point), so we must only read foward on tapes!
*/
if (artyp != ISREG)
return(0);
/*
* figure out where we are in the archive
*/
if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) {
/*
* we can be asked to move farther than there are bytes in this
* volume, if so, just go to file end and let normal buf_fill()
* deal with the end of file (it will go to next volume by
* itself)
*/
if ((mpos = cpos + sksz) > arsb.st_size) {
*skipped = arsb.st_size - cpos;
mpos = arsb.st_size;
} else
*skipped = sksz;
if (lseek(arfd, mpos, SEEK_SET) >= 0)
return(0);
}
syswarn(1, errno, "Forward positioning operation on archive failed");
lstrval = -1;
return(-1);
}
/*
* ar_rev()
* move the i/o position within the archive backwards the specified byte
* count as supported by the device. With tapes drives we RESET rdblksz to
* the PHYSICAL blocksize.
* NOTE: We should only be called to move backwards so we can rewrite the
* last records (the trailer) of an archive (APPEND).
* Return:
* 0 if moved the requested distance, -1 on complete failure
*/
int
ar_rev(off_t sksz)
{
off_t cpos;
struct mtop mb;
int phyblk;
/*
* make sure we do not have try to reverse on a flawed archive
*/
if (lstrval < 0)
return(lstrval);
switch(artyp) {
case ISPIPE:
if (sksz <= 0)
break;
/*
* cannot go backwards on these critters
*/
paxwarn(1, "Reverse positioning on pipes is not supported.");
lstrval = -1;
return(-1);
case ISREG:
case ISBLK:
case ISCHR:
default:
if (sksz <= 0)
break;
/*
* For things other than files, backwards movement has a very
* high probability of failure as we really do not know the
* true attributes of the device we are talking to (the device
* may not even have the ability to lseek() in any direction).
* First we figure out where we are in the archive.
*/
if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) {
syswarn(1, errno,
"Unable to obtain current archive byte offset");
lstrval = -1;
return(-1);
}
/*
* we may try to go backwards past the start when the archive
* is only a single record. If this hapens and we are on a
* multi volume archive, we need to go to the end of the
* previous volume and continue our movement backwards from
* there.
*/
if ((cpos -= sksz) < (off_t)0L) {
if (arvol > 1) {
/*
* this should never happen
*/
paxwarn(1,"Reverse position on previous volume.");
lstrval = -1;
return(-1);
}
cpos = (off_t)0L;
}
if (lseek(arfd, cpos, SEEK_SET) < 0) {
syswarn(1, errno, "Unable to seek archive backwards");
lstrval = -1;
return(-1);
}
break;
case ISTAPE:
/*
* Calculate and move the proper number of PHYSICAL tape
* blocks. If the sksz is not an even multiple of the physical
* tape size, we cannot do the move (this should never happen).
* (We also cannot handler trailers spread over two vols).
* get_phys() also makes sure we are in front of the filemark.
*/
if ((phyblk = get_phys()) <= 0) {
lstrval = -1;
return(-1);
}
/*
* make sure future tape reads only go by physical tape block
* size (set rdblksz to the real size).
*/
rdblksz = phyblk;
/*
* if no movement is required, just return (we must be after
* get_phys() so the physical blocksize is properly set)
*/
if (sksz <= 0)
break;
/*
* ok we have to move. Make sure the tape drive can do it.
*/
if (sksz % phyblk) {
paxwarn(1,
"Tape drive unable to backspace requested amount");
lstrval = -1;
return(-1);
}
/*
* move backwards the requested number of bytes
*/
mb.mt_op = MTBSR;
mb.mt_count = sksz/phyblk;
if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
syswarn(1,errno, "Unable to backspace tape %d blocks.",
mb.mt_count);
lstrval = -1;
return(-1);
}
break;
}
lstrval = 1;
return(0);
}
/*
* get_phys()
* Determine the physical block size on a tape drive. We need the physical
* block size so we know how many bytes we skip over when we move with
* mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
* return.
* This is one really SLOW routine...
* Return:
* physical block size if ok (ok > 0), -1 otherwise
*/
0