1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-22 20:37:34 +00:00

LibCore: Use syscall wrappers in MappedFile::map_from_fd_and_close()

This commit is contained in:
Andreas Kling 2021-11-23 11:36:00 +01:00
parent 58fb3ebf66
commit a62d16fbe9

View file

@ -7,6 +7,7 @@
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibCore/MappedFile.h> #include <LibCore/MappedFile.h>
#include <LibCore/System.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/mman.h> #include <sys/mman.h>
@ -26,18 +27,15 @@ ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map(String const& path)
ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] String const& path) ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] String const& path)
{ {
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
return Error::from_errno(errno);
ScopeGuard fd_close_guard = [fd] { ScopeGuard fd_close_guard = [fd] {
close(fd); close(fd);
}; };
struct stat st; auto stat = TRY(Core::System::fstat(fd));
if (fstat(fd, &st) < 0) auto size = stat.st_size;
return Error::from_errno(errno);
auto size = st.st_size;
auto* ptr = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0); auto* ptr = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) if (ptr == MAP_FAILED)