PLEASE NOTE: hfile is expected to be an async handle (i.e. opened with FILE_FLAG_OVERLAPPED)
| 54 | // PLEASE NOTE: hfile is expected to be an async handle |
| 55 | // (i.e. opened with FILE_FLAG_OVERLAPPED) |
| 56 | SSIZE_T pread(HANDLE hfile, char* src, size_t num_bytes, uint64_t offset) { |
| 57 | assert(num_bytes <= std::numeric_limits<DWORD>::max()); |
| 58 | OVERLAPPED overlapped = {0}; |
| 59 | ULARGE_INTEGER offset_union; |
| 60 | offset_union.QuadPart = offset; |
| 61 | |
| 62 | overlapped.Offset = offset_union.LowPart; |
| 63 | overlapped.OffsetHigh = offset_union.HighPart; |
| 64 | overlapped.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL); |
| 65 | |
| 66 | if (NULL == overlapped.hEvent) { |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | SSIZE_T result = 0; |
| 71 | |
| 72 | unsigned long bytes_read = 0; |
| 73 | DWORD last_error = ERROR_SUCCESS; |
| 74 | |
| 75 | BOOL read_result = ::ReadFile(hfile, src, static_cast<DWORD>(num_bytes), |
| 76 | &bytes_read, &overlapped); |
| 77 | if (TRUE == read_result) { |
| 78 | result = bytes_read; |
| 79 | } else if ((FALSE == read_result) && |
| 80 | ((last_error = GetLastError()) != ERROR_IO_PENDING)) { |
| 81 | result = (last_error == ERROR_HANDLE_EOF) ? 0 : -1; |
| 82 | } else { |
| 83 | if (ERROR_IO_PENDING == |
| 84 | last_error) { // Otherwise bytes_read already has the result. |
| 85 | BOOL overlapped_result = |
| 86 | ::GetOverlappedResult(hfile, &overlapped, &bytes_read, TRUE); |
| 87 | if (FALSE == overlapped_result) { |
| 88 | result = (::GetLastError() == ERROR_HANDLE_EOF) ? 0 : -1; |
| 89 | } else { |
| 90 | result = bytes_read; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ::CloseHandle(overlapped.hEvent); |
| 96 | |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | // read() based random-access |
| 101 | class WindowsRandomAccessFile : public RandomAccessFile { |