How can I get a file's ctime, atime, mtime and change them

huangapple go评论86阅读模式
英文:

How can I get a file's ctime, atime, mtime and change them

问题

你可以使用Go语言来获取文件的ctime、mtime和atime,并对它们进行更改。

在Go 1.1.2版本中:

  • 使用os.Stat函数只能获取到mtime。
  • 使用os.Chtimes函数可以更改mtime和atime,但无法更改ctime。
英文:

How can I get file's ctime, mtime, atime use Go and change them?

In Go 1.1.2,

  • os.Stat can only get mtime
  • os.Chtimes can change mtime and atime but not ctime.

答案1

得分: 28

Linux

ctime是inode或文件的更改时间。当文件属性发生更改时,如更改所有者、更改权限或将文件移动到其他文件系统时,ctime会更新,但在修改文件时也会更新。

文件的ctime和atime是与操作系统相关的。对于Linux来说,当inode或文件被更改时,ctime由Linux设置为当前时间戳。

以下是一个在Linux上通过将atime和mtime设置为它们的原始值来隐式更改ctime的示例。

package main

import (
	"fmt"
	"os"
	"syscall"
	"time"
)

func statTimes(name string) (atime, mtime, ctime time.Time, err error) {
	fi, err := os.Stat(name)
	if err != nil {
		return
	}
	mtime = fi.ModTime()
	stat := fi.Sys().(*syscall.Stat_t)
	atime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
	ctime = time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
	return
}

func main() {
	name := "stat.file"
	atime, mtime, ctime, err := statTimes(name)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(atime, mtime)
	fmt.Println(ctime)
	err = os.Chtimes(name, atime, mtime)
	if err != nil {
		fmt.Println(err)
		return
	}
	atime, mtime, ctime, err = statTimes(name)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(atime, mtime)
	fmt.Println(ctime)
}

输出:

2014-01-02 02:21:26.262111165 -0500 EST 2014-01-02 02:18:13.253154086 -0500 EST
2014-01-02 02:21:25.666108207 -0500 EST
2014-01-02 02:21:26.262111165 -0500 EST 2014-01-02 02:18:13.253154086 -0500 EST
2014-01-02 02:21:43.814198198 -0500 EST

参考链接

英文:

> Linux
>
> ctime
>
> ctime is the inode or file change time. The ctime gets updated when
> the file attributes are changed, like changing the owner, changing the
> permission or moving the file to an other filesystem but will also be
> updated when you modify a file.

The file ctime and atime are OS dependent. For Linux, ctime is set by Linux to the current timestamp when the inode or file is changed.

Here's an example, on Linux, of implicitly changing ctime by setting atime and mtime to their original values.

package main

import (
	"fmt"
	"os"
	"syscall"
	"time"
)

func statTimes(name string) (atime, mtime, ctime time.Time, err error) {
	fi, err := os.Stat(name)
	if err != nil {
		return
	}
	mtime = fi.ModTime()
	stat := fi.Sys().(*syscall.Stat_t)
	atime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
	ctime = time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
	return
}

func main() {
	name := "stat.file"
	atime, mtime, ctime, err := statTimes(name)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(atime, mtime)
	fmt.Println(ctime)
	err = os.Chtimes(name, atime, mtime)
	if err != nil {
		fmt.Println(err)
		return
	}
	atime, mtime, ctime, err = statTimes(name)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(atime, mtime)
	fmt.Println(ctime)
}

Output:

2014-01-02 02:21:26.262111165 -0500 EST 2014-01-02 02:18:13.253154086 -0500 EST
2014-01-02 02:21:25.666108207 -0500 EST
2014-01-02 02:21:26.262111165 -0500 EST 2014-01-02 02:18:13.253154086 -0500 EST
2014-01-02 02:21:43.814198198 -0500 EST

答案2

得分: 12

我知道这个很旧,但我整理了各种依赖于平台的文件访问时间结构,并将它们放入一个具有统一API的包中:

https://github.com/djherbis/atime

package main

import (
  "log"

  "github.com/djherbis/atime"
)

func main() {
  at, err := atime.Stat("myfile")
  if err != nil {
    log.Fatal(err.Error())
  }
  log.Println(at)
}
英文:

I know this is super old but I pulled together the various platform-dependent file access time structs and put them into one package with a unified API:

https://github.com/djherbis/atime

package main

import (
  "log"

  "github.com/djherbis/atime"
)

func main() {
  at, err := atime.Stat("myfile")
  if err != nil {
    log.Fatal(err.Error())
  }
  log.Println(at)
}

答案3

得分: 1

在Unix系统上,你可以通过syscall.Stat来获取文件的修改时间(mtime)和创建时间(ctime):

package main

import (
        "fmt"
        "log"
        "syscall"
)

func main() {
        var st syscall.Stat_t
        if err := syscall.Stat("/tmp/sysstat.go", &st); err != nil {
                log.Fatal(err)
        }
        fmt.Printf("mtime: %d\n", st.Mtimespec.Sec)
        fmt.Printf("ctime: %d\n", st.Ctimespec.Sec)
}

要更新这些值,你可以使用syscall.Utimes

英文:

On a Unix system, you can get a file's mtime and ctime via syscall.Stat:

package main

import (
        "fmt"
        "log"
        "syscall"
)

func main() {
        var st syscall.Stat_t
        if err := syscall.Stat("/tmp/sysstat.go", &st); err != nil {
                log.Fatal(err)
        }
        fmt.Printf("mtime: %d\n", st.Mtimespec.Sec)
        fmt.Printf("ctime: %d\n", st.Ctimespec.Sec)
}

To update these values, you should be able to use syscall.Utimes.

huangapple
  • 本文由 发表于 2014年1月2日 10:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/20875336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定