[*] Unify FS write utilities

This commit is contained in:
Reece Wilson 2023-08-11 06:21:42 +01:00
parent a534375008
commit 7100c807c8
3 changed files with 75 additions and 173 deletions

View File

@ -166,126 +166,6 @@ namespace Aurora::IO::FS
return true;
}
static bool WriteFileEx(const AuString &path, const Memory::MemoryViewRead &blob, bool bCheck)
{
bool status;
size_t offset;
HANDLE fileHandle;
std::wstring win32Path;
AuString pathNormalized;
AuUInt length;
status = false;
pathNormalized = NormalizePathRet(path);
if (pathNormalized.empty())
{
return false;
}
win32Path = Locale::ConvertFromUTF8(pathNormalized);
if (win32Path.empty())
{
return false;
}
CreateDirectories(pathNormalized, true);
if (bCheck)
{
fileHandle = ::CreateFileW(win32Path.c_str(),
GENERIC_WRITE,
NULL,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
}
else
{
fileHandle = ::CreateFileW(win32Path.c_str(),
GENERIC_WRITE | GENERIC_READ,
NULL,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (fileHandle == INVALID_HANDLE_VALUE)
{
fileHandle = ::CreateFileW(win32Path.c_str(),
GENERIC_WRITE,
NULL,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
}
if (fileHandle == INVALID_HANDLE_VALUE)
{
AuThreading::ContextYield();
fileHandle = ::CreateFileW(win32Path.c_str(),
GENERIC_WRITE | GENERIC_READ,
NULL,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
}
}
if (fileHandle == INVALID_HANDLE_VALUE)
{
SysPushErrorIO("Couldn't open handle: {}", path);
return false;
}
length = blob.length;
offset = 0;
while (length)
{
DWORD written;
int blockSize = AuMin(static_cast<AuUInt64>(kFileCopyBlock), static_cast<AuUInt64>(length));
if (!::WriteFile(fileHandle, &reinterpret_cast<const char *>(blob.ptr)[offset], blockSize, &written, NULL))
{
SysPushErrorIO();
goto out;
}
if (!written)
{
SysPushErrorIO();
goto out;
}
offset += written;
length -= written;
}
status = true;
::SetEndOfFile(fileHandle);
::FlushFileBuffers(fileHandle);
out:
AuWin32CloseHandle(fileHandle);
return status;
}
AUKN_SYM bool WriteFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
return WriteFileEx(path, blob, false);
}
AUKN_SYM bool WriteNewFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
return WriteFileEx(path, blob, true);
}
AUKN_SYM bool ReadFile(const AuString &path, AuByteBuffer &buffer)
{
std::wstring win32Path;

View File

@ -212,48 +212,6 @@ namespace Aurora::IO::FS
return true;
}
static bool WriteFileEx(const AuString &path, const Memory::MemoryViewRead &blob, bool bCheck)
{
auto pFile = OpenWriteUnique(path);
SysCheckReturn(pFile, false);
if (bCheck)
{
if (pFile->GetLength())
{
SysPushErrorIO("File exists: {}", path);
return {};
}
}
AuUInt written = blob.length;
if (!pFile->Write(Memory::MemoryViewStreamRead(blob, written)))
{
SysPushErrorMem();
return false;
}
pFile->WriteEoS();
if (written != blob.length)
{
SysPushErrorIO();
return false;
}
return true;
}
AUKN_SYM bool WriteFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
return WriteFileEx(path, blob, false);
}
AUKN_SYM bool WriteNewFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
return WriteFileEx(path, blob, true);
}
AUKN_SYM bool ReadFile(const AuString &path, AuByteBuffer &buffer)
{
AuMemoryViewWrite writeView;

View File

@ -296,30 +296,94 @@ namespace Aurora::IO::FS
AUKN_SYM bool WriteNewString(const AuString &path, const AuString &str)
{
char bom[3]
AuIO::IOHandle handle;
bool bOk {};
AuUInt uLength {};
static const char bom[3]
{
'\xEF', '\xBB', '\xBF'
};
auto pStream = FS::OpenWriteUnique(path);
auto createRequest = AuIO::IIOHandle::HandleCreate::ReadWrite(path);
createRequest.bAlwaysCreateDirTree = true;
createRequest.bFailIfNonEmptyFile = true;
if (!handle->InitFromPath(createRequest))
{
return false;
}
auto pStream = FS::OpenBlockingFileStreamFromHandle(AuUnsafeRaiiToShared(handle.AsPointer()));
if (!pStream)
{
return false;
}
if (pStream->GetLength())
bOk = pStream->Write(AuMemoryViewStreamRead { bom });
bOk &= pStream->Write(AuMemoryViewStreamRead { str, uLength });
bOk &= uLength == str.length();
pStream->WriteEoS();
pStream->Flush();
return bOk;
}
AUKN_SYM bool WriteNewFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
bool bOk {};
AuUInt uLength {};
AuIO::IOHandle handle;
auto createRequest = AuIO::IIOHandle::HandleCreate::ReadWrite(path);
createRequest.bAlwaysCreateDirTree = true;
createRequest.bFailIfNonEmptyFile = true;
if (!handle->InitFromPath(createRequest))
{
SysPushErrorIO("File exists: {}", path);
return {};
return false;
}
bool ok {};
ok = pStream->Write(AuMemoryViewStreamRead { bom });
ok &= pStream->Write(AuMemoryViewStreamRead { str });
pStream->Flush();
pStream->WriteEoS();
auto pStream = FS::OpenBlockingFileStreamFromHandle(AuUnsafeRaiiToShared(handle.AsPointer()));
if (!pStream)
{
return false;
}
return ok;
bOk &= pStream->Write(AuMemoryViewStreamRead { blob, uLength });
pStream->WriteEoS();
pStream->Flush();
bOk &= uLength == blob.length;
return bOk;
}
AUKN_SYM bool WriteFile(const AuString &path, const Memory::MemoryViewRead &blob)
{
bool bOk {};
AuIO::IOHandle handle;
auto createRequest = AuIO::IIOHandle::HandleCreate::ReadWrite(path);
createRequest.bAlwaysCreateDirTree = true;
createRequest.bFailIfNonEmptyFile = false;
if (!handle->InitFromPath(createRequest))
{
return false;
}
auto pStream = FS::OpenBlockingFileStreamFromHandle(AuUnsafeRaiiToShared(handle.AsPointer()));
if (!pStream)
{
return false;
}
bOk &= pStream->Write(AuMemoryViewStreamRead { blob });
pStream->WriteEoS();
pStream->Flush();
return bOk;
}
AUKN_SYM bool NormalizePath(AuString &out, const AuString &in)