[Urgency] Error ctim value get from file.Stat().Sys().(*syscall.Stat_t)

56 views
Skip to first unread message

GoFreshMan

unread,
Jun 20, 2019, 12:56:07 PM6/20/19
to golang-nuts
linux version: 5.1.9-300.fc30.x86_64
linux dist: fedora 30
go version: go1.12.5 linux/amd64


[ ~/Rhizobium]$ stat README.md
 文件:README.md
 大小:461            块:8          IO 块:4096   普通文件
设备:805h/2053d      Inode4199368     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/     dba)   Gid:( 1000/     dba)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2019-06-17 16:39:21.000000000 +0800 (last access)
最近更改:2019-06-18 10:40:27.000000000 +0800 (last change)
最近改动:2019-06-18 14:05:06.950815138 +0800 (last modified)
创建时间:2019-06-17 16:39:21.343782864 +0800 (create time)




In my go program:
package common

import (
"fmt"
"os"
"os/user"
"syscall"
"testing"
"time"
)

func TestGetFileState(t *testing.T) {
f, _ := os.Stat("/home/dba/Rhizobium/README.md")
x := GetFileState1(f)
fmt.Printf("%+v\n", f)
fmt.Printf("%+v", x)
}

func GetFileState1(info os.FileInfo) FileStat1 {
stat := info.Sys().(*syscall.Stat_t)
fileState := FileStat1{
Inode:   uint64(stat.Ino),
Device:  uint64(stat.Dev),
Name:    info.Name(),
Size:    info.Size(),
Mode:    info.Mode(),
CrtTime: time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec)),
ModTime: time.Unix(stat.Mtim.Sec, stat.Mtim.Nsec),
AcsTime: time.Unix(stat.Atim.Sec, stat.Atim.Nsec),
isDir:   info.IsDir(),
}
groupPointer, _ := user.LookupGroupId(fmt.Sprintf("%d", stat.Gid))
fileState.userGroup = *groupPointer

userPointer, _ := user.LookupId(fmt.Sprintf("%d", stat.Uid))
fileState.user = *userPointer
return fileState
}

type FileStat1 struct {
Inode     uint64
Device    uint64
Name      string
Size      int64 // bytes
Mode      os.FileMode
CrtTime   time.Time // 创建时间
AcsTime   time.Time // 上次读取时间
ModTime   time.Time // 上次修改时间
isDir     bool
user      user.User
userGroup user.Group
}

output:

=== RUN   TestGetFileState
&{name:README.md size:461 mode:436 modTime:{wall:0 ext:63696422427 loc:0x638c00} sys:{Dev:2053 Ino:4199368 Nlink:1 Mode:33204 Uid:1000 Gid:1000 X__pad0:0 Rdev:0 Size:461 Blksize:4096 Blocks:8 Atim:{Sec:1560760761 Nsec:0} Mtim:{Sec:1560825627 Nsec:0} Ctim:{Sec:1560837906 Nsec:950815138} X__unused:[0 0 0]}}
{Inode:4199368 Device:2053 Name:README.md Size:461 Mode:-rw-rw-r-- CrtTime:2019-06-18 14:05:06.950815138 +0800 CST AcsTime:2019-06-17 16:39:21 +0800 CST ModTime:2019-06-18 10:40:27 +0800 CST isDir:false user:{Uid:1000 Gid:1000 Username:dba Name:dba HomeDir:/home/dba} userGroup:{Gid:1000 Name:dba}}--- PASS: TestGetFileState (0.00s)
PASS
ok  command-line-arguments 0.002s


Question:

why  file.Stat().Sys().(*syscall.Stat_t) get file create time is :

 Ctim:{Sec:1560837906 Nsec:950815138} === 2019-06-18 14:05:06.950815138 +0800 CST

access time is 

{Sec:1560760761 Nsec:0} ==== 2019-06-17 16:39:21 +0800

It seems the ctim and atime just on the contray


utils_linux_test.go

David Finkel

unread,
Jun 20, 2019, 6:41:35 PM6/20/19
to GoFreshMan, golang-nuts
We'd have to know more about the history of that particular file on your system in order to provide more detailed information, but a couple things to note that probably help explaining your confusion:
  • ctime is not the create time, it's the inode change time, so it gets updated whenever you change permissions, update the link-count (via a rename or  hardlink), or file-size change (among other things)
  • atime may not be reliable. E.G. filesystems mounted with the "noatime" option won't ever generate a write to the inode on its own to update the atime of a file; and "relatime" will limit how often it updates the atime timestamp. 



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/298e7dc4-1817-4933-a7d1-e9be00e9fa13%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages