mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:37:36 +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:
parent
48e848a9fd
commit
7c9ca8baab
3 changed files with 16 additions and 1 deletions
|
@ -8,6 +8,8 @@
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/DirIterator.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
@ -60,6 +62,18 @@ bool DirIterator::advance_next()
|
||||||
m_next = DirectoryEntry::from_stat(m_dir, *de);
|
m_next = DirectoryEntry::from_stat(m_dir, *de);
|
||||||
#else
|
#else
|
||||||
m_next = DirectoryEntry::from_dirent(*de);
|
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
|
#endif
|
||||||
|
|
||||||
if (m_next->name.is_empty())
|
if (m_next->name.is_empty())
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
static DirectoryEntry::Type directory_entry_type_from_stat(mode_t st_mode)
|
DirectoryEntry::Type DirectoryEntry::directory_entry_type_from_stat(mode_t st_mode)
|
||||||
{
|
{
|
||||||
switch (st_mode & S_IFMT) {
|
switch (st_mode & S_IFMT) {
|
||||||
case S_IFIFO:
|
case S_IFIFO:
|
||||||
|
|
|
@ -28,6 +28,7 @@ struct DirectoryEntry {
|
||||||
ByteString name;
|
ByteString name;
|
||||||
ino_t inode_number;
|
ino_t inode_number;
|
||||||
|
|
||||||
|
static Type directory_entry_type_from_stat(mode_t st_mode);
|
||||||
static DirectoryEntry from_dirent(dirent const&);
|
static DirectoryEntry from_dirent(dirent const&);
|
||||||
static DirectoryEntry from_stat(DIR*, dirent const&);
|
static DirectoryEntry from_stat(DIR*, dirent const&);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue