| 1646 | } |
| 1647 | |
| 1648 | Result<int64_t> FileRead(int fd, uint8_t* buffer, int64_t nbytes) { |
| 1649 | #if defined(_WIN32) |
| 1650 | HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); |
| 1651 | #endif |
| 1652 | int64_t total_bytes_read = 0; |
| 1653 | |
| 1654 | while (total_bytes_read < nbytes) { |
| 1655 | const int64_t chunksize = |
| 1656 | std::min(static_cast<int64_t>(ARROW_MAX_IO_CHUNKSIZE), nbytes - total_bytes_read); |
| 1657 | int64_t bytes_read = 0; |
| 1658 | #if defined(_WIN32) |
| 1659 | DWORD dwBytesRead = 0; |
| 1660 | if (!ReadFile(handle, buffer, static_cast<uint32_t>(chunksize), &dwBytesRead, |
| 1661 | nullptr)) { |
| 1662 | auto errnum = GetLastError(); |
| 1663 | // Return a normal EOF when the write end of a pipe was closed |
| 1664 | if (errnum != ERROR_HANDLE_EOF && errnum != ERROR_BROKEN_PIPE) { |
| 1665 | return IOErrorFromWinError(GetLastError(), "Error reading bytes from file"); |
| 1666 | } |
| 1667 | } |
| 1668 | bytes_read = dwBytesRead; |
| 1669 | #else |
| 1670 | bytes_read = static_cast<int64_t>(read(fd, buffer, static_cast<size_t>(chunksize))); |
| 1671 | if (bytes_read == -1) { |
| 1672 | if (errno == EINTR) { |
| 1673 | continue; |
| 1674 | } |
| 1675 | return IOErrorFromErrno(errno, "Error reading bytes from file"); |
| 1676 | } |
| 1677 | #endif |
| 1678 | |
| 1679 | if (bytes_read == 0) { |
| 1680 | // EOF |
| 1681 | break; |
| 1682 | } |
| 1683 | buffer += bytes_read; |
| 1684 | total_bytes_read += bytes_read; |
| 1685 | } |
| 1686 | return total_bytes_read; |
| 1687 | } |
| 1688 | |
| 1689 | Result<int64_t> FileReadAt(int fd, uint8_t* buffer, int64_t position, int64_t nbytes) { |
| 1690 | int64_t bytes_read = 0; |