1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:47: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

@ -0,0 +1,54 @@
/*
* Copyright (c) 2023, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Noncopyable.h>
#ifdef KERNEL
# include <Kernel/Devices/Storage/StorageDevice.h>
#else
# include <AK/MaybeOwned.h>
# include <LibCore/File.h>
#endif
namespace Partition {
class PartitionableDevice {
AK_MAKE_NONCOPYABLE(PartitionableDevice);
public:
#ifdef KERNEL
PartitionableDevice(Kernel::StorageDevice&);
// Userland doesn't get an implicit constructor.
#endif
PartitionableDevice(PartitionableDevice&&) = default;
// Unused, and "move out of reference" isn't well-defined anyway:
PartitionableDevice& operator=(PartitionableDevice&&) = delete;
#ifdef KERNEL
static ErrorOr<PartitionableDevice> create(Kernel::StorageDevice& device);
#else
static ErrorOr<PartitionableDevice> create(MaybeOwned<Core::File> device_file);
#endif
~PartitionableDevice() = default;
PartitionableDevice clone_unowned();
ErrorOr<PartitionableDevice> clone_owned();
size_t block_size() const;
ErrorOr<void> read_block(size_t block_index, Bytes block_buffer);
private:
#ifdef KERNEL
Kernel::StorageDevice& m_device;
#else
explicit PartitionableDevice(MaybeOwned<Core::File>, size_t block_size);
MaybeOwned<Core::File> m_device_file;
size_t m_block_size;
#endif
};
}