ladybird/Userland/Libraries/LibCore/DirectoryEntry.cpp
Ali Mohammad Pur 7edc69dc94 LibCore: Ignore non-S_IFMT bits in directory_entry_type_from_stat
The st_mode field in struct stat is a bitfield of more than just the
file type, this commit masks off the non-filetype bits to ensure no
unrelated bits are checked.
2023-12-01 20:46:16 +01:00

86 lines
2.2 KiB
C++

/*
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "DirectoryEntry.h"
#include <sys/stat.h>
namespace Core {
static DirectoryEntry::Type directory_entry_type_from_stat(mode_t st_mode)
{
switch (st_mode & S_IFMT) {
case S_IFIFO:
return DirectoryEntry::Type::NamedPipe;
case S_IFCHR:
return DirectoryEntry::Type::CharacterDevice;
case S_IFDIR:
return DirectoryEntry::Type::Directory;
case S_IFBLK:
return DirectoryEntry::Type::BlockDevice;
case S_IFREG:
return DirectoryEntry::Type::File;
case S_IFLNK:
return DirectoryEntry::Type::SymbolicLink;
case S_IFSOCK:
return DirectoryEntry::Type::Socket;
default:
return DirectoryEntry::Type::Unknown;
}
VERIFY_NOT_REACHED();
}
#if !defined(AK_OS_SOLARIS) && !defined(AK_OS_HAIKU)
static DirectoryEntry::Type directory_entry_type_from_posix(unsigned char dt_constant)
{
switch (dt_constant) {
case DT_UNKNOWN:
return DirectoryEntry::Type::Unknown;
case DT_FIFO:
return DirectoryEntry::Type::NamedPipe;
case DT_CHR:
return DirectoryEntry::Type::CharacterDevice;
case DT_DIR:
return DirectoryEntry::Type::Directory;
case DT_BLK:
return DirectoryEntry::Type::BlockDevice;
case DT_REG:
return DirectoryEntry::Type::File;
case DT_LNK:
return DirectoryEntry::Type::SymbolicLink;
case DT_SOCK:
return DirectoryEntry::Type::Socket;
# ifndef AK_OS_OPENBSD
case DT_WHT:
return DirectoryEntry::Type::Whiteout;
# endif
}
VERIFY_NOT_REACHED();
}
#endif
DirectoryEntry DirectoryEntry::from_stat(DIR* d, dirent const& de)
{
struct stat statbuf;
fstat(dirfd(d), &statbuf);
return DirectoryEntry {
.type = directory_entry_type_from_stat(statbuf.st_mode),
.name = de.d_name,
.inode_number = de.d_ino,
};
}
#if !defined(AK_OS_SOLARIS) && !defined(AK_OS_HAIKU)
DirectoryEntry DirectoryEntry::from_dirent(dirent const& de)
{
return DirectoryEntry {
.type = directory_entry_type_from_posix(de.d_type),
.name = de.d_name,
.inode_number = de.d_ino,
};
}
#endif
}