aboutsummaryrefslogtreecommitdiff
path: root/kernel/filesystem/src/vfs.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/filesystem/src/vfs.cpp')
-rw-r--r--kernel/filesystem/src/vfs.cpp77
1 files changed, 64 insertions, 13 deletions
diff --git a/kernel/filesystem/src/vfs.cpp b/kernel/filesystem/src/vfs.cpp
index 573994a..2ecf847 100644
--- a/kernel/filesystem/src/vfs.cpp
+++ b/kernel/filesystem/src/vfs.cpp
@@ -4,13 +4,17 @@
#include "devices/device.hpp"
#include "devices/storage/storage_management.hpp"
+#include "filesystem/custody.hpp"
+#include "filesystem/device_file.hpp"
#include "filesystem/ext2/ext2_filesystem.hpp"
+#include "filesystem/inode.hpp"
+#include "filesystem/inode_file.hpp"
#include "filesystem/mount.hpp"
+#include "filesystem/open_file_description.hpp"
#include <kstd/cstring>
#include <algorithm>
-#include <array>
#include <optional>
#include <string_view>
@@ -23,12 +27,11 @@ namespace filesystem
// TODO BA-FS26 @Felix better solution? while dynamic memory not available?
// TODO BA-FS26 remove when dynamic memory available;
constinit auto static root_fs = std::optional<ext2::ext2_filesystem>{};
- // TODO BA-FS26 use kstd::vector when available
- constinit auto static filesystems = std::array<std::optional<ext2::ext2_filesystem>, 1>{};
- // TODO BA-FS26 @Felix
- // TODO BA-FS26 depends on the length of ram_disk_device name
- constinit std::array<char, 10> device_mount_path = {'/', 'd', 'e', 'v', '/', 'x', 'x', 'x', 'x', '\0'};
+ // TODO BA-FS26 remove when dynamic memory available;
+ constinit auto static temp_device_file = std::optional<device_file>{};
+ // TODO BA-FS26 remove when dynamic memory available;
+ constinit auto static temp_inode_file = std::optional<inode_file>{};
} // namespace
auto vfs::init() -> void
@@ -71,19 +74,67 @@ namespace filesystem
return *active_vfs;
}
+ auto vfs::open(std::string_view path) -> std::optional<open_file_description>
+ {
+ if (auto custody = resolve_path(path))
+ {
+ auto * node = custody->get_inode();
+ if (node->is_device())
+ {
+ temp_device_file.emplace(node->backing_device());
+ temp_device_file->open();
+ return open_file_description{&*temp_device_file};
+ }
+
+ temp_inode_file.emplace(node);
+ temp_inode_file->open();
+ return open_file_description{&*temp_inode_file};
+ }
+
+ return std::nullopt;
+ }
+
auto vfs::make_device_node(devices::device * device) -> void
{
- auto device_name = device->name();
- kstd::libc::memcpy(&device_mount_path[5], device_name.data(), device_name.size());
+ if (!device)
+ {
+ kapi::system::panic("[FILESYSTEM] make_device_node called with null device.");
+ }
+
+ auto const device_name = device->name();
+
+ // TODO BA-FS26 this logic isn't needed anymore when kstd::vector available, just use push_back
+ auto const slot = std::ranges::find_if(m_device_nodes, [](auto const & entry) { return !entry.has_value(); });
+ if (slot == m_device_nodes.end())
+ {
+ kapi::system::panic("[FILESYSTEM] No free slot available for device nodes.");
+ }
+
+ slot->emplace(device_node_entry{device_name, inode{device}});
+ }
+
+ auto vfs::resolve_path(std::string_view path) -> std::optional<custody>
+ {
+ // TODO BA-FS26 implement real path resolution with mounts and directories etc.
+ // For now, just support device nodes at /dev/<device_name>.
- // TODO BA-FS26 use kstd::vector and push_back when available
- filesystems[0].emplace(ext2::ext2_filesystem{});
- if (filesystems[0]->mount(device) != 0)
+ constexpr auto device_prefix = std::string_view{"/dev/"};
+ if (path.starts_with(device_prefix))
{
- kapi::system::panic("[FILESYSTEM] Failed to mount device filesystem.");
+ auto const device_name = path.substr(device_prefix.size());
+ auto entry = std::ranges::find_if(m_device_nodes, [&](auto const & device_entry) {
+ return device_entry.has_value() && device_entry->name == device_name;
+ });
+
+ if (entry != m_device_nodes.end())
+ {
+ return custody{nullptr, &entry->value().node};
+ }
+
+ return std::nullopt;
}
- m_mounts[0] = mount{std::string_view{device_mount_path.data()}, &*filesystems[0]};
+ return std::nullopt;
}
} // namespace filesystem \ No newline at end of file