Kernel: Add Custody::try_create_absolute_path()

This adds a way to get a Custody's absolute path as KString, which
enables it to fail gracefully on OOM.
This commit is contained in:
Max Wipfli 2021-07-06 12:40:35 +02:00 committed by Andreas Kling
parent 1f792faf34
commit 95f769ea51
Notes: sideshowbarker 2024-07-18 10:14:09 +09:00
2 changed files with 31 additions and 0 deletions

View File

@ -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())

View File

@ -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; }