From 5b6e93f96ac9710f678e5a2f4fb0f8f4cbcff58d Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 14 Jan 2023 19:57:29 -0500 Subject: [PATCH] LibCore: Add `MappedFile::map_from_file()` This method relies on `map_from_fd_and_close()` but takes a `File` instead of a fd. --- Userland/Libraries/LibCore/MappedFile.cpp | 6 ++++++ Userland/Libraries/LibCore/MappedFile.h | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibCore/MappedFile.cpp b/Userland/Libraries/LibCore/MappedFile.cpp index 2f5b41b455..ca4666cab2 100644 --- a/Userland/Libraries/LibCore/MappedFile.cpp +++ b/Userland/Libraries/LibCore/MappedFile.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,11 @@ ErrorOr> MappedFile::map(StringView path) return map_from_fd_and_close(fd, path); } +ErrorOr> MappedFile::map_from_file(NonnullOwnPtr stream, StringView path) +{ + return map_from_fd_and_close(stream->leak_fd(Badge {}), path); +} + ErrorOr> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] StringView path) { TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC)); diff --git a/Userland/Libraries/LibCore/MappedFile.h b/Userland/Libraries/LibCore/MappedFile.h index f2ca779b33..7ad2b709f3 100644 --- a/Userland/Libraries/LibCore/MappedFile.h +++ b/Userland/Libraries/LibCore/MappedFile.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include namespace Core { @@ -20,6 +20,7 @@ class MappedFile : public RefCounted { public: static ErrorOr> map(StringView path); + static ErrorOr> map_from_file(NonnullOwnPtr, StringView path); static ErrorOr> map_from_fd_and_close(int fd, StringView path); ~MappedFile();