From 0a5c9ef0a484c096f55108d21954e91c3daffbc6 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 16 Oct 2024 14:25:17 -0400 Subject: [PATCH] kernel: Move get_mount_point from VMUtils to KPartition. There's nothing VM-specific about it; it just builds paths that partitions should be mounted at. Also move the tests. --- .../kernel/disk_device_manager/KPartition.h | 5 +- .../kernel/disk_device_manager/KPartition.cpp | 63 +++-- src/system/kernel/vm/Jamfile | 1 - src/system/kernel/vm/VMAnonymousCache.cpp | 3 +- src/system/kernel/vm/VMUtils.cpp | 58 ----- src/system/kernel/vm/VMUtils.h | 17 -- .../DiskDeviceManagerTestAddon.cpp | 13 ++ .../system/kernel/disk_device_manager/Jamfile | 6 + .../disk_device_manager/KPartitionTest.cpp | 153 ++++++++++++ .../KPartitionTest.h} | 11 +- .../system/kernel/vm/KernelVMTestAddon.cpp | 13 -- .../system/kernel/vm/VMGetMountPointTest.cpp | 218 ------------------ 12 files changed, 227 insertions(+), 334 deletions(-) delete mode 100644 src/system/kernel/vm/VMUtils.cpp delete mode 100644 src/system/kernel/vm/VMUtils.h create mode 100644 src/tests/system/kernel/disk_device_manager/DiskDeviceManagerTestAddon.cpp create mode 100644 src/tests/system/kernel/disk_device_manager/KPartitionTest.cpp rename src/tests/system/kernel/{vm/VMGetMountPointTest.h => disk_device_manager/KPartitionTest.h} (65%) delete mode 100644 src/tests/system/kernel/vm/KernelVMTestAddon.cpp delete mode 100644 src/tests/system/kernel/vm/VMGetMountPointTest.cpp diff --git a/headers/private/kernel/disk_device_manager/KPartition.h b/headers/private/kernel/disk_device_manager/KPartition.h index 9b43d7f0db..fe7aec1f7a 100644 --- a/headers/private/kernel/disk_device_manager/KPartition.h +++ b/headers/private/kernel/disk_device_manager/KPartition.h @@ -115,15 +115,14 @@ public: virtual status_t GetPath(KPath *path) const; // no setter (see BDiskDevice) -- built on the fly + status_t GetMountPoint(KPath* mountPoint) const; + void SetVolumeID(dev_t volumeID); dev_t VolumeID() const; void SetMountCookie(void *cookie); void *MountCookie() const; - virtual status_t Mount(uint32 mountFlags, const char *parameters); - virtual status_t Unmount(); - // Parameters status_t SetParameters(const char *parameters); diff --git a/src/system/kernel/disk_device_manager/KPartition.cpp b/src/system/kernel/disk_device_manager/KPartition.cpp index 1679367d80..93f7ac6d40 100644 --- a/src/system/kernel/disk_device_manager/KPartition.cpp +++ b/src/system/kernel/disk_device_manager/KPartition.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -729,6 +730,52 @@ KPartition::GetPath(KPath* path) const } +status_t +KPartition::GetMountPoint(KPath* mountPoint) const +{ + if (!mountPoint || !ContainsFileSystem()) + return B_BAD_VALUE; + + ASSERT(!IsMounted()); + // fetching the actual mounted point isn't implemented (yet) + + int nameLength = 0; + const char* volumeName = ContentName(); + if (volumeName != NULL) + nameLength = strlen(volumeName); + if (nameLength == 0) { + volumeName = Name(); + if (volumeName != NULL) + nameLength = strlen(volumeName); + if (nameLength == 0) { + volumeName = "unnamed volume"; + nameLength = strlen(volumeName); + } + } + + BStackOrHeapArray basePath(nameLength + 2); + if (!basePath.IsValid()) + return B_NO_MEMORY; + int32 len = snprintf(basePath, nameLength + 2, "/%s", volumeName); + for (int32 i = 1; i < len; i++) + if (basePath[i] == '/') + basePath[i] = '-'; + char* path = mountPoint->LockBuffer(); + int32 pathLen = mountPoint->BufferSize(); + strncpy(path, basePath, pathLen); + + struct stat dummy; + for (int i = 1; ; i++) { + if (stat(path, &dummy) != 0) + break; + snprintf(path, pathLen, "%s%d", (char*)basePath, i); + } + + mountPoint->UnlockBuffer(); + return B_OK; +} + + void KPartition::SetVolumeID(dev_t volumeID) { @@ -781,22 +828,6 @@ KPartition::MountCookie() const } -status_t -KPartition::Mount(uint32 mountFlags, const char* parameters) -{ - // not implemented - return B_ERROR; -} - - -status_t -KPartition::Unmount() -{ - // not implemented - return B_ERROR; -} - - status_t KPartition::SetParameters(const char* parameters) { diff --git a/src/system/kernel/vm/Jamfile b/src/system/kernel/vm/Jamfile index c28480f6f2..a646e719c4 100644 --- a/src/system/kernel/vm/Jamfile +++ b/src/system/kernel/vm/Jamfile @@ -24,7 +24,6 @@ KernelMergeObject kernel_vm.o : VMTranslationMap.cpp VMUserAddressSpace.cpp VMUserArea.cpp - VMUtils.cpp : $(TARGET_KERNEL_PIC_CCFLAGS) ; diff --git a/src/system/kernel/vm/VMAnonymousCache.cpp b/src/system/kernel/vm/VMAnonymousCache.cpp index c26cabf8e8..6247423a61 100644 --- a/src/system/kernel/vm/VMAnonymousCache.cpp +++ b/src/system/kernel/vm/VMAnonymousCache.cpp @@ -58,7 +58,6 @@ #include #include "IORequest.h" -#include "VMUtils.h" #if ENABLE_SWAP_SUPPORT @@ -1691,7 +1690,7 @@ swap_init_post_modules() else { KPath devPath, mountPoint; visitor.fBestPartition->GetPath(&devPath); - get_mount_point(visitor.fBestPartition, &mountPoint); + visitor.fBestPartition->GetMountPoint(&mountPoint); const char* mountPath = mountPoint.Path(); mkdir(mountPath, S_IRWXU | S_IRWXG | S_IRWXO); swapDeviceID = _kern_mount(mountPath, devPath.Path(), diff --git a/src/system/kernel/vm/VMUtils.cpp b/src/system/kernel/vm/VMUtils.cpp deleted file mode 100644 index 40ccee94cb..0000000000 --- a/src/system/kernel/vm/VMUtils.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2011-2012 Haiku, Inc. All rights reserved. - * Distributed under the terms of the MIT License. - * - * Authors: - * Hamish Morrison, hamish@lavabit.com - * Alexander von Gluck IV, kallisti5@unixzen.com - */ - - -#include "VMUtils.h" - -#include - -#include - - -status_t -get_mount_point(KPartition* partition, KPath* mountPoint) -{ - if (!mountPoint || !partition->ContainsFileSystem()) - return B_BAD_VALUE; - - int nameLength = 0; - const char* volumeName = partition->ContentName(); - if (volumeName != NULL) - nameLength = strlen(volumeName); - if (nameLength == 0) { - volumeName = partition->Name(); - if (volumeName != NULL) - nameLength = strlen(volumeName); - if (nameLength == 0) { - volumeName = "unnamed volume"; - nameLength = strlen(volumeName); - } - } - - BStackOrHeapArray basePath(nameLength + 2); - if (!basePath.IsValid()) - return B_NO_MEMORY; - int32 len = snprintf(basePath, nameLength + 2, "/%s", volumeName); - for (int32 i = 1; i < len; i++) - if (basePath[i] == '/') - basePath[i] = '-'; - char* path = mountPoint->LockBuffer(); - int32 pathLen = mountPoint->BufferSize(); - strncpy(path, basePath, pathLen); - - struct stat dummy; - for (int i = 1; ; i++) { - if (stat(path, &dummy) != 0) - break; - snprintf(path, pathLen, "%s%d", (char*)basePath, i); - } - - mountPoint->UnlockBuffer(); - return B_OK; -} diff --git a/src/system/kernel/vm/VMUtils.h b/src/system/kernel/vm/VMUtils.h deleted file mode 100644 index 263ec9a771..0000000000 --- a/src/system/kernel/vm/VMUtils.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright 2018 Kacper Kasper - * All rights reserved. Distributed under the terms of the MIT license. - */ -#ifndef _KERNEL_VM_UTILS_H -#define _KERNEL_VM_UTILS_H - - -#include -#include - - -status_t -get_mount_point(KPartition* partition, KPath* mountPoint); - - -#endif // _KERNEL_VM_UTILS_H diff --git a/src/tests/system/kernel/disk_device_manager/DiskDeviceManagerTestAddon.cpp b/src/tests/system/kernel/disk_device_manager/DiskDeviceManagerTestAddon.cpp new file mode 100644 index 0000000000..27fb1ac3bf --- /dev/null +++ b/src/tests/system/kernel/disk_device_manager/DiskDeviceManagerTestAddon.cpp @@ -0,0 +1,13 @@ +#include +#include + +#include "KPartitionTest.h" + + +BTestSuite* +getTestSuite() +{ + BTestSuite *suite = new BTestSuite("DiskDeviceManager"); + suite->addTest("KPartitionGetMountPointTest", KPartitionGetMountPointTest::Suite()); + return suite; +} diff --git a/src/tests/system/kernel/disk_device_manager/Jamfile b/src/tests/system/kernel/disk_device_manager/Jamfile index b6b8040956..c6a1b8c88d 100644 --- a/src/tests/system/kernel/disk_device_manager/Jamfile +++ b/src/tests/system/kernel/disk_device_manager/Jamfile @@ -44,6 +44,12 @@ SharedLibrary test_disk_device_manager.so : : be libkernelland_emu.so [ TargetLibsupc++ ] ; +UnitTestLib libkerneldiskdevicemanagertest.so : + DiskDeviceManagerTestAddon.cpp + KPartitionTest.cpp + : test_disk_device_manager.so [ TargetLibstdc++ ] +; + SimpleTest DiskDeviceManagerTest : DiskDeviceManagerTest.cpp : test_disk_device_manager.so be diff --git a/src/tests/system/kernel/disk_device_manager/KPartitionTest.cpp b/src/tests/system/kernel/disk_device_manager/KPartitionTest.cpp new file mode 100644 index 0000000000..acaf56c482 --- /dev/null +++ b/src/tests/system/kernel/disk_device_manager/KPartitionTest.cpp @@ -0,0 +1,153 @@ +/* + * Copyright 2018 Kacper Kasper + * All rights reserved. Distributed under the terms of the MIT License. + */ + + +#include "KPartitionTest.h" + +#include + +#include +#include + +#include +#include +#include + + +struct stat; + +extern "C" int +stat(const char* path, struct stat* s) +{ + if(strcmp(path, "/testduplicate") == 0) + return 0; + else + return -1; +} + +using BPrivate::DiskDevice::KPartition; + + +// #pragma mark - + + +KPartitionGetMountPointTest::KPartitionGetMountPointTest(std::string name) + : BTestCase(name) +{ +} + +#define ADD_TEST(s, cls, m) \ + s->addTest(new CppUnit::TestCaller(#cls "::" #m, &cls::m)); + + +CppUnit::Test* +KPartitionGetMountPointTest::Suite() +{ + CppUnit::TestSuite *suite = new CppUnit::TestSuite("KPartitionGetMountPointTest"); + + ADD_TEST(suite, KPartitionGetMountPointTest, TestPartitionWithoutFilesystemReturnsBadValue); + ADD_TEST(suite, KPartitionGetMountPointTest, TestPartitionContentNameUsedFirst); + ADD_TEST(suite, KPartitionGetMountPointTest, TestPartitionNameUsedSecond); + ADD_TEST(suite, KPartitionGetMountPointTest, TestPartitionWithoutAnyNameIsNotRoot); + ADD_TEST(suite, KPartitionGetMountPointTest, TestPartitionNameWithSlashesRemoved); + ADD_TEST(suite, KPartitionGetMountPointTest, TestPartitionMountPointExists); + + return suite; +} + + +void +KPartitionGetMountPointTest::TestPartitionWithoutFilesystemReturnsBadValue() +{ + KPartition partition; + partition.SetName(""); + partition.SetContentName(""); + partition.SetFlags(0); + + KPath path; + status_t status = partition.GetMountPoint(&path); + + CPPUNIT_ASSERT_EQUAL(status, B_BAD_VALUE); +} + + +void +KPartitionGetMountPointTest::TestPartitionContentNameUsedFirst() +{ + KPartition partition; + partition.SetName("test1"); + partition.SetContentName("test2"); + partition.SetFlags(B_PARTITION_FILE_SYSTEM); + + KPath path; + status_t status = partition.GetMountPoint(&path); + + CPPUNIT_ASSERT_EQUAL(status, B_OK); + CPPUNIT_ASSERT(strcmp(path.Path(), "/test2") == 0); +} + + +void +KPartitionGetMountPointTest::TestPartitionNameUsedSecond() +{ + KPartition partition; + partition.SetName("test1"); + partition.SetContentName(""); + partition.SetFlags(B_PARTITION_FILE_SYSTEM); + + KPath path; + status_t status = partition.GetMountPoint(&path); + + CPPUNIT_ASSERT_EQUAL(status, B_OK); + CPPUNIT_ASSERT(strcmp(path.Path(), "/test1") == 0); +} + + +void +KPartitionGetMountPointTest::TestPartitionWithoutAnyNameIsNotRoot() +{ + KPartition partition; + partition.SetName(""); + partition.SetContentName(""); + partition.SetFlags(B_PARTITION_FILE_SYSTEM); + + KPath path; + status_t status = partition.GetMountPoint(&path); + + CPPUNIT_ASSERT_EQUAL(status, B_OK); + CPPUNIT_ASSERT(strcmp(path.Path(), "/") != 0); +} + + +void +KPartitionGetMountPointTest::TestPartitionNameWithSlashesRemoved() +{ + KPartition partition; + partition.SetName(""); + partition.SetContentName("testing/slashes"); + partition.SetFlags(B_PARTITION_FILE_SYSTEM); + + KPath path; + status_t status = partition.GetMountPoint(&path); + + CPPUNIT_ASSERT_EQUAL(status, B_OK); + CPPUNIT_ASSERT(strcmp(path.Path(), "/testing/slashes") != 0); +} + + +void +KPartitionGetMountPointTest::TestPartitionMountPointExists() +{ + KPartition partition; + partition.SetName(""); + partition.SetContentName("testduplicate"); + partition.SetFlags(B_PARTITION_FILE_SYSTEM); + + KPath path; + status_t status = partition.GetMountPoint(&path); + + CPPUNIT_ASSERT_EQUAL(status, B_OK); + CPPUNIT_ASSERT(strcmp(path.Path(), "/testduplicate") != 0); +} diff --git a/src/tests/system/kernel/vm/VMGetMountPointTest.h b/src/tests/system/kernel/disk_device_manager/KPartitionTest.h similarity index 65% rename from src/tests/system/kernel/vm/VMGetMountPointTest.h rename to src/tests/system/kernel/disk_device_manager/KPartitionTest.h index 46c99bf948..7bb5fc369c 100644 --- a/src/tests/system/kernel/vm/VMGetMountPointTest.h +++ b/src/tests/system/kernel/disk_device_manager/KPartitionTest.h @@ -2,19 +2,18 @@ * Copyright 2018 Kacper Kasper * All rights reserved. Distributed under the terms of the MIT License. */ -#ifndef _VM_GET_MOUNT_POINT_TEST_H_ -#define _VM_GET_MOUNT_POINT_TEST_H_ +#ifndef _KPARTITION_TEST_H_ +#define _KPARTITION_TEST_H_ #include -class VMGetMountPointTest : public BTestCase { +class KPartitionGetMountPointTest : public BTestCase { public: - VMGetMountPointTest(std::string name = ""); + KPartitionGetMountPointTest(std::string name = ""); static CppUnit::Test *Suite(); - void TestNullMountPointReturnsBadValue(); void TestPartitionWithoutFilesystemReturnsBadValue(); void TestPartitionContentNameUsedFirst(); void TestPartitionNameUsedSecond(); @@ -23,4 +22,4 @@ class VMGetMountPointTest : public BTestCase { void TestPartitionMountPointExists(); }; -#endif /* _VM_GET_MOUNT_POINT_TEST_H_ */ +#endif /* _KPARTITION_TEST_H_ */ diff --git a/src/tests/system/kernel/vm/KernelVMTestAddon.cpp b/src/tests/system/kernel/vm/KernelVMTestAddon.cpp deleted file mode 100644 index 03cbffc20a..0000000000 --- a/src/tests/system/kernel/vm/KernelVMTestAddon.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include - -#include "VMGetMountPointTest.h" - - -BTestSuite* -getTestSuite() -{ - BTestSuite *suite = new BTestSuite("KernelVM"); - suite->addTest("VMGetMountPointTest", VMGetMountPointTest::Suite()); - return suite; -} diff --git a/src/tests/system/kernel/vm/VMGetMountPointTest.cpp b/src/tests/system/kernel/vm/VMGetMountPointTest.cpp deleted file mode 100644 index c0a02a61e0..0000000000 --- a/src/tests/system/kernel/vm/VMGetMountPointTest.cpp +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright 2018 Kacper Kasper - * All rights reserved. Distributed under the terms of the MIT License. - */ - - -#include "VMGetMountPointTest.h" - -#include - -#include - -#include -#include -#include - - -// Kernel stubs - - -extern "C" team_id -team_get_kernel_team_id(void) -{ - return 0; -} - - -extern "C" team_id -team_get_current_team_id(void) -{ - return 0; -} - - -extern "C" status_t -vfs_normalize_path(const char* path, char* buffer, size_t bufferSize, - bool traverseLink, bool kernel) -{ - return B_NOT_SUPPORTED; -} - -struct stat; - -extern "C" int -stat(const char* path, struct stat* s) -{ - if(strcmp(path, "/testduplicate") == 0) - return 0; - else - return -1; -} - -namespace BPrivate { -namespace DiskDevice { - -class KPartition { -public: - KPartition(std::string name, std::string contentName, bool containsFilesystem) - : fName(name), fContentName(contentName), fContainsFilesystem(containsFilesystem) - {} - - const char *Name() const; - const char *ContentName() const; - bool ContainsFileSystem() const; - -private: - std::string fName; - std::string fContentName; - bool fContainsFilesystem; -}; - - -const char * -KPartition::Name() const -{ - return fName.c_str(); -} - - -const char * -KPartition::ContentName() const -{ - return fContentName.c_str(); -} - - -bool -KPartition::ContainsFileSystem() const -{ - return fContainsFilesystem; -} - -} -} - - -using BPrivate::DiskDevice::KPartition; - - -status_t -get_mount_point(KPartition* partition, KPath* mountPoint); - - -// #pragma mark - - - -VMGetMountPointTest::VMGetMountPointTest(std::string name) - : BTestCase(name) -{ -} - -#define ADD_TEST(s, cls, m) \ - s->addTest(new CppUnit::TestCaller(#cls "::" #m, &cls::m)); - - -CppUnit::Test* -VMGetMountPointTest::Suite() -{ - CppUnit::TestSuite *suite = new CppUnit::TestSuite("VMGetMountPointTest"); - - ADD_TEST(suite, VMGetMountPointTest, TestNullMountPointReturnsBadValue); - ADD_TEST(suite, VMGetMountPointTest, TestPartitionWithoutFilesystemReturnsBadValue); - ADD_TEST(suite, VMGetMountPointTest, TestPartitionContentNameUsedFirst); - ADD_TEST(suite, VMGetMountPointTest, TestPartitionNameUsedSecond); - ADD_TEST(suite, VMGetMountPointTest, TestPartitionWithoutAnyNameIsNotRoot); - ADD_TEST(suite, VMGetMountPointTest, TestPartitionNameWithSlashesRemoved); - ADD_TEST(suite, VMGetMountPointTest, TestPartitionMountPointExists); - - return suite; -} - - -void -VMGetMountPointTest::TestNullMountPointReturnsBadValue() -{ - status_t status = get_mount_point(NULL, NULL); - - CPPUNIT_ASSERT_EQUAL(status, B_BAD_VALUE); -} - - -void -VMGetMountPointTest::TestPartitionWithoutFilesystemReturnsBadValue() -{ - KPartition partition("", "", false); - KPath path; - - status_t status = get_mount_point(&partition, &path); - - CPPUNIT_ASSERT_EQUAL(status, B_BAD_VALUE); -} - - -void -VMGetMountPointTest::TestPartitionContentNameUsedFirst() -{ - KPartition partition("test1", "test2", true); - KPath path; - - status_t status = get_mount_point(&partition, &path); - - CPPUNIT_ASSERT_EQUAL(status, B_OK); - CPPUNIT_ASSERT(strcmp(path.Path(), "/test2") == 0); -} - - -void -VMGetMountPointTest::TestPartitionNameUsedSecond() -{ - KPartition partition("test1", "", true); - KPath path; - - status_t status = get_mount_point(&partition, &path); - - CPPUNIT_ASSERT_EQUAL(status, B_OK); - CPPUNIT_ASSERT(strcmp(path.Path(), "/test1") == 0); -} - - -void -VMGetMountPointTest::TestPartitionWithoutAnyNameIsNotRoot() -{ - KPartition partition("", "", true); - KPath path; - - status_t status = get_mount_point(&partition, &path); - - CPPUNIT_ASSERT_EQUAL(status, B_OK); - CPPUNIT_ASSERT(strcmp(path.Path(), "/") != 0); -} - - -void -VMGetMountPointTest::TestPartitionNameWithSlashesRemoved() -{ - KPartition partition("", "testing/slashes", true); - KPath path; - - status_t status = get_mount_point(&partition, &path); - - CPPUNIT_ASSERT_EQUAL(status, B_OK); - CPPUNIT_ASSERT(strcmp(path.Path(), "/testing/slashes") != 0); -} - - -void -VMGetMountPointTest::TestPartitionMountPointExists() -{ - KPartition partition("", "testduplicate", true); - KPath path; - - status_t status = get_mount_point(&partition, &path); - - CPPUNIT_ASSERT_EQUAL(status, B_OK); - CPPUNIT_ASSERT(strcmp(path.Path(), "/testduplicate") != 0); -} - -