| 32 | namespace Orc { |
| 33 | |
| 34 | Result<std::vector<uint8_t>> MapFile(const std::filesystem::path& path) |
| 35 | { |
| 36 | auto file = CreateFileApi( |
| 37 | path.c_str(), |
| 38 | GENERIC_READ, |
| 39 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 40 | NULL, |
| 41 | OPEN_EXISTING, |
| 42 | FILE_ATTRIBUTE_NORMAL, |
| 43 | NULL); |
| 44 | if (!file) |
| 45 | { |
| 46 | Log::Debug("Failed CreateFile [{}]", file.error()); |
| 47 | return file.error(); |
| 48 | } |
| 49 | |
| 50 | auto bytesToRead = GetFileSize(file->value()); |
| 51 | if (bytesToRead.has_error()) |
| 52 | { |
| 53 | Log::Debug("Failed GetFileSizeEx [{}]", bytesToRead.error()); |
| 54 | return bytesToRead.error(); |
| 55 | } |
| 56 | |
| 57 | if (*bytesToRead > UINT32_MAX) |
| 58 | { |
| 59 | Log::Debug("File is too big"); |
| 60 | return std::make_error_code(std::errc::no_buffer_space); |
| 61 | } |
| 62 | |
| 63 | std::vector<uint8_t> buffer; |
| 64 | |
| 65 | try |
| 66 | { |
| 67 | buffer.resize(*bytesToRead); |
| 68 | } |
| 69 | catch (const std::exception& e) |
| 70 | { |
| 71 | Log::Debug("Failed to resize buffer [{}]", e.what()); |
| 72 | return std::make_error_code(std::errc::no_buffer_space); |
| 73 | } |
| 74 | |
| 75 | DWORD bytesRead = 0; |
| 76 | if (!ReadFile(file->value(), buffer.data(), static_cast<DWORD>(buffer.size()), &bytesRead, NULL)) |
| 77 | { |
| 78 | auto ec = LastWin32Error(); |
| 79 | Log::Debug("Failed ReadFile [{}]", ec); |
| 80 | return ec; |
| 81 | } |
| 82 | |
| 83 | return buffer; |
| 84 | } |
| 85 | |
| 86 | } // namespace Orc |
nothing calls this directly
no test coverage detected