star/filedate/filedate_other.go

33 lines
639 B
Go
Raw Permalink Normal View History

//go:build !windows && !darwin
package filedate
import (
"fmt"
"golang.org/x/sys/unix"
"time"
)
func SetFileTime(path string, ctime, atime, mtime time.Time) error {
var ts [2]unix.Timespec
if atime.IsZero() {
ts[0].Nsec = unix.UTIME_OMIT
} else {
ts[0] = unix.NsecToTimespec(atime.UnixNano())
}
if mtime.IsZero() {
ts[1].Nsec = unix.UTIME_OMIT
} else {
ts[1] = unix.NsecToTimespec(mtime.UnixNano())
}
// 使用 AT_FDCWD 表示相对当前工作目录
err := unix.UtimesNanoAt(unix.AT_FDCWD, path, ts[:], unix.AT_SYMLINK_NOFOLLOW)
if err != nil {
return fmt.Errorf("utimensat failed: %w", err)
}
return nil
}