Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error in Readdir on regular mem file #170

Merged
merged 1 commit into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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