From 9f0797047fd2cadff8f7ffa4c5672b5908d67c21 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 30 Sep 2023 20:34:08 +0300 Subject: [PATCH] Kernel/LibC: dirent now contains file type --- kernel/include/kernel/FS/RamFS/Inode.h | 1 + kernel/kernel/FS/Ext2/Inode.cpp | 1 + kernel/kernel/FS/RamFS/Inode.cpp | 23 +++++++++++++++++++++++ libc/include/dirent.h | 14 ++++++++++++-- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/kernel/include/kernel/FS/RamFS/Inode.h b/kernel/include/kernel/FS/RamFS/Inode.h index 6a80925db1..c3ac0e0e9f 100644 --- a/kernel/include/kernel/FS/RamFS/Inode.h +++ b/kernel/include/kernel/FS/RamFS/Inode.h @@ -103,6 +103,7 @@ namespace Kernel char name[m_name_max + 1]; size_t name_len = 0; ino_t ino; + uint8_t type; }; private: diff --git a/kernel/kernel/FS/Ext2/Inode.cpp b/kernel/kernel/FS/Ext2/Inode.cpp index ade4789c46..b489a9ae01 100644 --- a/kernel/kernel/FS/Ext2/Inode.cpp +++ b/kernel/kernel/FS/Ext2/Inode.cpp @@ -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'; diff --git a/kernel/kernel/FS/RamFS/Inode.cpp b/kernel/kernel/FS/RamFS/Inode.cpp index 4615b3e585..f030ec1232 100644 --- a/kernel/kernel/FS/RamFS/Inode.cpp +++ b/kernel/kernel/FS/RamFS/Inode.cpp @@ -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 RamDirectoryInode::add_inode(BAN::StringView name, BAN::RefPtr 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()) { diff --git a/libc/include/dirent.h b/libc/include/dirent.h index 1ea2f25397..64285b5c90 100644 --- a/libc/include/dirent.h +++ b/libc/include/dirent.h @@ -13,10 +13,20 @@ __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. */ - char d_name[]; /* Filename string of entry. */ + ino_t d_ino; /* File serial number. */ + unsigned char d_type; /* File type. One of DT_* definitions. */ + char d_name[]; /* Filename string of entry. */ }; int alphasort(const struct dirent** d1, const struct dirent** d2);