1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:07:36 +00:00

AK: Use an enum to specify the open mode instead of a bool

Let's replace this bool with an `enum class` in order to enhance
readability. This is done by repurposing `MappedFile`'s `OpenMode` into
a shared `enum` simply called `Mode`.
This commit is contained in:
Lucas CHOLLET 2023-11-05 15:31:17 -05:00 committed by Tim Schumacher
parent 00ad8419cf
commit b00476abac
7 changed files with 24 additions and 25 deletions

View file

@ -44,7 +44,7 @@ static constexpr LoaderPluginInitializer s_initializers[] = {
ErrorOr<NonnullRefPtr<Loader>, LoaderError> Loader::create(StringView path)
{
auto stream = TRY(Core::MappedFile::map(path, Core::MappedFile::OpenMode::ReadOnly));
auto stream = TRY(Core::MappedFile::map(path, Core::MappedFile::Mode::ReadOnly));
return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream)))));
}

View file

@ -15,9 +15,9 @@
namespace Core {
ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map(StringView path, OpenMode mode)
ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map(StringView path, Mode mode)
{
auto const file_mode = mode == OpenMode::ReadOnly ? O_RDONLY : O_RDWR;
auto const file_mode = mode == Mode::ReadOnly ? O_RDONLY : O_RDWR;
auto fd = TRY(Core::System::open(path, file_mode | O_CLOEXEC, 0));
return map_from_fd_and_close(fd, path, mode);
}
@ -27,7 +27,7 @@ ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_file(NonnullOwnPtr<Core:
return map_from_fd_and_close(stream->leak_fd(Badge<MappedFile> {}), path);
}
ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] StringView path, OpenMode mode)
ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] StringView path, Mode mode)
{
TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
@ -41,11 +41,11 @@ ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[m
int protection;
int flags;
switch (mode) {
case OpenMode::ReadOnly:
case Mode::ReadOnly:
protection = PROT_READ;
flags = MAP_SHARED;
break;
case OpenMode::ReadWrite:
case Mode::ReadWrite:
protection = PROT_READ | PROT_WRITE;
// Don't map a read-write mapping shared as a precaution.
flags = MAP_PRIVATE;
@ -57,8 +57,8 @@ ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[m
return adopt_own(*new MappedFile(ptr, size, mode));
}
MappedFile::MappedFile(void* ptr, size_t size, OpenMode mode)
: FixedMemoryStream(Bytes { ptr, size }, mode == OpenMode::ReadWrite)
MappedFile::MappedFile(void* ptr, size_t size, Mode mode)
: FixedMemoryStream(Bytes { ptr, size }, mode)
, m_data(ptr)
, m_size(size)
{

View file

@ -22,15 +22,9 @@ class MappedFile : public FixedMemoryStream {
AK_MAKE_NONMOVABLE(MappedFile);
public:
// Reflects a simplified version of mmap protection and flags.
enum class OpenMode {
ReadOnly,
ReadWrite,
};
static ErrorOr<NonnullOwnPtr<MappedFile>> map(StringView path, OpenMode mode = OpenMode::ReadOnly);
static ErrorOr<NonnullOwnPtr<MappedFile>> map(StringView path, Mode mode = Mode::ReadOnly);
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_file(NonnullOwnPtr<Core::File>, StringView path);
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path, OpenMode mode = OpenMode::ReadOnly);
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path, Mode mode = Mode::ReadOnly);
virtual ~MappedFile();
// Non-stream APIs for using MappedFile as a simple POSIX API wrapper.
@ -39,7 +33,7 @@ public:
ReadonlyBytes bytes() const { return { m_data, m_size }; }
private:
explicit MappedFile(void*, size_t, OpenMode);
explicit MappedFile(void*, size_t, Mode);
void* m_data { nullptr };
size_t m_size { 0 };