Fix QTemporaryFile::open() failing after remove().

If a QTemporaryFile is constructed using a template file path,
the path is generated in QTemporaryFileEngine::open() and then
filePathIsTemplate is set to false. If remove() and then open()
are called on the same QTemporaryFile, the path is not regenerated.
This change ensures that if the file path was generated, it will be
generated again in the scenario above.

Task-number: QTBUG-2557
Change-Id: I718ceb89daa9a9d46fdbe811fecc3d57d6dc08c2
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mitch Curtis 2012-05-30 11:55:28 +02:00 committed by Qt by Nokia
parent 872f0b94ac
commit f3f648f920
2 changed files with 29 additions and 1 deletions

View File

@ -223,7 +223,8 @@ class QTemporaryFileEngine : public QFSFileEngine
Q_DECLARE_PRIVATE(QFSFileEngine) Q_DECLARE_PRIVATE(QFSFileEngine)
public: public:
QTemporaryFileEngine(const QString &file, bool fileIsTemplate = true) QTemporaryFileEngine(const QString &file, bool fileIsTemplate = true)
: QFSFileEngine(), filePathIsTemplate(fileIsTemplate) : QFSFileEngine(), filePathIsTemplate(fileIsTemplate),
filePathWasTemplate(fileIsTemplate)
{ {
Q_D(QFSFileEngine); Q_D(QFSFileEngine);
d->fileEntry = QFileSystemEntry(file); d->fileEntry = QFileSystemEntry(file);
@ -244,6 +245,7 @@ public:
bool close(); bool close();
bool filePathIsTemplate; bool filePathIsTemplate;
bool filePathWasTemplate;
}; };
QTemporaryFileEngine::~QTemporaryFileEngine() QTemporaryFileEngine::~QTemporaryFileEngine()
@ -379,6 +381,12 @@ bool QTemporaryFileEngine::remove()
QFSFileEngine::close(); QFSFileEngine::close();
if (QFSFileEngine::remove()) { if (QFSFileEngine::remove()) {
d->fileEntry.clear(); d->fileEntry.clear();
// If a QTemporaryFile is constructed using a template file path, the path
// is generated in QTemporaryFileEngine::open() and then filePathIsTemplate
// is set to false. If remove() and then open() are called on the same
// QTemporaryFile, the path is not regenerated. Here we ensure that if the
// file path was generated, it will be generated again in the scenario above.
filePathIsTemplate = filePathWasTemplate;
return true; return true;
} }
return false; return false;

View File

@ -76,6 +76,7 @@ private slots:
void nonWritableCurrentDir(); void nonWritableCurrentDir();
void write(); void write();
void openCloseOpenClose(); void openCloseOpenClose();
void removeAndReOpen();
void size(); void size();
void resize(); void resize();
void openOnRootDrives(); void openOnRootDrives();
@ -316,6 +317,25 @@ void tst_QTemporaryFile::openCloseOpenClose()
QVERIFY(!QFile::exists(fileName)); QVERIFY(!QFile::exists(fileName));
} }
void tst_QTemporaryFile::removeAndReOpen()
{
QString fileName;
{
QTemporaryFile file;
file.open();
fileName = file.fileName();
QVERIFY(QFile::exists(fileName));
file.remove();
QVERIFY(!QFile::exists(fileName));
QVERIFY(file.open());
fileName = file.fileName();
QVERIFY(QFile::exists(fileName));
}
QVERIFY(!QFile::exists(fileName));
}
void tst_QTemporaryFile::size() void tst_QTemporaryFile::size()
{ {
QTemporaryFile file; QTemporaryFile file;