1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

readFile: Only read the full file if it's a non-regular one

Make `SourceAccessor::readFile` stream when reading a regular file, and
only fallback to a non-streaming version otherwise.
This commit is contained in:
Théophane Hufschmitt 2024-02-15 11:25:58 +01:00
parent ffcd87fb2b
commit 590e87e14d
2 changed files with 6 additions and 11 deletions

View file

@ -33,13 +33,6 @@ std::filesystem::path PosixSourceAccessor::makeAbsPath(const CanonPath & path)
: root / path.rel(); : root / path.rel();
} }
std::string PosixSourceAccessor::readFile(const CanonPath & path)
{
// Can't be implemented as a wrapper of the `Sink` based overload
// since this needs to support non-regular files (like `/dev/stdin`).
return nix::readFile(path.abs());
}
void PosixSourceAccessor::readFile( void PosixSourceAccessor::readFile(
const CanonPath & path, const CanonPath & path,
Sink & sink, Sink & sink,
@ -57,8 +50,12 @@ void PosixSourceAccessor::readFile(
if (fstat(fd.get(), &st) == -1) if (fstat(fd.get(), &st) == -1)
throw SysError("statting file"); throw SysError("statting file");
// logger->cout("%s %d", path.c_str(), st.st_size); if (!S_ISREG(st.st_mode)) {
assert(S_ISREG(st.st_mode) && "sinks are only compatible with regular files since they need to know the size in advance"); std::string fileContent = nix::readFile(path.abs());
sizeCallback(fileContent.size());
sink(fileContent);
return;
}
sizeCallback(st.st_size); sizeCallback(st.st_size);

View file

@ -25,8 +25,6 @@ struct PosixSourceAccessor : virtual SourceAccessor
*/ */
time_t mtime = 0; time_t mtime = 0;
std::string readFile(const CanonPath & path) override;
void readFile( void readFile(
const CanonPath & path, const CanonPath & path,
Sink & sink, Sink & sink,