From fffc52fe7a3393e38603b6d028c721e38a1d1cc3 Mon Sep 17 00:00:00 2001 From: jhb Date: Wed, 21 Jan 2004 20:12:23 +0000 Subject: 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. --- lib/libstand/bzipfs.c | 16 +++++++++++----- lib/libstand/dosfs.c | 5 ++++- lib/libstand/ext2fs.c | 1 + lib/libstand/fstat.c | 2 ++ lib/libstand/gzipfs.c | 16 +++++++++++----- lib/libstand/nfs.c | 5 +++-- lib/libstand/nullfs.c | 15 +++++---------- lib/libstand/read.c | 4 ++-- lib/libstand/splitfs.c | 11 +++++++++-- lib/libstand/ufs.c | 1 + 10 files changed, 49 insertions(+), 27 deletions(-) (limited to 'lib/libstand') diff --git a/lib/libstand/bzipfs.c b/lib/libstand/bzipfs.c index c0a8d9b..7f89b33 100644 --- a/lib/libstand/bzipfs.c +++ b/lib/libstand/bzipfs.c @@ -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) */ diff --git a/lib/libstand/dosfs.c b/lib/libstand/dosfs.c index 792e0be..41547c8 100644 --- a/lib/libstand/dosfs.c +++ b/lib/libstand/dosfs.c @@ -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); diff --git a/lib/libstand/ext2fs.c b/lib/libstand/ext2fs.c index c233bde..153f163 100644 --- a/lib/libstand/ext2fs.c +++ b/lib/libstand/ext2fs.c @@ -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); diff --git a/lib/libstand/fstat.c b/lib/libstand/fstat.c index 3a5f39f..7d95360 100644 --- a/lib/libstand/fstat.c +++ b/lib/libstand/fstat.c @@ -59,5 +59,7 @@ fstat(fd, sb) } errno = (f->f_ops->fo_stat)(f, sb); + if (errno) + return (-1); return (0); } diff --git a/lib/libstand/gzipfs.c b/lib/libstand/gzipfs.c index 9cc9ad9..0d7c59b 100644 --- a/lib/libstand/gzipfs.c +++ b/lib/libstand/gzipfs.c @@ -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) */ diff --git a/lib/libstand/nfs.c b/lib/libstand/nfs.c index 82fa08b..1c439a8 100644 --- a/lib/libstand/nfs.c +++ b/lib/libstand/nfs.c @@ -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; } diff --git a/lib/libstand/nullfs.c b/lib/libstand/nullfs.c index ae12cb0..fea4c7a 100644 --- a/lib/libstand/nullfs.c +++ b/lib/libstand/nullfs.c @@ -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; } diff --git a/lib/libstand/read.c b/lib/libstand/read.c index 36f0f22..07401c1 100644 --- a/lib/libstand/read.c +++ b/lib/libstand/read.c @@ -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 */ diff --git a/lib/libstand/splitfs.c b/lib/libstand/splitfs.c index 8792464..35593ad 100644 --- a/lib/libstand/splitfs.c +++ b/lib/libstand/splitfs.c @@ -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; } diff --git a/lib/libstand/ufs.c b/lib/libstand/ufs.c index 8148fe9..6a3d758 100644 --- a/lib/libstand/ufs.c +++ b/lib/libstand/ufs.c @@ -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); -- cgit v1.1