Common (IO): adds ffPathGetMtime and uses it

This commit is contained in:
李通洲
2026-09-14 22:21:49 +08:00
parent e3b3b5dfc6
commit 375d0c6b25
4 changed files with 47 additions and 33 deletions
+16
View File
@@ -11,6 +11,7 @@
#include <poll.h>
#else
#include <sys/select.h>
#define st_mtim st_mtimespec // `struct stat` spells it `st_mtimespec` on Apple
#endif
#if FF_HAVE_WORDEXP
@@ -331,3 +332,18 @@ FFNativeFD ffGetNullFD(void) {
bool ffRemoveFile(const char* fileName) {
return unlink(fileName) == 0;
}
uint64_t ffPathGetMtime(const char* path) {
struct stat st;
if (stat(path, &st) != 0) {
return 0;
}
// An mtime at or before the Unix epoch means the filesystem did not fill it in. Reporting it
// as-is would hand the caller a value indistinguishable from "unknown", so treat it as such.
if (st.st_mtim.tv_sec <= 0) {
return 0;
}
return (uint64_t) st.st_mtim.tv_sec * 1000ull + (uint64_t) st.st_mtim.tv_nsec / 1000000ull;
}
+17
View File
@@ -1,6 +1,7 @@
#include "fastfetch.h"
#include "common/io.h"
#include "common/strutil.h"
#include "common/time.h"
#include "common/windows/nt.h"
#include "common/windows/unicode.h"
@@ -499,3 +500,19 @@ FFNativeFD ffGetNullFD(void) {
bool ffRemoveFile(const char* fileName) {
return DeleteFileA(fileName) != FALSE;
}
uint64_t ffPathGetMtime(const char* path) {
FF_AUTO_CLOSE_FD HANDLE handle = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (handle == INVALID_HANDLE_VALUE) { // file doesn't exist or isn't accessible
return 0;
}
FILE_BASIC_INFORMATION fileInfo;
IO_STATUS_BLOCK iosb;
if (!NT_SUCCESS(NtQueryInformationFile(handle, &iosb, &fileInfo, sizeof(fileInfo), FileBasicInformation))) {
return 0;
}
return ffFileTimeToUnixMs((uint64_t) fileInfo.LastWriteTime.QuadPart);
}