mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 20:45:14 +00:00
LibCore: Avoid running fstatat on skipped entries in DirIterator
Fixes #22630
This commit is contained in:
parent
f4ee9a2333
commit
20aa56891a
Notes:
sideshowbarker
2024-07-16 22:17:03 +09:00
Author: https://github.com/implicitfield Commit: https://github.com/SerenityOS/serenity/commit/20aa56891a Pull-request: https://github.com/SerenityOS/serenity/pull/22632 Issue: https://github.com/SerenityOS/serenity/issues/22630 Reviewed-by: https://github.com/ADKaster ✅
1 changed files with 25 additions and 17 deletions
|
@ -41,6 +41,13 @@ DirIterator::DirIterator(DirIterator&& other)
|
|||
other.m_dir = nullptr;
|
||||
}
|
||||
|
||||
static constexpr bool dirent_has_d_type =
|
||||
#if defined(AK_OS_SOLARIS) || defined(AK_OS_HAIKU)
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
#endif
|
||||
|
||||
bool DirIterator::advance_next()
|
||||
{
|
||||
if (!m_dir)
|
||||
|
@ -58,23 +65,10 @@ bool DirIterator::advance_next()
|
|||
return false;
|
||||
}
|
||||
|
||||
#if defined(AK_OS_SOLARIS) || defined(AK_OS_HAIKU)
|
||||
m_next = DirectoryEntry::from_stat(m_dir, *de);
|
||||
#else
|
||||
m_next = DirectoryEntry::from_dirent(*de);
|
||||
|
||||
// dirent structures from readdir aren't guaranteed to contain valid file types,
|
||||
// as it is possible that the underlying filesystem doesn't keep track of those.
|
||||
if (m_next->type == DirectoryEntry::Type::Unknown && !m_next->name.is_empty()) {
|
||||
struct stat statbuf;
|
||||
if (fstatat(dirfd(m_dir), de->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) < 0) {
|
||||
m_error = Error::from_errno(errno);
|
||||
dbgln("DirIteration error: {}", m_error.value());
|
||||
return false;
|
||||
}
|
||||
m_next->type = DirectoryEntry::directory_entry_type_from_stat(statbuf.st_mode);
|
||||
}
|
||||
#endif
|
||||
if constexpr (dirent_has_d_type)
|
||||
m_next = DirectoryEntry::from_dirent(*de);
|
||||
else
|
||||
m_next = DirectoryEntry::from_stat(m_dir, *de);
|
||||
|
||||
if (m_next->name.is_empty())
|
||||
return false;
|
||||
|
@ -85,6 +79,20 @@ bool DirIterator::advance_next()
|
|||
if (m_flags & Flags::SkipParentAndBaseDir && (m_next->name == "." || m_next->name == ".."))
|
||||
continue;
|
||||
|
||||
if constexpr (dirent_has_d_type) {
|
||||
// dirent structures from readdir aren't guaranteed to contain valid file types,
|
||||
// as it is possible that the underlying filesystem doesn't keep track of those.
|
||||
if (m_next->type == DirectoryEntry::Type::Unknown) {
|
||||
struct stat statbuf;
|
||||
if (fstatat(dirfd(m_dir), de->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) < 0) {
|
||||
m_error = Error::from_errno(errno);
|
||||
dbgln("DirIteration error: {}", m_error.value());
|
||||
return false;
|
||||
}
|
||||
m_next->type = DirectoryEntry::directory_entry_type_from_stat(statbuf.st_mode);
|
||||
}
|
||||
}
|
||||
|
||||
return !m_next->name.is_empty();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue