added: timestamps to file stats
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 18 Jan 2024 00:24:07 +0000 (18:24 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 18 Jan 2024 00:24:07 +0000 (18:24 -0600)
core/os/file.onyx
core/runtime/platform/wasi/wasi_fs.onyx
shared/include/bh.h

index c2c791f877bee386426eeb55f676d48b1212f665..965046fd99f62a8d09f0b77d5c23adead8f0025f 100644 (file)
@@ -50,6 +50,9 @@ FileType :: enum {
 FileStat :: struct {
     size: i64;
     type: FileType;
+    change_time: u64;   // in milliseconds
+    accessed_time: u64; // in milliseconds
+    modified_time: u64; // in milliseconds
 }
 
 file_exists :: fs.__file_exists
index de1c23a348da5578aff1ba9751704b12b42fa0ea..b963188ceddb1339a56676e6a1e8734b666c063e 100644 (file)
@@ -119,6 +119,9 @@ __file_stat :: (path: str, out: &os.FileStat) -> bool {
         if err == .Success {
             exists = true;
             out.size = ~~ fs.size;
+            out.change_time = fs.ctim / 1000000;
+            out.accessed_time = fs.atim / 1000000;
+            out.modified_time = fs.mtim / 1000000;
 
             switch fs.filetype {
                 case .RegularFile do out.type = .RegularFile;
@@ -356,4 +359,4 @@ __dir_remove :: (path: str) -> bool {
 #if !#defined(runtime.vars.WASIX) {
     __chdir  :: (path: str) => false
     __getcwd :: () => ""
-}
\ No newline at end of file
+}
index 5f55a52bb8b3702362a6470a71d8c068f7f08f46..76bf84a4944465aed9fbbe18ea060fe524c7a99c 100644 (file)
@@ -428,6 +428,9 @@ typedef struct bh_file_contents {
 typedef struct bh_file_stats {
     isize size;
     u32 file_type;
+    u64 change_time;
+    u64 accessed_time;
+    u64 modified_time;
 } bh_file_stats;
 
 bh_file_error bh_file_get_standard(bh_file* file, bh_file_standard stand);
@@ -1905,6 +1908,10 @@ b32 bh_file_contents_free(bh_file_contents* contents) {
     return 1;
 }
 
+static u64 timespec_to_ms(struct timespec t) {
+    return t.tv_sec * 1000 + t.tv_nsec / 1000000;
+}
+
 b32 bh_file_stat(char const* filename, bh_file_stats* out) {
     struct stat s;
     if (stat(filename, &s) == -1) {
@@ -1912,6 +1919,9 @@ b32 bh_file_stat(char const* filename, bh_file_stats* out) {
     }
 
     out->size = s.st_size;
+    out->modified_time = timespec_to_ms(s.st_mtim);
+    out->accessed_time = timespec_to_ms(s.st_atim);
+    out->change_time   = timespec_to_ms(s.st_ctim);
 
     if ((s.st_mode & S_IFMT) == S_IFDIR) out->file_type = BH_FILE_TYPE_DIRECTORY;
     if ((s.st_mode & S_IFMT) == S_IFREG) out->file_type = BH_FILE_TYPE_FILE;