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 return 0
case vfs.ENOENT: case vfs.ENOENT:
return -fuse.ENOENT return -fuse.ENOENT
case vfs.ENOTEMPTY:
return -fuse.ENOTEMPTY
case vfs.EEXIST: case vfs.EEXIST:
return -fuse.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: case vfs.ESPIPE:
return -fuse.ESPIPE return -fuse.ESPIPE
case vfs.EBADF: case vfs.EBADF:
@ -527,8 +531,6 @@ func translateError(err error) (errc int) {
return -fuse.EROFS return -fuse.EROFS
case vfs.ENOSYS: case vfs.ENOSYS:
return -fuse.ENOSYS return -fuse.ENOSYS
case vfs.EPERM:
return -fuse.EPERM
} }
fs.Errorf(nil, "IO error: %v", err) fs.Errorf(nil, "IO error: %v", err)
return -fuse.EIO return -fuse.EIO

View File

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

View File

@ -7,7 +7,7 @@ import (
"os" "os"
) )
// Error describes low level errors in a cross platform way // Error describes low level errors in a cross platform way.
type Error byte type Error byte
// NB if changing errors translateError in cmd/mount/fs.go, cmd/cmount/fs.go // NB if changing errors translateError in cmd/mount/fs.go, cmd/cmount/fs.go
@ -27,6 +27,7 @@ var (
ENOENT = os.ErrNotExist ENOENT = os.ErrNotExist
EEXIST = os.ErrExist EEXIST = os.ErrExist
EPERM = os.ErrPermission EPERM = os.ErrPermission
// ECLOSED see errors_{old,new}.go
) )
var errorNames = []string{ 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) // fs.Debugf(fh.o, "ReadFileHandle.Read size %d offset %d", reqSize, off)
if fh.closed { if fh.closed {
fs.Errorf(fh.o, "ReadFileHandle.Read error: %v", EBADF) fs.Errorf(fh.o, "ReadFileHandle.Read error: %v", EBADF)
return 0, EBADF return 0, ECLOSED
} }
doSeek := off != fh.offset doSeek := off != fh.offset
if doSeek && fh.noSeek { 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 // Must be called with fh.mu held
func (fh *ReadFileHandle) close() error { func (fh *ReadFileHandle) close() error {
if fh.closed { if fh.closed {
return EBADF return ECLOSED
} }
fh.closed = true fh.closed = true

View File

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

View File

@ -11,6 +11,11 @@
// use paths with slashes in. // use paths with slashes in.
// //
// It also includes directory caching // 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 package vfs
import ( import (

View File

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

View File

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