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

87 lines
2.3 KiB
C++
Raw Normal View History

/***
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;
2023-07-29 08:58:02 +00:00
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 = ::CreateFileW(win32Path.c_str(),
GENERIC_WRITE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
if (!AuFS::FileExists(pathex))
{
2023-07-29 08:58:02 +00:00
SysPushErrorResourceMissing("Missing file: {}", path);
return false;
}
2023-07-29 08:58:02 +00:00
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;
}
}