aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/mount_table.cpp
diff options
context:
space:
mode:
authorMarcel Braun <marcel.braun@ost.ch>2026-04-12 19:15:38 +0200
committerMarcel Braun <marcel.braun@ost.ch>2026-04-12 19:15:38 +0200
commit4d2a1d028f8ba28b655026b93124e71a12562619 (patch)
treef49deef4dd3e8728fd1000b04c0908966f37663f /kernel/src/filesystem/mount_table.cpp
parent21fd1281cf19572e202d583689b99c33ec68da50 (diff)
parentcb7edbe6d4454ee5b217b522f62f4a7b92475a32 (diff)
downloadteachos-develop-BA-FS26.tar.xz
teachos-develop-BA-FS26.zip
Merge branch 'ext2' into 'develop-BA-FS26'HEADdevelop-BA-FS26
ext2 and tests See merge request teachos/kernel!22
Diffstat (limited to 'kernel/src/filesystem/mount_table.cpp')
-rw-r--r--kernel/src/filesystem/mount_table.cpp71
1 files changed, 69 insertions, 2 deletions
diff --git a/kernel/src/filesystem/mount_table.cpp b/kernel/src/filesystem/mount_table.cpp
index 737434e..3b1dee3 100644
--- a/kernel/src/filesystem/mount_table.cpp
+++ b/kernel/src/filesystem/mount_table.cpp
@@ -1,17 +1,83 @@
#include "kernel/filesystem/mount_table.hpp"
+#include "kernel/filesystem/dentry.hpp"
#include "kernel/filesystem/mount.hpp"
#include <kstd/memory>
+#include <kstd/vector>
+#include <algorithm>
#include <cstddef>
+#include <ranges>
#include <string_view>
namespace kernel::filesystem
{
- void mount_table::add_mount(kstd::shared_ptr<mount> mount)
+ namespace
+ {
+ auto is_descendant_of(kstd::shared_ptr<mount> const & candidate, kstd::shared_ptr<mount> const & ancestor) -> bool
+ {
+ for (auto current = candidate; current; current = current->get_parent_mount())
+ {
+ if (current == ancestor)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ auto is_strict_prefix(std::string_view prefix, std::string_view path) -> bool
+ {
+ return prefix != "/" && path.starts_with(prefix) && path.size() > prefix.size() && path[prefix.size()] == '/';
+ }
+
+ auto is_visible_mount(kstd::shared_ptr<mount> const & candidate,
+ kstd::vector<kstd::shared_ptr<mount>> const & mounts) -> bool
+ {
+ return std::ranges::none_of(mounts, [&](auto const & other) {
+ return other != candidate && is_strict_prefix(other->get_mount_path(), candidate->get_mount_path()) &&
+ !is_descendant_of(candidate, other);
+ });
+ }
+ } // namespace
+
+ auto mount_table::has_child_mounts(kstd::shared_ptr<mount> const & parent_mount) const -> bool
+ {
+ return std::ranges::any_of(
+ m_mounts, [&parent_mount](auto const & mount) { return mount->get_parent_mount() == parent_mount; });
+ }
+
+ void mount_table::add_mount(kstd::shared_ptr<mount> const & mount)
{
m_mounts.push_back(mount);
+ if (auto mount_dentry = mount->get_mount_dentry())
+ {
+ mount_dentry->set_flag(dentry::dentry_flags::dcache_mounted);
+ }
+ }
+
+ auto mount_table::remove_mount(std::string_view path) -> operation_result
+ {
+ auto mount_it = std::ranges::find_if(std::ranges::reverse_view(m_mounts), [&](auto const & mount) {
+ return mount->get_mount_path() == path && is_visible_mount(mount, m_mounts);
+ });
+
+ if (mount_it == std::ranges::reverse_view(m_mounts).end())
+ {
+ return operation_result::mount_not_found;
+ }
+
+ auto const & mount = *mount_it;
+ if (has_child_mounts(mount))
+ {
+ return operation_result::has_child_mounts;
+ }
+
+ mount->get_mount_dentry()->unset_flag(dentry::dentry_flags::dcache_mounted);
+ m_mounts.erase(std::ranges::find(m_mounts, mount));
+ return operation_result::removed;
}
auto mount_table::find_longest_prefix_mount(std::string_view path) const -> kstd::shared_ptr<mount>
@@ -25,8 +91,9 @@ namespace kernel::filesystem
// /a/b/c should match /a/b but not /a/bb or /a/b/c/d, / should match everything
bool is_prefix = path.starts_with(mp) && (mp == "/" || path.size() == mp.size() || path[mp.size()] == '/');
+ bool visible = is_visible_mount(mount, m_mounts);
- if (is_prefix && mp.size() >= best_len)
+ if (is_prefix && visible && mp.size() >= best_len)
{
mount_with_longest_prefix = mount;
best_len = mp.size();