1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00
serenity/Userland/Applications/PartitionEditor/PartitionModel.cpp
Samuel Bowman 7a8953a833 PartitionEditor: Add the beginnings of a partition editor :^)
This adds a new application PartitionEditor which will eventually be
used to create and edit partition tables. Since LibPartition does not
know how to write partition tables yet, it is currently read-only.

Devices are discovered by scanning /dev for block device files.
Since block devices are chmod 600, PartitionEditor be must run as root.

By default Serenity uses the entire disk for the ext2 filesystem
without a partition table. This isn't useful for testing as the
partition list for the default disk will be empty. To test properly,
I created a few disk images using various partitioning schemes
(MBR, EBR, and GPT) and attached them using the following command:

export SERENITY_EXTRA_QEMU_ARGS="
  -drive file=/path/to/mbr.img,format=raw,index=1,media=disk
  -drive file=/path/to/ebr.img,format=raw,index=2,media=disk
  -drive file=/path/to/gpt.img,format=raw,index=3,media=disk"
2022-07-21 20:13:44 +01:00

88 lines
2.6 KiB
C++

/*
* Copyright (c) 2022, Samuel Bowman <sam@sambowman.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Applications/PartitionEditor/PartitionModel.h>
#include <LibPartition/EBRPartitionTable.h>
#include <LibPartition/GUIDPartitionTable.h>
#include <LibPartition/MBRPartitionTable.h>
namespace PartitionEditor {
String PartitionModel::column_name(int column) const
{
switch (column) {
case Column::Partition:
return "Partition";
case Column::StartBlock:
return "Start Block";
case Column::EndBlock:
return "End Block";
default:
VERIFY_NOT_REACHED();
}
}
GUI::Variant PartitionModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
{
if (role != GUI::ModelRole::Display)
return {};
if (!m_partition_table)
return {};
auto optional_partition = m_partition_table->partition(index.row());
if (optional_partition.has_value()) {
auto partition = optional_partition.release_value();
switch (index.column()) {
case Column::Partition:
return index.row() + 1;
case Column::StartBlock:
return partition.start_block();
case Column::EndBlock:
// FIXME: Either MBR end block is off by one (if supposed to be exclusive bound)
// or GPT end block is off by one (if supposed to be inclusive bound).
// This is an issue in LibPartition.
return partition.end_block();
default:
VERIFY_NOT_REACHED();
}
}
return {};
}
ErrorOr<void> PartitionModel::set_device_path(String const& path)
{
auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
auto mbr_table_or_error = Partition::MBRPartitionTable::try_to_initialize(file);
if (!mbr_table_or_error.is_error()) {
dbgln("Found MBR partition table on {}", path);
m_partition_table = move(mbr_table_or_error.value());
invalidate();
return {};
}
auto ebr_table_or_error = Partition::EBRPartitionTable::try_to_initialize(file);
if (!ebr_table_or_error.is_error()) {
dbgln("Found EBR partition table on {}", path);
m_partition_table = move(ebr_table_or_error.value());
invalidate();
return {};
}
auto guid_table_or_error = Partition::GUIDPartitionTable::try_to_initialize(file);
if (!guid_table_or_error.is_error()) {
dbgln("Found GUID partition table on {}", path);
m_partition_table = move(guid_table_or_error.value());
invalidate();
return {};
}
return Error::from_errno(ENOTSUP);
}
}