forked from Bananymous/banan-os
Kernel/LibC: Rework dirent structure
dirent now contains statically sized d_name. This allows using sizeof on the name and dirent properly, which some programs seem to be using.
This commit is contained in:
parent
e77de1804f
commit
0af74fccda
|
@ -1,21 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
namespace Kernel::API
|
||||
{
|
||||
|
||||
struct DirectoryEntry
|
||||
{
|
||||
size_t rec_len { 0 };
|
||||
struct dirent dirent;
|
||||
DirectoryEntry* next() const { return (DirectoryEntry*)((uintptr_t)this + rec_len); }
|
||||
};
|
||||
|
||||
struct DirectoryEntryList
|
||||
{
|
||||
size_t entry_count { 0 };
|
||||
DirectoryEntry array[];
|
||||
};
|
||||
|
||||
}
|
|
@ -31,7 +31,7 @@ namespace Kernel
|
|||
|
||||
protected:
|
||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override;
|
||||
virtual BAN::ErrorOr<void> list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) override;
|
||||
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override;
|
||||
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
||||
virtual BAN::ErrorOr<void> create_directory_impl(BAN::StringView, mode_t, uid_t, gid_t) override;
|
||||
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override;
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
#include <BAN/Vector.h>
|
||||
#include <BAN/WeakPtr.h>
|
||||
|
||||
#include <kernel/API/DirectoryEntry.h>
|
||||
#include <kernel/Credentials.h>
|
||||
#include <kernel/Debug.h>
|
||||
#include <kernel/Lock/Mutex.h>
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
|
@ -19,8 +19,6 @@
|
|||
namespace Kernel
|
||||
{
|
||||
|
||||
using namespace API;
|
||||
|
||||
class FileBackedRegion;
|
||||
class SharedFileData;
|
||||
|
||||
|
@ -92,7 +90,7 @@ namespace Kernel
|
|||
|
||||
// Directory API
|
||||
BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode(BAN::StringView);
|
||||
BAN::ErrorOr<void> list_next_inodes(off_t, DirectoryEntryList*, size_t);
|
||||
BAN::ErrorOr<size_t> list_next_inodes(off_t, struct dirent* list, size_t list_size);
|
||||
BAN::ErrorOr<void> create_file(BAN::StringView, mode_t, uid_t, gid_t);
|
||||
BAN::ErrorOr<void> create_directory(BAN::StringView, mode_t, uid_t, gid_t);
|
||||
BAN::ErrorOr<void> unlink(BAN::StringView);
|
||||
|
@ -127,7 +125,7 @@ namespace Kernel
|
|||
|
||||
// Directory API
|
||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
|
||||
virtual BAN::ErrorOr<void> list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||
virtual BAN::ErrorOr<void> create_directory_impl(BAN::StringView, mode_t, uid_t, gid_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) { return BAN::Error::from_errno(ENOTSUP); }
|
||||
|
|
|
@ -141,7 +141,7 @@ namespace Kernel
|
|||
|
||||
protected:
|
||||
virtual BAN::ErrorOr<BAN::RefPtr<Inode>> find_inode_impl(BAN::StringView) override final;
|
||||
virtual BAN::ErrorOr<void> list_next_inodes_impl(off_t, DirectoryEntryList*, size_t) override final;
|
||||
virtual BAN::ErrorOr<size_t> list_next_inodes_impl(off_t, struct dirent*, size_t) override final;
|
||||
virtual BAN::ErrorOr<void> create_file_impl(BAN::StringView, mode_t, uid_t, gid_t) override final;
|
||||
virtual BAN::ErrorOr<void> create_directory_impl(BAN::StringView, mode_t, uid_t, gid_t) override final;
|
||||
virtual BAN::ErrorOr<void> unlink_impl(BAN::StringView) override;
|
||||
|
|
|
@ -47,7 +47,7 @@ namespace Kernel
|
|||
BAN::ErrorOr<size_t> read(int fd, BAN::ByteSpan);
|
||||
BAN::ErrorOr<size_t> write(int fd, BAN::ConstByteSpan);
|
||||
|
||||
BAN::ErrorOr<void> read_dir_entries(int fd, DirectoryEntryList* list, size_t list_size);
|
||||
BAN::ErrorOr<size_t> read_dir_entries(int fd, struct dirent* list, size_t list_len);
|
||||
|
||||
BAN::ErrorOr<BAN::StringView> path_of(int) const;
|
||||
BAN::ErrorOr<BAN::RefPtr<Inode>> inode_of(int);
|
||||
|
|
|
@ -149,7 +149,7 @@ namespace Kernel
|
|||
|
||||
BAN::ErrorOr<void> mount(BAN::StringView source, BAN::StringView target);
|
||||
|
||||
BAN::ErrorOr<long> sys_readdir(int fd, DirectoryEntryList* buffer, size_t buffer_size);
|
||||
BAN::ErrorOr<long> sys_readdir(int fd, struct dirent* list, size_t list_len);
|
||||
|
||||
BAN::ErrorOr<long> sys_mmap(const sys_mmap_t*);
|
||||
BAN::ErrorOr<long> sys_munmap(void* addr, size_t len);
|
||||
|
|
|
@ -299,16 +299,13 @@ done:
|
|||
m_fs.delete_inode(ino());
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> Ext2Inode::list_next_inodes_impl(off_t offset, DirectoryEntryList* list, size_t list_size)
|
||||
BAN::ErrorOr<size_t> Ext2Inode::list_next_inodes_impl(off_t offset, struct dirent* list, size_t list_size)
|
||||
{
|
||||
ASSERT(mode().ifdir());
|
||||
ASSERT(offset >= 0);
|
||||
|
||||
if (static_cast<BAN::make_unsigned_t<decltype(offset)>>(offset) >= max_used_data_block_count())
|
||||
{
|
||||
list->entry_count = 0;
|
||||
return {};
|
||||
}
|
||||
return 0;
|
||||
|
||||
// FIXME: can we actually assume directories have all their blocks allocated
|
||||
const uint32_t block_index = fs_block_of_data_block_index(offset).value();
|
||||
|
@ -318,26 +315,25 @@ done:
|
|||
m_fs.read_block(block_index, block_buffer);
|
||||
|
||||
// First determine if we have big enough list
|
||||
size_t entry_count = 0;
|
||||
{
|
||||
BAN::ConstByteSpan entry_span = block_buffer.span();
|
||||
|
||||
size_t needed_size = sizeof(DirectoryEntryList);
|
||||
while (entry_span.size() >= sizeof(Ext2::LinkedDirectoryEntry))
|
||||
{
|
||||
auto& entry = entry_span.as<const Ext2::LinkedDirectoryEntry>();
|
||||
if (entry.inode)
|
||||
needed_size += sizeof(DirectoryEntry) + entry.name_len + 1;
|
||||
entry_count++;
|
||||
entry_span = entry_span.slice(entry.rec_len);
|
||||
}
|
||||
|
||||
if (needed_size > list_size)
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
if (entry_count > list_size)
|
||||
return BAN::Error::from_errno(ENOBUFS);
|
||||
}
|
||||
|
||||
// Second fill the list
|
||||
{
|
||||
DirectoryEntry* ptr = list->array;
|
||||
list->entry_count = 0;
|
||||
dirent* dirp = list;
|
||||
|
||||
BAN::ConstByteSpan entry_span = block_buffer.span();
|
||||
while (entry_span.size() >= sizeof(Ext2::LinkedDirectoryEntry))
|
||||
|
@ -345,20 +341,16 @@ done:
|
|||
auto& entry = entry_span.as<const Ext2::LinkedDirectoryEntry>();
|
||||
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';
|
||||
|
||||
ptr = ptr->next();
|
||||
list->entry_count++;
|
||||
dirp->d_ino = entry.inode;
|
||||
dirp->d_type = entry.file_type;
|
||||
strncpy(dirp->d_name, entry.name, entry.name_len);
|
||||
dirp++;
|
||||
}
|
||||
entry_span = entry_span.slice(entry.rec_len);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
return entry_count;
|
||||
}
|
||||
|
||||
static bool mode_has_valid_type(mode_t mode)
|
||||
|
|
|
@ -70,7 +70,7 @@ namespace Kernel
|
|||
return find_inode_impl(name);
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> Inode::list_next_inodes(off_t offset, DirectoryEntryList* list, size_t list_len)
|
||||
BAN::ErrorOr<size_t> Inode::list_next_inodes(off_t offset, struct dirent* list, size_t list_len)
|
||||
{
|
||||
LockGuard _(m_mutex);
|
||||
if (!mode().ifdir())
|
||||
|
|
|
@ -432,9 +432,9 @@ namespace Kernel
|
|||
return BAN::RefPtr<Inode>(inode);
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> TmpDirectoryInode::list_next_inodes_impl(off_t data_block_index, DirectoryEntryList* list, size_t list_len)
|
||||
BAN::ErrorOr<size_t> TmpDirectoryInode::list_next_inodes_impl(off_t data_block_index, struct dirent* list, size_t list_len)
|
||||
{
|
||||
if (list_len < (size_t)blksize())
|
||||
if (list_len == 0)
|
||||
{
|
||||
dprintln("buffer is too small");
|
||||
return BAN::Error::from_errno(ENOBUFS);
|
||||
|
@ -442,13 +442,12 @@ namespace Kernel
|
|||
|
||||
auto block_index = this->block_index(data_block_index);
|
||||
|
||||
list->entry_count = 0;
|
||||
|
||||
// if we reach a non-allocated block, it marks the end
|
||||
if (!block_index.has_value())
|
||||
return {};
|
||||
return 0;
|
||||
|
||||
auto* dirp = list->array;
|
||||
dirent* dirp = list;
|
||||
size_t entry_count = 0;
|
||||
|
||||
const size_t byte_count = BAN::Math::min<size_t>(size() - data_block_index * blksize(), blksize());
|
||||
m_fs.with_block_buffer(block_index.value(), [&](BAN::ByteSpan bytespan) {
|
||||
|
@ -462,21 +461,21 @@ namespace Kernel
|
|||
{
|
||||
// TODO: dirents should be aligned
|
||||
|
||||
dirp->dirent.d_ino = entry.ino;
|
||||
dirp->dirent.d_type = entry.type;
|
||||
strncpy(dirp->dirent.d_name, entry.name, entry.name_len);
|
||||
dirp->dirent.d_name[entry.name_len] = '\0';
|
||||
dirp->rec_len = sizeof(DirectoryEntry) + entry.name_len + 1;
|
||||
dirp = dirp->next();
|
||||
entry_count++;
|
||||
if (entry_count > list_len)
|
||||
return;
|
||||
|
||||
list->entry_count++;
|
||||
dirp->d_ino = entry.ino;
|
||||
dirp->d_type = entry.type;
|
||||
strncpy(dirp->d_name, entry.name, entry.name_len);
|
||||
dirp++;
|
||||
}
|
||||
|
||||
bytespan = bytespan.slice(entry.rec_len);
|
||||
}
|
||||
});
|
||||
|
||||
return {};
|
||||
return entry_count;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> TmpDirectoryInode::create_file_impl(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
|
||||
|
|
|
@ -351,15 +351,13 @@ namespace Kernel
|
|||
return nwrite;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> OpenFileDescriptorSet::read_dir_entries(int fd, DirectoryEntryList* list, size_t list_size)
|
||||
BAN::ErrorOr<size_t> OpenFileDescriptorSet::read_dir_entries(int fd, struct dirent* list, size_t list_len)
|
||||
{
|
||||
TRY(validate_fd(fd));
|
||||
auto& open_file = m_open_files[fd];
|
||||
if (!(open_file->flags & O_RDONLY))
|
||||
return BAN::Error::from_errno(EACCES);
|
||||
TRY(open_file->inode->list_next_inodes(open_file->offset, list, list_size));
|
||||
open_file->offset++;
|
||||
return {};
|
||||
return TRY(open_file->inode->list_next_inodes(open_file->offset++, list, list_len));
|
||||
}
|
||||
|
||||
BAN::ErrorOr<BAN::StringView> OpenFileDescriptorSet::path_of(int fd) const
|
||||
|
|
|
@ -1220,12 +1220,11 @@ namespace Kernel
|
|||
return clean_poweroff(command);
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_readdir(int fd, DirectoryEntryList* list, size_t list_size)
|
||||
BAN::ErrorOr<long> Process::sys_readdir(int fd, struct dirent* list, size_t list_len)
|
||||
{
|
||||
LockGuard _(m_process_lock);
|
||||
TRY(validate_pointer_access(list, list_size));
|
||||
TRY(m_open_file_descriptors.read_dir_entries(fd, list, list_size));
|
||||
return 0;
|
||||
TRY(validate_pointer_access(list, sizeof(dirent) * list_len));
|
||||
return TRY(m_open_file_descriptors.read_dir_entries(fd, list, list_len));
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_setpwd(const char* path)
|
||||
|
|
|
@ -5,16 +5,14 @@
|
|||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <kernel/API/DirectoryEntry.h>
|
||||
|
||||
struct __DIR
|
||||
{
|
||||
int fd { -1 };
|
||||
size_t entry_count { 0 };
|
||||
size_t entry_index { 0 };
|
||||
Kernel::API::DirectoryEntry* current { nullptr };
|
||||
|
||||
size_t buffer_size { 0 };
|
||||
Kernel::API::DirectoryEntryList buffer[];
|
||||
// FIXME: we should probably allocate entries dynamically
|
||||
// based if syscall returns ENOBUFS
|
||||
dirent entries[128];
|
||||
};
|
||||
|
||||
int closedir(DIR* dirp)
|
||||
|
@ -26,7 +24,6 @@ int closedir(DIR* dirp)
|
|||
}
|
||||
|
||||
close(dirp->fd);
|
||||
dirp->fd = -1;
|
||||
free(dirp);
|
||||
|
||||
return 0;
|
||||
|
@ -45,13 +42,13 @@ int dirfd(DIR* dirp)
|
|||
|
||||
DIR* fdopendir(int fd)
|
||||
{
|
||||
DIR* dirp = (DIR*)malloc(sizeof(DIR) + 4096);
|
||||
DIR* dirp = (DIR*)malloc(sizeof(DIR));
|
||||
if (dirp == nullptr)
|
||||
return nullptr;
|
||||
|
||||
dirp->fd = fd;
|
||||
dirp->current = nullptr;
|
||||
dirp->buffer_size = 4096;
|
||||
dirp->entry_count = 0;
|
||||
dirp->entry_index = 0;
|
||||
|
||||
return dirp;
|
||||
}
|
||||
|
@ -73,20 +70,15 @@ struct dirent* readdir(DIR* dirp)
|
|||
}
|
||||
|
||||
dirp->entry_index++;
|
||||
if (dirp->current && dirp->entry_index < dirp->buffer->entry_count)
|
||||
{
|
||||
dirp->current = dirp->current->next();
|
||||
return &dirp->current->dirent;
|
||||
}
|
||||
if (dirp->entry_index < dirp->entry_count)
|
||||
return &dirp->entries[dirp->entry_index];
|
||||
|
||||
if (syscall(SYS_READ_DIR, dirp->fd, dirp->buffer, dirp->buffer_size) == -1)
|
||||
return nullptr;
|
||||
|
||||
if (dirp->buffer->entry_count == 0)
|
||||
long entry_count = syscall(SYS_READ_DIR, dirp->fd, dirp->entries, sizeof(dirp->entries) / sizeof(dirp->entries[0]));
|
||||
if (entry_count <= 0)
|
||||
return nullptr;
|
||||
|
||||
dirp->entry_count = entry_count;
|
||||
dirp->entry_index = 0;
|
||||
dirp->current = dirp->buffer->array;
|
||||
|
||||
return &dirp->current->dirent;
|
||||
return &dirp->entries[0];
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ __BEGIN_DECLS
|
|||
#define __need_ino_t
|
||||
#include <sys/types.h>
|
||||
|
||||
struct __DIR;
|
||||
typedef struct __DIR DIR;
|
||||
|
||||
#define DT_UNKNOWN 0
|
||||
|
@ -26,7 +25,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. */
|
||||
char d_name[256]; /* Filename string of entry. */
|
||||
};
|
||||
|
||||
int alphasort(const struct dirent** d1, const struct dirent** d2);
|
||||
|
|
Loading…
Reference in New Issue