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

#include <kernel/filesystem/inode.hpp>

#include <kapi/system.hpp>

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

#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.");
    }
  }

  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();
  }

  // TODO BA-FS26 fix warning (do not use recursion)
  auto dentry::get_full_path() const -> kstd::string
  {
    if (m_parent)
    {
      auto parent_path = m_parent->get_full_path();
      if (parent_path != "/")
      {
        parent_path += '/';
      }
      return parent_path + m_name.view();
    }

    return m_name.empty() ? "/" : m_name.view();
  }

  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