diff --git a/kernel/include/kernel/FS/RamFS/Inode.h b/kernel/include/kernel/FS/RamFS/Inode.h index 6a80925d..c3ac0e0e 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 ade4789c..b489a9ae 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 4615b3e5..f030ec12 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 1ea2f253..64285b5c 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);