From 1a4cc8d57b928509a64f9679e5c0e7afaa05cb54 Mon Sep 17 00:00:00 2001 From: d3fault Date: Fri, 24 Nov 2017 16:07:53 -0700 Subject: Add QIODevice::NewOnly and QIODevice::ExistingOnly OpenMode flags When QFile::open is called with the NewOnly flag, the call will fail if the file already exists. As usual, if the file does not exist, it will be created. Like QTemporaryFile, there is a guarantee from the operating system that you are not accidentally creating a new file on top of an older file. When QFile::open is called with the ExistingOnly flag, the call will fail if the file does not exist. The ExistingOnly flag only provides new functionality when used with the WriteOnly flag. For ReadOnly it provides no change in functionality, as ReadOnly by itself already never creates. Task-number: QTBUG-52244 Change-Id: I8e3206728f245f95172c225bf297023fb078fc6d Reviewed-by: Edward Welbourne --- tests/auto/corelib/io/qfile/tst_qfile.cpp | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'tests/auto/corelib/io/qfile/tst_qfile.cpp') diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 06c3a9578f..bfb14da8b8 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -168,6 +168,8 @@ private slots: void getch(); void ungetChar(); void createFile(); + void createFileNewOnly(); + void openFileExistingOnly(); void append(); void permissions_data(); void permissions(); @@ -1211,6 +1213,48 @@ void tst_QFile::createFile() QVERIFY( QFile::exists( "createme.txt" ) ); } +void tst_QFile::createFileNewOnly() +{ + QFile::remove("createme.txt"); + QVERIFY(!QFile::exists("createme.txt")); + + QFile f("createme.txt"); + QVERIFY2(f.open(QIODevice::NewOnly), msgOpenFailed(f).constData()); + f.close(); + QVERIFY(QFile::exists("createme.txt")); + + QVERIFY(!f.open(QIODevice::NewOnly)); + QVERIFY(QFile::exists("createme.txt")); + QFile::remove("createme.txt"); +} + +void tst_QFile::openFileExistingOnly() +{ + QFile::remove("dontcreateme.txt"); + QVERIFY(!QFile::exists("dontcreateme.txt")); + + QFile f("dontcreateme.txt"); + QVERIFY(!f.open(QIODevice::ExistingOnly | QIODevice::ReadOnly)); + QVERIFY(!f.open(QIODevice::ExistingOnly | QIODevice::WriteOnly)); + QVERIFY(!f.open(QIODevice::ExistingOnly | QIODevice::ReadWrite)); + QVERIFY(!f.open(QIODevice::ExistingOnly)); + QVERIFY(!QFile::exists("dontcreateme.txt")); + + QVERIFY2(f.open(QIODevice::NewOnly), msgOpenFailed(f).constData()); + f.close(); + QVERIFY(QFile::exists("dontcreateme.txt")); + + QVERIFY2(f.open(QIODevice::ExistingOnly | QIODevice::ReadOnly), msgOpenFailed(f).constData()); + f.close(); + QVERIFY2(f.open(QIODevice::ExistingOnly | QIODevice::WriteOnly), msgOpenFailed(f).constData()); + f.close(); + QVERIFY2(f.open(QIODevice::ExistingOnly | QIODevice::ReadWrite), msgOpenFailed(f).constData()); + f.close(); + QVERIFY(!f.open(QIODevice::ExistingOnly)); + QVERIFY(QFile::exists("dontcreateme.txt")); + QFile::remove("dontcreateme.txt"); +} + void tst_QFile::append() { const QString name("appendme.txt"); -- cgit v1.2.3