mirror of
https://github.com/RGBCube/serenity
synced 2025-05-24 21:45:08 +00:00
AK: Add method to create MappedFile from fd
This commit is contained in:
parent
95ee7069d5
commit
bc75ca4fe5
2 changed files with 10 additions and 0 deletions
|
@ -21,6 +21,15 @@ Result<NonnullRefPtr<MappedFile>, OSError> MappedFile::map(const String& path)
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return OSError(errno);
|
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] {
|
ScopeGuard fd_close_guard = [fd] {
|
||||||
close(fd);
|
close(fd);
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,6 +20,7 @@ class MappedFile : public RefCounted<MappedFile> {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static Result<NonnullRefPtr<MappedFile>, OSError> map(const String& path);
|
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();
|
~MappedFile();
|
||||||
|
|
||||||
void* data() { return m_data; }
|
void* data() { return m_data; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue