mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-21 12:55:31 +08:00
refs #782 CAtomicFile on Windows: flush buffer before attempting to rename,
and use ReplaceFile to rename if target already exists.
This commit is contained in:
committed by
Klaus Basan
parent
52cd84dd99
commit
5035e7b5fb
@@ -21,6 +21,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#elif defined(Q_OS_WIN32)
|
#elif defined(Q_OS_WIN32)
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <io.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//! \var qt_ntfs_permission_lookup
|
//! \var qt_ntfs_permission_lookup
|
||||||
@@ -73,6 +74,10 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
if (! isOpen()) { return; }
|
if (! isOpen()) { return; }
|
||||||
|
|
||||||
|
#if defined(Q_OS_WIN32)
|
||||||
|
FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(handle())));
|
||||||
|
#endif
|
||||||
|
|
||||||
QFile::close();
|
QFile::close();
|
||||||
|
|
||||||
if (error() == NoError) { replaceOriginal(); }
|
if (error() == NoError) { replaceOriginal(); }
|
||||||
@@ -135,13 +140,16 @@ namespace BlackMisc
|
|||||||
const auto prefix = "\\\\?\\"; // support long paths: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx#maxpath
|
const auto prefix = "\\\\?\\"; // support long paths: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx#maxpath
|
||||||
return (prefix + QDir::toNativeSeparators(QDir::cleanPath(QFileInfo(s).absoluteFilePath()))).toStdWString();
|
return (prefix + QDir::toNativeSeparators(QDir::cleanPath(QFileInfo(s).absoluteFilePath()))).toStdWString();
|
||||||
};
|
};
|
||||||
auto result = MoveFileEx(encode(fileName()).c_str(), encode(m_originalFilename).c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
|
auto replace = exists(m_originalFilename);
|
||||||
|
auto result = replace
|
||||||
|
? ReplaceFile(encode(m_originalFilename).c_str(), encode(fileName()).c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr)
|
||||||
|
: MoveFileEx(encode(fileName()).c_str(), encode(m_originalFilename).c_str(), MOVEFILE_WRITE_THROUGH);
|
||||||
if (! result)
|
if (! result)
|
||||||
{
|
{
|
||||||
m_renameError = true;
|
m_renameError = true;
|
||||||
wchar_t *s = nullptr;
|
wchar_t *s = nullptr;
|
||||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), 0, reinterpret_cast<LPWSTR>(&s), 0, nullptr);
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), 0, reinterpret_cast<LPWSTR>(&s), 0, nullptr);
|
||||||
setErrorString(QString::fromWCharArray(s).simplified());
|
setErrorString((replace ? "ReplaceFile: " : "MoveFileEx: ") + QString::fromWCharArray(s).simplified());
|
||||||
LocalFree(reinterpret_cast<HLOCAL>(s));
|
LocalFree(reinterpret_cast<HLOCAL>(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ namespace BlackMisc
|
|||||||
* when it is closed, so that it overwrites the target file as a single, atomic transaction.
|
* when it is closed, so that it overwrites the target file as a single, atomic transaction.
|
||||||
*
|
*
|
||||||
* If the application crashes while data is still being written, the original file is unchanged.
|
* If the application crashes while data is still being written, the original file is unchanged.
|
||||||
|
*
|
||||||
|
* \fixme Consider using QSaveFile.
|
||||||
*/
|
*/
|
||||||
class BLACKMISC_EXPORT CAtomicFile : public QFile
|
class BLACKMISC_EXPORT CAtomicFile : public QFile
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user