From 603516e8c0c778920c9e49c59d508aeb04b09f7c Mon Sep 17 00:00:00 2001 From: Liav A Date: Sat, 6 Jan 2024 11:38:43 +0200 Subject: [PATCH] LibCore: Add new flag for DirIterator to not use fstatat This will be useful in the upcoming listdir utility (in the next commit) to get the file type which is obtained in the get_dir_entries syscall, so it's not changed later by the fstatat syscall. This will ensure that we get the raw file type value as it's represented by directory entries from the get_dir_entries syscall. --- Userland/Libraries/LibCore/DirIterator.cpp | 4 +++- Userland/Libraries/LibCore/DirIterator.h | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibCore/DirIterator.cpp b/Userland/Libraries/LibCore/DirIterator.cpp index 84fe5a5777..7e4acb8398 100644 --- a/Userland/Libraries/LibCore/DirIterator.cpp +++ b/Userland/Libraries/LibCore/DirIterator.cpp @@ -82,7 +82,9 @@ bool DirIterator::advance_next() 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) { + // However, if we were requested to not do stat on the entries of this directory, + // the calling code will be given the raw unknown type. + if ((m_flags & Flags::NoStat) == 0 && 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); diff --git a/Userland/Libraries/LibCore/DirIterator.h b/Userland/Libraries/LibCore/DirIterator.h index bf9561e359..70c5fd41d4 100644 --- a/Userland/Libraries/LibCore/DirIterator.h +++ b/Userland/Libraries/LibCore/DirIterator.h @@ -20,6 +20,7 @@ public: NoFlags = 0x0, SkipDots = 0x1, SkipParentAndBaseDir = 0x2, + NoStat = 0x4, }; explicit DirIterator(ByteString path, Flags = Flags::NoFlags);