mirror of
https://github.com/SerenityOS/serenity.git
synced 2026-11-07 04:29:30 -10:00
Kernel/VFS: Don't overwrite files when renaming a directory
This commit is contained in:
1 parent
7125a6cc6c
commit
5df6434e8c
3 files changed
+23
No files matched your search
@@ -636,6 +636,9 @@ ErrorOr<void> VirtualFileSystem::rename(VFSRootContext const& vfs_root_context,
|
||||
if (!new_custody_or_error.is_error()) {
|
||||
auto& new_inode = new_custody_or_error.value()->inode();
|
||||
|
||||
if (old_inode.is_directory() && !new_inode.is_directory())
|
||||
return ENOTDIR;
|
||||
|
||||
if (old_inode.index() != new_inode.index() && old_inode.is_directory() && new_inode.is_directory()) {
|
||||
size_t child_count = 0;
|
||||
TRY(new_inode.traverse_as_directory([&child_count](auto&) -> ErrorOr<void> {
|
||||
|
||||
@@ -4,6 +4,7 @@ set(TEST_SOURCES
|
||||
TestFIFO.cpp
|
||||
TestPathLength.cpp
|
||||
TestPthreadCond.cpp
|
||||
TestRename.cpp
|
||||
)
|
||||
|
||||
foreach(libtest_source IN LISTS TEST_SOURCES)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (c) 2026, Lucas Chollet <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/System.h>
|
||||
#include <LibFileSystem/TempFile.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
TEST_CASE(rename_directory_over_file_fails)
|
||||
{
|
||||
auto directory = TRY_OR_FAIL(FileSystem::TempFile::create_temp_directory());
|
||||
auto file = TRY_OR_FAIL(FileSystem::TempFile::create_temp_file());
|
||||
|
||||
auto error = Core::System::rename(directory->path(), file->path());
|
||||
EXPECT(error.is_error());
|
||||
EXPECT_EQ(error.error().code(), ENOTDIR);
|
||||
}
|
||||
Reference in new issue
Block a user