1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 10:37:44 +00:00

LibCore+AK: Move MappedFile from AK to LibCore

MappedFile is strictly a userspace thing, so it doesn't belong in AK
(which is supposed to be user/kernel agnostic.)
This commit is contained in:
Andreas Kling 2021-11-23 11:32:25 +01:00
parent c1c9da6c35
commit 58fb3ebf66
48 changed files with 101 additions and 103 deletions

View file

@ -16,6 +16,7 @@ set(SOURCES
LocalServer.cpp
LocalSocket.cpp
LockFile.cpp
MappedFile.cpp
MimeData.cpp
NetworkJob.cpp
NetworkResponse.cpp

View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <LibCore/MappedFile.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
namespace Core {
ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map(String const& path)
{
int fd = open(path.characters(), O_RDONLY | O_CLOEXEC, 0);
if (fd < 0)
return Error::from_errno(errno);
return map_from_fd_and_close(fd, path);
}
ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] String const& path)
{
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
return Error::from_errno(errno);
ScopeGuard fd_close_guard = [fd] {
close(fd);
};
struct stat st;
if (fstat(fd, &st) < 0)
return Error::from_errno(errno);
auto size = st.st_size;
auto* ptr = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED)
return Error::from_errno(errno);
#ifdef __serenity__
if (set_mmap_name(ptr, size, path.characters()) < 0) {
perror("set_mmap_name");
}
#endif
return adopt_ref(*new MappedFile(ptr, size));
}
MappedFile::MappedFile(void* ptr, size_t size)
: m_data(ptr)
, m_size(size)
{
}
MappedFile::~MappedFile()
{
auto rc = munmap(m_data, m_size);
VERIFY(rc == 0);
}
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/Noncopyable.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <AK/Result.h>
namespace Core {
class MappedFile : public RefCounted<MappedFile> {
AK_MAKE_NONCOPYABLE(MappedFile);
AK_MAKE_NONMOVABLE(MappedFile);
public:
static ErrorOr<NonnullRefPtr<MappedFile>> map(String const& path);
static ErrorOr<NonnullRefPtr<MappedFile>> map_from_fd_and_close(int fd, String const& path);
~MappedFile();
void* data() { return m_data; }
const void* 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);
void* m_data { nullptr };
size_t m_size { 0 };
};
}

View file

