aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/dentry.cpp
blob: 7617b28b8ced2a7925d75dc8642639527aed8ace (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
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
#include <kernel/filesystem/dentry.hpp>

#include <kernel/filesystem/inode.hpp>

#include <kapi/system.hpp>

#include <kstd/memory>
#include <kstd/string>
#include <kstd/vector>

#include <algorithm>
#include <cstdint>
#include <string_view>

namespace kernel::filesystem
{
  dentry::dentry(kstd::shared_ptr<dentry> const & parent, kstd::shared_ptr<inode> const & inode, std::string_view name)
      : m_name(name)
      , m_parent(parent)
      , m_inode(inode)
  {
    if (!m_inode)
    {
      kapi::system::panic("[FILESYSTEM] dentry constructed with null inode.");
    }
    if (m_name.empty())
    {
      kapi::system::panic("[FILESYSTEM] dentry constructed with empty name.");
    }
  }

  auto dentry::get_inode() const -> kstd::shared_ptr<inode> const &
  {
    return m_inode;
  }

  auto dentry::get_parent() const -> kstd::shared_ptr<dentry> const &
  {
    return m_parent;
  }

  auto dentry::get_name() const -> std::string_view
  {
    return m_name.view();
  }

  auto dentry::get_full_path() const -> kstd::string
  {
    kstd::string path = m_name;

    auto parent = m_parent;
    while (parent)
    {
      auto parent_name = parent->m_name;
      if (parent_name == "/")
      {
        path = "/" + path;
      }
      else
      {
        path = parent_name + "/" + path;
      }

      parent = parent->m_parent;
    }

    return path;
  }

  auto dentry::add_child(kstd::shared_ptr<dentry> const & child) -> void
  {
    m_children.push_back(child);
  }

  auto dentry::find_child(std::string_view name) const -> kstd::shared_ptr<dentry>
  {
    auto it = std::ranges::find_if(m_children, [&](auto const & child) { return child->m_name == name; });
    return (it != m_children.end()) ? *it : nullptr;
  }

  auto dentry::set_flag(dentry_flags flag) -> void
  {
    m_flags |= static_cast<uint32_t>(flag);
  }

  auto dentry::unset_flag(dentry_flags flag) -> void
  {
    m_flags &= ~static_cast<uint32_t>(flag);
  }

  auto dentry::has_flag(dentry_flags flag) const -> bool
  {
    return (m_flags & static_cast<uint32_t>(flag)) != 0;
  }
}  // namespace kernel::filesystem