Extract GetLastModified to Platform

This commit is contained in:
Tulio Leao 2020-11-23 00:23:07 -03:00
parent 4fe5b1dc79
commit f372117939
4 changed files with 34 additions and 24 deletions

View File

@ -121,30 +121,7 @@ namespace File
uint64_t GetLastModified(const std::string& path)
{
uint64_t lastModified = 0;
#ifdef _WIN32
auto pathW = String::ToWideChar(path);
auto hFile = CreateFileW(pathW.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (hFile != INVALID_HANDLE_VALUE)
{
FILETIME ftCreate, ftAccess, ftWrite;
if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
{
lastModified = (static_cast<uint64_t>(ftWrite.dwHighDateTime) << 32ULL)
| static_cast<uint64_t>(ftWrite.dwLowDateTime);
}
CloseHandle(hFile);
}
#else
struct stat statInfo
{
};
if (stat(path.c_str(), &statInfo) == 0)
{
lastModified = statInfo.st_mtime;
}
#endif
return lastModified;
return Platform::GetLastModified(path);
}
} // namespace File

View File

@ -18,6 +18,7 @@
# include <cstring>
# include <ctime>
# include <pwd.h>
# include <sys/stat.h>
namespace Platform
{
@ -159,6 +160,19 @@ namespace Platform
return -1;
# endif // __EMSCRIPTEN__
}
uint64_t GetLastModified(const std::string& path)
{
uint64_t lastModified = 0;
struct stat statInfo
{
};
if (stat(path.c_str(), &statInfo) == 0)
{
lastModified = statInfo.st_mtime;
}
return lastModified;
}
} // namespace Platform
#endif

View File

@ -535,6 +535,24 @@ namespace Platform
log_warning("Execute() not implemented for Windows!");
return -1;
}
uint64_t GetLastModified(const std::string& path)
{
uint64_t lastModified = 0;
auto pathW = String::ToWideChar(path);
auto hFile = CreateFileW(pathW.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (hFile != INVALID_HANDLE_VALUE)
{
FILETIME ftCreate, ftAccess, ftWrite;
if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
{
lastModified = (static_cast<uint64_t>(ftWrite.dwHighDateTime) << 32ULL)
| static_cast<uint64_t>(ftWrite.dwLowDateTime);
}
CloseHandle(hFile);
}
return lastModified;
}
} // namespace Platform
#endif

View File

@ -35,6 +35,7 @@ namespace Platform
std::string GetCurrentExecutablePath();
std::string GetCurrentExecutableDirectory();
bool FileExists(const std::string path);
uint64_t GetLastModified(const std::string& path);
rct2_time GetTimeLocal();
rct2_date GetDateLocal();
bool FindApp(const std::string& app, std::string* output);