mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 17:58:18 +03: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
Notes:
sideshowbarker
2024-07-18 10:14:09 +09:00
Author: https://github.com/MaxWipfli Commit: https://github.com/SerenityOS/serenity/commit/95f769ea513 Pull-request: https://github.com/SerenityOS/serenity/pull/8492 Reviewed-by: https://github.com/alimpfard
@ -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())
|
||||
|
@ -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; }
|
||||
|
Loading…
Reference in New Issue
Block a user