mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-31 01:56:28 +01:00
Common (IO): adds ffPathGetMtime and uses it
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user