Skip to content

Commit bb87682

Browse files
committed
freebsd: use utimensat syscall in favor of utimes
The utimes syscall follows symlinks and changes the times on the link target, rather than the link itself. The lutimes syscall can be used to modify times on a symlink, though both syscalls are deprecated in favor of utimensat. utimensat can be called with the AF_SYMLINK_NOFOLLOW flag to change the times on a symlink. Signed-off-by: Samuel Karp <[email protected]>
1 parent c13fe94 commit bb87682

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

fs/copy_darwinopenbsdsolaris.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ func copyDevice(dst string, fi os.FileInfo) error {
3333
}
3434
return unix.Mknod(dst, uint32(fi.Mode()), int(st.Rdev))
3535
}
36+
37+
func utimesNano(name string, atime, mtime syscall.Timespec) error {
38+
timespec := []syscall.Timespec{atime, mtime}
39+
return syscall.UtimesNano(name, timespec)
40+
}

fs/copy_freebsd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ func copyDevice(dst string, fi os.FileInfo) error {
3333
}
3434
return unix.Mknod(dst, uint32(fi.Mode()), st.Rdev)
3535
}
36+
37+
func utimesNano(name string, atime, mtime syscall.Timespec) error {
38+
at := unix.NsecToTimespec(atime.Nano())
39+
mt := unix.NsecToTimespec(mtime.Nano())
40+
utimes := [2]unix.Timespec{at, mt}
41+
return unix.UtimesNanoAt(unix.AT_FDCWD, name, utimes[0:], unix.AT_SYMLINK_NOFOLLOW)
42+
}

fs/copy_unix.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ func copyFileInfo(fi os.FileInfo, name string) error {
5252
}
5353
}
5454

55-
timespec := []syscall.Timespec{StatAtime(st), StatMtime(st)}
56-
if err := syscall.UtimesNano(name, timespec); err != nil {
55+
if err := utimesNano(name, StatAtime(st), StatMtime(st)); err != nil {
5756
return errors.Wrapf(err, "failed to utime %s", name)
5857
}
5958

0 commit comments

Comments
 (0)