blob: 73f7da320ebb00b0630877f208e4afa707432cd7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
* Copyright (c) 2022, Samuel Bowman <sam@sambowman.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/NumberFormat.h>
#include <Applications/PartitionEditor/PartitionModel.h>
#include <LibCore/DeprecatedFile.h>
#include <LibPartition/EBRPartitionTable.h>
#include <LibPartition/GUIDPartitionTable.h>
#include <LibPartition/MBRPartitionTable.h>
namespace PartitionEditor {
DeprecatedString 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";
case Column::TotalBlocks:
return "Total Blocks";
case Column::Size:
return "Size";
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:
return partition.end_block();
case Column::TotalBlocks:
return partition.end_block() - partition.start_block() + 1;
case Column::Size:
return human_readable_size((partition.end_block() - partition.start_block() + 1) * m_partition_table->block_size());
default:
VERIFY_NOT_REACHED();
}
}
return {};
}
ErrorOr<void> PartitionModel::set_device_path(DeprecatedString const& path)
{
auto file = TRY(Core::DeprecatedFile::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);
}
}
|