Skip to content

Commit

Permalink
Return error in Readdir on regular mem file
Browse files Browse the repository at this point in the history
Fixes spf13#169
  • Loading branch information
bep committed May 31, 2018
1 parent 6364489 commit 787d034
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions afero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

func (m myFileInfo) String() string {
Expand Down
3 changes: 3 additions & 0 deletions mem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func (f *File) Sync() 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

f.fileData.Lock()
Expand Down

0 comments on commit 787d034

Please sign in to comment.