QString absoluteName;
if (fileEngine.isNull()) {
- if (!dirEntry.isRelative()) {
+ if (!dirEntry.isRelative() && dirEntry.isClean()) {
absoluteDirEntry = dirEntry;
return;
}
}
}
+bool QFileSystemEntry::isClean() const
+{
+ resolveFilePath();
+ int dots = 0;
+ bool dotok = true; // checking for ".." or "." starts to relative paths
+ bool slashok = true;
+ for (QString::const_iterator iter = m_filePath.constBegin(); iter != m_filePath.constEnd(); iter++) {
+ if (*iter == QLatin1Char('/')) {
+ if (dots == 1 || dots == 2)
+ return false; // path contains "./" or "../"
+ if (!slashok)
+ return false; // path contains "//"
+ dots = 0;
+ dotok = true;
+ slashok = false;
+ } else if (dotok) {
+ slashok = true;
+ if (*iter == QLatin1Char('.')) {
+ dots++;
+ if (dots > 2)
+ dotok = false;
+ } else {
+ //path element contains a character other than '.', it's clean
+ dots = 0;
+ dotok = false;
+ }
+ }
+ }
+ return (dots != 1 && dots != 2); // clean if path doesn't end in . or ..
+}
+
QT_END_NAMESPACE
QString completeSuffix() const;
bool isAbsolute() const;
bool isRelative() const;
+ bool isClean() const;
#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
bool isDriveRoot() const;
QTest::newRow("4") << "c:/machine/share/dir1" << "c:/machine/share/dir1";
QTest::newRow("5") << "c:\\machine\\share\\dir1" << "c:/machine/share/dir1";
#endif
+ //test dirty paths are cleaned (QTBUG-19995)
+ QTest::newRow("/home/qt/.") << QDir::rootPath() + "home/qt/." << QDir::rootPath() + "home/qt";
+ QTest::newRow("/system/data/../config") << QDir::rootPath() + "system/data/../config" << QDir::rootPath() + "system/config";
+ QTest::newRow("//home//qt/") << QDir::rootPath() + "/home//qt/" << QDir::rootPath() + "home/qt";
+ QTest::newRow("foo/../bar") << "foo/../bar" << QDir::currentPath() + "/bar";
QTest::newRow("resource") << ":/prefix/foo.bar" << ":/prefix/foo.bar";
}
<< "./entrylist" << "*.cpp" << int(QDir::Name) << int(QDir::Files)
<< true;
+ QTest::newRow("QTBUG-20495") << QDir::currentPath() + "/entrylist/.." << "*.cpp" << int(QDir::Name) << int(QDir::Files)
+ << "." << "*.cpp" << int(QDir::Name) << int(QDir::Files)
+ << true;
+
+ QTest::newRow("QTBUG-20495-root") << QDir::rootPath() + "tmp/.." << "*.cpp" << int(QDir::Name) << int(QDir::Files)
+ << QDir::rootPath() << "*.cpp" << int(QDir::Name) << int(QDir::Files)
+ << true;
+
QTest::newRow("diff-filters") << SRCDIR << "*.cpp" << int(QDir::Name) << int(QDir::Files)
<< SRCDIR << "*.cpp" << int(QDir::Name) << int(QDir::Dirs)
<< false;
void absoluteOrRelative_data();
void absoluteOrRelative();
#endif
+ void isClean_data();
+ void isClean();
};
#if defined(WIN_STUFF)
}
#endif
+void tst_QFileSystemEntry::isClean_data()
+{
+ QTest::addColumn<QString>("path");
+ QTest::addColumn<bool>("isClean");
+
+ QTest::newRow("simple") << "foo" << true;
+ QTest::newRow("complex") << "/foo/bar/bz" << true;
+ QTest::newRow(".file") << "/foo/.file" << true;
+ QTest::newRow("..file") << "/foo/..file" << true;
+ QTest::newRow("...") << "/foo/.../bar" << true;
+ QTest::newRow("./") << "./" << false;
+ QTest::newRow("../") << "../" << false;
+ QTest::newRow(".") << "." << false;
+ QTest::newRow("..") << ".." << false;
+ QTest::newRow("/.") << "/." << false;
+ QTest::newRow("/..") << "/.." << false;
+ QTest::newRow("/../") << "foo/../bar" << false;
+ QTest::newRow("/./") << "foo/./bar" << false;
+ QTest::newRow("//") << "foo//bar" << false;
+}
+
+void tst_QFileSystemEntry::isClean()
+{
+ QFETCH(QString, path);
+ QFETCH(bool, isClean);
+
+ QFileSystemEntry fi(path);
+ QCOMPARE(fi.isClean(), isClean);
+}
+
QTEST_MAIN(tst_QFileSystemEntry)
#include <tst_qfilesystementry.moc>