diff options
Diffstat (limited to 'kernel/src/filesystem/mount_table.cpp')
| -rw-r--r-- | kernel/src/filesystem/mount_table.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/kernel/src/filesystem/mount_table.cpp b/kernel/src/filesystem/mount_table.cpp new file mode 100644 index 0000000..e4baac7 --- /dev/null +++ b/kernel/src/filesystem/mount_table.cpp @@ -0,0 +1,79 @@ +#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 <ranges> +#include <string_view> + +namespace kernel::filesystem +{ + 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->parent_mount() == parent_mount; }); + } + + auto mount_table::add_mount(kstd::shared_ptr<mount> const & mount) -> void + { + m_mounts.push_back(mount); + + if (auto source_mount = mount->source_mount()) + { + source_mount->increment_ref_count(); + } + + if (auto mount_dentry = mount->mount_dentry()) + { + mount_dentry->set_flag(dentry::dentry_flags::is_mount_point); + } + } + + auto mount_table::remove_mount(std::string_view path) -> operation_result + { + auto mount_it = find_mount_iterator(path); + if (mount_it == m_mounts.end()) + { + return operation_result::mount_not_found; + } + + auto const & mount = *mount_it; + if (!mount->is_ready_to_unmount()) + { + return operation_result::cannot_be_unmounted; + } + if (has_child_mounts(mount)) + { + return operation_result::has_child_mounts; + } + + if (auto source_mount = mount->source_mount()) + { + source_mount->decrement_ref_count(); + } + + if (auto mount_dentry = mount->mount_dentry()) + { + mount_dentry->unset_flag(dentry::dentry_flags::is_mount_point); + } + + m_mounts.erase(mount_it); + return operation_result::removed; + } + + auto mount_table::find_mount(std::string_view path) const -> kstd::shared_ptr<mount> + { + auto mount_it = find_mount_iterator(path); + return (mount_it != m_mounts.end()) ? *mount_it : nullptr; + } + + auto mount_table::find_mount_iterator(std::string_view path) const + -> kstd::vector<kstd::shared_ptr<mount>>::const_iterator + { + return std::ranges::find_last_if(m_mounts, [&](auto const & mount) { return mount->mount_path() == path; }).begin(); + } +} // namespace kernel::filesystem
\ No newline at end of file |
