os: implement os.(*File).ReadDir for -target=wasi

Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
This commit is contained in:
Achille Roussel
2023-05-03 08:56:05 -07:00
committed by Ron Evans
parent 666312f63f
commit ee3af40cab
8 changed files with 259 additions and 38 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
//go:build baremetal || js || wasi || windows
//go:build baremetal || js || windows
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
+117
View File
@@ -0,0 +1,117 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file was derived from src/os/dir_darwin.go since the logic for wasi is
// fairly similar: we use fdopendir, fdclosedir, and readdir from wasi-libc in
// a similar way that the darwin code uses functions from libc.
//go:build wasi
package os
import (
"io"
"runtime"
"syscall"
"unsafe"
)
// opaque DIR* returned by fdopendir
//
// We add an unused field so it is not the empty struct, which is usually
// a special case in Go.
type dirInfo struct{ _ int32 }
func (d *dirInfo) close() {
syscall.Fdclosedir(uintptr(unsafe.Pointer(d)))
}
func (f *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) {
if f.dirinfo == nil {
dir, errno := syscall.Fdopendir(syscallFd(f.handle.(unixFileHandle)))
if errno != nil {
return nil, nil, nil, &PathError{Op: "fdopendir", Path: f.name, Err: errno}
}
f.dirinfo = (*dirInfo)(unsafe.Pointer(dir))
}
d := uintptr(unsafe.Pointer(f.dirinfo))
// see src/os/dir_unix.go
if n == 0 {
n = -1
}
for n != 0 {
dirent, errno := syscall.Readdir(d)
if errno != nil {
if errno == syscall.EINTR {
continue
}
return names, dirents, infos, &PathError{Op: "readdir", Path: f.name, Err: errno}
}
if dirent == nil { // EOF
break
}
if dirent.Ino == 0 {
continue
}
name := dirent.Name()
// Check for useless names before allocating a string.
if string(name) == "." || string(name) == ".." {
continue
}
if n > 0 {
n--
}
if mode == readdirName {
names = append(names, string(name))
} else if mode == readdirDirEntry {
de, err := newUnixDirent(f.name, string(name), dtToType(dirent.Type))
if IsNotExist(err) {
// File disappeared between readdir and stat.
// Treat as if it didn't exist.
continue
}
if err != nil {
return nil, dirents, nil, err
}
dirents = append(dirents, de)
} else {
info, err := lstat(f.name + "/" + string(name))
if IsNotExist(err) {
// File disappeared between readdir + stat.
// Treat as if it didn't exist.
continue
}
if err != nil {
return nil, nil, infos, err
}
infos = append(infos, info)
}
runtime.KeepAlive(f)
}
if n > 0 && len(names)+len(dirents)+len(infos) == 0 {
return nil, nil, nil, io.EOF
}
return names, dirents, infos, nil
}
func dtToType(typ uint8) FileMode {
switch typ {
case syscall.DT_BLK:
return ModeDevice
case syscall.DT_CHR:
return ModeDevice | ModeCharDevice
case syscall.DT_DIR:
return ModeDir
case syscall.DT_FIFO:
return ModeNamedPipe
case syscall.DT_LNK:
return ModeSymlink
case syscall.DT_REG:
return 0
}
return ^FileMode(0)
}
+2 -1
View File
@@ -187,7 +187,8 @@ func (f *File) Close() (err error) {
} else {
// Some platforms manage extra state other than the system handle which
// needs to be released when the file is closed. For example, darwin
// files have a DIR object holding a dup of the file descriptor.
// files have a DIR object holding a dup of the file descriptor, and
// linux files hold a buffer which needs to be released to a pool.
//
// These platform-specific logic is provided by the (*file).close method
// which is why we do not call the handle's Close method directly.
+9 -4
View File
@@ -30,12 +30,19 @@ func TestTempDir(t *testing.T) {
}
func TestChdir(t *testing.T) {
// create and cd into a new directory
// Save and restore the current working directory after the test, otherwise
// we might break other tests that depend on it.
//
// Note that it doesn't work if Chdir is broken, but then this test should
// fail and highlight the issue if that is the case.
oldDir, err := Getwd()
if err != nil {
t.Errorf("Getwd() returned %v", err)
return
}
defer Chdir(oldDir)
// create and cd into a new directory
dir := "_os_test_TestChDir"
Remove(dir)
err = Mkdir(dir, 0755)
@@ -60,9 +67,7 @@ func TestChdir(t *testing.T) {
t.Errorf("Close %s: %s", file, err)
}
// cd back to original directory
// TODO: emulate "cd .." in wasi-libc better?
//err = Chdir("..")
err = Chdir(oldDir)
err = Chdir("..")
if err != nil {
t.Errorf("Chdir ..: %s", err)
}