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

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

Closed
weberc2-tempus opened this issue Nov 14, 2023 · 2 comments · Fixed by #38
Closed

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

weberc2-tempus opened this issue Nov 14, 2023 · 2 comments · Fixed by #38

Comments

@weberc2-tempus
Copy link
Contributor

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
}
@pjbgf
Copy link
Member

pjbgf commented Nov 14, 2023

I am happy to review a PR for this @weberc2-tempus.

weberc2-tempus added a commit to weberc2-tempus/go-billy that referenced this issue Nov 14, 2023
Presently, it returns an empty list of files and a `nil` error value.
This PR aims to fix go-git#37
@weberc2-tempus
Copy link
Contributor Author

weberc2-tempus commented Nov 14, 2023

Hi @pjbgf, I've opened #38. Thank you in advance for reviewing!

weberc2-tempus added a commit to weberc2-tempus/go-billy that referenced this issue Nov 17, 2023
Presently, it returns an empty list of files and a `nil` error value. This PR
aims to fix go-git#37.
weberc2-tempus added a commit to weberc2-tempus/go-billy that referenced this issue Nov 20, 2023
Presently, it returns an empty list of files and a `nil` error value. This PR
aims to fix go-git#37.
@pjbgf pjbgf closed this as completed in #38 Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants