2019-02-19 08:47
浏览 54


os.Chtimes always to follow symlinks and change the real files timestamp. Is there a method to change the symlinks timestamp in?

Just like touch -h does.

图片转代码服务由CSDN问答提供 功能建议

os.Chtimes 始终遵循符号链接并更改实际文件的时间戳。 是否存在 一种更改符号链接时间戳的方法?

就像 touch -h 一样。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douao2000 2019-02-19 15:31

    Not sure it's possible, at least from the syscall package.

    Looking at the source-code for say syscall.Chtimes:

    func Chtimes(name string, atime time.Time, mtime time.Time) error {
        var utimes [2]syscall.Timespec
        utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
        utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
        if e := syscall.UtimesNano(fixLongPath(name), utimes[0:]); e != nil {
            return &PathError{"chtimes", name, e}
        return nil

    duplicating this code - and removing the fixLongPath call which I assumed followed the symlinks - still affects the target file, not the source symlink.

    Even trying this operation on a symlink which points to a non-existent file, returns a runtime error no such file or directory.

    A CGO pkg - could, but that seems overkill.

    点赞 打赏 评论
  • duan5991518 2019-02-19 09:02

    You can check if a symlink exists and if so, remove it and create another one.

    if _, err := os.Lstat(symlinkPath); err == nil {
    err := os.Symlink(filePath, symlinkPath)
    if err != nil {
    点赞 打赏 评论