123 lines
2.5 KiB
C++
123 lines
2.5 KiB
C++
#include "ehs/io/FileMonitor_W32.h"
|
|
#include "ehs/Log.h"
|
|
#include "ehs/UTF.h"
|
|
|
|
#include <chrono>
|
|
|
|
namespace ehs
|
|
{
|
|
FileMonitor::~FileMonitor()
|
|
{
|
|
if (!IsInitialized())
|
|
return;
|
|
|
|
if (!CloseHandle(hdl))
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to close file at file path, \"" + filePath + "\", with error #" + GetLastError() + ".");
|
|
}
|
|
|
|
FileMonitor::FileMonitor()
|
|
: hdl(INVALID_HANDLE_VALUE), time{}
|
|
{
|
|
}
|
|
|
|
FileMonitor::FileMonitor(Str_8 filePath)
|
|
: BaseFileMonitor((Str_8&&)filePath), hdl(INVALID_HANDLE_VALUE), time{}
|
|
{
|
|
FileMonitor::Initialize();
|
|
}
|
|
|
|
FileMonitor::FileMonitor(FileMonitor&& fm) noexcept
|
|
: BaseFileMonitor((BaseFileMonitor&&)fm), hdl(fm.hdl), time(fm.time)
|
|
{
|
|
fm.hdl = INVALID_HANDLE_VALUE;
|
|
fm.time = {};
|
|
}
|
|
|
|
FileMonitor::FileMonitor(const FileMonitor& fm)
|
|
: BaseFileMonitor(fm), hdl(INVALID_HANDLE_VALUE), time{}
|
|
{
|
|
FileMonitor::Initialize();
|
|
}
|
|
|
|
FileMonitor& FileMonitor::operator=(FileMonitor&& fm) noexcept
|
|
{
|
|
if (this == &fm)
|
|
return *this;
|
|
|
|
Release();
|
|
|
|
BaseFileMonitor::operator=((BaseFileMonitor&&)fm);
|
|
|
|
hdl = fm.hdl;
|
|
time = fm.time;
|
|
|
|
fm.hdl = INVALID_HANDLE_VALUE;
|
|
fm.time = {};
|
|
|
|
return *this;
|
|
}
|
|
|
|
FileMonitor& FileMonitor::operator=(const FileMonitor& fm)
|
|
{
|
|
if (this == &fm)
|
|
return *this;
|
|
|
|
Release();
|
|
|
|
BaseFileMonitor::operator=(fm);
|
|
|
|
hdl = INVALID_HANDLE_VALUE;
|
|
time = {};
|
|
|
|
Initialize();
|
|
|
|
return *this;
|
|
}
|
|
|
|
void FileMonitor::Initialize()
|
|
{
|
|
if (!IsValid() || IsInitialized())
|
|
return;
|
|
|
|
hdl = CreateFileW(UTF::To_16(filePath), GENERIC_READ, FILE_SHARE_READ,
|
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (hdl == INVALID_HANDLE_VALUE)
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to open file at file path, \"" + filePath + "\", with error #" + GetLastError() + ".");
|
|
}
|
|
|
|
void FileMonitor::Release()
|
|
{
|
|
if (!IsValid() || !IsInitialized())
|
|
return;
|
|
|
|
if (!CloseHandle(hdl))
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to close file at file path, \"" + filePath + "\", with error #" + GetLastError() + ".");
|
|
|
|
hdl = INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
UInt_8 FileMonitor::Poll()
|
|
{
|
|
UInt_8 mask = EHS_FE_NONE;
|
|
|
|
FILETIME lastWriteTime = {};
|
|
if (!GetFileTime(hdl, nullptr, nullptr, &lastWriteTime))
|
|
{
|
|
Release();
|
|
return mask;
|
|
}
|
|
|
|
if (time.dwLowDateTime == lastWriteTime.dwLowDateTime && time.dwHighDateTime == lastWriteTime.dwHighDateTime)
|
|
return mask;
|
|
|
|
time = lastWriteTime;
|
|
mask = EHS_FE_MODIFIED;
|
|
|
|
return mask;
|
|
}
|
|
|
|
bool FileMonitor::IsInitialized() const
|
|
{
|
|
return hdl != INVALID_HANDLE_VALUE;
|
|
}
|
|
} |