diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2022-07-10 16:25:16 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2022-07-11 14:42:27 +0200 |
commit | 092ef06e00552f0b9c4eea8ebc84b3c7b1895cba (patch) | |
tree | e796268c751fb0bbed180707da37c6275b25293f /tests | |
parent | e19bd973e3d15d62c6e959f52ae5db297b2fad7c (diff) |
Fix directory listing in manual filetest; add recursive find
- QFileInfo::fileName() is not the name of a directory, so we need
absoluteFilePath() to construct a QDir
- QDir::entryList() returns only the name suffix, not the whole path
- stop at any arbitrary depth, and add a find command which does full
recursion
Amends 04a5a74685ceae7bc288f5960b9b5cab5ed28d79
Change-Id: I9870db092125a797e8b654e98954ac611dde1ab2
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/manual/filetest/main.cpp | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/tests/manual/filetest/main.cpp b/tests/manual/filetest/main.cpp index 5a1ac4cebf..db495242db 100644 --- a/tests/manual/filetest/main.cpp +++ b/tests/manual/filetest/main.cpp @@ -16,6 +16,7 @@ static const char usage1[] = "Usage: "; static const char usage2[] =" [KEYWORD] [ARGUMENTS]\n\n" "Keywords: ls FILES list file information\n" +" find FILES list file information recursively\n" " stat FILES print detailed file information\n" " mv SOURCE TARGET rename files using QFile::rename\n" " cp SOURCE TARGET copy files using QFile::copy\n" @@ -69,7 +70,7 @@ static inline std::string permissions(const QFileInfo &fi) return result; } -static int ls(int argCount, const char **args, bool recursive = false) +static int ls(int argCount, const char **args, int depth = 0, int maxDepth = 1) { for (int i = 0 ; i < argCount; ++i) { const QFileInfo fi(QString::fromLocal8Bit(args[i])); @@ -88,13 +89,13 @@ static int ls(int argCount, const char **args, bool recursive = false) std::cout << std::endl; - if (recursive && fi.isDir()) { - QDir dir(fi.fileName()); + if (depth < maxDepth && fi.isDir()) { + QDir dir(fi.absoluteFilePath()); const QStringList entries = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot); for (const QString &s : entries) { - QByteArray encoded = QFile::encodeName(s); + QByteArray encoded = QFile::encodeName(dir.filePath(s)); const char *ptr = encoded.constData(); - ls(1, &ptr, false); + ls(1, &ptr, depth + 1, maxDepth); } } } @@ -185,7 +186,10 @@ int main(int argc, char *argv[]) QCoreApplication a(argc, argv); Q_UNUSED(a); if (argc >= 3 && !qstrcmp(argv[1], "ls")) - return ls(argc -2, const_cast<const char **>(argv + 2), true); + return ls(argc -2, const_cast<const char **>(argv + 2)); + + if (argc >= 3 && !qstrcmp(argv[1], "find")) + return ls(argc -2, const_cast<const char **>(argv + 2), 0, std::numeric_limits<int>::max()); if (argc >= 3 && !qstrcmp(argv[1], "stat")) return stat(argc -2, argv + 2); |