mirror of
https://github.com/NixOS/nix.git
synced 2025-11-12 05:26:02 +01:00
fix(libstore/find-cycles): use readFull for robust file reading
Replaced raw `read()` with `readFull()` helper, which properly handles partial reads and `EINTR`. The previous code manually checked for errors but didn't handle the case where `read()` returns fewer bytes than requested.
This commit is contained in:
parent
2e9ea55795
commit
591851cd5e
1 changed files with 6 additions and 7 deletions
|
|
@ -107,14 +107,13 @@ void scanForCycleEdges2(const std::string & path, CycleEdgeScanSink & sink)
|
||||||
// Stream file contents into sink
|
// Stream file contents into sink
|
||||||
// RefScanSink handles buffer boundaries automatically
|
// RefScanSink handles buffer boundaries automatically
|
||||||
std::vector<char> buf(65536);
|
std::vector<char> buf(65536);
|
||||||
while (true) {
|
size_t remaining = st.st_size;
|
||||||
ssize_t n = read(fd.get(), buf.data(), buf.size());
|
|
||||||
if (n == -1)
|
|
||||||
throw SysError("reading file '%1%'", path);
|
|
||||||
if (n == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
sink(std::string_view(buf.data(), n));
|
while (remaining > 0) {
|
||||||
|
size_t toRead = std::min(remaining, buf.size());
|
||||||
|
readFull(fd.get(), buf.data(), toRead);
|
||||||
|
sink(std::string_view(buf.data(), toRead));
|
||||||
|
remaining -= toRead;
|
||||||
}
|
}
|
||||||
} else if (S_ISDIR(st.st_mode)) {
|
} else if (S_ISDIR(st.st_mode)) {
|
||||||
// Handle directories - recursively scan contents
|
// Handle directories - recursively scan contents
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue