Fix QDir::entryList to work for directories that end with '.lnk'

In addition to checking the .lnk extension, check that the
the specified path is not a path to a directory.

Pick-to: 6.2
Fixes: QTBUG-85058
Change-Id: I83cef3d94c6ffa82a88f374c5b41779e88fe40b8
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Karsten Heimrich 2021-09-28 13:56:16 +02:00
parent 6ad88e7898
commit 55ab987c9a
7 changed files with 11 additions and 8 deletions

View File

@ -144,6 +144,7 @@ public:
QAbstractFileEngine::FileTime whatTime, QSystemError &error); QAbstractFileEngine::FileTime whatTime, QSystemError &error);
static QString owner(const QFileSystemEntry &entry, QAbstractFileEngine::FileOwner own); static QString owner(const QFileSystemEntry &entry, QAbstractFileEngine::FileOwner own);
static QString nativeAbsoluteFilePath(const QString &path); static QString nativeAbsoluteFilePath(const QString &path);
static bool isDirPath(const QString &path, bool *existed);
#endif #endif
//homePath, rootPath and tempPath shall return clean paths //homePath, rootPath and tempPath shall return clean paths
static QString homePath(); static QString homePath();

View File

@ -1022,8 +1022,6 @@ bool QFileSystemEngine::fillMetaData(HANDLE fHandle, QFileSystemMetaData &data,
return data.hasFlags(what); return data.hasFlags(what);
} }
static bool isDirPath(const QString &dirPath, bool *existed);
//static //static
bool QFileSystemEngine::fillMetaData(const QFileSystemEntry &entry, QFileSystemMetaData &data, bool QFileSystemEngine::fillMetaData(const QFileSystemEntry &entry, QFileSystemMetaData &data,
QFileSystemMetaData::MetaDataFlags what) QFileSystemMetaData::MetaDataFlags what)
@ -1103,7 +1101,8 @@ static inline bool rmDir(const QString &path)
return ::RemoveDirectory((wchar_t*)QFSFileEnginePrivate::longFileName(path).utf16()); return ::RemoveDirectory((wchar_t*)QFSFileEnginePrivate::longFileName(path).utf16());
} }
static bool isDirPath(const QString &dirPath, bool *existed) //static
bool QFileSystemEngine::isDirPath(const QString &dirPath, bool *existed)
{ {
QString path = dirPath; QString path = dirPath;
if (path.length() == 2 && path.at(1) == QLatin1Char(':')) if (path.length() == 2 && path.at(1) == QLatin1Char(':'))
@ -1141,7 +1140,7 @@ static bool createDirectoryWithParents(const QString &nativeName, bool shouldMkd
}; };
const auto isDir = [](const QString &nativeName) { const auto isDir = [](const QString &nativeName) {
bool exists = false; bool exists = false;
return isDirPath(nativeName, &exists) && exists; return QFileSystemEngine::isDirPath(nativeName, &exists) && exists;
}; };
// Do not try to mkdir a UNC root path or a drive letter. // Do not try to mkdir a UNC root path or a drive letter.
if (isUNCRoot(nativeName) || isDriveName(nativeName)) if (isUNCRoot(nativeName) || isDriveName(nativeName))

View File

@ -59,7 +59,7 @@ QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry, QDir::Fi
{ {
Q_UNUSED(nameFilters); Q_UNUSED(nameFilters);
Q_UNUSED(flags); Q_UNUSED(flags);
if (nativePath.endsWith(QLatin1String(".lnk"))) { if (nativePath.endsWith(u".lnk"_qs) && !QFileSystemEngine::isDirPath(dirPath, nullptr)) {
QFileSystemMetaData metaData; QFileSystemMetaData metaData;
QFileSystemEntry link = QFileSystemEngine::getLinkTarget(entry, metaData); QFileSystemEntry link = QFileSystemEngine::getLinkTarget(entry, metaData);
nativePath = link.nativeFilePath(); nativePath = link.nativeFilePath();

View File

@ -722,13 +722,16 @@ void tst_QDir::entryList_data()
<< QString("qdir.pro,qrc_qdir.cpp,tst_qdir.cpp").split(','); << QString("qdir.pro,qrc_qdir.cpp,tst_qdir.cpp").split(',');
QTest::newRow("testdir1") << (m_dataPath + "/testdir") << QStringList() QTest::newRow("testdir1") << (m_dataPath + "/testdir") << QStringList()
<< (int)(QDir::AllDirs) << (int)(QDir::NoSort) << (int)(QDir::AllDirs) << (int)(QDir::NoSort)
<< QString(".,..,dir,spaces").split(','); << QString(".,..,dir,dir.lnk,spaces").split(',');
QTest::newRow("resources1") << QString(":/tst_qdir/resources/entryList") << QStringList("*.data") QTest::newRow("resources1") << QString(":/tst_qdir/resources/entryList") << QStringList("*.data")
<< (int)(QDir::NoFilter) << (int)(QDir::NoSort) << (int)(QDir::NoFilter) << (int)(QDir::NoSort)
<< QString("file1.data,file2.data,file3.data").split(','); << QString("file1.data,file2.data,file3.data").split(',');
QTest::newRow("resources2") << QString(":/tst_qdir/resources/entryList") << QStringList("*.data") QTest::newRow("resources2") << QString(":/tst_qdir/resources/entryList") << QStringList("*.data")
<< (int)(QDir::Files) << (int)(QDir::NoSort) << (int)(QDir::Files) << (int)(QDir::NoSort)
<< QString("file1.data,file2.data,file3.data").split(','); << QString("file1.data,file2.data,file3.data").split(',');
QTest::newRow("testdir.lnk") << (m_dataPath + "/testdir/dir.lnk") << QStringList()
<< (int)(QDir::NoFilter) << (int)(QDir::NoSort)
<< QString(".,..,aaaaa.txt,subdir,subdir.lnk").split(',');
} }
void tst_QDir::entryList() void tst_QDir::entryList()
@ -1685,9 +1688,9 @@ void tst_QDir::dotAndDotDot()
{ {
QDir dir(QString((m_dataPath + "/testdir/"))); QDir dir(QString((m_dataPath + "/testdir/")));
QStringList entryList = dir.entryList(QDir::Dirs); QStringList entryList = dir.entryList(QDir::Dirs);
QCOMPARE(entryList, QStringList() << QString(".") << QString("..") << QString("dir") << QString("spaces")); QCOMPARE(entryList, QStringList({ u"."_qs, u".."_qs, u"dir"_qs, u"dir.lnk"_qs, u"spaces"_qs }));
entryList = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); entryList = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
QCOMPARE(entryList, QStringList() << QString("dir") << QString("spaces")); QCOMPARE(entryList, QStringList({ u"dir"_qs, u"dir.lnk"_qs, u"spaces"_qs }));
} }
void tst_QDir::homePath() void tst_QDir::homePath()