Kernel/VFS: Allow rename to overwrite a symlink

Per POSIX:
If either the old or new argument names a symbolic link, rename() shall
operate on the symbolic link itself, and shall not resolve the last
component of the argument.
This commit is contained in:
Lucas Chollet authored and Sönke Holz committed 2026-10-19 17:29:35 +02:00
1 parent 125f24fa0f
commit 5b2dba5f45
2 files changed
+18 -1

No files matched your search

+1 -1
View File
@@ -623,7 +623,7 @@ ErrorOr<void> VirtualFileSystem::rename(VFSRootContext const& vfs_root_context,
auto& old_inode = old_custody->inode();
RefPtr<Custody> new_parent_custody;
auto new_custody_or_error = resolve_path(vfs_root_context, credentials, new_path, new_base, &new_parent_custody);
auto new_custody_or_error = resolve_path(vfs_root_context, credentials, new_path, new_base, &new_parent_custody, O_NOFOLLOW_NOERROR);
if (new_custody_or_error.is_error()) {
if (new_custody_or_error.error().code() != ENOENT || !new_parent_custody)
return new_custody_or_error.release_error();
+17
View File
@@ -8,6 +8,23 @@
#include <LibFileSystem/TempFile.h>
#include <LibTest/TestCase.h>
TEST_CASE(rename_file_over_symlink)
{
auto file = TRY_OR_FAIL(FileSystem::TempFile::create_temp_file());
TRY_OR_FAIL(Core::System::truncate(file->path(), 256));
static constexpr StringView LINK_PATH = "/tmp/link"sv;
(void)Core::System::unlink(LINK_PATH);
TRY_OR_FAIL(Core::System::symlink(file->path(), LINK_PATH));
TRY_OR_FAIL(Core::System::rename(file->path(), LINK_PATH));
auto stat = TRY_OR_FAIL(Core::System::lstat(LINK_PATH));
EXPECT(!S_ISLNK(stat.st_mode));
EXPECT(S_ISREG(stat.st_mode));
EXPECT_EQ(stat.st_size, 256u);
}
TEST_CASE(rename_directory_over_file_fails)
{
auto directory = TRY_OR_FAIL(FileSystem::TempFile::create_temp_directory());