mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 05:48:12 +00:00
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:
parent
1f792faf34
commit
95f769ea51
2 changed files with 31 additions and 0 deletions
|
@ -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())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue