aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/mount_table.cpp
blob: 195f48a31eb1d57fabdebb3a2a7c4aa3969c3ab4 (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
#include "kernel/filesystem/mount_table.hpp"

#include "kernel/filesystem/mount.hpp"

#include <kstd/memory>

#include <cstddef>
#include <string_view>

namespace kernel::filesystem
{
  void mount_table::add_mount(kstd::shared_ptr<mount> const & mount)
  {
    m_mounts.push_back(mount);
  }

  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()] == '/');

      if (is_prefix && mp.size() >= best_len)
      {
        mount_with_longest_prefix = mount;
        best_len = mp.size();
      }
    }

    return mount_with_longest_prefix;
  }
}  // namespace kernel::filesystem