From 7100c807c81bd0f5d40b065df529d761eb7f5e15 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Fri, 11 Aug 2023 06:21:42 +0100 Subject: [PATCH] [*] Unify FS write utilities --- Source/IO/FS/FS.NT.cpp | 120 --------------------------------------- Source/IO/FS/FS.Unix.cpp | 42 -------------- Source/IO/FS/FS.cpp | 86 ++++++++++++++++++++++++---- 3 files changed, 75 insertions(+), 173 deletions(-) diff --git a/Source/IO/FS/FS.NT.cpp b/Source/IO/FS/FS.NT.cpp index d2cf601d..07d1d0d9 100644 --- a/Source/IO/FS/FS.NT.cpp +++ b/Source/IO/FS/FS.NT.cpp @@ -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(kFileCopyBlock), static_cast(length)); - - if (!::WriteFile(fileHandle, &reinterpret_cast(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; diff --git a/Source/IO/FS/FS.Unix.cpp b/Source/IO/FS/FS.Unix.cpp index 14fa6704..5c2181ea 100755 --- a/Source/IO/FS/FS.Unix.cpp +++ b/Source/IO/FS/FS.Unix.cpp @@ -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; diff --git a/Source/IO/FS/FS.cpp b/Source/IO/FS/FS.cpp index 0cd9541b..ba2046d0 100644 --- a/Source/IO/FS/FS.cpp +++ b/Source/IO/FS/FS.cpp @@ -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)