diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2024-04-10 18:40:14 +0200 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2024-04-12 14:59:40 +0200 |
commit | 9d8e233284aa9cd0757e1181451f4220473c71fe (patch) | |
tree | 719cadbfbbf74a8ee13f11a516e9218f058686df /src/corelib/io/qtemporaryfile.cpp | |
parent | efd6786e52639ef1ceab819265b0c33d7ef8294e (diff) |
QSaveFile[win]: Use SetFileInformationByHandle for atomic rename
With our previous setup we would CloseHandle the file handle and then
MoveFileEx the file to the final destination. This opens up the
opportunity for other processes to open the file in the meantime and
cause the MoveFileEx to fail.
With SetFileInformationByHandle we can, without closing the handle,
rename the file to the final destination.
Fixes: QTBUG-122208
Pick-to: 6.7
Change-Id: Id3c8e0b5703da280c84e466ed88287e99788d077
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qtemporaryfile.cpp')
-rw-r--r-- | src/corelib/io/qtemporaryfile.cpp | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index a99ad22223..59f52bd082 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -184,8 +184,9 @@ static bool createFileFromTemplate(NativeFileHandle &file, QTemporaryFileName &t const DWORD shareMode = (flags & QTemporaryFileEngine::Win32NonShared) ? 0u : (FILE_SHARE_READ | FILE_SHARE_WRITE); + const DWORD extraAccessFlags = (flags & QTemporaryFileEngine::Win32NonShared) ? DELETE : 0; file = CreateFile((const wchar_t *)path.constData(), - GENERIC_READ | GENERIC_WRITE, + GENERIC_READ | GENERIC_WRITE | extraAccessFlags, shareMode, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); @@ -390,6 +391,12 @@ bool QTemporaryFileEngine::renameOverwrite(const QString &newName) QFSFileEngine::close(); return ok; } +#ifdef Q_OS_WIN + if (d_func()->nativeRenameOverwrite(newName)) { + QFSFileEngine::close(); + return true; + } +#endif QFSFileEngine::close(); return QFSFileEngine::renameOverwrite(newName); } |