| 1588 | Status FileSeek(int fd, int64_t pos) { return FileSeek(fd, pos, SEEK_SET); } |
| 1589 | |
| 1590 | Result<int64_t> FileGetSize(int fd) { |
| 1591 | #if defined(_WIN32) |
| 1592 | struct __stat64 st; |
| 1593 | #else |
| 1594 | struct stat st; |
| 1595 | #endif |
| 1596 | st.st_size = -1; |
| 1597 | |
| 1598 | #if defined(_WIN32) |
| 1599 | int ret = _fstat64(fd, &st); |
| 1600 | #else |
| 1601 | int ret = fstat(fd, &st); |
| 1602 | #endif |
| 1603 | |
| 1604 | if (ret == -1) { |
| 1605 | return Status::IOError("error stat()ing file"); |
| 1606 | } |
| 1607 | if (st.st_size == 0) { |
| 1608 | // Maybe the file doesn't support getting its size, double-check by |
| 1609 | // trying to tell() (seekable files usually have a size, while |
| 1610 | // non-seekable files don't) |
| 1611 | RETURN_NOT_OK(FileTell(fd)); |
| 1612 | } else if (st.st_size < 0) { |
| 1613 | return Status::IOError("error getting file size"); |
| 1614 | } |
| 1615 | return st.st_size; |
| 1616 | } |
| 1617 | |
| 1618 | // |
| 1619 | // Reading data |