ladybird/Userland/Libraries/LibPartition/PartitionableDevice.h
Ben Wiederhake 3d6b838df3 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.
2023-06-05 14:50:09 +02:00

55 lines
1.3 KiB
C++

/*
* 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
};
}