AuroraRuntime/Source/IO/FS/FSTimes.NT.cpp

102 lines
2.8 KiB
C++

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FSTimes.NT.cpp
Date: 2023-2-4
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
#include "FSTimes.NT.hpp"
#include <Source/Time/Time.hpp>
namespace Aurora::IO::FS
{
const FILETIME *ConvertFileTime(FILETIME *temp, AuOptionalEx<AuInt64> time)
{
if (!time)
{
return nullptr;
}
auto word = AuTime::ConvertTimestampNs(time.value());
temp->dwLowDateTime = AuBitsToLower(word);
temp->dwHighDateTime = AuBitsToHigher(word);
return temp;
}
AUKN_SYM bool UpdateFileTimes(const AuString &path, const UpdateTimes &times)
{
HANDLE hFile;
FILETIME created;
FILETIME modified;
FILETIME access;
if (path.empty())
{
SysPushErrorArg("Empty path provided");
return false;
}
auto pathex = NormalizePathRet(path);
if (pathex.empty())
{
SysPushErrorMemory();
return false;
}
auto win32Path = Locale::ConvertFromUTF8(pathex);
if (win32Path.empty())
{
return false;
}
hFile = Win32Open(win32Path.c_str(),
GENERIC_WRITE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
false,
OPEN_EXISTING,
0,
0);
if (hFile == INVALID_HANDLE_VALUE)
{
if (!AuFS::FileExists(pathex))
{
SysPushErrorResourceMissing("Missing file: {}", path);
return false;
}
if (GetLastError() == ERROR_SHARING_VIOLATION)
{
RuntimeWaitForSecondaryTick();
hFile = Win32Open(win32Path.c_str(),
GENERIC_WRITE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
false,
OPEN_EXISTING,
0,
0);
}
}
if (hFile == INVALID_HANDLE_VALUE)
{
SysPushErrorIO("Couldn't open a handle for: {}", path);
return false;
}
bool bRet = ::SetFileTime(hFile,
ConvertFileTime(&created, times.createdNs),
ConvertFileTime(&access, times.accessedNs),
ConvertFileTime(&modified, times.modifiedNs));
if (!bRet)
{
SysPushErrorIO();
}
AuWin32CloseHandle(hFile);
return bRet;
}
}