1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 02:54:59 +00:00

Kernel/TmpFS: Prevent TmpFS::add_child() from adding duplicate children

If asked to add an already existing name to a directory inode, fail with
EEXIST, consistent with other filesystems.
This commit is contained in:
Andreas Kling 2022-01-01 22:18:59 +01:00
parent 63e8cf8d59
commit db4388f21b

View file

@ -271,13 +271,19 @@ ErrorOr<void> TmpFSInode::add_child(Inode& child, StringView name, mode_t)
if (name.length() > NAME_MAX)
return ENAMETOOLONG;
MutexLocker locker(m_inode_lock);
for (auto const& existing_child : m_children) {
if (existing_child.name->view() == name)
return EEXIST;
}
auto name_kstring = TRY(KString::try_create(name));
// Balanced by `delete` in remove_child()
auto* child_entry = new (nothrow) Child { move(name_kstring), static_cast<TmpFSInode&>(child) };
if (!child_entry)
return ENOMEM;
MutexLocker locker(m_inode_lock);
m_children.append(*child_entry);
did_add_child(child.identifier(), name);
return {};