From dade77d719e1ab1c592afcf8bfd2a2c6877e822c Mon Sep 17 00:00:00 2001 From: visualfc Date: Thu, 5 Jun 2025 14:53:28 +0800 Subject: [PATCH] syscall: Stat_t for js/wasip1 --- runtime/internal/clite/syscall/fs_wasip1.go | 24 +++++++++++ runtime/internal/clite/syscall/fs_wasm.go | 11 ----- runtime/internal/clite/syscall/syscall_js.go | 26 +++++++++++ runtime/internal/lib/os/stat_js.go | 45 ++++++++++++++++++++ runtime/internal/lib/os/stat_wasip1.go | 43 +++++++++++++++++++ 5 files changed, 138 insertions(+), 11 deletions(-) create mode 100644 runtime/internal/clite/syscall/fs_wasip1.go create mode 100644 runtime/internal/clite/syscall/syscall_js.go create mode 100644 runtime/internal/lib/os/stat_js.go create mode 100644 runtime/internal/lib/os/stat_wasip1.go diff --git a/runtime/internal/clite/syscall/fs_wasip1.go b/runtime/internal/clite/syscall/fs_wasip1.go new file mode 100644 index 00000000..fe7f7560 --- /dev/null +++ b/runtime/internal/clite/syscall/fs_wasip1.go @@ -0,0 +1,24 @@ +// Copyright 2023 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. + +//go:build wasip1 + +package syscall + +type Stat_t struct { + Dev uint64 + Ino uint64 + Filetype uint8 + Nlink uint64 + Size uint64 + Atime uint64 + Mtime uint64 + Ctime uint64 + + Mode int + + // Uid and Gid are always zero on wasip1 platforms + Uid uint32 + Gid uint32 +} diff --git a/runtime/internal/clite/syscall/fs_wasm.go b/runtime/internal/clite/syscall/fs_wasm.go index ffc3a993..d19e7f3d 100644 --- a/runtime/internal/clite/syscall/fs_wasm.go +++ b/runtime/internal/clite/syscall/fs_wasm.go @@ -310,14 +310,3 @@ func hasPrefix(s, prefix string) bool { func hasSuffix(s, suffix string) bool { return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix } - -type Stat_t struct { - Dev uint64 - Ino uint64 - Filetype uint8 - Nlink uint64 - Size uint64 - Atime uint64 - Mtime uint64 - Ctime uint64 -} diff --git a/runtime/internal/clite/syscall/syscall_js.go b/runtime/internal/clite/syscall/syscall_js.go new file mode 100644 index 00000000..d8017537 --- /dev/null +++ b/runtime/internal/clite/syscall/syscall_js.go @@ -0,0 +1,26 @@ +// Copyright 2018 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. + +//go:build js && wasm + +package syscall + +type Stat_t struct { + Dev int64 + Ino uint64 + Mode uint32 + Nlink uint32 + Uid uint32 + Gid uint32 + Rdev int64 + Size int64 + Blksize int32 + Blocks int32 + Atime int64 + AtimeNsec int64 + Mtime int64 + MtimeNsec int64 + Ctime int64 + CtimeNsec int64 +} diff --git a/runtime/internal/lib/os/stat_js.go b/runtime/internal/lib/os/stat_js.go new file mode 100644 index 00000000..8c2185f3 --- /dev/null +++ b/runtime/internal/lib/os/stat_js.go @@ -0,0 +1,45 @@ +// 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. + +//go:build js && wasm + +package os + +import ( + "time" + + "github.com/goplus/llgo/runtime/internal/clite/syscall" +) + +func fillFileStatFromSys(fs *fileStat, name string) { + fs.name = basename(name) + fs.size = fs.sys.Size + fs.modTime = time.Unix(fs.sys.Mtime, fs.sys.MtimeNsec) + fs.mode = FileMode(fs.sys.Mode & 0777) + switch fs.sys.Mode & syscall.S_IFMT { + case syscall.S_IFBLK: + fs.mode |= ModeDevice + case syscall.S_IFCHR: + fs.mode |= ModeDevice | ModeCharDevice + case syscall.S_IFDIR: + fs.mode |= ModeDir + case syscall.S_IFIFO: + fs.mode |= ModeNamedPipe + case syscall.S_IFLNK: + fs.mode |= ModeSymlink + case syscall.S_IFREG: + // nothing to do + case syscall.S_IFSOCK: + fs.mode |= ModeSocket + } + if fs.sys.Mode&syscall.S_ISGID != 0 { + fs.mode |= ModeSetgid + } + if fs.sys.Mode&syscall.S_ISUID != 0 { + fs.mode |= ModeSetuid + } + if fs.sys.Mode&syscall.S_ISVTX != 0 { + fs.mode |= ModeSticky + } +} diff --git a/runtime/internal/lib/os/stat_wasip1.go b/runtime/internal/lib/os/stat_wasip1.go new file mode 100644 index 00000000..1b7cc21d --- /dev/null +++ b/runtime/internal/lib/os/stat_wasip1.go @@ -0,0 +1,43 @@ +// Copyright 2023 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. + +//go:build wasip1 + +package os + +import ( + "time" + + "github.com/goplus/llgo/runtime/internal/clite/syscall" +) + +func fillFileStatFromSys(fs *fileStat, name string) { + fs.name = basename(name) + fs.size = int64(fs.sys.Size) + fs.modTime = time.Unix(0, int64(fs.sys.Mtime)) + + switch fs.sys.Filetype { + case syscall.FILETYPE_BLOCK_DEVICE: + fs.mode |= ModeDevice + case syscall.FILETYPE_CHARACTER_DEVICE: + fs.mode |= ModeDevice | ModeCharDevice + case syscall.FILETYPE_DIRECTORY: + fs.mode |= ModeDir + case syscall.FILETYPE_SOCKET_DGRAM: + fs.mode |= ModeSocket + case syscall.FILETYPE_SOCKET_STREAM: + fs.mode |= ModeSocket + case syscall.FILETYPE_SYMBOLIC_LINK: + fs.mode |= ModeSymlink + } + + // WASI does not support unix-like permissions, but Go programs are likely + // to expect the permission bits to not be zero so we set defaults to help + // avoid breaking applications that are migrating to WASM. + if fs.sys.Filetype == syscall.FILETYPE_DIRECTORY { + fs.mode |= 0700 + } else { + fs.mode |= 0600 + } +}