Return error in Readdir on regular mem file

Fixes #169
This commit is contained in:
Bjørn Erik Pedersen 2018-05-31 11:43:57 +02:00
parent 63644898a8
commit 787d034dfe
2 changed files with 22 additions and 0 deletions

View File

@ -570,6 +570,25 @@ func TestReaddir(t *testing.T) {
} }
} }
// https://github.com/spf13/afero/issues/169
func TestReaddirRegularFile(t *testing.T) {
defer removeAllTestFiles(t)
for _, fs := range Fss {
f := tmpFile(fs)
defer f.Close()
_, err := f.Readdirnames(-1)
if err == nil {
t.Fatal("Expected error")
}
_, err = f.Readdir(-1)
if err == nil {
t.Fatal("Expected error")
}
}
}
type myFileInfo []os.FileInfo type myFileInfo []os.FileInfo
func (m myFileInfo) String() string { func (m myFileInfo) String() string {

View File

@ -131,6 +131,9 @@ func (f *File) Sync() error {
} }
func (f *File) Readdir(count int) (res []os.FileInfo, err error) { func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
if !f.fileData.dir {
return nil, &os.PathError{Op: "readdir", Path: f.fileData.name, Err: errors.New("not a dir")}
}
var outLength int64 var outLength int64
f.fileData.Lock() f.fileData.Lock()