HaxeFoundation / HaxeFoundation/hashlink

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

Open
#619 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
901
Forks
201
Avg merge
18h 9m
Merged PRs (30d)
8

Description

Code to reproduce:

```haxe
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:

https://github.com/HaxeFoundation/hashlink/blob/31de39d5c5a0c4ede0dfcea7dbb40d5d0e43700c/src/std/sys.c#L389

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: https://github.com/HaxeFoundation/hxcpp/pull/1063

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.