MappedFile.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ScopeGuard.h>
  8. #include <LibCore/File.h>
  9. #include <LibCore/MappedFile.h>
  10. #include <LibCore/System.h>
  11. #include <fcntl.h>
  12. #include <sys/mman.h>
  13. #include <unistd.h>
  14. namespace Core {
  15. ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map(StringView path, Mode mode)
  16. {
  17. auto const file_mode = mode == Mode::ReadOnly ? O_RDONLY : O_RDWR;
  18. auto fd = TRY(Core::System::open(path, file_mode | O_CLOEXEC, 0));
  19. return map_from_fd_and_close(fd, path, mode);
  20. }
  21. ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_file(NonnullOwnPtr<Core::File> stream, StringView path)
  22. {
  23. return map_from_fd_and_close(stream->leak_fd(Badge<MappedFile> {}), path);
  24. }
  25. ErrorOr<NonnullOwnPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] StringView path, Mode mode)
  26. {
  27. TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
  28. ScopeGuard fd_close_guard = [fd] {
  29. ::close(fd);
  30. };
  31. auto stat = TRY(Core::System::fstat(fd));
  32. auto size = stat.st_size;
  33. int protection;
  34. int flags;
  35. switch (mode) {
  36. case Mode::ReadOnly:
  37. protection = PROT_READ;
  38. flags = MAP_SHARED;
  39. break;
  40. case Mode::ReadWrite:
  41. protection = PROT_READ | PROT_WRITE;
  42. // Don't map a read-write mapping shared as a precaution.
  43. flags = MAP_PRIVATE;
  44. break;
  45. }
  46. auto* ptr = TRY(Core::System::mmap(nullptr, size, protection, flags, fd, 0, 0, path));
  47. return adopt_own(*new MappedFile(ptr, size, mode));
  48. }
  49. MappedFile::MappedFile(void* ptr, size_t size, Mode mode)
  50. : FixedMemoryStream(Bytes { ptr, size }, mode)
  51. , m_data(ptr)
  52. , m_size(size)
  53. {
  54. }
  55. MappedFile::~MappedFile()
  56. {
  57. auto res = Core::System::munmap(m_data, m_size);
  58. if (res.is_error())
  59. dbgln("Failed to unmap MappedFile (@ {:p}): {}", m_data, res.error());
  60. }
  61. }