AK: Add method to create MappedFile from fd

This commit is contained in:
Timothy 2021-08-06 11:47:55 +10:00 committed by Andreas Kling
parent 95ee7069d5
commit bc75ca4fe5
Notes: sideshowbarker 2024-07-18 07:19:36 +09:00
2 changed files with 10 additions and 0 deletions

View file

@ -21,6 +21,15 @@ Result<NonnullRefPtr<MappedFile>, OSError> MappedFile::map(const String& path)
if (fd < 0)
return OSError(errno);
return map_from_fd_and_close(fd, path);
}
Result<NonnullRefPtr<MappedFile>, 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);
};

View file

@ -20,6 +20,7 @@ class MappedFile : public RefCounted<MappedFile> {
public:
static Result<NonnullRefPtr<MappedFile>, OSError> map(const String& path);
static Result<NonnullRefPtr<MappedFile>, OSError> map_from_fd_and_close(int fd, String const& path);
~MappedFile();
void* data() { return m_data; }