HaxeFoundation/hashlink

sys.FileStat.ctime is not accurate on Linux/macOS

joshtynjala opened this issue · 0 comments

Code to reproduce:

var filePath = "path/to/test.txt";
sys.io.File.saveContent(filePath, "hello");
trace("created:", sys.FileSystem.stat(filePath).ctime);
trace("modified:", sys.FileSystem.stat(filePath).mtime);
haxe.Timer.delay(() -> {
  sys.io.File.saveContent(filePath, "goodbye");
  trace("created:", sys.FileSystem.stat(filePath).ctime);
  trace("modified:", sys.FileSystem.stat(filePath).mtime);
}, 2000);

The creation time should not change, but it does.

The relevant code:

*i++ = s.st_ctime;

The value of st_ctime from stat() does not actually refer to the file creation time. It may be accurate when a file is first created, but this value may be updated after the file is modified in certain ways (apparently, including the file size changing, which is very common).

On Linux, the value of stx_btime.tv_sec from a separate statx() call may be used to get the actual file creation time.

On macOS, the value of st_birthtime from the existing stat() call may be used to get the actual file creation time.

I created a pull request for hxcpp with the necessary change, but I wasn't able to successfully build HashLink on macOS, so I'm simply reporting the issue instead.

Related hxpp pull: HaxeFoundation/hxcpp#1063