Files
wazero/internal/sysfs/futimens_windows.go
Crypt Keeper fb6147ca94
Some checks failed
Release CLI / Pre-release build (push) Has been cancelled
Release CLI / Pre-release test (macos-12) (push) Has been cancelled
Release CLI / Pre-release test (ubuntu-22.04) (push) Has been cancelled
Release CLI / Pre-release test (windows-2022) (push) Has been cancelled
Release CLI / Release (push) Has been cancelled
Emulates AT_SYMLINK_NOFOLLOW instead of sometimes implementing it (#1588)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-07-22 08:03:47 +08:00

74 lines
2.0 KiB
Go

package sysfs
import (
"syscall"
"time"
"github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/platform"
)
// Define values even if not used except as sentinels.
const (
_UTIME_NOW = -1
_UTIME_OMIT = -2
SupportsSymlinkNoFollow = false
)
func utimens(path string, times *[2]syscall.Timespec) error {
return utimensPortable(path, times)
}
func futimens(fd uintptr, times *[2]syscall.Timespec) error {
// Before Go 1.20, ERROR_INVALID_HANDLE was returned for too many reasons.
// Kick out so that callers can use path-based operations instead.
if !platform.IsAtLeastGo120 {
return sys.ENOSYS
}
// Per docs, zero isn't a valid timestamp as it cannot be differentiated
// from nil. In both cases, it is a marker like syscall.UTIME_OMIT.
// See https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfiletime
a, w := timespecToFiletime(times)
if a == nil && w == nil {
return nil // both omitted, so nothing to change
}
// Attempt to get the stat by handle, which works for normal files
h := syscall.Handle(fd)
// Note: This returns ERROR_ACCESS_DENIED when the input is a directory.
return syscall.SetFileTime(h, nil, a, w)
}
func timespecToFiletime(times *[2]syscall.Timespec) (a, w *syscall.Filetime) {
// Handle when both inputs are current system time.
if times == nil || times[0].Nsec == UTIME_NOW && times[1].Nsec == UTIME_NOW {
now := time.Now().UnixNano()
ft := syscall.NsecToFiletime(now)
return &ft, &ft
}
// Now, either one of the inputs is current time, or neither. This
// means we don't have a risk of re-reading the clock.
a = timespecToFileTime(times, 0)
w = timespecToFileTime(times, 1)
return
}
func timespecToFileTime(times *[2]syscall.Timespec, i int) *syscall.Filetime {
if times[i].Nsec == UTIME_OMIT {
return nil
}
var nsec int64
if times[i].Nsec == UTIME_NOW {
nsec = time.Now().UnixNano()
} else {
nsec = syscall.TimespecToNsec(times[i])
}
ft := syscall.NsecToFiletime(nsec)
return &ft
}