MCPcopy Create free account
hub / github.com/Snapchat/Valdi / loadFromFd

Method loadFromFd

valdi_core/src/valdi_core/cpp/Utils/DiskUtils.cpp:143–169  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

141}
142
143Result<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
171Result<Void> DiskUtils::store(const Path& path, const BytesView& bytes) {
172 return store(path, bytes.asStringView());

Callers

nothing calls this directly

Calls 6

statFromFdFunction · 0.85
onLoadFromFdErrorFunction · 0.85
existsMethod · 0.65
sizeMethod · 0.45
resizeMethod · 0.45
toBytesViewMethod · 0.45

Tested by

no test coverage detected