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 <kai.koehne@qt.io>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
This commit is contained in:
Thiago Macieira 2017-06-29 11:06:09 -07:00
parent 38bc95aece
commit 3766265602
3 changed files with 13 additions and 15 deletions

View File

@ -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

View File

@ -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<QTemporaryFileEngine*>(d->fileEngine)->setFileTemplate(name);
}
/*!

View File

@ -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;