From bc75ca4fe55b0a19852c20e677bb4008156ceaff Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 6 Aug 2021 11:47:55 +1000 Subject: [PATCH] AK: Add method to create MappedFile from fd --- AK/MappedFile.cpp | 9 +++++++++ AK/MappedFile.h | 1 + 2 files changed, 10 insertions(+) diff --git a/AK/MappedFile.cpp b/AK/MappedFile.cpp index 3957d042a3..6323c1bb93 100644 --- a/AK/MappedFile.cpp +++ b/AK/MappedFile.cpp @@ -21,6 +21,15 @@ Result, OSError> MappedFile::map(const String& path) if (fd < 0) return OSError(errno); + return map_from_fd_and_close(fd, path); +} + +Result, OSError> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] String const& path) +{ + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { + return OSError(errno); + } + ScopeGuard fd_close_guard = [fd] { close(fd); }; diff --git a/AK/MappedFile.h b/AK/MappedFile.h index 23a8eeccd6..9002d8beb1 100644 --- a/AK/MappedFile.h +++ b/AK/MappedFile.h @@ -20,6 +20,7 @@ class MappedFile : public RefCounted { public: static Result, OSError> map(const String& path); + static Result, OSError> map_from_fd_and_close(int fd, String const& path); ~MappedFile(); void* data() { return m_data; }