| 141 | } |
| 142 | |
| 143 | Result<BytesView> DiskUtils::loadFromFd(int fd) { |
| 144 | auto stat = statFromFd(fd); |
| 145 | if (!stat.exists()) { |
| 146 | return onLoadFromFdError(fd, "File does not exist"); |
| 147 | } |
| 148 | |
| 149 | auto fileSize = stat.size(); |
| 150 | |
| 151 | auto bytes = makeShared<ByteBuffer>(); |
| 152 | bytes->resize(fileSize); |
| 153 | |
| 154 | size_t totalRead = 0; |
| 155 | while (totalRead < fileSize) { |
| 156 | auto result = read(fd, (*bytes)[totalRead], fileSize - totalRead); |
| 157 | if (result < 0) { |
| 158 | return onLoadFromFdError(fd, strerror(errno)); |
| 159 | } |
| 160 | |
| 161 | if (result == 0) { |
| 162 | return onLoadFromFdError(fd, "Unexpectedly reached end of file"); |
| 163 | } |
| 164 | |
| 165 | totalRead += result; |
| 166 | } |
| 167 | |
| 168 | return bytes->toBytesView(); |
| 169 | } |
| 170 | |
| 171 | Result<Void> DiskUtils::store(const Path& path, const BytesView& bytes) { |
| 172 | return store(path, bytes.asStringView()); |
nothing calls this directly
no test coverage detected