1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:57:35 +00:00

LibPartition: Migrate from DeprecatedFile to File

The implemented cloning mechanism should be sound:
- If a PartitionTable is passed a File with
  ShouldCloseFileDescriptor::Yes, then it will keep it alive until the
  PartitionTable is destroyed.
- If a PartitionTable is passed a File with
  ShouldCloseFileDescriptor::No, then the caller has to ensure that the
  file descriptor remains alive.
If the caller is EBRPartitionTable, the same consideration holds.
If the caller is PartitionEditor::PartitionModel, this is satisfied by
keeping an OwnPtr<Core::File> around which is the originally opened
file.

Therefore, we never leak any fds, and never access a Core::File or fd
after destroying it.
This commit is contained in:
Ben Wiederhake 2023-05-21 20:33:09 +02:00 committed by Jelle Raaijmakers
parent c197fb4037
commit 3d6b838df3
14 changed files with 221 additions and 177 deletions

View file

@ -6,7 +6,6 @@
#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>
@ -68,28 +67,33 @@ GUI::Variant PartitionModel::data(GUI::ModelIndex const& index, GUI::ModelRole r
ErrorOr<void> PartitionModel::set_device_path(DeprecatedString const& path)
{
auto file = TRY(Core::DeprecatedFile::open(path, Core::OpenMode::ReadOnly));
auto strong_file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
auto weak_file = TRY(Core::File::adopt_fd(strong_file->fd(), Core::File::OpenMode::Read, Core::File::ShouldCloseFileDescriptor::No));
auto device = TRY(Partition::PartitionableDevice::create(move(weak_file)));
auto mbr_table_or_error = Partition::MBRPartitionTable::try_to_initialize(file);
auto mbr_table_or_error = Partition::MBRPartitionTable::try_to_initialize(TRY(device.clone_owned()));
if (!mbr_table_or_error.is_error()) {
dbgln("Found MBR partition table on {}", path);
m_partition_table = move(mbr_table_or_error.value());
m_backing_file = move(strong_file);
invalidate();
return {};
}
auto ebr_table_or_error = Partition::EBRPartitionTable::try_to_initialize(file);
auto ebr_table_or_error = Partition::EBRPartitionTable::try_to_initialize(TRY(device.clone_owned()));
if (!ebr_table_or_error.is_error()) {
dbgln("Found EBR partition table on {}", path);
m_partition_table = move(ebr_table_or_error.value());
m_backing_file = move(strong_file);
invalidate();
return {};
}
auto guid_table_or_error = Partition::GUIDPartitionTable::try_to_initialize(file);
auto guid_table_or_error = Partition::GUIDPartitionTable::try_to_initialize(TRY(device.clone_owned()));
if (!guid_table_or_error.is_error()) {
dbgln("Found GUID partition table on {}", path);
m_partition_table = move(guid_table_or_error.value());
m_backing_file = move(strong_file);
invalidate();
return {};
}