refs #581, #592, #545 CAtomicFile can support ReadWrite mode by first copying the file.

This commit is contained in:
Mathew Sutcliffe
2016-02-06 18:25:04 +00:00
parent 3bc145cc0c
commit 3175e640bb
2 changed files with 15 additions and 12 deletions

View File

@@ -22,14 +22,18 @@ namespace BlackMisc
bool CAtomicFile::open(CAtomicFile::OpenMode mode)
{
Q_ASSERT_X(!(mode & (ReadOnly | Append)), Q_FUNC_INFO, "ReadOnly and Append flags are incompatible with CAtomicFile");
m_originalFilename = fileName();
QFileInfo fileInfo(fileName());
setFileName(QFileInfo(fileInfo.dir(), ".tmp." + fileInfo.fileName() + "." + randomSuffix()).filePath());
if (exists()) { remove(); }
bool ok = QFile::open(mode);
bool ok = true;
if (mode & ReadOnly)
{
if (exists(m_originalFilename) && ! copy(m_originalFilename, fileName())) { ok = false; }
}
if (ok && ! QFile::open(mode)) { ok = false; }
if (! ok) { setFileName(m_originalFilename); }
return ok;
}