Skip to content

Commit 275c9a0

Browse files
authored
Simplify utimens. (#2449)
Use `syscall.UtimesNano` to avoid a macOS `go:linkname`. --------- Signed-off-by: Nuno Cruces <[email protected]>
1 parent 5e7c35e commit 275c9a0

3 files changed

Lines changed: 6 additions & 34 deletions

File tree

‎internal/sysfs/futimens.go‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,10 @@ package sysfs
44

55
import (
66
"syscall"
7-
"unsafe"
87

98
"github.com/tetratelabs/wazero/experimental/sys"
109
)
1110

12-
func timesToPtr(times *[2]syscall.Timespec) unsafe.Pointer { //nolint:unused
13-
if times != nil {
14-
return unsafe.Pointer(&times[0])
15-
}
16-
return unsafe.Pointer(nil)
17-
}
18-
1911
func timesToTimespecs(atim int64, mtim int64) (times *[2]syscall.Timespec) {
2012
// When both inputs are omitted, there is nothing to change.
2113
if atim == sys.UTIME_OMIT && mtim == sys.UTIME_OMIT {

‎internal/sysfs/futimens_darwin.go‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,27 @@ package sysfs
22

33
import (
44
"syscall"
5-
_ "unsafe"
5+
"unsafe"
66

77
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
88
)
99

10-
const (
11-
_AT_FDCWD = -0x2
12-
_AT_SYMLINK_NOFOLLOW = 0x0020
13-
_UTIME_OMIT = -2
14-
)
15-
16-
//go:noescape
17-
//go:linkname utimensat syscall.utimensat
18-
func utimensat(dirfd int, path string, times *[2]syscall.Timespec, flags int) error
10+
const _UTIME_OMIT = -2
1911

2012
func utimens(path string, atim, mtim int64) experimentalsys.Errno {
2113
times := timesToTimespecs(atim, mtim)
2214
if times == nil {
2315
return 0
2416
}
25-
var flags int
26-
return experimentalsys.UnwrapOSError(utimensat(_AT_FDCWD, path, times, flags))
17+
return experimentalsys.UnwrapOSError(syscall.UtimesNano(path, times[:]))
2718
}
2819

2920
func futimens(fd uintptr, atim, mtim int64) experimentalsys.Errno {
3021
times := timesToTimespecs(atim, mtim)
3122
if times == nil {
3223
return 0
3324
}
34-
_p0 := timesToPtr(times)
25+
_p0 := unsafe.Pointer(&times[0])
3526

3627
// Warning: futimens only exists since High Sierra (10.13).
3728
_, _, e1 := syscall_syscall6(libc_futimens_trampoline_addr, fd, uintptr(_p0), 0, 0, 0, 0)

‎internal/sysfs/futimens_linux.go‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,18 @@ package sysfs
33
import (
44
"syscall"
55
"unsafe"
6-
_ "unsafe"
76

87
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
98
)
109

11-
const (
12-
_AT_FDCWD = -0x64
13-
_UTIME_OMIT = (1 << 30) - 2
14-
)
10+
const _UTIME_OMIT = (1 << 30) - 2
1511

1612
func utimens(path string, atim, mtim int64) experimentalsys.Errno {
1713
times := timesToTimespecs(atim, mtim)
1814
if times == nil {
1915
return 0
2016
}
21-
22-
var flags int
23-
var _p0 *byte
24-
_p0, err := syscall.BytePtrFromString(path)
25-
if err == nil {
26-
err = utimensat(_AT_FDCWD, uintptr(unsafe.Pointer(_p0)), times, flags)
27-
}
28-
return experimentalsys.UnwrapOSError(err)
17+
return experimentalsys.UnwrapOSError(syscall.UtimesNano(path, times[:]))
2918
}
3019

3120
// On linux, implement futimens via utimensat with the NUL path.

0 commit comments

Comments
 (0)