From 38bc95aeceea5b4edbcfaee446d94b6062530770 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 29 Jun 2017 11:22:01 -0700 Subject: QTemporaryFile: add a simpler rename() (non-virtual) override Calling the parent version is still ok, but if you call the new one you get a bit of benefit. Since we control the file name, we don't have to worry about a case-changing renaming (by choice). We also know that the file is a regular one, because we created it. [ChangeLog][Important Behavior Changes][QTemporaryFile] rename() no longer attempts to do block copying, as that usually indicates a mistake in the user's code. Instead, either create the temporary file in the same directory as the new name to be, or use QSaveFile. Change-Id: I1eba2b016de74620bfc8fffd14ccaac0cdb9fe87 Reviewed-by: David Faure Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qtemporaryfile.cpp | 32 ++++++++++++++++++++++++++++++++ src/corelib/io/qtemporaryfile.h | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index efab2a30ff..a7fa485399 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -54,6 +54,8 @@ #if defined(QT_BUILD_CORE_LIB) #include "qcoreapplication.h" +#else +#define tr(X) QString::fromLatin1(X) #endif QT_BEGIN_NAMESPACE @@ -686,6 +688,36 @@ void QTemporaryFile::setFileTemplate(const QString &name) static_cast(d->fileEngine)->setFileTemplate(name); } +/*! + \internal + + This is just a simplified version of QFile::rename() because we know a few + extra details about what kind of file we have. The documentation is hidden + from the user because QFile::rename() should be enough. +*/ +bool QTemporaryFile::rename(const QString &newName) +{ + Q_D(QTemporaryFile); + auto tef = static_cast(d->fileEngine); + if (!tef || !tef->isReallyOpen() || !tef->filePathWasTemplate) + return QFile::rename(newName); + + unsetError(); + close(); + if (error() == QFile::NoError) { + 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; + } + + d->setError(QFile::RenameError, tef->errorString()); + } + return false; +} + /*! \fn QTemporaryFile *QTemporaryFile::createLocalFile(const QString &fileName) \overload diff --git a/src/corelib/io/qtemporaryfile.h b/src/corelib/io/qtemporaryfile.h index 3dc2e75f50..7fc5a299fc 100644 --- a/src/corelib/io/qtemporaryfile.h +++ b/src/corelib/io/qtemporaryfile.h @@ -80,6 +80,10 @@ public: QString fileName() const Q_DECL_OVERRIDE; QString fileTemplate() const; void setFileTemplate(const QString &name); + + // Hides QFile::rename + bool rename(const QString &newName); + #if QT_DEPRECATED_SINCE(5,1) QT_DEPRECATED inline static QTemporaryFile *createLocalFile(const QString &fileName) { return createNativeFile(fileName); } -- cgit v1.2.3