-1

I have a file with this from the info (mac os):

Created: Tuesday, 26 May 2020 at 11:21
Modified: 26 May 2021 at 15:40

And after some research I tried to do:

ctim := fi.Sys().(*syscall.Stat_t).Ctim

atim := fi.Sys().(*syscall.Stat_t).Atim
mtim := fi.Sys().(*syscall.Stat_t).Mtim
log.Println("ctim:", time.Unix(ctim.Sec, ctim.Nsec))
log.Println("atim:", time.Unix(atim.Sec, atim.Nsec))
log.Println("mtim:", time.Unix(mtim.Sec, mtim.Nsec))

but they all return:

app_1  | 2021/05/26 15:40:17 ctim: 2021-05-26 15:40:17.199113879 +0000 UTC
app_1  | 2021/05/26 15:40:17 atim: 2021-05-26 15:40:16.457499729 +0000 UTC
app_1  | 2021/05/26 15:40:17 mtim: 2021-05-26 15:40:05.982391804 +0000 UTC

Also I'm using docker + docker-compose, building from golang:1.14-stretch and saving binary within debian:bullseye-slim.

Clearly this is not the creation time of the file. Any idea how I can get this information?

Jay Cee
  • 1,855
  • 5
  • 28
  • 48
  • 1
    https://unix.stackexchange.com/questions/20460/how-do-i-do-a-ls-and-then-sort-the-results-by-date-created – JimB May 26 '21 at 16:13

1 Answers1

7

ctime isn't "creation time", it's "inode change time".

OSX has "birth time", which is what the Finder displays as "creation date", and in Go on OSX (GOOS=darwin) it's available as the Birthtimespec field of syscall.Stat_t.

However, birth time isn't defined by POSIX, and many other Unix systems either don't have the concept, or don't expose it to user programs. Linux was one of those until quite recently; its stat syscall doesn't return birthtime, and so Go's os.Stat doesn't either. The statx syscall, added in Linux 4.11, does return it, but that's not in the Go stdlib.

statx is supported by golang.org/x/sys/unix but it's a rather low-level interface (for instance it requires an integer file descriptor number of an open directory; it doesn't work directly on an os.File). It also might return an ENOSYS error if it's running on a version of Linux that's too old, and even if it's new enough, the Btime you get back might be zero if the file you call it on is on a filesystem without birth time support, or if the Linux filesystem driver for that filesystem simply hasn't been updated.

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • Very interesting answer, thank you very much. Indeed I might encounter files that have been created a while ago on an old unix system and thus their header might not have a created time field. feels like it'll be prone to errors more than anything else. – Jay Cee May 26 '21 at 16:17
  • Note that most of the BSDs now have birthtime as well, with all the same caveats. – torek May 27 '21 at 05:17