vfs: add ECLOSED and tidy errors

This commit is contained in:
Nick Craig-Wood 2017-11-03 11:35:36 +00:00
parent 22ee839d05
commit 2083ac6e2a
10 changed files with 47 additions and 17 deletions

View File

@ -515,10 +515,14 @@ func translateError(err error) (errc int) {
return 0
case vfs.ENOENT:
return -fuse.ENOENT
case vfs.ENOTEMPTY:
return -fuse.ENOTEMPTY
case vfs.EEXIST:
return -fuse.EEXIST
case vfs.EPERM:
return -fuse.EPERM
case vfs.ECLOSED:
return -fuse.EBADF
case vfs.ENOTEMPTY:
return -fuse.ENOTEMPTY
case vfs.ESPIPE:
return -fuse.ESPIPE
case vfs.EBADF:
@ -527,8 +531,6 @@ func translateError(err error) (errc int) {
return -fuse.EROFS
case vfs.ENOSYS:
return -fuse.ENOSYS
case vfs.EPERM:
return -fuse.EPERM
}
fs.Errorf(nil, "IO error: %v", err)
return -fuse.EIO

View File

@ -74,10 +74,14 @@ func translateError(err error) error {
return nil
case vfs.ENOENT:
return fuse.ENOENT
case vfs.ENOTEMPTY:
return fuse.Errno(syscall.ENOTEMPTY)
case vfs.EEXIST:
return fuse.EEXIST
case vfs.EPERM:
return fuse.EPERM
case vfs.ECLOSED:
return fuse.Errno(syscall.EBADF)
case vfs.ENOTEMPTY:
return fuse.Errno(syscall.ENOTEMPTY)
case vfs.ESPIPE:
return fuse.Errno(syscall.ESPIPE)
case vfs.EBADF:
@ -86,8 +90,6 @@ func translateError(err error) error {
return fuse.Errno(syscall.EROFS)
case vfs.ENOSYS:
return fuse.ENOSYS
case vfs.EPERM:
return fuse.EPERM
}
return err
}

View File

@ -7,7 +7,7 @@ import (
"os"
)
// Error describes low level errors in a cross platform way
// Error describes low level errors in a cross platform way.
type Error byte
// NB if changing errors translateError in cmd/mount/fs.go, cmd/cmount/fs.go
@ -27,6 +27,7 @@ var (
ENOENT = os.ErrNotExist
EEXIST = os.ErrExist
EPERM = os.ErrPermission
// ECLOSED see errors_{old,new}.go
)
var errorNames = []string{

10
vfs/errors_new.go Normal file
View File

@ -0,0 +1,10 @@
// Errors for go1.8+
//+build go1.8
package vfs
import "os"
// ECLOSED is returned when a handle is closed twice
var ECLOSED = os.ErrClosed

10
vfs/errors_old.go Normal file
View File

@ -0,0 +1,10 @@
// Errors for pre go1.8
//+build !go1.8
package vfs
import "errors"
// ECLOSED is returned when a handle is closed twice
var ECLOSED = errors.New("file already closed")

View File

@ -175,7 +175,7 @@ func (fh *ReadFileHandle) ReadAt(p []byte, off int64) (n int, err error) {
// fs.Debugf(fh.o, "ReadFileHandle.Read size %d offset %d", reqSize, off)
if fh.closed {
fs.Errorf(fh.o, "ReadFileHandle.Read error: %v", EBADF)
return 0, EBADF
return 0, ECLOSED
}
doSeek := off != fh.offset
if doSeek && fh.noSeek {
@ -305,7 +305,7 @@ func (fh *ReadFileHandle) Read(p []byte) (n int, err error) {
// Must be called with fh.mu held
func (fh *ReadFileHandle) close() error {
if fh.closed {
return EBADF
return ECLOSED
}
fh.closed = true

View File

@ -76,7 +76,7 @@ func TestReadFileHandleMethods(t *testing.T) {
assert.True(t, fh.closed)
// Close again
assert.Equal(t, EBADF, fh.Close())
assert.Equal(t, ECLOSED, fh.Close())
}
func TestReadFileHandleSeek(t *testing.T) {
@ -177,7 +177,7 @@ func TestReadFileHandleReadAt(t *testing.T) {
// check reading on closed file
fh.noSeek = true
n, err = fh.ReadAt(buf, 100)
assert.Equal(t, EBADF, err)
assert.Equal(t, ECLOSED, err)
}
func TestReadFileHandleFlush(t *testing.T) {

View File

@ -11,6 +11,11 @@
// use paths with slashes in.
//
// It also includes directory caching
//
// The vfs package returns Error values to signal precisely which
// error conditions have ocurred. It may also return general errors
// it receives. It tries to use os Error values (eg os.ErrExist)
// where possible.
package vfs
import (

View File

@ -88,7 +88,7 @@ func (fh *WriteFileHandle) WriteAt(p []byte, off int64) (n int, err error) {
defer fh.mu.Unlock()
if fh.closed {
fs.Errorf(fh.remote, "WriteFileHandle.Write error: %v", EBADF)
return 0, EBADF
return 0, ECLOSED
}
if fh.offset != off {
fs.Errorf(fh.remote, "WriteFileHandle.Write can't seek in file")
@ -129,7 +129,7 @@ func (fh *WriteFileHandle) Offset() (offset int64) {
// Must be called with fh.mu held
func (fh *WriteFileHandle) close() error {
if fh.closed {
return EBADF
return ECLOSED
}
fh.closed = true
fh.file.addWriters(-1)

View File

@ -61,7 +61,7 @@ func TestWriteFileHandleMethods(t *testing.T) {
// Check double close
err = fh.Close()
assert.Equal(t, EBADF, err)
assert.Equal(t, ECLOSED, err)
// check vfs
root, err := vfs.Root()
@ -105,7 +105,7 @@ func TestWriteFileHandleWriteAt(t *testing.T) {
// Check can't write on closed handle
n, err = fh.WriteAt([]byte("hello"), 0)
assert.Equal(t, EBADF, err)
assert.Equal(t, ECLOSED, err)
assert.Equal(t, 0, n)
// check vfs