Kernel/LibC: dirent now contains file type

This commit is contained in:
Bananymous 2023-09-30 20:34:08 +03:00
parent dedb2a2399
commit d883d212b1
4 changed files with 37 additions and 2 deletions

View File

@ -103,6 +103,7 @@ namespace Kernel
char name[m_name_max + 1];
size_t name_len = 0;
ino_t ino;
uint8_t type;
};
private:

View File

@ -291,6 +291,7 @@ namespace Kernel
if (entry.inode)
{
ptr->dirent.d_ino = entry.inode;
ptr->dirent.d_type = entry.file_type;
ptr->rec_len = sizeof(DirectoryEntry) + entry.name_len + 1;
memcpy(ptr->dirent.d_name, entry.name, entry.name_len);
ptr->dirent.d_name[entry.name_len] = '\0';

View File

@ -160,6 +160,7 @@ namespace Kernel
// "."
{
ptr->dirent.d_ino = ino();
ptr->dirent.d_type = DT_DIR;
ptr->rec_len = sizeof(DirectoryEntry) + 2;
strcpy(ptr->dirent.d_name, ".");
ptr = ptr->next();
@ -168,6 +169,7 @@ namespace Kernel
// ".."
{
ptr->dirent.d_ino = m_parent;
ptr->dirent.d_type = DT_DIR;
ptr->rec_len = sizeof(DirectoryEntry) + 3;
strcpy(ptr->dirent.d_name, "..");
ptr = ptr->next();
@ -176,6 +178,7 @@ namespace Kernel
for (auto& entry : m_entries)
{
ptr->dirent.d_ino = entry.ino;
ptr->dirent.d_type = entry.type;
ptr->rec_len = sizeof(DirectoryEntry) + entry.name_len + 1;
strcpy(ptr->dirent.d_name, entry.name);
ptr = ptr->next();
@ -201,6 +204,25 @@ namespace Kernel
return {};
}
static uint8_t get_type(Inode::Mode mode)
{
if (mode.ifreg())
return DT_REG;
if (mode.ifdir())
return DT_DIR;
if (mode.ifchr())
return DT_CHR;
if (mode.ifblk())
return DT_BLK;
if (mode.ififo())
return DT_FIFO;
if (mode.ifsock())
return DT_SOCK;
if (mode.iflnk())
return DT_LNK;
return DT_UNKNOWN;
}
BAN::ErrorOr<void> RamDirectoryInode::add_inode(BAN::StringView name, BAN::RefPtr<RamInode> inode)
{
if (name.size() > m_name_max)
@ -215,6 +237,7 @@ namespace Kernel
strcpy(entry.name, name.data());
entry.name_len = name.size();
entry.ino = inode->ino();
entry.type = get_type(inode->mode());
if (auto ret = m_fs.add_inode(inode); ret.is_error())
{

View File

@ -13,9 +13,19 @@ __BEGIN_DECLS
struct __DIR;
typedef struct __DIR DIR;
#define DT_UNKNOWN 0
#define DT_REG 1
#define DT_DIR 2
#define DT_CHR 3
#define DT_BLK 4
#define DT_FIFO 5
#define DT_SOCK 6
#define DT_LNK 7
struct dirent
{
ino_t d_ino; /* File serial number. */
unsigned char d_type; /* File type. One of DT_* definitions. */
char d_name[]; /* Filename string of entry. */
};