diff --git a/internal/engine/wazevo/entrypoint_amd64.go b/internal/engine/wazevo/entrypoint_amd64.go index 18f60af3a..6cb0c457e 100644 --- a/internal/engine/wazevo/entrypoint_amd64.go +++ b/internal/engine/wazevo/entrypoint_amd64.go @@ -1,5 +1,3 @@ -//go:build amd64 && !tinygo - package wazevo import _ "unsafe" diff --git a/internal/engine/wazevo/entrypoint_arm64.go b/internal/engine/wazevo/entrypoint_arm64.go index e16d64f65..0b8b55335 100644 --- a/internal/engine/wazevo/entrypoint_arm64.go +++ b/internal/engine/wazevo/entrypoint_arm64.go @@ -1,5 +1,3 @@ -//go:build arm64 && !tinygo - package wazevo import _ "unsafe" diff --git a/internal/engine/wazevo/entrypoint_others.go b/internal/engine/wazevo/entrypoint_other.go similarity index 90% rename from internal/engine/wazevo/entrypoint_others.go rename to internal/engine/wazevo/entrypoint_other.go index 8f9d64b2b..10cf5d4e1 100644 --- a/internal/engine/wazevo/entrypoint_others.go +++ b/internal/engine/wazevo/entrypoint_other.go @@ -1,4 +1,4 @@ -//go:build (!arm64 && !amd64) || tinygo +//go:build !(arm64 || amd64) package wazevo diff --git a/internal/platform/mmap_unix.go b/internal/platform/mmap_unix.go index 6b7c617b2..20f97d5eb 100644 --- a/internal/platform/mmap_unix.go +++ b/internal/platform/mmap_unix.go @@ -1,4 +1,4 @@ -//go:build (linux || darwin || freebsd || netbsd || dragonfly || solaris) && !tinygo +//go:build linux || darwin || freebsd || netbsd || dragonfly || solaris package platform diff --git a/internal/platform/mmap_unsupported.go b/internal/platform/mmap_unsupported.go index 06517b7b6..03a584537 100644 --- a/internal/platform/mmap_unsupported.go +++ b/internal/platform/mmap_unsupported.go @@ -1,4 +1,4 @@ -//go:build !(linux || darwin || freebsd || netbsd || dragonfly || solaris || windows) || tinygo +//go:build !(linux || darwin || freebsd || netbsd || dragonfly || solaris || windows) package platform diff --git a/internal/platform/mprotect_bsd.go b/internal/platform/mprotect_bsd.go index d9b264863..a07082bf8 100644 --- a/internal/platform/mprotect_bsd.go +++ b/internal/platform/mprotect_bsd.go @@ -1,4 +1,4 @@ -//go:build (freebsd || netbsd || dragonfly) && !tinygo +//go:build freebsd || netbsd || dragonfly package platform diff --git a/internal/platform/mprotect_syscall.go b/internal/platform/mprotect_syscall.go index 71baabb44..39393c6e7 100644 --- a/internal/platform/mprotect_syscall.go +++ b/internal/platform/mprotect_syscall.go @@ -1,4 +1,4 @@ -//go:build (linux || darwin) && !tinygo +//go:build linux || darwin package platform diff --git a/internal/platform/mprotect_unsupported.go b/internal/platform/mprotect_unsupported.go index e367b2030..ac1009d25 100644 --- a/internal/platform/mprotect_unsupported.go +++ b/internal/platform/mprotect_unsupported.go @@ -1,4 +1,4 @@ -//go:build solaris && !tinygo +//go:build solaris package platform diff --git a/internal/platform/time_notcgo.go b/internal/platform/time_notcgo.go deleted file mode 100644 index 0697b7c70..000000000 --- a/internal/platform/time_notcgo.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build !cgo && !windows - -package platform - -func nanotime() int64 { - return nanotimePortable() -} diff --git a/internal/platform/time_cgo.go b/internal/platform/time_notwindows.go similarity index 89% rename from internal/platform/time_cgo.go rename to internal/platform/time_notwindows.go index ff01d90ce..a1bdc8d5d 100644 --- a/internal/platform/time_cgo.go +++ b/internal/platform/time_notwindows.go @@ -1,4 +1,4 @@ -//go:build cgo && !windows +//go:build !windows package platform diff --git a/internal/sock/sock_supported.go b/internal/sock/sock_supported.go index e317be832..32f9bc4b6 100644 --- a/internal/sock/sock_supported.go +++ b/internal/sock/sock_supported.go @@ -1,4 +1,4 @@ -//go:build !plan9 && !js && !tinygo +//go:build !(plan9 || js) package sock diff --git a/internal/sock/sock_unsupported.go b/internal/sock/sock_unsupported.go index 77026754f..76ec031ef 100644 --- a/internal/sock/sock_unsupported.go +++ b/internal/sock/sock_unsupported.go @@ -1,4 +1,4 @@ -//go:build plan9 || js || tinygo +//go:build plan9 || js package sock diff --git a/internal/sysfs/datasync_linux.go b/internal/sysfs/datasync_linux.go index 5a8a415c5..792d0db2e 100644 --- a/internal/sysfs/datasync_linux.go +++ b/internal/sysfs/datasync_linux.go @@ -1,5 +1,3 @@ -//go:build linux && !tinygo - package sysfs import ( diff --git a/internal/sysfs/datasync_tinygo.go b/internal/sysfs/datasync_tinygo.go deleted file mode 100644 index e58fc9142..000000000 --- a/internal/sysfs/datasync_tinygo.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build tinygo - -package sysfs - -import ( - "os" - - "github.com/tetratelabs/wazero/experimental/sys" -) - -func datasync(f *os.File) sys.Errno { - return sys.ENOSYS -} diff --git a/internal/sysfs/dirfs.go b/internal/sysfs/dirfs.go index 04384038f..aba7a65e2 100644 --- a/internal/sysfs/dirfs.go +++ b/internal/sysfs/dirfs.go @@ -3,6 +3,7 @@ package sysfs import ( "io/fs" "os" + "path" experimentalsys "github.com/tetratelabs/wazero/experimental/sys" "github.com/tetratelabs/wazero/internal/platform" @@ -63,6 +64,48 @@ func (d *dirFS) Mkdir(path string, perm fs.FileMode) (errno experimentalsys.Errn return } +// Chmod implements the same method as documented on sys.FS +func (d *dirFS) Chmod(path string, perm fs.FileMode) experimentalsys.Errno { + err := os.Chmod(d.join(path), perm) + return experimentalsys.UnwrapOSError(err) +} + +// Rename implements the same method as documented on sys.FS +func (d *dirFS) Rename(from, to string) experimentalsys.Errno { + from, to = d.join(from), d.join(to) + return rename(from, to) +} + +// Rmdir implements the same method as documented on sys.FS +func (d *dirFS) Rmdir(path string) experimentalsys.Errno { + return rmdir(d.join(path)) +} + +// Unlink implements the same method as documented on sys.FS +func (d *dirFS) Unlink(path string) (err experimentalsys.Errno) { + return unlink(d.join(path)) +} + +// Link implements the same method as documented on sys.FS +func (d *dirFS) Link(oldName, newName string) experimentalsys.Errno { + err := os.Link(d.join(oldName), d.join(newName)) + return experimentalsys.UnwrapOSError(err) +} + +// Symlink implements the same method as documented on sys.FS +func (d *dirFS) Symlink(oldName, link string) experimentalsys.Errno { + // Creating a symlink with an absolute path string fails with a "not permitted" error. + // https://github.com/WebAssembly/wasi-filesystem/blob/v0.2.0/path-resolution.md#symlinks + if path.IsAbs(oldName) { + return experimentalsys.EPERM + } + // Note: do not resolve `oldName` relative to this dirFS. The link result is always resolved + // when dereference the `link` on its usage (e.g. readlink, read, etc). + // https://github.com/bytecodealliance/cap-std/blob/v1.0.4/cap-std/src/fs/dir.rs#L404-L409 + err := os.Symlink(oldName, d.join(link)) + return experimentalsys.UnwrapOSError(err) +} + // Readlink implements the same method as documented on sys.FS func (d *dirFS) Readlink(path string) (string, experimentalsys.Errno) { // Note: do not use syscall.Readlink as that causes race on Windows. @@ -74,11 +117,6 @@ func (d *dirFS) Readlink(path string) (string, experimentalsys.Errno) { return platform.ToPosixPath(dst), 0 } -// Rmdir implements the same method as documented on sys.FS -func (d *dirFS) Rmdir(path string) experimentalsys.Errno { - return rmdir(d.join(path)) -} - // Utimens implements the same method as documented on sys.FS func (d *dirFS) Utimens(path string, atim, mtim int64) experimentalsys.Errno { return utimens(d.join(path), atim, mtim) diff --git a/internal/sysfs/dirfs_supported.go b/internal/sysfs/dirfs_supported.go deleted file mode 100644 index a949bd54d..000000000 --- a/internal/sysfs/dirfs_supported.go +++ /dev/null @@ -1,48 +0,0 @@ -//go:build !tinygo - -package sysfs - -import ( - "io/fs" - "os" - "path" - - experimentalsys "github.com/tetratelabs/wazero/experimental/sys" -) - -// Link implements the same method as documented on sys.FS -func (d *dirFS) Link(oldName, newName string) experimentalsys.Errno { - err := os.Link(d.join(oldName), d.join(newName)) - return experimentalsys.UnwrapOSError(err) -} - -// Unlink implements the same method as documented on sys.FS -func (d *dirFS) Unlink(path string) (err experimentalsys.Errno) { - return unlink(d.join(path)) -} - -// Rename implements the same method as documented on sys.FS -func (d *dirFS) Rename(from, to string) experimentalsys.Errno { - from, to = d.join(from), d.join(to) - return rename(from, to) -} - -// Chmod implements the same method as documented on sys.FS -func (d *dirFS) Chmod(path string, perm fs.FileMode) experimentalsys.Errno { - err := os.Chmod(d.join(path), perm) - return experimentalsys.UnwrapOSError(err) -} - -// Symlink implements the same method as documented on sys.FS -func (d *dirFS) Symlink(oldName, link string) experimentalsys.Errno { - // Creating a symlink with an absolute path string fails with a "not permitted" error. - // https://github.com/WebAssembly/wasi-filesystem/blob/v0.2.0/path-resolution.md#symlinks - if path.IsAbs(oldName) { - return experimentalsys.EPERM - } - // Note: do not resolve `oldName` relative to this dirFS. The link result is always resolved - // when dereference the `link` on its usage (e.g. readlink, read, etc). - // https://github.com/bytecodealliance/cap-std/blob/v1.0.4/cap-std/src/fs/dir.rs#L404-L409 - err := os.Symlink(oldName, d.join(link)) - return experimentalsys.UnwrapOSError(err) -} diff --git a/internal/sysfs/dirfs_unsupported.go b/internal/sysfs/dirfs_unsupported.go deleted file mode 100644 index 98b1a3b84..000000000 --- a/internal/sysfs/dirfs_unsupported.go +++ /dev/null @@ -1,34 +0,0 @@ -//go:build tinygo - -package sysfs - -import ( - "io/fs" - - experimentalsys "github.com/tetratelabs/wazero/experimental/sys" -) - -// Link implements the same method as documented on sys.FS -func (d *dirFS) Link(oldName, newName string) experimentalsys.Errno { - return experimentalsys.ENOSYS -} - -// Unlink implements the same method as documented on sys.FS -func (d *dirFS) Unlink(path string) (err experimentalsys.Errno) { - return experimentalsys.ENOSYS -} - -// Rename implements the same method as documented on sys.FS -func (d *dirFS) Rename(from, to string) experimentalsys.Errno { - return experimentalsys.ENOSYS -} - -// Chmod implements the same method as documented on sys.FS -func (d *dirFS) Chmod(path string, perm fs.FileMode) experimentalsys.Errno { - return experimentalsys.ENOSYS -} - -// Symlink implements the same method as documented on sys.FS -func (d *dirFS) Symlink(oldName, link string) experimentalsys.Errno { - return experimentalsys.ENOSYS -} diff --git a/internal/sysfs/file_unix.go b/internal/sysfs/file_unix.go index f201e813d..92eaf3542 100644 --- a/internal/sysfs/file_unix.go +++ b/internal/sysfs/file_unix.go @@ -1,4 +1,4 @@ -//go:build unix && !tinygo +//go:build unix package sysfs diff --git a/internal/sysfs/file_unsupported.go b/internal/sysfs/file_unsupported.go index a028b9479..d75cb3841 100644 --- a/internal/sysfs/file_unsupported.go +++ b/internal/sysfs/file_unsupported.go @@ -1,4 +1,4 @@ -//go:build !(unix || windows) || tinygo +//go:build !(unix || windows) package sysfs diff --git a/internal/sysfs/futimens.go b/internal/sysfs/futimens.go index 7f6b11094..1f670ca11 100644 --- a/internal/sysfs/futimens.go +++ b/internal/sysfs/futimens.go @@ -1,4 +1,4 @@ -//go:build (linux || darwin) && !tinygo +//go:build linux || darwin package sysfs diff --git a/internal/sysfs/futimens_linux.go b/internal/sysfs/futimens_linux.go index db3b1b8b6..3ec68537b 100644 --- a/internal/sysfs/futimens_linux.go +++ b/internal/sysfs/futimens_linux.go @@ -1,5 +1,3 @@ -//go:build !tinygo - package sysfs import ( diff --git a/internal/sysfs/futimens_test.go b/internal/sysfs/futimens_test.go index abb66e44d..5c0f9b9b3 100644 --- a/internal/sysfs/futimens_test.go +++ b/internal/sysfs/futimens_test.go @@ -10,7 +10,6 @@ import ( "time" "github.com/tetratelabs/wazero/experimental/sys" - experimentalsys "github.com/tetratelabs/wazero/experimental/sys" "github.com/tetratelabs/wazero/internal/platform" "github.com/tetratelabs/wazero/internal/testing/require" ) @@ -26,11 +25,11 @@ func TestUtimens(t *testing.T) { func TestFileUtimens(t *testing.T) { testUtimens(t, true) - testEBADFIfFileClosed(t, func(f experimentalsys.File) experimentalsys.Errno { - return f.Utimens(experimentalsys.UTIME_OMIT, experimentalsys.UTIME_OMIT) + testEBADFIfFileClosed(t, func(f sys.File) sys.Errno { + return f.Utimens(sys.UTIME_OMIT, sys.UTIME_OMIT) }) - testEBADFIfDirClosed(t, func(d experimentalsys.File) experimentalsys.Errno { - return d.Utimens(experimentalsys.UTIME_OMIT, experimentalsys.UTIME_OMIT) + testEBADFIfDirClosed(t, func(d sys.File) sys.Errno { + return d.Utimens(sys.UTIME_OMIT, sys.UTIME_OMIT) }) } diff --git a/internal/sysfs/futimens_unsupported.go b/internal/sysfs/futimens_unsupported.go index 69d564942..668f3a61e 100644 --- a/internal/sysfs/futimens_unsupported.go +++ b/internal/sysfs/futimens_unsupported.go @@ -1,4 +1,4 @@ -//go:build (!windows && !linux && !darwin) || tinygo +//go:build !(windows || linux || darwin) package sysfs diff --git a/internal/sysfs/ino_tinygo.go b/internal/sysfs/ino_tinygo.go deleted file mode 100644 index 2099231cf..000000000 --- a/internal/sysfs/ino_tinygo.go +++ /dev/null @@ -1,14 +0,0 @@ -//go:build tinygo - -package sysfs - -import ( - "io/fs" - - experimentalsys "github.com/tetratelabs/wazero/experimental/sys" - "github.com/tetratelabs/wazero/sys" -) - -func inoFromFileInfo(_ string, info fs.FileInfo) (sys.Inode, experimentalsys.Errno) { - return 0, experimentalsys.ENOTSUP -} diff --git a/internal/sysfs/ino.go b/internal/sysfs/ino_unix.go similarity index 90% rename from internal/sysfs/ino.go rename to internal/sysfs/ino_unix.go index 8344cd16f..969fcd63f 100644 --- a/internal/sysfs/ino.go +++ b/internal/sysfs/ino_unix.go @@ -1,4 +1,4 @@ -//go:build !windows && !plan9 && !tinygo +//go:build unix package sysfs diff --git a/internal/sysfs/ino_plan9.go b/internal/sysfs/ino_unsupported.go similarity index 90% rename from internal/sysfs/ino_plan9.go rename to internal/sysfs/ino_unsupported.go index 9c669a475..64bdebdf3 100644 --- a/internal/sysfs/ino_plan9.go +++ b/internal/sysfs/ino_unsupported.go @@ -1,3 +1,5 @@ +//go:build !(unix || windows) + package sysfs import ( diff --git a/internal/sysfs/nonblock_unix.go b/internal/sysfs/nonblock_unix.go index 4477ee977..568ded18d 100644 --- a/internal/sysfs/nonblock_unix.go +++ b/internal/sysfs/nonblock_unix.go @@ -1,4 +1,4 @@ -//go:build !windows && !plan9 && !tinygo +//go:build unix package sysfs diff --git a/internal/sysfs/nonblock_unsupported.go b/internal/sysfs/nonblock_unsupported.go index 3e141a7b5..52caa4e4b 100644 --- a/internal/sysfs/nonblock_unsupported.go +++ b/internal/sysfs/nonblock_unsupported.go @@ -1,4 +1,4 @@ -//go:build plan9 || tinygo +//go:build !(unix || windows) package sysfs diff --git a/internal/sysfs/open_file_freebsd.go b/internal/sysfs/open_file_bsd.go similarity index 94% rename from internal/sysfs/open_file_freebsd.go rename to internal/sysfs/open_file_bsd.go index 42adaa214..755f98525 100644 --- a/internal/sysfs/open_file_freebsd.go +++ b/internal/sysfs/open_file_bsd.go @@ -1,3 +1,5 @@ +//go:build freebsd || dragonfly + package sysfs import ( diff --git a/internal/sysfs/open_file_sun.go b/internal/sysfs/open_file_sun.go deleted file mode 100644 index bdf7dd84d..000000000 --- a/internal/sysfs/open_file_sun.go +++ /dev/null @@ -1,31 +0,0 @@ -//go:build illumos || solaris - -package sysfs - -import ( - "syscall" - - "github.com/tetratelabs/wazero/experimental/sys" -) - -const supportedSyscallOflag = sys.O_DIRECTORY | sys.O_DSYNC | sys.O_NOFOLLOW | sys.O_NONBLOCK | sys.O_RSYNC - -func withSyscallOflag(oflag sys.Oflag, flag int) int { - if oflag&sys.O_DIRECTORY != 0 { - // See https://github.com/illumos/illumos-gate/blob/edd580643f2cf1434e252cd7779e83182ea84945/usr/src/uts/common/sys/fcntl.h#L90 - flag |= 0x1000000 - } - if oflag&sys.O_DSYNC != 0 { - flag |= syscall.O_DSYNC - } - if oflag&sys.O_NOFOLLOW != 0 { - flag |= syscall.O_NOFOLLOW - } - if oflag&sys.O_NONBLOCK != 0 { - flag |= syscall.O_NONBLOCK - } - if oflag&sys.O_RSYNC != 0 { - flag |= syscall.O_RSYNC - } - return flag -} diff --git a/internal/sysfs/open_file_linux.go b/internal/sysfs/open_file_supported.go similarity index 92% rename from internal/sysfs/open_file_linux.go rename to internal/sysfs/open_file_supported.go index 3fe2bb6e1..4443696a4 100644 --- a/internal/sysfs/open_file_linux.go +++ b/internal/sysfs/open_file_supported.go @@ -1,4 +1,4 @@ -//go:build !tinygo +//go:build linux || netbsd || openbsd || solaris package sysfs diff --git a/internal/sysfs/open_file_tinygo.go b/internal/sysfs/open_file_tinygo.go deleted file mode 100644 index ccf6847c0..000000000 --- a/internal/sysfs/open_file_tinygo.go +++ /dev/null @@ -1,25 +0,0 @@ -//go:build tinygo - -package sysfs - -import ( - "io/fs" - "os" - - "github.com/tetratelabs/wazero/experimental/sys" -) - -const supportedSyscallOflag = sys.Oflag(0) - -func withSyscallOflag(oflag sys.Oflag, flag int) int { - // O_DIRECTORY not defined - // O_DSYNC not defined - // O_NOFOLLOW not defined - // O_NONBLOCK not defined - // O_RSYNC not defined - return flag -} - -func openFile(path string, oflag sys.Oflag, perm fs.FileMode) (*os.File, sys.Errno) { - return nil, sys.ENOSYS -} diff --git a/internal/sysfs/open_file_notwindows.go b/internal/sysfs/open_file_unix.go similarity index 94% rename from internal/sysfs/open_file_notwindows.go rename to internal/sysfs/open_file_unix.go index 670e35910..9ae1e2bcd 100644 --- a/internal/sysfs/open_file_notwindows.go +++ b/internal/sysfs/open_file_unix.go @@ -1,4 +1,4 @@ -//go:build !windows && !tinygo +//go:build !windows package sysfs diff --git a/internal/sysfs/open_file_unsupported.go b/internal/sysfs/open_file_unsupported.go index 9f7a6d088..234fa4687 100644 --- a/internal/sysfs/open_file_unsupported.go +++ b/internal/sysfs/open_file_unsupported.go @@ -1,10 +1,8 @@ -//go:build !darwin && !linux && !windows && !illumos && !solaris && !freebsd +//go:build !(freebsd || dragonfly || darwin || linux || netbsd || openbsd || solaris || windows) package sysfs -import ( - "github.com/tetratelabs/wazero/experimental/sys" -) +import "github.com/tetratelabs/wazero/experimental/sys" const supportedSyscallOflag = sys.Oflag(0) diff --git a/internal/sysfs/poll.go b/internal/sysfs/poll.go index a2e1103e0..f5c982952 100644 --- a/internal/sysfs/poll.go +++ b/internal/sysfs/poll.go @@ -1,4 +1,4 @@ -//go:build windows || (linux && !tinygo) || darwin +//go:build windows || linux || darwin package sysfs diff --git a/internal/sysfs/poll_linux.go b/internal/sysfs/poll_linux.go index 49bf4fd06..dab7bb2ca 100644 --- a/internal/sysfs/poll_linux.go +++ b/internal/sysfs/poll_linux.go @@ -1,5 +1,3 @@ -//go:build !tinygo - package sysfs import ( diff --git a/internal/sysfs/poll_unsupported.go b/internal/sysfs/poll_unsupported.go index 2301a067e..53d09d8af 100644 --- a/internal/sysfs/poll_unsupported.go +++ b/internal/sysfs/poll_unsupported.go @@ -1,4 +1,4 @@ -//go:build !(linux || darwin || windows) || tinygo +//go:build !(linux || darwin || windows) package sysfs diff --git a/internal/sysfs/poll_windows_test.go b/internal/sysfs/poll_windows_test.go index 5a47f6f36..60f5aba32 100644 --- a/internal/sysfs/poll_windows_test.go +++ b/internal/sysfs/poll_windows_test.go @@ -134,6 +134,7 @@ func TestPoll_Windows(t *testing.T) { t.Run("poll should return immediately when duration is zero (no data)", func(t *testing.T) { r, w, err := os.Pipe() + require.NoError(t, err) defer r.Close() defer w.Close() diff --git a/internal/sysfs/rename.go b/internal/sysfs/rename_unix.go similarity index 84% rename from internal/sysfs/rename.go rename to internal/sysfs/rename_unix.go index 37c53571d..d3d75fc58 100644 --- a/internal/sysfs/rename.go +++ b/internal/sysfs/rename_unix.go @@ -1,4 +1,4 @@ -//go:build !windows && !plan9 && !tinygo +//go:build unix package sysfs diff --git a/internal/sysfs/rename_plan9.go b/internal/sysfs/rename_unsupported.go similarity index 86% rename from internal/sysfs/rename_plan9.go rename to internal/sysfs/rename_unsupported.go index 474cc7595..49c8360eb 100644 --- a/internal/sysfs/rename_plan9.go +++ b/internal/sysfs/rename_unsupported.go @@ -1,3 +1,5 @@ +//go:build !(unix || windows) + package sysfs import ( diff --git a/internal/sysfs/sock_supported.go b/internal/sysfs/sock_supported.go index 6c976fb86..165dcf292 100644 --- a/internal/sysfs/sock_supported.go +++ b/internal/sysfs/sock_supported.go @@ -1,4 +1,4 @@ -//go:build (linux || darwin || windows) && !tinygo +//go:build linux || darwin || windows package sysfs diff --git a/internal/sysfs/sock_unix.go b/internal/sysfs/sock_unix.go index 99ef018a4..0f570f37b 100644 --- a/internal/sysfs/sock_unix.go +++ b/internal/sysfs/sock_unix.go @@ -1,4 +1,4 @@ -//go:build (linux || darwin) && !tinygo +//go:build linux || darwin package sysfs diff --git a/internal/sysfs/sock_unsupported.go b/internal/sysfs/sock_unsupported.go index 8c27fed7f..cd53b070a 100644 --- a/internal/sysfs/sock_unsupported.go +++ b/internal/sysfs/sock_unsupported.go @@ -1,4 +1,4 @@ -//go:build (!linux && !darwin && !windows) || tinygo +//go:build !(linux || darwin || windows) package sysfs diff --git a/internal/sysfs/stat_bsd.go b/internal/sysfs/stat_bsd.go deleted file mode 100644 index 254e204cd..000000000 --- a/internal/sysfs/stat_bsd.go +++ /dev/null @@ -1,37 +0,0 @@ -//go:build (amd64 || arm64) && (darwin || freebsd) - -package sysfs - -import ( - "io/fs" - "os" - - experimentalsys "github.com/tetratelabs/wazero/experimental/sys" - "github.com/tetratelabs/wazero/sys" -) - -// dirNlinkIncludesDot is true because even though os.File filters out dot -// entries, the underlying syscall.Stat includes them. -// -// Note: this is only used in tests -const dirNlinkIncludesDot = true - -func lstat(path string) (sys.Stat_t, experimentalsys.Errno) { - if info, err := os.Lstat(path); err != nil { - return sys.Stat_t{}, experimentalsys.UnwrapOSError(err) - } else { - return sys.NewStat_t(info), 0 - } -} - -func stat(path string) (sys.Stat_t, experimentalsys.Errno) { - if info, err := os.Stat(path); err != nil { - return sys.Stat_t{}, experimentalsys.UnwrapOSError(err) - } else { - return sys.NewStat_t(info), 0 - } -} - -func statFile(f fs.File) (sys.Stat_t, experimentalsys.Errno) { - return defaultStatFile(f) -} diff --git a/internal/sysfs/stat_linux.go b/internal/sysfs/stat_supported.go similarity index 92% rename from internal/sysfs/stat_linux.go rename to internal/sysfs/stat_supported.go index 4ca5c3b86..93b63906d 100644 --- a/internal/sysfs/stat_linux.go +++ b/internal/sysfs/stat_supported.go @@ -1,4 +1,4 @@ -//go:build (amd64 || arm64 || ppc64le || riscv64 || s390x) && linux +//go:build linux || darwin || freebsd || netbsd || openbsd || dragonfly || solaris // Note: This expression is not the same as compiler support, even if it looks // similar. Platform functions here are used in interpreter mode as well. diff --git a/internal/sysfs/stat_unsupported.go b/internal/sysfs/stat_unsupported.go index b966c4333..d73027fab 100644 --- a/internal/sysfs/stat_unsupported.go +++ b/internal/sysfs/stat_unsupported.go @@ -1,4 +1,4 @@ -//go:build (!((amd64 || arm64 || ppc64le || riscv64 || s390x) && linux) && !((amd64 || arm64) && (darwin || freebsd)) && !((amd64 || arm64) && windows)) || js +//go:build !(linux || darwin || freebsd || netbsd || openbsd || dragonfly || solaris || windows) package sysfs diff --git a/internal/sysfs/stat_windows.go b/internal/sysfs/stat_windows.go index 4456dd782..23e76a986 100644 --- a/internal/sysfs/stat_windows.go +++ b/internal/sysfs/stat_windows.go @@ -1,5 +1,3 @@ -//go:build (amd64 || arm64) && windows - package sysfs import ( diff --git a/internal/sysfs/unlink.go b/internal/sysfs/unlink_unix.go similarity index 86% rename from internal/sysfs/unlink.go rename to internal/sysfs/unlink_unix.go index e3f051008..14e4a9f72 100644 --- a/internal/sysfs/unlink.go +++ b/internal/sysfs/unlink_unix.go @@ -1,4 +1,4 @@ -//go:build !windows && !plan9 && !tinygo +//go:build unix package sysfs diff --git a/internal/sysfs/unlink_plan9.go b/internal/sysfs/unlink_unsupported.go similarity index 70% rename from internal/sysfs/unlink_plan9.go rename to internal/sysfs/unlink_unsupported.go index 16ed06ab2..cc36502e8 100644 --- a/internal/sysfs/unlink_plan9.go +++ b/internal/sysfs/unlink_unsupported.go @@ -1,12 +1,14 @@ +//go:build !(unix || windows) + package sysfs import ( - "syscall" + "os" "github.com/tetratelabs/wazero/experimental/sys" ) func unlink(name string) sys.Errno { - err := syscall.Remove(name) + err := os.Remove(name) return sys.UnwrapOSError(err) } diff --git a/sys/stat_bsd.go b/sys/stat_bsd.go index 3bf9b5d14..bd392aaea 100644 --- a/sys/stat_bsd.go +++ b/sys/stat_bsd.go @@ -1,4 +1,4 @@ -//go:build (amd64 || arm64) && (darwin || freebsd) +//go:build darwin || freebsd || netbsd package sys @@ -13,16 +13,16 @@ func statFromFileInfo(info fs.FileInfo) Stat_t { if d, ok := info.Sys().(*syscall.Stat_t); ok { st := Stat_t{} st.Dev = uint64(d.Dev) - st.Ino = d.Ino + st.Ino = Inode(d.Ino) st.Mode = info.Mode() st.Nlink = uint64(d.Nlink) - st.Size = d.Size + st.Size = int64(d.Size) atime := d.Atimespec - st.Atim = atime.Sec*1e9 + atime.Nsec + st.Atim = EpochNanos(atime.Sec)*1e9 + EpochNanos(atime.Nsec) mtime := d.Mtimespec - st.Mtim = mtime.Sec*1e9 + mtime.Nsec + st.Mtim = EpochNanos(mtime.Sec)*1e9 + EpochNanos(mtime.Nsec) ctime := d.Ctimespec - st.Ctim = ctime.Sec*1e9 + ctime.Nsec + st.Ctim = EpochNanos(ctime.Sec)*1e9 + EpochNanos(ctime.Nsec) return st } return defaultStatFromFileInfo(info) diff --git a/sys/stat_linux.go b/sys/stat_linux.go deleted file mode 100644 index dded36347..000000000 --- a/sys/stat_linux.go +++ /dev/null @@ -1,32 +0,0 @@ -//go:build (amd64 || arm64 || ppc64le || riscv64 || s390x) && linux - -// Note: This expression is not the same as compiler support, even if it looks -// similar. Platform functions here are used in interpreter mode as well. - -package sys - -import ( - "io/fs" - "syscall" -) - -const sysParseable = true - -func statFromFileInfo(info fs.FileInfo) Stat_t { - if d, ok := info.Sys().(*syscall.Stat_t); ok { - st := Stat_t{} - st.Dev = uint64(d.Dev) - st.Ino = uint64(d.Ino) - st.Mode = info.Mode() - st.Nlink = uint64(d.Nlink) - st.Size = d.Size - atime := d.Atim - st.Atim = atime.Sec*1e9 + atime.Nsec - mtime := d.Mtim - st.Mtim = mtime.Sec*1e9 + mtime.Nsec - ctime := d.Ctim - st.Ctim = ctime.Sec*1e9 + ctime.Nsec - return st - } - return defaultStatFromFileInfo(info) -} diff --git a/sys/stat_posix.go b/sys/stat_posix.go new file mode 100644 index 000000000..0e0f47e5a --- /dev/null +++ b/sys/stat_posix.go @@ -0,0 +1,29 @@ +//go:build linux || openbsd || dragonfly || solaris + +package sys + +import ( + "io/fs" + "syscall" +) + +const sysParseable = true + +func statFromFileInfo(info fs.FileInfo) Stat_t { + if d, ok := info.Sys().(*syscall.Stat_t); ok { + st := Stat_t{} + st.Dev = uint64(d.Dev) + st.Ino = Inode(d.Ino) + st.Mode = info.Mode() + st.Nlink = uint64(d.Nlink) + st.Size = int64(d.Size) + atime := d.Atim + st.Atim = EpochNanos(atime.Sec)*1e9 + EpochNanos(atime.Nsec) + mtime := d.Mtim + st.Mtim = EpochNanos(mtime.Sec)*1e9 + EpochNanos(mtime.Nsec) + ctime := d.Ctim + st.Ctim = EpochNanos(ctime.Sec)*1e9 + EpochNanos(ctime.Nsec) + return st + } + return defaultStatFromFileInfo(info) +} diff --git a/sys/stat_test.go b/sys/stat_test.go index f58b16535..396f4edeb 100644 --- a/sys/stat_test.go +++ b/sys/stat_test.go @@ -116,8 +116,14 @@ func Test_NewStat_t(t *testing.T) { for _, tt := range tests { tc := tt t.Run(tc.name, func(t *testing.T) { + switch runtime.GOOS { + case "linux", "darwin", "freebsd", "netbsd", "openbsd", "dragonfly", "solaris", "illumos": + break + default: + tc.expectDevIno = false + } st := sys.NewStat_t(tc.info) - if tc.expectDevIno && (runtime.GOOS == "linux" || runtime.GOOS == "darwin" || runtime.GOOS == "freebsd") { + if tc.expectDevIno { require.NotEqual(t, uint64(0), st.Dev) require.NotEqual(t, uint64(0), st.Ino) } else { diff --git a/sys/stat_unsupported.go b/sys/stat_unsupported.go index cc800b2e3..6fb9b8096 100644 --- a/sys/stat_unsupported.go +++ b/sys/stat_unsupported.go @@ -1,4 +1,4 @@ -//go:build (!((amd64 || arm64 || ppc64le || riscv64 || s390x) && linux) && !((amd64 || arm64) && (darwin || freebsd)) && !((amd64 || arm64) && windows)) || js +//go:build !(linux || darwin || freebsd || netbsd || openbsd || dragonfly || solaris || windows) package sys diff --git a/sys/stat_windows.go b/sys/stat_windows.go index 1a7070f48..91450ffdf 100644 --- a/sys/stat_windows.go +++ b/sys/stat_windows.go @@ -1,5 +1,3 @@ -//go:build (amd64 || arm64) && windows - package sys import (