1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:37:43 +00:00

Kernel: Improvements to Custody absolute path serialization

- Renamed try_create_absolute_path() => try_serialize_absolute_path()
- Use KResultOr and TRY() to propagate errors
- Don't call this when it's only for debug logging
This commit is contained in:
Andreas Kling 2021-09-06 12:24:36 +02:00
parent f173f73f10
commit cda2b9e71c
6 changed files with 21 additions and 33 deletions

View file

@ -71,10 +71,14 @@ Custody::~Custody()
{
}
OwnPtr<KString> Custody::try_create_absolute_path() const
KResultOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
{
if (!parent())
return KString::try_create("/"sv);
if (!parent()) {
auto string = KString::try_create("/"sv);
if (!string)
return ENOMEM;
return string.release_nonnull();
}
Vector<Custody const*, 32> custody_chain;
size_t path_length = 0;
@ -87,7 +91,7 @@ OwnPtr<KString> Custody::try_create_absolute_path() const
char* buffer;
auto string = KString::try_create_uninitialized(path_length - 1, buffer);
if (!string)
return string;
return ENOMEM;
size_t string_index = 0;
for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
buffer[string_index] = '/';
@ -98,7 +102,7 @@ OwnPtr<KString> Custody::try_create_absolute_path() const
}
VERIFY(string->length() == string_index);
buffer[string_index] = 0;
return string;
return string.release_nonnull();
}
String Custody::absolute_path() const

View file

@ -33,7 +33,7 @@ public:
Inode& inode() { return *m_inode; }
Inode const& inode() const { return *m_inode; }
StringView name() const { return m_name->view(); }
OwnPtr<KString> try_create_absolute_path() const;
KResultOr<NonnullOwnPtr<KString>> try_serialize_absolute_path() const;
String absolute_path() const;
int mount_flags() const { return m_mount_flags; }

View file

@ -54,9 +54,8 @@ KResult VirtualFileSystem::mount(FileSystem& fs, Custody& mount_point, int flags
{
return m_mounts.with_exclusive([&](auto& mounts) -> KResult {
auto& inode = mount_point.inode();
dbgln("VirtualFileSystem: Mounting {} at {} (inode: {}) with flags {}",
dbgln("VirtualFileSystem: Mounting {} at inode {} with flags {}",
fs.class_name(),
mount_point.try_create_absolute_path(),
inode.identifier(),
flags);
// FIXME: check that this is not already a mount point
@ -69,7 +68,7 @@ KResult VirtualFileSystem::mount(FileSystem& fs, Custody& mount_point, int flags
KResult VirtualFileSystem::bind_mount(Custody& source, Custody& mount_point, int flags)
{
return m_mounts.with_exclusive([&](auto& mounts) -> KResult {
dbgln("VirtualFileSystem: Bind-mounting {} at {}", source.try_create_absolute_path(), mount_point.try_create_absolute_path());
dbgln("VirtualFileSystem: Bind-mounting inode {} at inode {}", source.inode().identifier(), mount_point.inode().identifier());
// FIXME: check that this is not already a mount point
Mount mount { source.inode(), mount_point, flags };
mounts.append(move(mount));
@ -79,7 +78,7 @@ KResult VirtualFileSystem::bind_mount(Custody& source, Custody& mount_point, int
KResult VirtualFileSystem::remount(Custody& mount_point, int new_flags)
{
dbgln("VirtualFileSystem: Remounting {}", mount_point.try_create_absolute_path());
dbgln("VirtualFileSystem: Remounting inode {}", mount_point.inode().identifier());
auto* mount = find_mount_for_guest(mount_point.inode().identifier());
if (!mount)
@ -323,9 +322,7 @@ KResult VirtualFileSystem::mknod(StringView path, mode_t mode, dev_t dev, Custod
KResultOr<NonnullRefPtr<FileDescription>> VirtualFileSystem::create(StringView path, int options, mode_t mode, Custody& parent_custody, Optional<UidAndGid> owner)
{
auto basename = KLexicalPath::basename(path);
auto parent_path = parent_custody.try_create_absolute_path();
if (!parent_path)
return ENOMEM;
auto parent_path = TRY(parent_custody.try_serialize_absolute_path());
auto full_path = KLexicalPath::try_join(parent_path->view(), basename);
if (!full_path)
return ENOMEM;
@ -764,9 +761,7 @@ KResult VirtualFileSystem::validate_path_against_process_veil(Custody const& cus
{
if (Process::current().veil_state() == VeilState::None)
return KSuccess;
auto absolute_path = custody.try_create_absolute_path();
if (!absolute_path)
return ENOMEM;
auto absolute_path = TRY(custody.try_serialize_absolute_path());
return validate_path_against_process_veil(absolute_path->view(), options);
}