src-d / go-billy

The missing interface filesystem abstraction for Go
https://godoc.org/gopkg.in/src-d/go-billy.v4
Apache License 2.0
199 stars 43 forks source link

memfs `ReadDir()` doesn't return an error when the directory doesn't exist #85

Closed weberc2-tempus closed 10 months ago

weberc2-tempus commented 10 months ago

EDIT: Oops, filed this against src-d/go-billy instead of go-git/go-billy. Re-filed issue here

Version

v5.4.1

Description

memfs.New().ReadDir("asdf") returns nil, nil instead of nil, <directory-not-found-err>.

Minimal example

func TestMemoryReadDir(t *testing.T) {
    if _, err := osfs.New("/tmp").ReadDir("asdf"); err == nil {
        t.Fatal("osfs: expected error; found <nil>")
    } else {
        t.Logf("osfs: error was correctly returned!")
    }

    if _, err := memfs.New().ReadDir("asdf"); err == nil {
        t.Fatal("memfs: expected error; found <nil>")
    } else {
        t.Logf("memfs: error was correctly returned!")
    }
}

// Output
//   osfs: error was correctly returned!
//   memfs: expected error; found <nil>

Proposed changes

func (fs *Memory) ReadDir(path string) ([]os.FileInfo, error) {
    if f, has := fs.s.Get(path); has {
        if target, isLink := fs.resolveLink(path, f); isLink {
            return fs.ReadDir(target)
        }
+   } else {
+       return nil, &fs.PathError{Op: "open", Path: path, Err: unix.ENOENT}
}

    var entries []os.FileInfo
    for _, f := range fs.s.Children(path) {
        fi, _ := f.Stat()
        entries = append(entries, fi)
    }

    sort.Sort(ByName(entries))

    return entries, nil
}
weberc2-tempus commented 10 months ago

Oops, filed this against src-d/go-billy instead of go-git/go-billy. Re-filed issue here