QSaveFile: error out in open when the file is a directory

Change-Id: Ifb1697fedf6dd28fe317282c8b4824f34ec61981
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Olivier Goffart 2014-05-29 00:39:02 +02:00 committed by The Qt Project
parent 565ee701a0
commit 523440a15f
2 changed files with 31 additions and 0 deletions

View File

@ -206,6 +206,12 @@ bool QSaveFile::open(OpenMode mode)
return false;
}
if (existingFile.isDir()) {
d->setError(QFileDevice::WriteError, QSaveFile::tr("Filename refers to a directory"));
d->writeError = QFileDevice::WriteError;
return false;
}
// Resolve symlinks. Don't use QFileInfo::canonicalFilePath so it still give the expected
// target even if the file does not exist
d->finalFileName = d->fileName;

View File

@ -94,6 +94,7 @@ private slots:
void transactionalWriteCanceled();
void transactionalWriteErrorRenaming();
void symlink();
void directory();
};
static inline QByteArray msgCannotOpen(const QFileDevice &f)
@ -454,5 +455,29 @@ void tst_QSaveFile::symlink()
#endif
}
void tst_QSaveFile::directory()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
const QString subdir = dir.path() + QLatin1String("/subdir");
QVERIFY(QDir(dir.path()).mkdir(QStringLiteral("subdir")));
{
QFile sf(subdir);
QVERIFY(!sf.open(QIODevice::WriteOnly));
}
#ifdef Q_OS_UNIX
//link to a directory
const QString linkToDir = dir.path() + QLatin1String("/linkToDir");
QVERIFY(QFile::link(subdir, linkToDir));
{
QFile sf(linkToDir);
QVERIFY(!sf.open(QIODevice::WriteOnly));
}
#endif
}
QTEST_MAIN(tst_QSaveFile)
#include "tst_qsavefile.moc"