diff options
| author | marcel.braun <marcel.braun@ost.ch> | 2026-03-23 21:41:37 +0100 |
|---|---|---|
| committer | Lukas Oesch <lukasoesch20@gmail.com> | 2026-03-26 21:18:55 +0100 |
| commit | fbb4eefce7bf825b0406f6fa63de318153a3b95a (patch) | |
| tree | 6be6701f47ecfd1b63dde39f3f8da2b8d953d45e | |
| parent | 336b25458b75e28c93c0bab23ccd359042f9df41 (diff) | |
| download | teachos-fbb4eefce7bf825b0406f6fa63de318153a3b95a.tar.xz teachos-fbb4eefce7bf825b0406f6fa63de318153a3b95a.zip | |
Implement resolve_path
| -rw-r--r-- | kernel/include/kernel/filesystem/dentry.hpp | 6 | ||||
| -rw-r--r-- | kernel/include/kernel/filesystem/open_file_description.hpp | 2 | ||||
| -rw-r--r-- | kernel/include/kernel/filesystem/vfs.hpp | 2 | ||||
| -rw-r--r-- | kernel/src/filesystem/dentry.cpp | 18 | ||||
| -rw-r--r-- | kernel/src/filesystem/vfs.cpp | 46 |
5 files changed, 64 insertions, 10 deletions
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 <kstd/vector> #include <cstdint> +#include <string_view> namespace filesystem { @@ -18,11 +19,14 @@ namespace filesystem dcache_mounted = 1 << 15 }; - dentry(kstd::shared_ptr<dentry> const & parent, kstd::shared_ptr<inode> const & node); + dentry(kstd::shared_ptr<dentry> const & parent, kstd::shared_ptr<inode> const & node, std::string_view name = {}); [[nodiscard]] auto get_inode() const -> kstd::shared_ptr<inode> const &; [[nodiscard]] auto get_parent() const -> kstd::shared_ptr<dentry> const &; + auto add_child(kstd::shared_ptr<dentry> const & child) -> void; + [[nodiscard]] auto find_child(std::string_view name) const -> kstd::shared_ptr<dentry>; + 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<inode> const & inode); + explicit open_file_description(kstd::shared_ptr<inode> 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<devices::device> const & device) -> void; - [[nodiscard]] auto resolve_path(std::string_view path) -> std::optional<dentry>; + [[nodiscard]] auto resolve_path(std::string_view path) -> kstd::shared_ptr<dentry>; kstd::shared_ptr<filesystem> m_root_fs; kstd::shared_ptr<dentry> 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 <kstd/memory> +#include <algorithm> #include <cstdint> +#include <string_view> namespace filesystem { - dentry::dentry(kstd::shared_ptr<dentry> const & parent, kstd::shared_ptr<inode> const & node) - : m_parent(parent) + dentry::dentry(kstd::shared_ptr<dentry> const & parent, kstd::shared_ptr<inode> 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<dentry> const & child) -> void + { + m_children.push_back(child); + } + + auto dentry::find_child(std::string_view name) const -> kstd::shared_ptr<dentry> + { + 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<uint32_t>(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 <algorithm> #include <optional> +#include <ranges> #include <string_view> namespace filesystem @@ -42,7 +43,6 @@ namespace filesystem if (auto boot_device = storage_mgmt.determine_boot_device()) { m_root_fs = kstd::make_shared<ext2::ext2_filesystem>(); - m_root_fs->mount(boot_device); m_root_dentry = kstd::make_shared<dentry>(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_inode>(device)}); } - auto vfs::resolve_path(std::string_view path) -> std::optional<dentry> + auto vfs::resolve_path(std::string_view path) -> kstd::shared_ptr<dentry> { // TODO BA-FS26 implement real path resolution with mounts and directories etc. // For now, just support device nodes at /dev/<device_name>. @@ -148,13 +148,49 @@ namespace filesystem if (entry != m_device_nodes.end()) { - return dentry{nullptr, entry->value().node}; + return kstd::make_shared<dentry>(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<dentry>(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<kstd::shared_ptr<dentry>>{}; + // }) + // | 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 |
