summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPartition
diff options
context:
space:
mode:
authorSamuel Bowman <sam@sambowman.tech>2022-03-20 19:59:05 -0400
committerLinus Groh <mail@linusgroh.de>2022-07-21 20:13:44 +0100
commit6a1c85aa61a3c52365a6adc4a425798a79e6ceea (patch)
tree42ef48fe32e65ccc36cf6eb01e9999c86bf00abc /Userland/Libraries/LibPartition
parentf6ab636d3168a03b3b665da3492455a0d26cfa29 (diff)
downloadserenity-6a1c85aa61a3c52365a6adc4a425798a79e6ceea.zip
LibPartition: Make PartitionTable kernel/userland agnostic
Previously, PartitionTable was constructed using a Kernel::StorageDevice making it only usable in the kernel. This commit adds a new constructor that takes a Core::File instead, making it usable in userland as well. This also adds the m_block_size field which stores the block size of the underlying device obtained by calling StorageDevice::block_size() in the kernel or by using the STORAGE_DEVICE_GET_BLOCK_SIZE ioctl in userland. This avoids the need for an #ifdef every time block size is needed.
Diffstat (limited to 'Userland/Libraries/LibPartition')
-rw-r--r--Userland/Libraries/LibPartition/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibPartition/PartitionTable.cpp15
-rw-r--r--Userland/Libraries/LibPartition/PartitionTable.h18
3 files changed, 30 insertions, 4 deletions
diff --git a/Userland/Libraries/LibPartition/CMakeLists.txt b/Userland/Libraries/LibPartition/CMakeLists.txt
index 08682886dc..111d7c95c5 100644
--- a/Userland/Libraries/LibPartition/CMakeLists.txt
+++ b/Userland/Libraries/LibPartition/CMakeLists.txt
@@ -7,3 +7,4 @@ set(SOURCES
)
serenity_lib(LibPartition partition)
+target_link_libraries(LibPartition LibCore)
diff --git a/Userland/Libraries/LibPartition/PartitionTable.cpp b/Userland/Libraries/LibPartition/PartitionTable.cpp
index dc5685667d..b3e16ea2dc 100644
--- a/Userland/Libraries/LibPartition/PartitionTable.cpp
+++ b/Userland/Libraries/LibPartition/PartitionTable.cpp
@@ -6,14 +6,27 @@
#include <LibPartition/PartitionTable.h>
+#ifndef KERNEL
+# include <sys/ioctl.h>
+#endif
+
namespace Partition {
+#ifdef KERNEL
PartitionTable::PartitionTable(Kernel::StorageDevice const& device)
: m_device(device)
+ , m_block_size(device.block_size())
+{
+}
+#else
+PartitionTable::PartitionTable(NonnullRefPtr<Core::File> device_file)
+ : m_device_file(device_file)
{
+ VERIFY(ioctl(m_device_file->leak_fd(), STORAGE_DEVICE_GET_BLOCK_SIZE, &m_block_size) >= 0);
}
+#endif
-Optional<DiskPartitionMetadata> PartitionTable::partition(unsigned index)
+Optional<DiskPartitionMetadata> PartitionTable::partition(unsigned index) const
{
if (index > partitions_count())
return {};
diff --git a/Userland/Libraries/LibPartition/PartitionTable.h b/Userland/Libraries/LibPartition/PartitionTable.h
index 2ca4db59a0..fcbccf9bf8 100644
--- a/Userland/Libraries/LibPartition/PartitionTable.h
+++ b/Userland/Libraries/LibPartition/PartitionTable.h
@@ -6,25 +6,37 @@
#pragma once
-#include <Kernel/Storage/StorageDevice.h>
#include <LibPartition/DiskPartitionMetadata.h>
+#ifdef KERNEL
+# include <Kernel/Storage/StorageDevice.h>
+#else
+# include <LibCore/File.h>
+#endif
+
namespace Partition {
class PartitionTable {
public:
- Optional<DiskPartitionMetadata> partition(unsigned index);
+ Optional<DiskPartitionMetadata> partition(unsigned index) const;
size_t partitions_count() const { return m_partitions.size(); }
virtual ~PartitionTable() = default;
virtual bool is_valid() const = 0;
Vector<DiskPartitionMetadata> partitions() const { return m_partitions; }
+ size_t block_size() const { return m_block_size; }
protected:
+#ifdef KERNEL
explicit PartitionTable(Kernel::StorageDevice const&);
-
NonnullRefPtr<Kernel::StorageDevice> m_device;
+#else
+ explicit PartitionTable(NonnullRefPtr<Core::File>);
+ NonnullRefPtr<Core::File> m_device_file;
+#endif
+
Vector<DiskPartitionMetadata> m_partitions;
+ size_t m_block_size;
};
}