|
| 1 | +#include <sourcemeta/core/io_error.h> |
| 2 | +#include <sourcemeta/core/io_fileview.h> |
| 3 | + |
| 4 | +#if defined(_WIN32) |
| 5 | +#define WIN32_LEAN_AND_MEAN |
| 6 | +#include <windows.h> |
| 7 | +#else |
| 8 | +#include <fcntl.h> // open, O_RDONLY |
| 9 | +#include <sys/mman.h> // mmap, munmap |
| 10 | +#include <sys/stat.h> // fstat |
| 11 | +#include <unistd.h> // close |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace sourcemeta::core { |
| 15 | + |
| 16 | +#if defined(_WIN32) |
| 17 | + |
| 18 | +FileView::FileView(const std::filesystem::path &path) { |
| 19 | + this->file_handle_ = |
| 20 | + CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, |
| 21 | + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 22 | + if (this->file_handle_ == INVALID_HANDLE_VALUE) { |
| 23 | + throw FileViewError(path, "Could not open the file"); |
| 24 | + } |
| 25 | + |
| 26 | + LARGE_INTEGER file_size; |
| 27 | + if (GetFileSizeEx(this->file_handle_, &file_size) == 0) { |
| 28 | + CloseHandle(this->file_handle_); |
| 29 | + throw FileViewError(path, "Could not determine the file size"); |
| 30 | + } |
| 31 | + this->size_ = static_cast<std::size_t>(file_size.QuadPart); |
| 32 | + |
| 33 | + this->mapping_handle_ = CreateFileMappingW(this->file_handle_, nullptr, |
| 34 | + PAGE_READONLY, 0, 0, nullptr); |
| 35 | + if (this->mapping_handle_ == nullptr) { |
| 36 | + CloseHandle(this->file_handle_); |
| 37 | + throw FileViewError(path, "Could not create a file mapping"); |
| 38 | + } |
| 39 | + |
| 40 | + this->data_ = static_cast<const std::uint8_t *>( |
| 41 | + MapViewOfFile(this->mapping_handle_, FILE_MAP_READ, 0, 0, 0)); |
| 42 | + if (this->data_ == nullptr) { |
| 43 | + CloseHandle(this->mapping_handle_); |
| 44 | + CloseHandle(this->file_handle_); |
| 45 | + throw FileViewError(path, "Could not map the file into memory"); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +FileView::~FileView() { |
| 50 | + if (this->data_ != nullptr) { |
| 51 | + UnmapViewOfFile(this->data_); |
| 52 | + } |
| 53 | + |
| 54 | + if (this->mapping_handle_ != nullptr) { |
| 55 | + CloseHandle(this->mapping_handle_); |
| 56 | + } |
| 57 | + |
| 58 | + if (this->file_handle_ != nullptr && |
| 59 | + this->file_handle_ != INVALID_HANDLE_VALUE) { |
| 60 | + CloseHandle(this->file_handle_); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#else |
| 65 | + |
| 66 | +FileView::FileView(const std::filesystem::path &path) { |
| 67 | + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 68 | + this->file_descriptor_ = open(path.c_str(), O_RDONLY); |
| 69 | + if (this->file_descriptor_ == -1) { |
| 70 | + throw FileViewError(path, "Could not open the file"); |
| 71 | + } |
| 72 | + |
| 73 | + struct stat file_stat; |
| 74 | + if (fstat(this->file_descriptor_, &file_stat) != 0) { |
| 75 | + close(this->file_descriptor_); |
| 76 | + throw FileViewError(path, "Could not determine the file size"); |
| 77 | + } |
| 78 | + this->size_ = static_cast<std::size_t>(file_stat.st_size); |
| 79 | + |
| 80 | + void *mapped = mmap(nullptr, this->size_, PROT_READ, MAP_PRIVATE, |
| 81 | + this->file_descriptor_, 0); |
| 82 | + if (mapped == MAP_FAILED) { |
| 83 | + close(this->file_descriptor_); |
| 84 | + throw FileViewError(path, "Could not map the file into memory"); |
| 85 | + } |
| 86 | + |
| 87 | + this->data_ = static_cast<const std::uint8_t *>(mapped); |
| 88 | +} |
| 89 | + |
| 90 | +FileView::~FileView() { |
| 91 | + if (this->data_ != nullptr && this->size_ > 0) { |
| 92 | + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 93 | + munmap(const_cast<std::uint8_t *>(this->data_), this->size_); |
| 94 | + } |
| 95 | + |
| 96 | + if (this->file_descriptor_ != -1) { |
| 97 | + close(this->file_descriptor_); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#endif |
| 102 | + |
| 103 | +auto FileView::size() const noexcept -> std::size_t { return this->size_; } |
| 104 | + |
| 105 | +} // namespace sourcemeta::core |
0 commit comments