diff options
Diffstat (limited to 'kernel/src/filesystem/dentry.cpp')
| -rw-r--r-- | kernel/src/filesystem/dentry.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/kernel/src/filesystem/dentry.cpp b/kernel/src/filesystem/dentry.cpp new file mode 100644 index 0000000..3d8e01a --- /dev/null +++ b/kernel/src/filesystem/dentry.cpp @@ -0,0 +1,95 @@ +#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) + , m_flags(0) + { + 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::parent() const -> kstd::shared_ptr<dentry> const & + { + return m_parent; + } + + auto dentry::name() const -> std::string_view + { + return m_name; + } + + auto dentry::absolute_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
\ No newline at end of file |
