1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-01 08:27:59 +00:00

Clean up error handling in libstand filesystem code to be more consistent:

- bzipfs and gzipfs now properly return errno values directly from their
  read routines rather than returning -1.
- missing errno values on error returns for the seek routines on almost
  all filesystems were added.
- fstat() now returns -1 if an error occurs rather than ignoring it.
- nfs's readdir() routine now reports valid errno values if an error or
  EOF occurs rather than EPERM  (It was just returning 0 for success and
  1 for failure).
- nullfs used the wrong semantics for every function besides close() and
  seek().  Getting it right for close() appears to be an accident at that.
- read() for buffered files no longer returns 0 (EOF) if an error occurs,
  but returns -1 instead.
This commit is contained in:
John Baldwin 2004-01-21 20:12:23 +00:00
parent 58d120f6d3
commit fb6b710c39
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=124811
10 changed files with 49 additions and 27 deletions

View File

@ -223,10 +223,12 @@ bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
while (bzf->bzf_bzstream.avail_out) {
if ((bzf->bzf_bzstream.avail_in == 0) && (bzf_fill(bzf) == -1)) {
printf("bzf_read: fill error\n");
return(-1);
return(EIO);
}
if (bzf->bzf_bzstream.avail_in == 0) { /* oops, unexpected EOF */
printf("bzf_read: unexpected EOF\n");
if (bzf->bzf_bzstream.avail_out == size)
return (EIO);
break;
}
@ -236,8 +238,7 @@ bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
}
if (error != BZ_OK) { /* argh, decompression error */
printf("bzf_read: BZ2_bzDecompress returned %d\n", error);
errno = EIO;
return(-1);
return(EIO);
}
}
if (resid != NULL)
@ -259,8 +260,11 @@ bzf_seek(struct open_file *f, off_t offset, int where)
case SEEK_CUR:
target = offset + bzf->bzf_bzstream.total_out_lo32;
break;
default:
case SEEK_END:
target = -1;
default:
errno = EINVAL;
return (-1);
}
/* Can we get there from here? */
@ -271,7 +275,9 @@ bzf_seek(struct open_file *f, off_t offset, int where)
/* skip forwards if required */
while (target > bzf->bzf_bzstream.total_out_lo32) {
if (bzf_read(f, discard, min(sizeof(discard), target - bzf->bzf_bzstream.total_out_lo32), NULL) == -1)
errno = bzf_read(f, discard, min(sizeof(discard),
target - bzf->bzf_bzstream.total_out_lo32), NULL);
if (errno)
return(-1);
}
/* This is where we are (be honest if we overshot) */

View File

@ -308,11 +308,14 @@ dos_seek(struct open_file *fd, off_t offset, int whence)
off = size;
break;
default:
errno = EINVAL;
return(-1);
}
off += offset;
if (off < 0 || off > size)
if (off < 0 || off > size) {
errno = EINVAL;
return(-1);
}
f->offset = (u_int)off;
f->c = 0;
return(off);

View File

@ -858,6 +858,7 @@ ext2fs_seek(struct open_file *f, off_t offset, int where)
fp->f_seekp = fp->f_di.di_size - offset;
break;
default:
errno = EINVAL;
return (-1);
}
return (fp->f_seekp);

View File

@ -59,5 +59,7 @@ fstat(fd, sb)
}
errno = (f->f_ops->fo_stat)(f, sb);
if (errno)
return (-1);
return (0);
}

View File

@ -255,10 +255,12 @@ zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
while (zf->zf_zstream.avail_out) {
if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
printf("zf_read: fill error\n");
return(-1);
return(EIO);
}
if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */
printf("zf_read: unexpected EOF\n");
if (zf->zf_zstream.avail_out == size)
return (EIO);
break;
}
@ -268,8 +270,7 @@ zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
}
if (error != Z_OK) { /* argh, decompression error */
printf("inflate: %s\n", zf->zf_zstream.msg);
errno = EIO;
return(-1);
return(EIO);
}
}
if (resid != NULL)
@ -305,8 +306,11 @@ zf_seek(struct open_file *f, off_t offset, int where)
case SEEK_CUR:
target = offset + zf->zf_zstream.total_out;
break;
default:
case SEEK_END:
target = -1;
default:
errno = EINVAL;
return (-1);
}
/* rewind if required */
@ -315,7 +319,9 @@ zf_seek(struct open_file *f, off_t offset, int where)
/* skip forwards if required */
while (target > zf->zf_zstream.total_out) {
if (zf_read(f, discard, min(sizeof(discard), target - zf->zf_zstream.total_out), NULL) == -1)
errno = zf_read(f, discard, min(sizeof(discard),
target - zf->zf_zstream.total_out), NULL);
if (errno)
return(-1);
}
/* This is where we are (be honest if we overshot) */

