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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#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
{
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>
{
kstd::shared_ptr<mount> mount_with_longest_prefix = nullptr;
std::size_t best_len = 0;
for (auto const & mount : m_mounts)
{
auto mp = mount->get_mount_path();
// /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 && visible && mp.size() >= best_len)
{
mount_with_longest_prefix = mount;
best_len = mp.size();
}
}
return mount_with_longest_prefix;
}
} // namespace kernel::filesystem
|