@ -5,11 +5,11 @@
*/
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/Platform.h>
#include <AK/StringBuilder.h>
#include <AK/Types.h>
#include <LibCore/File.h>
#include <LibCore/MappedFile.h>
#include <LibCoredump/Backtrace.h>
#include <LibCoredump/Reader.h>
#include <LibELF/Core.h>
@ -30,7 +30,7 @@ ELFObjectInfo const* Backtrace::object_info_for_region(ELF::Core::MemoryRegionIn
if (!Core::File::exists(path))
return nullptr;
auto file_or_error = MappedFile::map(path);
auto file_or_error = Core::MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;

View file

@ -14,14 +14,14 @@
namespace Coredump {
struct ELFObjectInfo {
ELFObjectInfo(NonnullRefPtr<MappedFile> file, NonnullOwnPtr<Debug::DebugInfo>&& debug_info, NonnullOwnPtr<ELF::Image> image)
ELFObjectInfo(NonnullRefPtr<Core::MappedFile> file, NonnullOwnPtr<Debug::DebugInfo>&& debug_info, NonnullOwnPtr<ELF::Image> image)
: file(move(file))
, debug_info(move(debug_info))
, image(move(image))
{
}
NonnullRefPtr<MappedFile> file;
NonnullRefPtr<Core::MappedFile> file;
NonnullOwnPtr<Debug::DebugInfo> debug_info;
NonnullOwnPtr<ELF::Image> image;
};

View file

@ -41,7 +41,7 @@ void Inspector::parse_loaded_libraries(Function<void(float)> on_progress)
if (on_progress)
on_progress(library_index / (float)number_of_libraries);
auto file_or_error = MappedFile::map(library.path);
auto file_or_error = Core::MappedFile::map(library.path);
if (file_or_error.is_error())
return;

View file

@ -17,7 +17,7 @@ namespace Coredump {
OwnPtr<Reader> Reader::create(StringView path)
{
auto file_or_error = MappedFile::map(path);
auto file_or_error = Core::MappedFile::map(path);
if (file_or_error.is_error())
return {};
@ -38,7 +38,7 @@ Reader::Reader(ByteBuffer buffer)
m_coredump_buffer = move(buffer);
}
Reader::Reader(NonnullRefPtr<MappedFile> file)
Reader::Reader(NonnullRefPtr<Core::MappedFile> file)
: Reader(file->bytes())
{
m_mapped_file = move(file);
@ -261,7 +261,6 @@ HashMap<String, String> Reader::metadata() const
struct LibraryData {
String name;
OwnPtr<MappedFile> file;
ELF::Image lib_elf;
};
@ -282,7 +281,7 @@ const Reader::LibraryData* Reader::library_containing(FlatPtr address) const
}
if (!cached_libs.contains(path)) {
auto file_or_error = MappedFile::map(path);
auto file_or_error = Core::MappedFile::map(path);
if (file_or_error.is_error())
return {};
auto image = ELF::Image(file_or_error.value()->bytes());

View file

@ -7,9 +7,9 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/MappedFile.h>
#include <AK/Noncopyable.h>
#include <AK/OwnPtr.h>
#include <LibCore/MappedFile.h>
#include <LibELF/Core.h>
#include <LibELF/Image.h>
@ -46,7 +46,7 @@ public:
struct LibraryData {
String name;
FlatPtr base_address { 0 };
NonnullRefPtr<MappedFile> file;
NonnullRefPtr<Core::MappedFile> file;
ELF::Image lib_elf;
};
const LibraryData* library_containing(FlatPtr address) const;
@ -61,7 +61,7 @@ public:
private:
explicit Reader(ReadonlyBytes);
explicit Reader(ByteBuffer);
explicit Reader(NonnullRefPtr<MappedFile>);
explicit Reader(NonnullRefPtr<Core::MappedFile>);
static Optional<ByteBuffer> decompress_coredump(ReadonlyBytes);
@ -86,7 +86,7 @@ private:
const JsonObject process_info() const;
// For uncompressed coredumps, we keep the MappedFile
RefPtr<MappedFile> m_mapped_file;
RefPtr<Core::MappedFile> m_mapped_file;
// For compressed coredumps, we decompress them into a ByteBuffer
ByteBuffer m_coredump_buffer;

View file

@ -452,7 +452,7 @@ void DebugSession::update_loaded_libs()
return IterationDecision::Continue;
}
auto file_or_error = MappedFile::map(object_path.value());
auto file_or_error = Core::MappedFile::map(object_path.value());
if (file_or_error.is_error())
return IterationDecision::Continue;

View file

@ -8,12 +8,12 @@
#include <AK/Demangle.h>
#include <AK/HashMap.h>
#include <AK/MappedFile.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <AK/String.h>
#include <LibC/sys/arch/i386/regs.h>
#include <LibCore/MappedFile.h>
#include <LibDebug/DebugInfo.h>
#include <LibDebug/ProcessInspector.h>
#include <signal.h>

View file

@ -7,19 +7,19 @@
#pragma once
#include "DebugInfo.h"
#include <AK/MappedFile.h>
#include <AK/Types.h>
#include <LibCore/MappedFile.h>
#include <LibELF/Image.h>
namespace Debug {
struct LoadedLibrary {
String name;
NonnullRefPtr<MappedFile> file;
NonnullRefPtr<Core::MappedFile> file;
NonnullOwnPtr<ELF::Image> image;
NonnullOwnPtr<DebugInfo> debug_info;
FlatPtr base_address {};
LoadedLibrary(String const& name, NonnullRefPtr<MappedFile> file, NonnullOwnPtr<ELF::Image> image, NonnullOwnPtr<DebugInfo>&& debug_info, FlatPtr base_address)
LoadedLibrary(String const& name, NonnullRefPtr<Core::MappedFile> file, NonnullOwnPtr<ELF::Image> image, NonnullOwnPtr<DebugInfo>&& debug_info, FlatPtr base_address)
: name(name)
, file(move(file))
, image(move(image))

View file

@ -5,10 +5,10 @@
*/
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/String.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/File.h>
#include <LibCore/MappedFile.h>
#include <LibCore/StandardPaths.h>
#include <LibELF/Image.h>
#include <LibGUI/FileIconProvider.h>
@ -147,7 +147,7 @@ Icon FileIconProvider::icon_for_executable(const String& path)
// If the icon for an app isn't in the cache we attempt to load the file as an ELF image and extract
// the serenity_app_icon_* sections which should contain the icons as raw PNG data. In the future it would
// be better if the binary signalled the image format being used or we deduced it, e.g. using magic bytes.
auto file_or_error = MappedFile::map(path);
auto file_or_error = Core::MappedFile::map(path);
if (file_or_error.is_error()) {
app_icon_cache.set(path, s_executable_icon);
return s_executable_icon;

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/MappedFile.h>
#include <LibCore/MappedFile.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
@ -73,7 +73,7 @@ void ImageWidget::animate()
void ImageWidget::load_from_file(StringView path)
{
auto file_or_error = MappedFile::map(path);
auto file_or_error = Core::MappedFile::map(path);
if (file_or_error.is_error())
return;

View file

@ -6,13 +6,13 @@
#include <AK/Checked.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/Memory.h>
#include <AK/MemoryStream.h>
#include <AK/Optional.h>
#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <AK/Try.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
#include <LibGfx/ShareableBitmap.h>
@ -134,7 +134,7 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_load_from_file(String const& path, in
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_load_from_fd_and_close(int fd, String const& path)
{
auto file = TRY(MappedFile::map_from_fd_and_close(fd, path));
auto file = TRY(Core::MappedFile::map_from_fd_and_close(fd, path));
if (auto decoder = ImageDecoder::try_create(file->bytes())) {
auto frame = TRY(decoder->frame(0));
if (auto& bitmap = frame.image)

View file

@ -202,7 +202,7 @@ RefPtr<BitmapFont> BitmapFont::load_from_file(String const& path)
if (Core::File::is_device(path))
return nullptr;
auto file_or_error = MappedFile::map(path);
auto file_or_error = Core::MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;

View file

@ -7,12 +7,12 @@
#pragma once
#include <AK/CharacterTypes.h>
#include <AK/MappedFile.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/Font.h>
#include <LibGfx/Size.h>
@ -128,7 +128,7 @@ private:
u8* m_rows { nullptr };
u8* m_glyph_widths { nullptr };
RefPtr<MappedFile> m_mapped_file;
RefPtr<Core::MappedFile> m_mapped_file;
u8 m_glyph_width { 0 };
u8 m_glyph_height { 0 };

View file

@ -8,11 +8,11 @@
#include <AK/Bitmap.h>
#include <AK/ByteReader.h>
#include <AK/MappedFile.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Size.h>

View file

@ -6,11 +6,11 @@
*/
#include <AK/Checked.h>
#include <AK/MappedFile.h>
#include <AK/Try.h>
#include <AK/Utf32View.h>
#include <AK/Utf8View.h>
#include <LibCore/File.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/TrueTypeFont/Cmap.h>
#include <LibGfx/TrueTypeFont/Font.h>
#include <LibGfx/TrueTypeFont/Glyf.h>
@ -227,7 +227,7 @@ GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_file(String path, unsigned index)
{
auto file = TRY(MappedFile::map(path));
auto file = TRY(Core::MappedFile::map(path));
auto font = TRY(try_load_from_externally_owned_memory(file->bytes(), index));
font->m_mapped_file = move(file);
return font;

View file

@ -87,7 +87,7 @@ private:
{
}
RefPtr<MappedFile> m_mapped_file;
RefPtr<Core::MappedFile> m_mapped_file;
ReadonlyBytes m_buffer;

View file

@ -14,7 +14,7 @@ namespace PCIDB {
RefPtr<Database> Database::open(const String& filename)
{
auto file_or_error = MappedFile::map(filename);
auto file_or_error = Core::MappedFile::map(filename);
if (file_or_error.is_error())
return nullptr;
auto res = adopt_ref(*new Database(file_or_error.release_value()));

View file

@ -7,12 +7,12 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/MappedFile.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibCore/MappedFile.h>
namespace PCIDB {
@ -64,7 +64,7 @@ public:
const StringView get_programming_interface(u8 class_id, u8 subclass_id, u8 programming_interface_id) const;
private:
explicit Database(NonnullRefPtr<MappedFile> file)
explicit Database(NonnullRefPtr<Core::MappedFile> file)
: m_file(move(file))
{
}
@ -77,7 +77,7 @@ private:
ClassMode,
};
NonnullRefPtr<MappedFile> m_file;
NonnullRefPtr<Core::MappedFile> m_file;
StringView m_view {};
HashMap<int, NonnullOwnPtr<Vendor>> m_vendors;
HashMap<int, NonnullOwnPtr<Class>> m_classes;

View file

@ -10,15 +10,15 @@
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <LibCore/File.h>
#include <LibCore/MappedFile.h>
#include <LibDebug/DebugInfo.h>
#include <LibSymbolication/Symbolication.h>
namespace Symbolication {
struct CachedELF {
NonnullRefPtr<MappedFile> mapped_file;
NonnullRefPtr<Core::MappedFile> mapped_file;
NonnullOwnPtr<Debug::DebugInfo> debug_info;
NonnullOwnPtr<ELF::Image> image;
};
@ -81,7 +81,7 @@ Optional<Symbol> symbolicate(String const& path, FlatPtr address, IncludeSourceP
}
}
if (!s_cache.contains(full_path)) {
auto mapped_file = MappedFile::map(full_path);
auto mapped_file = Core::MappedFile::map(full_path);
if (mapped_file.is_error()) {
dbgln("Failed to map {}: {}", full_path, mapped_file.error());
s_cache.set(full_path, {});

View file

@ -14,7 +14,7 @@ namespace USBDB {
RefPtr<Database> Database::open(const String& filename)
{
auto file_or_error = MappedFile::map(filename);
auto file_or_error = Core::MappedFile::map(filename);
if (file_or_error.is_error())
return nullptr;
auto res = adopt_ref(*new Database(file_or_error.release_value()));

View file

@ -7,12 +7,12 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/MappedFile.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibCore/MappedFile.h>
namespace USBDB {
@ -63,7 +63,7 @@ public:
const StringView get_protocol(u8 class_id, u8 subclass_id, u8 protocol_id) const;
private:
explicit Database(NonnullRefPtr<MappedFile> file)
explicit Database(NonnullRefPtr<Core::MappedFile> file)
: m_file(move(file))
{
}
@ -76,7 +76,7 @@ private:
ClassMode,
};
NonnullRefPtr<MappedFile> m_file;
NonnullRefPtr<Core::MappedFile> m_file;
StringView m_view {};
HashMap<int, NonnullOwnPtr<Vendor>> m_vendors;
HashMap<int, NonnullOwnPtr<Class>> m_classes;

View file

@ -6,9 +6,9 @@
#include "MatroskaReader.h"
#include <AK/Function.h>
#include <AK/MappedFile.h>
#include <AK/Optional.h>
#include <AK/Utf8View.h>
#include <LibCore/MappedFile.h>
namespace Video {
@ -43,7 +43,7 @@ constexpr u32 TIMESTAMP_ID = 0xE7;
OwnPtr<MatroskaDocument> MatroskaReader::parse_matroska_from_file(StringView path)
{
auto mapped_file_result = MappedFile::map(path);
auto mapped_file_result = Core::MappedFile::map(path);
if (mapped_file_result.is_error())
return {};