Kernel/LibC: dirent now contains file type

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

View File

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

View File

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

View File

@ -160,6 +160,7 @@ namespace Kernel
// "." // "."
{ {
ptr->dirent.d_ino = ino(); ptr->dirent.d_ino = ino();
ptr->dirent.d_type = DT_DIR;
ptr->rec_len = sizeof(DirectoryEntry) + 2; ptr->rec_len = sizeof(DirectoryEntry) + 2;
strcpy(ptr->dirent.d_name, "."); strcpy(ptr->dirent.d_name, ".");
ptr = ptr->next(); ptr = ptr->next();
@ -168,6 +169,7 @@ namespace Kernel
// ".." // ".."
{ {
ptr->dirent.d_ino = m_parent; ptr->dirent.d_ino = m_parent;
ptr->dirent.d_type = DT_DIR;
ptr->rec_len = sizeof(DirectoryEntry) + 3; ptr->rec_len = sizeof(DirectoryEntry) + 3;
strcpy(ptr->dirent.d_name, ".."); strcpy(ptr->dirent.d_name, "..");
ptr = ptr->next(); ptr = ptr->next();
@ -176,6 +178,7 @@ namespace Kernel
for (auto& entry : m_entries) for (auto& entry : m_entries)
{ {
ptr->dirent.d_ino = entry.ino; ptr->dirent.d_ino = entry.ino;
ptr->dirent.d_type = entry.type;
ptr->rec_len = sizeof(DirectoryEntry) + entry.name_len + 1; ptr->rec_len = sizeof(DirectoryEntry) + entry.name_len + 1;
strcpy(ptr->dirent.d_name, entry.name); strcpy(ptr->dirent.d_name, entry.name);
ptr = ptr->next(); ptr = ptr->next();
@ -201,6 +204,25 @@ namespace Kernel
return {}; 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) BAN::ErrorOr<void> RamDirectoryInode::add_inode(BAN::StringView name, BAN::RefPtr<RamInode> inode)
{ {
if (name.size() > m_name_max) if (name.size() > m_name_max)
@ -215,6 +237,7 @@ namespace Kernel
strcpy(entry.name, name.data()); strcpy(entry.name, name.data());
entry.name_len = name.size(); entry.name_len = name.size();
entry.ino = inode->ino(); entry.ino = inode->ino();
entry.type = get_type(inode->mode());
if (auto ret = m_fs.add_inode(inode); ret.is_error()) if (auto ret = m_fs.add_inode(inode); ret.is_error())
{ {

View File

@ -13,10 +13,20 @@ __BEGIN_DECLS
struct __DIR; struct __DIR;
typedef struct __DIR 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 struct dirent
{ {
ino_t d_ino; /* File serial number. */ ino_t d_ino; /* File serial number. */
char d_name[]; /* Filename string of entry. */ 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); int alphasort(const struct dirent** d1, const struct dirent** d2);