summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2025-07-19 21:25:05 +0300
committerAhmad Samir <a.samirh78@gmail.com>2025-08-09 19:42:51 +0300
commit3cf10c70d9a8a48270777e8ce9b6352d2e454cc3 (patch)
treed321c92d8ca3c6f570cb125e26481cd2d1aa2cc3 /src/corelib
parent5f012d8b256475fda2f4182245336390387972fc (diff)
QDirListing: add IncludeBrokenSymlinks flag
Sometimes you may want to list broken symlinks even if ResolveSymlinks is set, e.g. a symlink to dir `current -> app-ver-1.2`, app-ver-1.2 could have been removed, and will be replaced by app-ver-1.3 at some later time. This also eases porting QDir::Filters to QDirListing::IteratorFlags. [ChangeLog][QtCore][QDirListing] Added IncludeBrokenSymlinks flag which lists broken symbolic links regardless of the state of the ResolveSymlinks flag. This provides compatibility with the QDir::Filter::System flag in QDir. Change-Id: I3280a9ebd342f74c48da2bf42dc23b7e9212accb Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/io/qdirlisting.cpp28
-rw-r--r--src/corelib/io/qdirlisting.h1
2 files changed, 20 insertions, 9 deletions
diff --git a/src/corelib/io/qdirlisting.cpp b/src/corelib/io/qdirlisting.cpp
index ce08d027a0d..363f028cb62 100644
--- a/src/corelib/io/qdirlisting.cpp
+++ b/src/corelib/io/qdirlisting.cpp
@@ -91,9 +91,15 @@
\value ResolveSymlinks
Filter symbolic links based on the type of the target of the link,
- rather than the symbolic link itself. With this flag, broken symbolic
- links (where the target doesn't exist) are excluded. This flag is
- ignored on operating systems that don't support symbolic links.
+ rather than the symbolic link itself. Broken symbolic links (where
+ the target doesn't exist) are excluded, set IncludeBrokenSymlinks
+ to include them.
+ This flag is ignored on operating systems that don't support symbolic links.
+
+ \value IncludeBrokenSymlinks [since 6.11]
+ Lists broken symbolic links, where the target doesn't exist, regardless
+ of the status of the ResolveSymlinks flag.
+ This flag is ignored on operating systems that don't support symbolic links.
\value FilesOnly
Only regular files will be listed. When combined with ResolveSymlinks,
@@ -511,14 +517,18 @@ bool QDirListingPrivate::matchesFilters(QDirEntryInfo &entryInfo) const
if (!iteratorFlags.testAnyFlag(F::IncludeHidden) && entryInfo.isHidden())
return false;
- // With ResolveSymlinks, we look at the type of the link's target,
- // and exclude broken symlinks (where the target doesn't exist).
- if (iteratorFlags.testAnyFlag(F::ResolveSymlinks)) {
+ const bool includeBrokenSymlinks = iteratorFlags.testAnyFlags(F::IncludeBrokenSymlinks);
+ if (includeBrokenSymlinks && entryInfo.isSymLink() && !entryInfo.exists())
+ return true;
+
+ if (iteratorFlags.testFlag(F::ResolveSymlinks)) {
if (entryInfo.isSymLink() && !entryInfo.exists())
+ return false; // Exclude broken symlinks; anything else will be filtered below
+ } else {
+ constexpr auto f = F::ExcludeFiles | F::ExcludeDirs | F::ExcludeOther;
+ const bool filterByTargetType = iteratorFlags.testAnyFlags(f);
+ if (filterByTargetType && entryInfo.isSymLink())
return false;
- } else if ((iteratorFlags.testAnyFlags(F::FilesOnly)
- || iteratorFlags.testAnyFlags(F::DirsOnly)) && entryInfo.isSymLink()) {
- return false; // symlink is not a file or dir
}
if (iteratorFlags.testAnyFlag(F::ExcludeOther)
diff --git a/src/corelib/io/qdirlisting.h b/src/corelib/io/qdirlisting.h
index 59cfaadffac..acd240b81f7 100644
--- a/src/corelib/io/qdirlisting.h
+++ b/src/corelib/io/qdirlisting.h
@@ -42,6 +42,7 @@ public:
CaseSensitive = 0x000100,
Recursive = 0x000400,
FollowDirSymlinks = 0x000800,
+ IncludeBrokenSymlinks = 0x001000,
};
Q_DECLARE_FLAGS(IteratorFlags, IteratorFlag)