diff options
Diffstat (limited to 'src/corelib/io/qtemporaryfile.cpp')
-rw-r--r-- | src/corelib/io/qtemporaryfile.cpp | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index 13f2e447e7..4e48a18d91 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -45,8 +45,8 @@ QTemporaryFileName::QTemporaryFileName(const QString &templateName) { // Ensure there is a placeholder mask QString qfilename = QDir::fromNativeSeparators(templateName); - uint phPos = qfilename.size(); - uint phLength = 0; + qsizetype phPos = qfilename.size(); + qsizetype phLength = 0; while (phPos != 0) { --phPos; @@ -70,9 +70,8 @@ QTemporaryFileName::QTemporaryFileName(const QString &templateName) qfilename.append(".XXXXXX"_L1); // "Nativify" :-) - QFileSystemEntry::NativePath filename = QFileSystemEngine::absoluteName( - QFileSystemEntry(qfilename, QFileSystemEntry::FromInternalPath())) - .nativeFilePath(); + QFileSystemEntry::NativePath filename = + QFileSystemEntry(QDir::cleanPath(qfilename)).nativeFilePath(); // Find mask in native path phPos = filename.size(); @@ -157,13 +156,13 @@ QFileSystemEntry::NativePath QTemporaryFileName::generateNext() return path; } -#ifndef QT_NO_TEMPORARYFILE +#if QT_CONFIG(temporaryfile) /*! \internal Generates a unique file path from the template \a templ and creates a new - file based based on those parameters: the \c templ.length characters in \c + file based on those parameters: the \c templ.length characters in \c templ.path starting at \c templ.pos will be replaced by a random sequence of characters. \a mode specifies the file mode bits (not used on Windows). @@ -185,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); @@ -391,6 +391,18 @@ bool QTemporaryFileEngine::renameOverwrite(const QString &newName) QFSFileEngine::close(); return ok; } +#ifdef Q_OS_WIN + if (flags & Win32NonShared) { + QFileSystemEntry newEntry(newName, QFileSystemEntry::FromInternalPath()); + bool ok = d_func()->nativeRenameOverwrite(newEntry); + QFSFileEngine::close(); + if (ok) { + // Match what QFSFileEngine::renameOverwrite() does + setFileEntry(std::move(newEntry)); + } + return ok; + } +#endif QFSFileEngine::close(); return QFSFileEngine::renameOverwrite(newName); } @@ -631,6 +643,12 @@ QTemporaryFile::QTemporaryFile() } /*! + \fn QTemporaryFile::QTemporaryFile(const std::filesystem::path &templateName, QObject *parent) + \overload + \since 6.7 +*/ + +/*! Constructs a QTemporaryFile with a template filename of \a templateName. Upon opening the temporary file this will be used to create a unique filename. @@ -713,7 +731,7 @@ QTemporaryFile::~QTemporaryFile() return true upon success and will set the fileName() to the unique filename used. - \sa fileName() + \sa fileName(), QT_USE_NODISCARD_FILE_OPEN */ /*! @@ -792,6 +810,12 @@ QString QTemporaryFile::fileTemplate() const } /*! + \fn void QTemporaryFile::setFileTemplate(const std::filesystem::path &name) + \overload + \since 6.7 +*/ + +/*! Sets the static portion of the file name to \a name. If the file template contains XXXXXX that will automatically be replaced with the unique part of the filename, otherwise a filename will be @@ -813,6 +837,12 @@ void QTemporaryFile::setFileTemplate(const QString &name) } /*! + \fn bool QTemporaryFile::rename(const std::filesystem::path &newName) + \overload + \since 6.7 +*/ + +/*! Renames the current temporary file to \a newName and returns true if it succeeded. @@ -843,7 +873,6 @@ bool QTemporaryFile::rename(const QString &newName) if (tef->rename(newName)) { unsetError(); // engine was able to handle the new name so we just reset it - tef->setFileName(newName); d->fileName = newName; return true; } @@ -860,7 +889,11 @@ bool QTemporaryFile::rename(const QString &newName) Works on the given \a fileName rather than an existing QFile object. */ - +/*! + \fn QTemporaryFile *QTemporaryFile::createNativeFile(const std::filesystem::path &fileName) + \overload + \since 6.7 +*/ /*! If \a file is not already a native file, then a QTemporaryFile is created @@ -948,7 +981,7 @@ bool QTemporaryFile::open(OpenMode flags) return false; } -#endif // QT_NO_TEMPORARYFILE +#endif // QT_CONFIG(temporaryfile) QT_END_NAMESPACE |