View File

@ -674,6 +674,7 @@ nfs_seek(f, offset, where)
d->off = size - offset;
break;
default:
errno = EINVAL;
return (-1);
}
@ -741,7 +742,7 @@ nfs_readdir(struct open_file *f, struct dirent *d)
buf = rdata.d;
roff = (struct nfs_readdir_off *)buf;
if (ntohl(roff->cookie) != 0)
return 1;
return EIO;
}
roff = (struct nfs_readdir_off *)buf;
@ -749,7 +750,7 @@ nfs_readdir(struct open_file *f, struct dirent *d)
eof = ntohl((roff+1)->cookie);
if (eof) {
cookie = 0;
return 1;
return ENOENT;
}
goto refill;
}

View File

@ -74,8 +74,7 @@ __FBSDID("$FreeBSD$");
*/
int null_open (const char *path, struct open_file *f)
{
errno = EIO;
return -1;
return EINVAL;
}
int null_close(struct open_file *f)
@ -85,14 +84,12 @@ int null_close(struct open_file *f)
int null_read (struct open_file *f, void *buf, size_t size, size_t *resid)
{
errno = EIO;
return -1;
return EIO;
}
int null_write (struct open_file *f, void *buf, size_t size, size_t *resid)
{
errno = EIO;
return -1;
return EIO;
}
off_t null_seek (struct open_file *f, off_t offset, int where)
@ -103,12 +100,10 @@ off_t null_seek (struct open_file *f, off_t offset, int where)
int null_stat (struct open_file *f, struct stat *sb)
{
errno = EIO;
return -1;
return EIO;
}
int null_readdir(struct open_file *f, struct dirent *d)
{
errno = EIO;
return -1;
return EIO;
}

View File

@ -115,13 +115,13 @@ read(int fd, void *dest, size_t bcount)
if (resid >= SOPEN_RASIZE) {
/* bypass the rest of the request and leave the buffer empty */
if ((errno = (f->f_ops->fo_read)(f, dest, resid, &cresid)))
return(bcount - resid);
return (-1);
return(bcount - cresid);
}
/* fetch more data */
if ((errno = (f->f_ops->fo_read)(f, f->f_rabuf, SOPEN_RASIZE, &cresid)))
return(bcount - resid); /* behave like fread() */
return (-1);
f->f_raoffset = 0;
f->f_ralen = SOPEN_RASIZE - cresid;
/* no more data, return what we had */

View File

@ -250,6 +250,9 @@ splitfs_seek(struct open_file *f, off_t offset, int where)
case SEEK_END:
panic("splitfs_seek: SEEK_END not supported");
break;
default:
errno = EINVAL;
return (-1);
}
if (seek_by > 0) {
@ -261,8 +264,10 @@ splitfs_seek(struct open_file *f, off_t offset, int where)
void *tmp;
tmp = malloc(SEEK_BUF);
if (tmp == NULL)
if (tmp == NULL) {
errno = ENOMEM;
return (-1);
}
nread = 0;
for (; seek_by > 0; seek_by -= nread) {
@ -283,8 +288,10 @@ splitfs_seek(struct open_file *f, off_t offset, int where)
if (sf->file_pos + seek_by < 0)
panic("splitfs_seek: can't seek past the beginning of the slice");
new_pos = lseek(sf->curfd, seek_by, SEEK_CUR);
if (new_pos < 0)
if (new_pos < 0) {
errno = EINVAL;
return (-1);
}
sf->tot_pos += new_pos - sf->file_pos;
sf->file_pos = new_pos;
}

View File

@ -818,6 +818,7 @@ ufs_seek(f, offset, where)
fp->f_seekp = DIP(fp, di_size) - offset;
break;
default:
errno = EINVAL;
return (-1);
}
return (fp->f_seekp);