aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/mount_table.cpp
blob: 78ac727223fd8777a568772d0b900ad14f9c9a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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->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::is_mount_point);
    }
  }

  auto mount_table::remove_mount(std::string_view path) -> operation_result
  {
    // TODO BA-FS26 check wheter something is open in this mount
    // TODO BA-FS26 nearly the same code is in find_mount -> refactor to avoid code duplication
    auto mount_range =
        std::ranges::find_last_if(m_mounts, [&](auto const & mount) { return mount->get_mount_path() == path; });
    auto mount_it = mount_range.begin();

    if (mount_it == 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::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_range =
        std::ranges::find_last_if(m_mounts, [&](auto const & mount) { return mount->get_mount_path() == path; });
    auto mount_it = mount_range.begin();
    return (mount_it != m_mounts.end()) ? *mount_it : nullptr;
  }
}  // namespace kernel::filesystem