diff options
author | Samuel Bowman <sam@sambowman.tech> | 2022-02-12 14:21:28 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-21 20:13:44 +0100 |
commit | be1c5c6b9f2d69074e076efdee4816dd22a2ad70 (patch) | |
tree | 74cb9e632826fd48898355f87ed7df57089c7d87 /Userland/Libraries/LibPartition | |
parent | 27861477740873c74f660cbfe8862e634c02861f (diff) | |
download | serenity-be1c5c6b9f2d69074e076efdee4816dd22a2ad70.zip |
Kernel+LibPartition: Move DiskPartitionMetadata into LibPartition
This commit creates a new library LibPartition which will contain
partition related code sharable between Kernel and Userland and
includes DiskPartitionMetadata as the first shared class.
Diffstat (limited to 'Userland/Libraries/LibPartition')
-rw-r--r-- | Userland/Libraries/LibPartition/CMakeLists.txt | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibPartition/DiskPartitionMetadata.cpp | 98 | ||||
-rw-r--r-- | Userland/Libraries/LibPartition/DiskPartitionMetadata.h | 52 |
3 files changed, 155 insertions, 0 deletions
diff --git a/Userland/Libraries/LibPartition/CMakeLists.txt b/Userland/Libraries/LibPartition/CMakeLists.txt new file mode 100644 index 0000000000..83751ec44b --- /dev/null +++ b/Userland/Libraries/LibPartition/CMakeLists.txt @@ -0,0 +1,5 @@ +set(SOURCES + DiskPartitionMetadata.cpp +) + +serenity_lib(LibPartition partition) diff --git a/Userland/Libraries/LibPartition/DiskPartitionMetadata.cpp b/Userland/Libraries/LibPartition/DiskPartitionMetadata.cpp new file mode 100644 index 0000000000..0ce3eced3b --- /dev/null +++ b/Userland/Libraries/LibPartition/DiskPartitionMetadata.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibPartition/DiskPartitionMetadata.h> + +namespace Partition { + +DiskPartitionMetadata::PartitionType::PartitionType(u8 partition_type) +{ + m_partition_type[0] = partition_type; +} +DiskPartitionMetadata::PartitionType::PartitionType(Array<u8, 16> partition_type) + : m_partition_type_is_uuid(true) +{ + m_partition_type.span().overwrite(0, partition_type.data(), partition_type.size()); +} +UUID DiskPartitionMetadata::PartitionType::to_uuid() const +{ + VERIFY(is_uuid()); + return m_partition_type; +} +u8 DiskPartitionMetadata::PartitionType::to_byte_indicator() const +{ + VERIFY(!is_uuid()); + return m_partition_type[0]; +} +bool DiskPartitionMetadata::PartitionType::is_uuid() const +{ + return m_partition_type_is_uuid; +} +bool DiskPartitionMetadata::PartitionType::is_valid() const +{ + return !all_of(m_partition_type, [](auto const octet) { return octet == 0; }); +} + +DiskPartitionMetadata::DiskPartitionMetadata(u64 start_block, u64 end_block, u8 partition_type) + : m_start_block(start_block) + , m_end_block(end_block) + , m_type(partition_type) +{ + VERIFY(m_type.is_valid()); +} + +DiskPartitionMetadata::DiskPartitionMetadata(u64 start_block, u64 end_block, Array<u8, 16> partition_type) + : m_start_block(start_block) + , m_end_block(end_block) + , m_type(partition_type) +{ + VERIFY(m_type.is_valid()); +} + +DiskPartitionMetadata::DiskPartitionMetadata(u64 start_block, u64 end_block, Array<u8, 16> partition_type, UUID unique_guid, u64 special_attributes) + : m_start_block(start_block) + , m_end_block(end_block) + , m_type(partition_type) + , m_unique_guid(unique_guid) + , m_attributes(special_attributes) +{ + VERIFY(m_type.is_valid()); + VERIFY(!m_unique_guid.is_zero()); +} + +DiskPartitionMetadata DiskPartitionMetadata::offset(u64 blocks_count) const +{ + return { blocks_count + m_start_block, blocks_count + m_end_block, m_type.m_partition_type }; +} + +u64 DiskPartitionMetadata::start_block() const +{ + return m_start_block; +} + +u64 DiskPartitionMetadata::end_block() const +{ + return m_end_block; +} + +Optional<u64> DiskPartitionMetadata::special_attributes() const +{ + if (m_attributes == 0) + return {}; + return m_attributes; +} + +DiskPartitionMetadata::PartitionType const& DiskPartitionMetadata::type() const +{ + return m_type; +} + +const UUID& DiskPartitionMetadata::unique_guid() const +{ + return m_unique_guid; +} + +} diff --git a/Userland/Libraries/LibPartition/DiskPartitionMetadata.h b/Userland/Libraries/LibPartition/DiskPartitionMetadata.h new file mode 100644 index 0000000000..252be047a7 --- /dev/null +++ b/Userland/Libraries/LibPartition/DiskPartitionMetadata.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/UUID.h> + +namespace Partition { + +class DiskPartitionMetadata { +private: + class PartitionType { + friend class DiskPartitionMetadata; + + public: + explicit PartitionType(u8 partition_type); + explicit PartitionType(Array<u8, 16> partition_type); + UUID to_uuid() const; + u8 to_byte_indicator() const; + bool is_uuid() const; + bool is_valid() const; + + private: + Array<u8, 16> m_partition_type {}; + bool m_partition_type_is_uuid { false }; + }; + +public: + DiskPartitionMetadata(u64 block_offset, u64 block_limit, u8 partition_type); + DiskPartitionMetadata(u64 start_block, u64 end_block, Array<u8, 16> partition_type); + DiskPartitionMetadata(u64 block_offset, u64 block_limit, Array<u8, 16> partition_type, UUID unique_guid, u64 special_attributes); + u64 start_block() const; + u64 end_block() const; + + DiskPartitionMetadata offset(u64 blocks_count) const; + + Optional<u64> special_attributes() const; + PartitionType const& type() const; + const UUID& unique_guid() const; + +private: + u64 m_start_block; + u64 m_end_block; + PartitionType m_type; + UUID m_unique_guid {}; + u64 m_attributes { 0 }; +}; + +} |