diff options
-rw-r--r-- | src/corelib/io/qfile.cpp | 2 | ||||
-rw-r--r-- | src/corelib/io/qfileinfo.cpp | 22 | ||||
-rw-r--r-- | src/corelib/io/qfileinfo.h | 1 | ||||
-rw-r--r-- | tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp | 1 | ||||
-rw-r--r-- | tests/benchmarks/corelib/io/qfileinfo/main.cpp | 14 |
5 files changed, 39 insertions, 1 deletions
diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index 3af936122e..d991e86839 100644 --- a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -429,7 +429,7 @@ QFile::exists() const bool QFile::exists(const QString &fileName) { - return QFileInfo(fileName).exists(); + return QFileInfo::exists(fileName); } /*! diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp index 93444cb47c..1d5f16c9d9 100644 --- a/src/corelib/io/qfileinfo.cpp +++ b/src/corelib/io/qfileinfo.cpp @@ -667,6 +667,28 @@ bool QFileInfo::exists() const } /*! + Returns true if the \a file exists; otherwise returns false. + + \note If \a file is a symlink that points to a non-existing + file, false is returned. + + \note Using this function is faster for than using + \c QFileInfo(file).exists() for file system access. +*/ +bool QFileInfo::exists(const QString &file) +{ + QFileSystemEntry entry(file); + QFileSystemMetaData data; + QAbstractFileEngine *engine = + QFileSystemEngine::resolveEntryAndCreateLegacyEngine(entry, data); + // Expensive fallback to non-QFileSystemEngine implementation + if (engine) + return QFileInfo(file).exists(); + QFileSystemEngine::fillMetaData(entry, data, QFileSystemMetaData::ExistsAttribute); + return data.exists(); +} + +/*! Refreshes the information about the file, i.e. reads in information from the file system the next time a cached property is fetched. diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h index 211c18d0a0..0f18261943 100644 --- a/src/corelib/io/qfileinfo.h +++ b/src/corelib/io/qfileinfo.h @@ -84,6 +84,7 @@ public: void setFile(const QFile &file); void setFile(const QDir &dir, const QString &file); bool exists() const; + static bool exists(const QString &file); void refresh(); QString filePath() const; diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index af2578ac37..d2171cc64a 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -438,6 +438,7 @@ void tst_QFileInfo::exists() QFileInfo fi(path); QCOMPARE(fi.exists(), expected); + QCOMPARE(QFileInfo::exists(path), expected); } void tst_QFileInfo::absolutePath_data() diff --git a/tests/benchmarks/corelib/io/qfileinfo/main.cpp b/tests/benchmarks/corelib/io/qfileinfo/main.cpp index 2e63795978..594e5b7478 100644 --- a/tests/benchmarks/corelib/io/qfileinfo/main.cpp +++ b/tests/benchmarks/corelib/io/qfileinfo/main.cpp @@ -52,6 +52,8 @@ class qfileinfo : public QObject { Q_OBJECT private slots: + void existsTemporary(); + void existsStatic(); #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) void symLinkTargetPerformanceLNK(); void symLinkTargetPerformanceMounpoint(); @@ -70,6 +72,18 @@ void qfileinfo::cleanupTestCase() { } +void qfileinfo::existsTemporary() +{ + QString appPath = QCoreApplication::applicationFilePath(); + QBENCHMARK { QFileInfo(appPath).exists(); } +} + +void qfileinfo::existsStatic() +{ + QString appPath = QCoreApplication::applicationFilePath(); + QBENCHMARK { QFileInfo::exists(appPath); } +} + #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) void qfileinfo::symLinkTargetPerformanceLNK() { |