Kernel: You can now read/write to RamInodes

RamFS should be stored on physical pages and not in kmalloc, but
that will be implemented later :)
This commit is contained in:
Bananymous 2023-07-10 14:09:35 +03:00
parent 3b5bc63d1b
commit 45789fda08
2 changed files with 59 additions and 7 deletions

View File

@ -30,6 +30,9 @@ namespace Kernel
virtual BAN::StringView name() const override { ASSERT_NOT_REACHED(); } virtual BAN::StringView name() const override { ASSERT_NOT_REACHED(); }
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override;
virtual BAN::ErrorOr<size_t> write(size_t, const void*, size_t) override;
void add_link() { m_inode_info.nlink++; } void add_link() { m_inode_info.nlink++; }
protected: protected:

View File

@ -43,6 +43,34 @@ namespace Kernel
m_inode_info.rdev = 0; m_inode_info.rdev = 0;
} }
BAN::ErrorOr<size_t> RamInode::read(size_t offset, void* buffer, size_t bytes)
{
if (offset >= (size_t)size())
return 0;
size_t to_copy = BAN::Math::min<size_t>(m_inode_info.size - offset, bytes);
memcpy(buffer, m_data.data(), to_copy);
return to_copy;
}
BAN::ErrorOr<size_t> RamInode::write(size_t offset, const void* buffer, size_t bytes)
{
if (offset + bytes > (size_t)size())
{
TRY(m_data.resize(offset + bytes));
if (offset > (size_t)size())
memset(m_data.data() + offset, 0, offset - size());
m_inode_info.size = m_data.size();
m_inode_info.blocks = BAN::Math::div_round_up<size_t>(size(), blksize());
}
memcpy(m_data.data() + offset, buffer, bytes);
return bytes;
}
/* /*
RAM DIRECTORY INODE RAM DIRECTORY INODE
@ -100,8 +128,6 @@ namespace Kernel
} }
} }
sizeof(Entry);
return BAN::Error::from_errno(ENOENT); return BAN::Error::from_errno(ENOENT);
} }
@ -155,11 +181,34 @@ namespace Kernel
BAN::ErrorOr<void> RamDirectoryInode::create_file(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid) BAN::ErrorOr<void> RamDirectoryInode::create_file(BAN::StringView name, mode_t mode, uid_t uid, gid_t gid)
{ {
(void)name; if (name.size() > m_name_max)
(void)mode; return BAN::Error::from_errno(ENAMETOOLONG);
(void)uid;
(void)gid; for (auto& entry : m_entries)
ASSERT_NOT_REACHED(); if (name == entry.name)
return BAN::Error::from_errno(EEXIST);
BAN::RefPtr<RamInode> inode;
if (Mode{ mode }.ifreg())
inode = TRY(RamInode::create(m_fs, mode, uid, gid));
else if (Mode{ mode }.ifdir())
inode = TRY(RamDirectoryInode::create(m_fs, ino(), mode, uid, gid));
else
ASSERT_NOT_REACHED();
TRY(m_entries.push_back({ }));
Entry& entry = m_entries.back();
strcpy(entry.name, name.data());
entry.name_len = name.size();
entry.ino = inode->ino();
if (auto ret = m_fs.add_inode(inode); ret.is_error())
{
m_entries.pop_back();
return ret.release_error();
}
return {};
} }
} }