Merge pull request #61 from lioncash/size

FileWriterWin32: Handle large files within writeUBytes
This commit is contained in:
Phillip Stephens 2019-09-07 07:06:33 -07:00 committed by GitHub
commit 30158859f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 10 deletions

View File

@ -1,5 +1,10 @@
#include "athena/FileWriter.hpp" #include "athena/FileWriter.hpp"
#include "win32_largefilewrapper.h"
#include <algorithm>
#include <limits>
#undef min
#undef max
namespace athena::io { namespace athena::io {
FileWriter::FileWriter(std::string_view filename, bool overwrite, bool globalErr) FileWriter::FileWriter(std::string_view filename, bool overwrite, bool globalErr)
@ -89,26 +94,36 @@ atUint64 FileWriter::position() const {
LARGE_INTEGER li = {}; LARGE_INTEGER li = {};
LARGE_INTEGER res; LARGE_INTEGER res;
SetFilePointerEx(m_fileHandle, li, &res, FILE_CURRENT); SetFilePointerEx(m_fileHandle, li, &res, FILE_CURRENT);
return res.QuadPart; return static_cast<atUint64>(res.QuadPart);
} }
atUint64 FileWriter::length() const { return utility::fileSize(m_filename); } atUint64 FileWriter::length() const { return utility::fileSize(m_filename); }
void FileWriter::writeUBytes(const atUint8* data, atUint64 len) { void FileWriter::writeUBytes(const atUint8* data, atUint64 len) {
if (!isOpen()) { if (!isOpen()) {
if (m_globalErr) if (m_globalErr) {
atError(fmt("File not open for writing")); atError(fmt("File not open for writing"));
}
setError(); setError();
return; return;
} }
DWORD ret = 0; atUint64 remaining = len;
WriteFile(m_fileHandle, data, len, &ret, nullptr); do {
if (ret != len) { const auto toWrite = static_cast<DWORD>(std::min(remaining, atUint64{std::numeric_limits<DWORD>::max()}));
if (m_globalErr) DWORD written = 0;
atError(fmt("Unable to write to stream"));
setError(); 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 } // namespace athena::io