Three types of time for Linux system files (atime/ctime/mtime)

Use Go to manipulate files and process them differently based on creation time (or modification time).

On Mac, the file-related structure fields are in Stat_t under syscall/ztypes_darwin_arm64.go:

type Stat_t struct {
    
    
	Dev           int32
	Mode          uint16
	Nlink         uint16
	Ino           uint64
	Uid           uint32
	Gid           uint32
	Rdev          int32
	Pad_cgo_0     [4]byte
	Atimespec     Timespec
	Mtimespec     Timespec
	Ctimespec     Timespec
	Birthtimespec Timespec
	Size          int64
	Blocks        int64
	Blksize       int32
	Flags         uint32
	Gen           uint32
	Lspare        int32
	Qspare        [2]int64
}

There are four time-related fields, namely Atimespec, Mtimespec, Ctimespec and Birthtimespec. According to the following code:

package main

import (
	"fmt"
	"github.com/pkg/errors"
	"os"
	"syscall"
	"time"
)

func main() {
    
    
	file, err := os.Stat("/Users/fliter/blog")
	if err != nil && errors.Is(err, os.ErrNotExist) {
    
    
		fmt.Println("文件确实不存在,err is:", err)
		//创建多级目录
		//os.MkdirAll("/Users/fliter/blog", os.ModePerm)
	}

	stat_t := file.Sys().(*syscall.Stat_t)

	fmt.Println(stat_t)
	fmt.Println(timespecToTime(stat_t.Atimespec))     // 访问时间
	fmt.Println(timespecToTime(stat_t.Ctimespec))     // 修改时间
	fmt.Println(timespecToTime(stat_t.Mtimespec))     // 修改时间
	fmt.Println(timespecToTime(stat_t.Birthtimespec)) // 创建时间

}

func timespecToTime(ts syscall.Timespec) time.Time {
    
    
	return time.Unix(ts.Sec, ts.Nsec)
}

The creation time is not Ctimespec, but Birthtimespec.

Ctimespec and Mtimespec both seem to be modification times? ?

This involves the three times related to files in UNIX/Linux operating systems—atime, mtime, and ctime. mtime
Insert image description here
refers to the time when the file content was last modified, and ctime refers to the metadata of the file (such as permissions, owner, etc.) The time when it was last modified

In Linux, you can view it through the stat command

Guess you like

Origin blog.csdn.net/m0_73728511/article/details/133365256