Kenrel: RamInode now implements truncate()

This commit is contained in:
Bananymous 2023-07-10 16:07:09 +03:00
parent 1a1e584cba
commit 5a5656b2d3
2 changed files with 11 additions and 10 deletions

View File

@ -33,6 +33,8 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override; 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; virtual BAN::ErrorOr<size_t> write(size_t, const void*, size_t) override;
virtual BAN::ErrorOr<void> truncate(size_t) override;
void add_link() { m_inode_info.nlink++; } void add_link() { m_inode_info.nlink++; }
protected: protected:

View File

@ -57,20 +57,19 @@ namespace Kernel
BAN::ErrorOr<size_t> RamInode::write(size_t offset, const void* buffer, size_t bytes) BAN::ErrorOr<size_t> RamInode::write(size_t offset, const void* buffer, size_t bytes)
{ {
if (offset + bytes > (size_t)size()) if (offset + bytes > (size_t)size())
{ TRY(truncate(offset + bytes));
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); memcpy(m_data.data() + offset, buffer, bytes);
return bytes; return bytes;
} }
BAN::ErrorOr<void> RamInode::truncate(size_t new_size)
{
TRY(m_data.resize(new_size, 0));
m_inode_info.size = m_data.size();
m_inode_info.blocks = BAN::Math::div_round_up<size_t>(size(), blksize());
return {};
}
/* /*
RAM DIRECTORY INODE RAM DIRECTORY INODE