futimens_windows.go

 1package sysfs
 2
 3import (
 4	"syscall"
 5
 6	"github.com/tetratelabs/wazero/experimental/sys"
 7)
 8
 9func utimens(path string, atim, mtim int64) sys.Errno {
10	return chtimes(path, atim, mtim)
11}
12
13func futimens(fd uintptr, atim, mtim int64) error {
14	// Per docs, zero isn't a valid timestamp as it cannot be differentiated
15	// from nil. In both cases, it is a marker like sys.UTIME_OMIT.
16	// See https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfiletime
17	a, w := timespecToFiletime(atim, mtim)
18
19	if a == nil && w == nil {
20		return nil // both omitted, so nothing to change
21	}
22
23	// Attempt to get the stat by handle, which works for normal files
24	h := syscall.Handle(fd)
25
26	// Note: This returns ERROR_ACCESS_DENIED when the input is a directory.
27	return syscall.SetFileTime(h, nil, a, w)
28}
29
30func timespecToFiletime(atim, mtim int64) (a, w *syscall.Filetime) {
31	a = timespecToFileTime(atim)
32	w = timespecToFileTime(mtim)
33	return
34}
35
36func timespecToFileTime(tim int64) *syscall.Filetime {
37	if tim == sys.UTIME_OMIT {
38		return nil
39	}
40	ft := syscall.NsecToFiletime(tim)
41	return &ft
42}