mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:58:12 +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:
parent
062e0db46c
commit
d6571f54d8
8 changed files with 266 additions and 25 deletions
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue