diff --git a/src/athena/FileWriterWin32.cpp b/src/athena/FileWriterWin32.cpp index 684927d..7e2d117 100644 --- a/src/athena/FileWriterWin32.cpp +++ b/src/athena/FileWriterWin32.cpp @@ -1,5 +1,10 @@ #include "athena/FileWriter.hpp" -#include "win32_largefilewrapper.h" + +#include +#include + +#undef min +#undef max namespace athena::io { FileWriter::FileWriter(std::string_view filename, bool overwrite, bool globalErr) @@ -96,19 +101,29 @@ atUint64 FileWriter::length() const { return utility::fileSize(m_filename); } void FileWriter::writeUBytes(const atUint8* data, atUint64 len) { if (!isOpen()) { - if (m_globalErr) + if (m_globalErr) { atError(fmt("File not open for writing")); + } setError(); return; } - DWORD ret = 0; - WriteFile(m_fileHandle, data, len, &ret, nullptr); - if (ret != len) { - if (m_globalErr) - atError(fmt("Unable to write to stream")); - setError(); - } + atUint64 remaining = len; + do { + const auto toWrite = static_cast(std::min(remaining, atUint64{std::numeric_limits::max()})); + DWORD written = 0; + + if (WriteFile(m_fileHandle, data, toWrite, &written, nullptr) == FALSE) { + if (m_globalErr) { + atError(fmt("Unable to write to file")); + } + setError(); + return; + } + + remaining -= written; + data += written; + } while (remaining != 0); } } // namespace athena::io