From 376626560237c263c79103930590eafa4d4dd8d0 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 29 Jun 2017 11:06:09 -0700 Subject: [PATCH] QTemporaryFile: give the engine access to the template name Instead of storing it in d->fileEntry. The QFSFileEngine methods may try to use it before the file entry is filled with the actual file name and that's no good. This change is using a reference to avoid keeping an extra QString reference and to avoid going out of sync. Change-Id: I1eba2b016de74620bfc8fffd14cca9e340e4b1e2 Reviewed-by: Kai Koehne Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qsavefile.cpp | 2 +- src/corelib/io/qtemporaryfile.cpp | 13 ++----------- src/corelib/io/qtemporaryfile_p.h | 13 ++++++++++--- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/corelib/io/qsavefile.cpp b/src/corelib/io/qsavefile.cpp index 3f45ca5f91..3f7ce8d9e9 100644 --- a/src/corelib/io/qsavefile.cpp +++ b/src/corelib/io/qsavefile.cpp @@ -231,7 +231,7 @@ bool QSaveFile::open(OpenMode mode) d->finalFileName = existingFile.filePath(); } - d->fileEngine = new QTemporaryFileEngine; + d->fileEngine = new QTemporaryFileEngine(&d->finalFileName); // if the target file exists, we'll copy its permissions below, // but until then, let's ensure the temporary file is not accessible // to a third party diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index a7fa485399..b8054c3338 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -269,13 +269,6 @@ void QTemporaryFileEngine::setFileName(const QString &file) QFSFileEngine::setFileName(file); } -void QTemporaryFileEngine::setFileTemplate(const QString &fileTemplate) -{ - Q_D(QFSFileEngine); - if (filePathIsTemplate) - d->fileEntry = QFileSystemEntry(fileTemplate); -} - bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode) { Q_D(QFSFileEngine); @@ -286,7 +279,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode) if (!filePathIsTemplate) return QFSFileEngine::open(openMode); - QString qfilename = d->fileEntry.filePath(); + QString qfilename = templateName; // Ensure there is a placeholder mask uint phPos = qfilename.length(); @@ -425,7 +418,7 @@ QTemporaryFilePrivate::~QTemporaryFilePrivate() QAbstractFileEngine *QTemporaryFilePrivate::engine() const { if (!fileEngine) { - fileEngine = new QTemporaryFileEngine; + fileEngine = new QTemporaryFileEngine(&templateName); resetFileEngine(); } return fileEngine; @@ -684,8 +677,6 @@ void QTemporaryFile::setFileTemplate(const QString &name) { Q_D(QTemporaryFile); d->templateName = name; - if (d->fileEngine) - static_cast(d->fileEngine)->setFileTemplate(name); } /*! diff --git a/src/corelib/io/qtemporaryfile_p.h b/src/corelib/io/qtemporaryfile_p.h index bf40e1627a..2db07c36bc 100644 --- a/src/corelib/io/qtemporaryfile_p.h +++ b/src/corelib/io/qtemporaryfile_p.h @@ -85,22 +85,28 @@ class QTemporaryFileEngine : public QFSFileEngine { Q_DECLARE_PRIVATE(QFSFileEngine) public: + QTemporaryFileEngine(const QString *templateName) + : templateName(*templateName) + {} + void initialize(const QString &file, quint32 mode, bool nameIsTemplate = true) { Q_D(QFSFileEngine); Q_ASSERT(!isReallyOpen()); fileMode = mode; filePathIsTemplate = filePathWasTemplate = nameIsTemplate; - d->fileEntry = QFileSystemEntry(file); - if (!filePathIsTemplate) + if (filePathIsTemplate) { + d->fileEntry.clear(); + } else { + d->fileEntry = QFileSystemEntry(file); QFSFileEngine::setFileName(file); + } } ~QTemporaryFileEngine(); bool isReallyOpen() const; void setFileName(const QString &file) override; - void setFileTemplate(const QString &fileTemplate); bool open(QIODevice::OpenMode flags) override; bool remove() override; @@ -108,6 +114,7 @@ public: bool renameOverwrite(const QString &newName) override; bool close() override; + const QString &templateName; quint32 fileMode; bool filePathIsTemplate; bool filePathWasTemplate;