aboutsummaryrefslogtreecommitdiff
path: root/kernel/filesystem/src
diff options
context:
space:
mode:
authorLukas Oesch <lukasoesch20@gmail.com>2026-03-07 17:04:24 +0100
committerLukas Oesch <lukasoesch20@gmail.com>2026-03-17 16:42:50 +0100
commit9ae5a230b1cd1f842c1a7c64392a0bccdba01a2d (patch)
tree28cb234452bc694521ecd06557cbb79b9ee03b66 /kernel/filesystem/src
parent2f4001cdb1f528d8a0d255d81ac3a8b9aa522fac (diff)
downloadteachos-9ae5a230b1cd1f842c1a7c64392a0bccdba01a2d.tar.xz
teachos-9ae5a230b1cd1f842c1a7c64392a0bccdba01a2d.zip
implement read and write bytes from device
Diffstat (limited to 'kernel/filesystem/src')
-rw-r--r--kernel/filesystem/src/device_file.cpp119
-rw-r--r--kernel/filesystem/src/open_file_description.cpp4
2 files changed, 123 insertions, 0 deletions
diff --git a/kernel/filesystem/src/device_file.cpp b/kernel/filesystem/src/device_file.cpp
new file mode 100644
index 0000000..a6c234c
--- /dev/null
+++ b/kernel/filesystem/src/device_file.cpp
@@ -0,0 +1,119 @@
+#include "filesystem/device_file.hpp"
+
+#include "kapi/system.hpp"
+
+#include "devices/block_device.hpp"
+#include "devices/device.hpp"
+
+#include <kstd/cstring>
+
+#include <algorithm>
+#include <array>
+#include <cstddef>
+
+namespace filesystem
+{
+ device_file::device_file(devices::device * device)
+ : m_device(device)
+ {
+ if (m_device == nullptr)
+ {
+ kapi::system::panic("[FILESYSTEM] device_file constructed with null device.");
+ }
+ }
+
+ auto device_file::open() -> void
+ {
+ // Hook point for permission checks or lazy metadata loading.
+ }
+
+ auto device_file::read(void * buffer, size_t offset, size_t size) const -> size_t
+ {
+ if (m_device->is_block_device())
+ {
+ return process_blocks(offset, size, buffer,
+ [](size_t idx, size_t off, size_t len, size_t done, devices::block_device * device,
+ std::byte * scratch, void * buffer) {
+ auto * out = static_cast<std::byte *>(buffer);
+ if (off == 0 && len == device->block_size())
+ {
+ device->read_block(idx, out + done);
+ }
+ else
+ {
+ device->read_block(idx, scratch);
+ kstd::libc::memcpy(out + done, scratch + off, len);
+ }
+ });
+ }
+ else
+ {
+ kapi::system::panic("[FILESYSTEM] device_file::read called on non-block device.");
+ }
+ }
+
+ auto device_file::write(void const * buffer, size_t offset, size_t size) -> size_t
+ {
+ if (m_device->is_block_device())
+ {
+ return process_blocks(offset, size, const_cast<void *>(buffer),
+ [](size_t idx, size_t off, size_t len, size_t done, devices::block_device * device,
+ std::byte * scratch, void * buffer) {
+ auto const * in = static_cast<std::byte const *>(buffer);
+ if (off == 0 && len == device->block_size())
+ {
+ device->write_block(idx, in + done);
+ }
+ else
+ {
+ device->read_block(idx, scratch);
+ kstd::libc::memcpy(scratch + off, in + done, len);
+ device->write_block(idx, scratch);
+ }
+ });
+ }
+ else
+ {
+ kapi::system::panic("[FILESYSTEM] device_file::write called on non-block device.");
+ }
+ }
+
+ auto device_file::process_blocks(size_t offset, size_t size, void * buffer, block_op op) const -> size_t
+ {
+ if (buffer == nullptr)
+ {
+ kapi::system::panic("[FILESYSTEM] device_file::write called with null buffer.");
+ }
+
+ if (size == 0)
+ {
+ return 0;
+ }
+
+ auto * block_dev = static_cast<devices::block_device *>(m_device);
+
+ size_t const block_size = block_dev->block_size();
+ size_t const capacity = block_dev->capacity();
+
+ if (offset >= capacity)
+ return 0;
+ size_t const total_to_process = std::min(size, capacity - offset);
+
+ std::array<std::byte, 512> scratch_buffer{}; // TODO BA-FS26 better solution than fixed scratch_buffer ??
+ auto processed = 0uz;
+
+ while (processed < total_to_process)
+ {
+ size_t const absolute_offset = offset + processed;
+ size_t const block_index = absolute_offset / block_size;
+ size_t const in_block_offset = absolute_offset % block_size;
+ size_t const chunk_size = std::min(total_to_process - processed, block_size - in_block_offset);
+
+ op(block_index, in_block_offset, chunk_size, processed, block_dev, scratch_buffer.data(), buffer);
+
+ processed += chunk_size;
+ }
+
+ return processed;
+ }
+} // namespace filesystem
diff --git a/kernel/filesystem/src/open_file_description.cpp b/kernel/filesystem/src/open_file_description.cpp
index 8c20397..1f0410c 100644
--- a/kernel/filesystem/src/open_file_description.cpp
+++ b/kernel/filesystem/src/open_file_description.cpp
@@ -6,6 +6,10 @@
namespace filesystem
{
+ open_file_description::open_file_description(file * file)
+ : m_file(file)
+ {}
+
auto open_file_description::read(void * buffer, size_t size) -> size_t
{
// TODO BA-FS26 nullptr check