From 63b2de4e3c35c0972c8c66c8c037feb5bcb9f469 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 10 Oct 2022 19:00:24 +0200 Subject: [PATCH] Basic FDs --- kernel/include/fs/FileDescriptor.h | 35 ++++++++++++++++++++++++++++++ kernel/src/fs/FileDescriptor.cpp | 24 ++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 kernel/include/fs/FileDescriptor.h create mode 100644 kernel/src/fs/FileDescriptor.cpp diff --git a/kernel/include/fs/FileDescriptor.h b/kernel/include/fs/FileDescriptor.h new file mode 100644 index 00000000..798fa988 --- /dev/null +++ b/kernel/include/fs/FileDescriptor.h @@ -0,0 +1,35 @@ +#pragma once +#include "fs/VFS.h" +#include + +struct Descriptor +{ + bool is_open() + { + return m_is_open; + } + + bool can_read() + { + return m_can_read && m_is_open; + } + + void close() + { + m_is_open = false; + } + + ssize_t read(size_t size, char* buffer); + + void open(VFS::Node* node, bool can_read); + + Descriptor(const Descriptor& other); + Descriptor(); + + private: + uint64_t m_offset; + VFS::Node* m_node; + bool m_can_read; + + bool m_is_open; +} \ No newline at end of file diff --git a/kernel/src/fs/FileDescriptor.cpp b/kernel/src/fs/FileDescriptor.cpp new file mode 100644 index 00000000..84f91692 --- /dev/null +++ b/kernel/src/fs/FileDescriptor.cpp @@ -0,0 +1,24 @@ +#include "fs/FileDescriptor.h" + +Descriptor::Descriptor() : m_is_open(false) +{ +} + +Descriptor::Descriptor(const Descriptor& other) + : m_is_open(other.is_open()), m_can_read(other.can_read()), m_node(other.m_node), m_offset(other.m_offset) +{ +} + +void Descriptor::open(VFS::Node* node, bool can_read) +{ + m_can_read = can_read; + m_node = node; + m_offset = 0; +} + +ssize_t Descriptor::read(size_t size, char* buffer) +{ + ssize_t result = VFS::read(m_node, m_offset, size, buffer); + m_offset += result; + return result; +} \ No newline at end of file