More aggressive failure recovery for Win32 file reader/writer

This commit is contained in:
Jack Andersen 2017-11-14 18:10:56 -10:00
parent a31882663a
commit 26a0c4b46f
2 changed files with 20 additions and 11 deletions

View File

@ -35,8 +35,12 @@ FileReader::~FileReader()
void FileReader::open() void FileReader::open()
{ {
m_fileHandle = CreateFileW(m_filename.c_str(), GENERIC_READ, FILE_SHARE_READ, int attempt = 0;
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); do
{
m_fileHandle = CreateFileW(m_filename.c_str(), GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
} while (m_fileHandle == INVALID_HANDLE_VALUE && attempt++ < 100);
if (m_fileHandle == INVALID_HANDLE_VALUE) if (m_fileHandle == INVALID_HANDLE_VALUE)
{ {

View File

@ -31,16 +31,20 @@ FileWriter::~FileWriter()
void FileWriter::open(bool overwrite) void FileWriter::open(bool overwrite)
{ {
if (overwrite) int attempt = 0;
do
{ {
m_fileHandle = CreateFileW(m_filename.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, if (overwrite)
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); {
} m_fileHandle = CreateFileW(m_filename.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE,
else nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
{ }
m_fileHandle = CreateFileW(m_filename.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, else
nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); {
} m_fileHandle = CreateFileW(m_filename.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE,
nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
}
} while (m_fileHandle == INVALID_HANDLE_VALUE && attempt++ < 100);
if (m_fileHandle == INVALID_HANDLE_VALUE) if (m_fileHandle == INVALID_HANDLE_VALUE)
{ {
@ -65,6 +69,7 @@ void FileWriter::close()
return; return;
} }
FlushFileBuffers(m_fileHandle);
CloseHandle(m_fileHandle); CloseHandle(m_fileHandle);
m_fileHandle = 0; m_fileHandle = 0;
} }