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

LibCore: Make MappedFile a Stream

The internal reuse of FixedMemoryStream makes this straightforward.
There alread is one user of the new API, demonstrating the need for this
change beyond what I said out to use it for :^)
This commit is contained in:
kleines Filmröllchen 2023-09-12 20:21:23 +02:00 committed by Tim Schumacher
parent 062e0db46c
commit d6571f54d8
8 changed files with 266 additions and 25 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -14,10 +15,11 @@
namespace Core {
ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map(StringView path)
ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map(StringView path, OpenMode mode)
{
auto fd = TRY(Core::System::open(path, O_RDONLY | O_CLOEXEC, 0));
return map_from_fd_and_close(fd, path);
auto const file_mode = mode == OpenMode::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);
}
ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_file(NonnullOwnPtr<Core::File> stream, StringView path)
@ -25,24 +27,39 @@ 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)
ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] StringView path, OpenMode mode)
{
TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
ScopeGuard fd_close_guard = [fd] {
close(fd);
::close(fd);
};
auto stat = TRY(Core::System::fstat(fd));
auto size = stat.st_size;
auto* ptr = TRY(Core::System::mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0, 0, path));
int protection;
int flags;
switch (mode) {
case OpenMode::ReadOnly:
protection = PROT_READ;
flags = MAP_SHARED;
break;
case OpenMode::ReadWrite:
protection = PROT_READ | PROT_WRITE;
// Don't map a read-write mapping shared as a precaution.
flags = MAP_PRIVATE;
break;
}
return adopt_own(*new MappedFile(ptr, size));
auto* ptr = TRY(Core::System::mmap(nullptr, size, protection, flags, fd, 0, 0, path));
return adopt_own(*new MappedFile(ptr, size, mode));
}
MappedFile::MappedFile(void* ptr, size_t size)
: m_data(ptr)
MappedFile::MappedFile(void* ptr, size_t size, OpenMode mode)
: FixedMemoryStream(Bytes { ptr, size }, mode == OpenMode::ReadWrite)
, m_data(ptr)
, m_size(size)
{
}

View file

@ -7,7 +7,9 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Error.h>
#include <AK/MemoryStream.h>
#include <AK/Noncopyable.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/RefCounted.h>
@ -15,25 +17,29 @@
namespace Core {
class MappedFile {
class MappedFile : public FixedMemoryStream {
AK_MAKE_NONCOPYABLE(MappedFile);
AK_MAKE_NONMOVABLE(MappedFile);
public:
static ErrorOr<NonnullOwnPtr<MappedFile>> map(StringView path);
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);
~MappedFile();
// 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_from_file(NonnullOwnPtr<Core::File>, StringView path);
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path, OpenMode mode = OpenMode::ReadOnly);
virtual ~MappedFile();
// Non-stream APIs for using MappedFile as a simple POSIX API wrapper.
void* data() { return m_data; }
void const* data() const { return m_data; }
size_t size() const { return m_size; }
ReadonlyBytes bytes() const { return { m_data, m_size }; }
private:
explicit MappedFile(void*, size_t);
explicit MappedFile(void*, size_t, OpenMode);
void* m_data { nullptr };
size_t m_size { 0 };

View file

@ -174,7 +174,7 @@ Icon FileIconProvider::icon_for_executable(DeprecatedString const& path)
auto& mapped_file = file_or_error.value();
if (mapped_file->size() < SELFMAG) {
if (mapped_file->size().release_value() < SELFMAG) {
app_icon_cache.set(path, s_executable_icon);
return s_executable_icon;
}
@ -184,7 +184,7 @@ Icon FileIconProvider::icon_for_executable(DeprecatedString const& path)
return s_executable_icon;
}
auto image = ELF::Image((u8 const*)mapped_file->data(), mapped_file->size());
auto image = ELF::Image((u8 const*)mapped_file->data(), mapped_file->size().release_value());
if (!image.is_valid()) {
app_icon_cache.set(path, s_executable_icon);
return s_executable_icon;