Skip to content

Commit cd39edf

Browse files
committed
os: add TestReadNonDir, fix problem it found on darwin
1 parent f9c588c commit cd39edf

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/os/dir_darwin.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ func (d *dirInfo) close() {
2626

2727
func (f *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) {
2828
if f.dirinfo == nil {
29+
// Begin workaround
30+
// Without this test, darwinOpenDir would happily open non-directories, causing trouble in at least Glob().
31+
// Upstream doesn't have to check f.Stat; darwin's
32+
// fdopendir seems to take care of that when called
33+
// normally, perhaps we are bypassing that?
34+
sfi, err := f.Stat()
35+
if err != nil {
36+
return nil, nil, nil, &PathError{Op: "stat", Path: f.name, Err: err}
37+
}
38+
if !sfi.IsDir() {
39+
return nil, nil, nil, &PathError{Op: "readdir", Path: f.name, Err: syscall.ENOTDIR}
40+
}
41+
// End workaround
2942
dir, call, errno := darwinOpenDir(syscallFd(f.handle.(unixFileHandle)))
3043
if errno != nil {
3144
return nil, nil, nil, &PathError{Op: call, Path: f.name, Err: errno}

src/os/dir_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,13 @@ func TestReadDir(t *testing.T) {
169169
t.Fatalf("ReadDir %s: testdata directory not found", dirname)
170170
}
171171
}
172+
173+
// TestReadNonDir is a tinygo regression test to verify that opening a non-directory returns an error.
174+
// See note re f.Stat() in dir_darwin.go.
175+
func TestReadNonDir(t *testing.T) {
176+
dirname := "read_test.go"
177+
_, err := ReadDir(dirname)
178+
if err == nil {
179+
t.Fatalf("ReadDir %s: error on non-dir expected, none found", dirname)
180+
}
181+
}

0 commit comments

Comments
 (0)