From fbb4eefce7bf825b0406f6fa63de318153a3b95a Mon Sep 17 00:00:00 2001 From: "marcel.braun" Date: Mon, 23 Mar 2026 21:41:37 +0100 Subject: Implement resolve_path --- kernel/include/kernel/filesystem/dentry.hpp | 6 ++- .../kernel/filesystem/open_file_description.hpp | 2 +- kernel/include/kernel/filesystem/vfs.hpp | 2 +- kernel/src/filesystem/dentry.cpp | 18 ++++++++- kernel/src/filesystem/vfs.cpp | 46 +++++++++++++++++++--- 5 files changed, 64 insertions(+), 10 deletions(-) (limited to 'kernel') diff --git a/kernel/include/kernel/filesystem/dentry.hpp b/kernel/include/kernel/filesystem/dentry.hpp index db15b48..ca7ca95 100644 --- a/kernel/include/kernel/filesystem/dentry.hpp +++ b/kernel/include/kernel/filesystem/dentry.hpp @@ -8,6 +8,7 @@ #include #include +#include namespace filesystem { @@ -18,11 +19,14 @@ namespace filesystem dcache_mounted = 1 << 15 }; - dentry(kstd::shared_ptr const & parent, kstd::shared_ptr const & node); + dentry(kstd::shared_ptr const & parent, kstd::shared_ptr const & node, std::string_view name = {}); [[nodiscard]] auto get_inode() const -> kstd::shared_ptr const &; [[nodiscard]] auto get_parent() const -> kstd::shared_ptr const &; + auto add_child(kstd::shared_ptr const & child) -> void; + [[nodiscard]] auto find_child(std::string_view name) const -> kstd::shared_ptr; + auto set_flag(dentry_flags flag) -> void; auto unset_flag(dentry_flags flag) -> void; [[nodiscard]] auto has_flag(dentry_flags flag) const -> bool; diff --git a/kernel/include/kernel/filesystem/open_file_description.hpp b/kernel/include/kernel/filesystem/open_file_description.hpp index 3c97d50..e17f9fe 100644 --- a/kernel/include/kernel/filesystem/open_file_description.hpp +++ b/kernel/include/kernel/filesystem/open_file_description.hpp @@ -11,7 +11,7 @@ namespace filesystem { struct open_file_description { - open_file_description(kstd::shared_ptr const & inode); + explicit open_file_description(kstd::shared_ptr const & inode); ~open_file_description() = default; diff --git a/kernel/include/kernel/filesystem/vfs.hpp b/kernel/include/kernel/filesystem/vfs.hpp index b6d4c4c..317f2d1 100644 --- a/kernel/include/kernel/filesystem/vfs.hpp +++ b/kernel/include/kernel/filesystem/vfs.hpp @@ -39,7 +39,7 @@ namespace filesystem auto init_internal() -> void; auto make_device_node(kstd::shared_ptr const & device) -> void; - [[nodiscard]] auto resolve_path(std::string_view path) -> std::optional; + [[nodiscard]] auto resolve_path(std::string_view path) -> kstd::shared_ptr; kstd::shared_ptr m_root_fs; kstd::shared_ptr m_root_dentry; diff --git a/kernel/src/filesystem/dentry.cpp b/kernel/src/filesystem/dentry.cpp index e498b52..76949f2 100644 --- a/kernel/src/filesystem/dentry.cpp +++ b/kernel/src/filesystem/dentry.cpp @@ -6,12 +6,15 @@ #include +#include #include +#include namespace filesystem { - dentry::dentry(kstd::shared_ptr const & parent, kstd::shared_ptr const & node) - : m_parent(parent) + dentry::dentry(kstd::shared_ptr const & parent, kstd::shared_ptr const & node, std::string_view name) + : m_name(name) + , m_parent(parent) , m_inode(node) { if (!m_inode) @@ -30,6 +33,17 @@ namespace filesystem return m_parent; } + auto dentry::add_child(kstd::shared_ptr const & child) -> void + { + m_children.push_back(child); + } + + auto dentry::find_child(std::string_view name) const -> kstd::shared_ptr + { + auto it = std::ranges::find_if(m_children, [&](auto const & child) { return child->m_name == name; }); + return (it != m_children.end()) ? *it : nullptr; + } + auto dentry::set_flag(dentry_flags flag) -> void { m_flags |= static_cast(flag); diff --git a/kernel/src/filesystem/vfs.cpp b/kernel/src/filesystem/vfs.cpp index ece3080..0e8a76d 100644 --- a/kernel/src/filesystem/vfs.cpp +++ b/kernel/src/filesystem/vfs.cpp @@ -15,6 +15,7 @@ #include #include +#include #include namespace filesystem @@ -42,7 +43,6 @@ namespace filesystem if (auto boot_device = storage_mgmt.determine_boot_device()) { m_root_fs = kstd::make_shared(); - m_root_fs->mount(boot_device); m_root_dentry = kstd::make_shared(nullptr, m_root_fs->root_inode()); @@ -131,7 +131,7 @@ namespace filesystem m_device_nodes.push_back(device_node_entry{device->name().view(), kstd::make_shared(device)}); } - auto vfs::resolve_path(std::string_view path) -> std::optional + auto vfs::resolve_path(std::string_view path) -> kstd::shared_ptr { // TODO BA-FS26 implement real path resolution with mounts and directories etc. // For now, just support device nodes at /dev/. @@ -148,13 +148,49 @@ namespace filesystem if (entry != m_device_nodes.end()) { - return dentry{nullptr, entry->value().node}; + return kstd::make_shared(nullptr, entry->value().node); } - return std::nullopt; + return nullptr; } + else if (!path.empty() && path.front() == '/') + { + auto path_parts = + std::views::split(path, '/') | std::views::filter([](auto const & part) { return !part.empty(); }); + auto parent = m_root_dentry; - return std::nullopt; + for (auto const & part : path_parts) + { + if (auto child = parent->find_child(std::string_view{part})) + { + parent = child; + } + else if (auto found_inode = m_root_fs->lookup(parent->get_inode(), std::string_view{part})) + { + auto next_dentry = kstd::make_shared(parent, found_inode, std::string_view{part}); + parent->add_child(next_dentry); + parent = next_dentry; + } + else + { + return nullptr; + } + } + // | std::views::transform([this](auto const & part) { + // // TODO BA-FS26 implement real path resolution with mounts and directories etc. + // // For now, just return null for any non-device-node path. + // return std::optional>{}; + // }) + // | std::views::filter([](auto const & opt) { return opt.has_value(); }) + // | std::views::transform([](auto const & opt) { return opt.value(); }) + // | std::ranges::find_if(m_mount_table, [&](auto const & mount) { + // // TODO BA-FS26 implement real path resolution with mounts and directories etc. + // // For now, just check if the first path component matches a mount point. + // return part == mount.path(); + // }); + } + + return nullptr; } } // namespace filesystem \ No newline at end of file -- cgit v1.2.3