Expose FileUtils.listDir to QML plugins#7442
Conversation
…ir to QML plugins
🍎 MacOS DMG universal buildsDownload a MacOS DMG universal build of this PR for testing. 🪟 Windows buildsDownload a Windows build of this PR for testing and for arm64. (Built from commit 6291819) 📱 Android buildsDownload an Android arm64 build of this PR for testing. Other Android architectures |
|
This is an experiment, I was hoping to get an APK for tests on Android. |
|
@Ipickedausername , I've restarted the Android CI, there was an unrelated issue getting a cmake package. If your code compiles you should get APKs. FYI, I would be a big -1 on unrestrained access to listing and deleting folders on people's devices and computers. I would suggest that you find a way to script your plugin around the current storage access limitations we have in place. That said, a file/directory listing function within these confines would be most welcome :) |
Yes, I was expecting that answer 🙂 |
…move PlatformUtilities::removeDir
|
Hi ! |
nirvn
left a comment
There was a problem hiding this comment.
@Ipickedausername , sorry for the delay, added some hints on how to move forward here.
| const QString appDir = QDir::cleanPath( PlatformUtilities::instance()->applicationDirectory() ); | ||
| const QString appDirParent = QDir::cleanPath( appDir + "/.." ); | ||
| const QString cleanPath = QDir::cleanPath( path ); | ||
|
|
||
| if ( !cleanPath.startsWith( appDirParent ) ) | ||
| { | ||
| qWarning() << QStringLiteral( "Security: listDir called outside allowed directories: %1" ).arg( path ); | ||
| return QStringList(); | ||
| } |
There was a problem hiding this comment.
This does not quite do it. I think what we'd want to do here is to create a isWithinApplicationDirectories function that mimics isWithinProjectDirectory.
Also, it should check for all application directories.
| if ( !dir.exists() ) | ||
| return QStringList(); |
There was a problem hiding this comment.
Styling wise, we try our best not to introduce conditional blocks without { }, even if it's a single line.
| SECTION( "ListDir" ) | ||
| { | ||
| // listDir is restricted to applicationDirectory() — path outside returns empty list | ||
| QStringList outside = FileUtils::listDir( QDir::tempPath() ); | ||
| REQUIRE( outside.isEmpty() ); | ||
|
|
||
| // Within applicationDirectory() — returns entries | ||
| QString appDir = PlatformUtilities::instance()->applicationDirectory(); | ||
| QDir dir( appDir ); | ||
| if ( dir.exists() ) | ||
| { | ||
| QStringList all = FileUtils::listDir( appDir ); | ||
| REQUIRE( all.size() >= 0 ); // valid directory | ||
| } | ||
| } |
Edit:
QField plugins run in a sandboxed QML/JS engine and only have access to APIs explicitly exposed by QField. I would like to add a way to display project archives in Imported Projects/
Changes
FileUtils — adds listDir(path, filter):
Tests — adds a ListDir section to test_fileutils.cpp verifying correct filtering, empty results for non-existent directories, and rejection of paths outside applicationDirectory()
PlatformUtilities::removeDir initially included in this PR has been dropped following feedback.
PR made with the help of LLM's