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

@ -34,9 +34,13 @@ FileReader::~FileReader()
}
void FileReader::open()
{
int attempt = 0;
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)
{

View File

@ -30,6 +30,9 @@ FileWriter::~FileWriter()
}
void FileWriter::open(bool overwrite)
{
int attempt = 0;
do
{
if (overwrite)
{
@ -41,6 +44,7 @@ void FileWriter::open(bool overwrite)
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)
{
@ -65,6 +69,7 @@ void FileWriter::close()
return;
}
FlushFileBuffers(m_fileHandle);
CloseHandle(m_fileHandle);
m_fileHandle = 0;
}