1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:07:35 +00:00

LibCore: Fall back to fstat if readdir doesn't produce a valid file type

Per POSIX, It is valid for dirent structures obtained via readdir to
not name a valid type.
This commit is contained in:
implicitfield 2024-01-03 17:06:51 +04:00 committed by Ali Mohammad Pur
parent 48e848a9fd
commit 7c9ca8baab
3 changed files with 16 additions and 1 deletions

View file

@ -8,6 +8,8 @@
#include <AK/Vector.h>
#include <LibCore/DirIterator.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
namespace Core {
@ -60,6 +62,18 @@ bool DirIterator::advance_next()
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 (m_next->name.is_empty())