feat: GetFileModificationTimestamp

This commit is contained in:
Kevin Trogant 2026-05-18 13:53:57 +02:00
parent 523eb30aa7
commit a0e3a0723f
2 changed files with 44 additions and 0 deletions

View File

@ -304,6 +304,11 @@ RTC_API s8 ReadEntireFileS8(s8 path, arena *a);
/* Write the given buffer to a file */ /* Write the given buffer to a file */
RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length); RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length);
/* Retrieves a timestamp representing the last time of last modification.
* The interpretation of the returned value depends on the host system.
* If the operation fails, 0 is returned. */
RTC_API i64 GetFileModificationTimestamp(s8 path);
/* Atomics */ /* Atomics */
#if !defined(RTC_NO_ATOMICS) #if !defined(RTC_NO_ATOMICS)
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
@ -461,6 +466,7 @@ RTC_API THREAD_RETURN_TYPE JoinThread(thread *t);
#ifdef __linux__ #ifdef __linux__
#include <pthread.h> #include <pthread.h>
#include <sys/stat.h>
#elif defined(_WIN32) #elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
@ -750,6 +756,35 @@ WriteEntireFile(s8 path, byte *data, isize length)
return num == 1; return num == 1;
} }
RTC_API
i64 GetFileModificationTimestamp(s8 path)
{
char _p[260];
if (path.length >= CountOf(_p))
return 0;
memcpy(_p, path.data, path.length);
_p[path.length] = '\0';
#ifdef __linux__
struct stat st;
if (stat(_p, &st) != 0)
return 0;
#if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
/* Nanosecond precision */
return (i64)st.st_mtim.tv_nsec;
#else
return (i64)st.st_mtim;
#endif
#elif defined(_WIN32)
WIN32_FILE_ATTRIBUTE_DATA data;
if (GetFileAttributesExA(_p, GetFileExInfoStandard, &data) == 0)
return 0;
ULARGE_INTEGER u;
u.u.LowPart = data.ftLastWriteTime.dwLowDateTime;
u.u.HighPart = data.ftLastWriteTime.dwHighDateTime;
return (i64)u.QuadPart;
#endif
}
/* Threading */ /* Threading */
#ifdef __linux__ #ifdef __linux__
RTC_API thread * RTC_API thread *

View File

@ -68,6 +68,14 @@ TEST(ParseIntTest)
AssertFalse(testing, S8ParseI32(S8("Not a number"), 10).ok); AssertFalse(testing, S8ParseI32(S8("Not a number"), 10).ok);
} }
internal
TEST(FileTimestampsTest)
{
NoSuite;
i64 ts = GetFileModificationTimestamp(S8("somedata.txt"));
AssertNotEqual(testing, ts, 0);
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -78,5 +86,6 @@ main(int argc, char **argv)
RegisterStandaloneTest(testing, ReadFileTest); RegisterStandaloneTest(testing, ReadFileTest);
RegisterStandaloneTest(testing, ThreadTest); RegisterStandaloneTest(testing, ThreadTest);
RegisterStandaloneTest(testing, ParseIntTest); RegisterStandaloneTest(testing, ParseIntTest);
RegisterStandaloneTest(testing, FileTimestampsTest);
return ExecuteTests(testing, argc, argv) ? 0 : 1; return ExecuteTests(testing, argc, argv) ? 0 : 1;
} }