diff options
-rw-r--r-- | Kernel/FileSystem/Custody.cpp | 30 | ||||
-rw-r--r-- | Kernel/FileSystem/Custody.h | 1 |
2 files changed, 31 insertions, 0 deletions
diff --git a/Kernel/FileSystem/Custody.cpp b/Kernel/FileSystem/Custody.cpp index af21e32687..85500ea16d 100644 --- a/Kernel/FileSystem/Custody.cpp +++ b/Kernel/FileSystem/Custody.cpp @@ -35,6 +35,36 @@ Custody::~Custody() { } +OwnPtr<KString> Custody::try_create_absolute_path() const +{ + if (!parent()) + return KString::try_create("/"sv); + + Vector<Custody const*, 32> custody_chain; + size_t path_length = 0; + for (auto* custody = this; custody; custody = custody->parent()) { + custody_chain.append(custody); + path_length += custody->m_name->length() + 1; + } + VERIFY(path_length > 0); + + char* buffer; + auto string = KString::try_create_uninitialized(path_length - 1, buffer); + if (!string) + return string; + size_t string_index = 0; + for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) { + buffer[string_index] = '/'; + ++string_index; + auto& custody_name = *custody_chain[custody_index - 1]->m_name; + __builtin_memcpy(buffer + string_index, custody_name.characters(), custody_name.length()); + string_index += custody_name.length(); + } + VERIFY(string->length() == string_index); + buffer[string_index] = 0; + return string; +} + String Custody::absolute_path() const { if (!parent()) diff --git a/Kernel/FileSystem/Custody.h b/Kernel/FileSystem/Custody.h index 47b7815fd0..b2eaa1ee2c 100644 --- a/Kernel/FileSystem/Custody.h +++ b/Kernel/FileSystem/Custody.h @@ -30,6 +30,7 @@ public: Inode& inode() { return *m_inode; } const Inode& inode() const { return *m_inode; } StringView name() const { return m_name->view(); } + OwnPtr<KString> try_create_absolute_path() const; String absolute_path() const; int mount_flags() const { return m_mount_flags; } |