diff options
author | João Abecasis <joao@abecasis.name> | 2009-08-27 16:56:47 (GMT) |
---|---|---|
committer | João Abecasis <joao@abecasis.name> | 2009-08-31 17:30:58 (GMT) |
commit | 7c06af4528d7aa7186b12546c261d5d2a0a3641d (patch) | |
tree | 509c44f9b7f78c107845338d338e91a6293e8a9b /src/corelib | |
parent | 9d24605add8daabfa55e1085e71ed47b14ed7d83 (diff) | |
download | Qt-7c06af4528d7aa7186b12546c261d5d2a0a3641d.zip Qt-7c06af4528d7aa7186b12546c261d5d2a0a3641d.tar.gz Qt-7c06af4528d7aa7186b12546c261d5d2a0a3641d.tar.bz2 |
Reset QTemporaryFile's state after failed open() on Windows
This fixes a regression introduced in 4.5.2 where QTemporaryFile would
no longer attempt to generate a random name after a failed open. Under
certain situations, this led to a non-random file being left behind in
QDir::tempPath when using the fallback implementation of QFile::copy.
Avoid calling QFSFileEngine::setFileName() on a template, so as not to
process it as file name.
By consistently not calling setFileTemplate in the constructor, we also
delay allocation of the fileEngine. Changes made to that function also
keep it from unnecessarily allocating the fileEngine.
Task-number: 260165
Reviewed-by: Thiago Macieira
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/io/qtemporaryfile.cpp | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index bd039c8..47fa257 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -292,14 +292,20 @@ class QTemporaryFileEngine : public QFSFileEngine Q_DECLARE_PRIVATE(QFSFileEngine) public: QTemporaryFileEngine(const QString &file, bool fileIsTemplate = true) - : QFSFileEngine(file), filePathIsTemplate(fileIsTemplate) + : QFSFileEngine(), filePathIsTemplate(fileIsTemplate) { + Q_D(QFSFileEngine); + d->filePath = file; + + if (!filePathIsTemplate) + QFSFileEngine::setFileName(file); } ~QTemporaryFileEngine(); bool isReallyOpen(); void setFileName(const QString &file); + void setFileTemplate(const QString &fileTemplate); bool open(QIODevice::OpenMode flags); bool remove(); @@ -336,6 +342,13 @@ void QTemporaryFileEngine::setFileName(const QString &file) QFSFileEngine::setFileName(file); } +void QTemporaryFileEngine::setFileTemplate(const QString &fileTemplate) +{ + Q_D(QFSFileEngine); + if (filePathIsTemplate) + d->filePath = fileTemplate; +} + bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode) { Q_D(QFSFileEngine); @@ -382,12 +395,19 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode) return false; } + QString template_ = d->filePath; d->filePath = QString::fromLocal8Bit(filename); - filePathIsTemplate = false; d->nativeInitFileName(); - d->closeFileHandle = true; delete [] filename; - return QFSFileEngine::open(openMode); + + if (QFSFileEngine::open(openMode)) { + filePathIsTemplate = false; + return true; + } + + d->filePath = template_; + d->nativeFilePath.clear(); + return false; #endif } @@ -533,7 +553,8 @@ QTemporaryFile::QTemporaryFile() QTemporaryFile::QTemporaryFile(const QString &templateName) : QFile(*new QTemporaryFilePrivate, 0) { - setFileTemplate(templateName); + Q_D(QTemporaryFile); + d->templateName = templateName; } /*! @@ -567,7 +588,8 @@ QTemporaryFile::QTemporaryFile(QObject *parent) QTemporaryFile::QTemporaryFile(const QString &templateName, QObject *parent) : QFile(*new QTemporaryFilePrivate, parent) { - setFileTemplate(templateName); + Q_D(QTemporaryFile); + d->templateName = templateName; } #endif @@ -671,10 +693,10 @@ QString QTemporaryFile::fileTemplate() const */ void QTemporaryFile::setFileTemplate(const QString &name) { - Q_ASSERT(!isOpen()); Q_D(QTemporaryFile); - fileEngine()->setFileName(name); d->templateName = name; + if (d->fileEngine) + static_cast<QTemporaryFileEngine*>(d->fileEngine)->setFileTemplate(name); } /*! |