Files
wazero/internal/platform/stat.go
Crypt Keeper 3609d74c92 Implements stat device/inode on WASI and GOOS=js (#1041)
This implements stat device and inode for WASI and GOOS=js, though it
does not implement the host side for windows, yet. Doing windows
requires plumbing as the values needed aren't exposed in Go. When we
re-do the syscallfs file type to have a stat method, we can address that
glitch. Meanwhile, I can find no Go sourcebase that does any better,
though the closest is the implementation details of os.SameFile.

I verified this with wasi-testsuite which now passes all but 1 case
which is unrelated (we haven't yet implemented `lseek`).

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-16 22:22:39 -06:00

35 lines
1.0 KiB
Go

package platform
import "os"
// StatTimes returns platform-specific values if os.FileInfo Sys is available.
// Otherwise, it returns the mod time for all values.
func StatTimes(t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64) {
if t.Sys() == nil { // possibly fake filesystem
return mtimes(t)
}
return statTimes(t)
}
// StatDeviceInode returns platform-specific values if os.FileInfo Sys is
// available. Otherwise, it returns zero which makes file identity comparison
// unsupported.
//
// Returning zero for now works in most cases, except notably wasi-libc
// code that needs to compare file identity via the underlying data as
// opposed to a host function similar to os.SameFile.
// See https://github.com/WebAssembly/wasi-filesystem/issues/65
func StatDeviceInode(t os.FileInfo) (dev, inode uint64) {
if t.Sys() == nil { // possibly fake filesystem
return
}
return statDeviceInode(t)
}
func mtimes(t os.FileInfo) (atimeNsec, mtimeNsec, ctimeNsec int64) {
mtimeNsec = t.ModTime().UnixNano()
atimeNsec = mtimeNsec
ctimeNsec = mtimeNsec
return
}