From 590e87e14da312cae1a3ec1ed0f3de2a690f6446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Hufschmitt?= Date: Thu, 15 Feb 2024 11:25:58 +0100 Subject: [PATCH] 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. --- src/libutil/posix-source-accessor.cc | 15 ++++++--------- src/libutil/posix-source-accessor.hh | 2 -- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index e1eaeb012..b96df39f0 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -33,13 +33,6 @@ std::filesystem::path PosixSourceAccessor::makeAbsPath(const CanonPath & path) : 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( const CanonPath & path, Sink & sink, @@ -57,8 +50,12 @@ void PosixSourceAccessor::readFile( if (fstat(fd.get(), &st) == -1) throw SysError("statting file"); - // logger->cout("%s %d", path.c_str(), st.st_size); - assert(S_ISREG(st.st_mode) && "sinks are only compatible with regular files since they need to know the size in advance"); + if (!S_ISREG(st.st_mode)) { + std::string fileContent = nix::readFile(path.abs()); + sizeCallback(fileContent.size()); + sink(fileContent); + return; + } sizeCallback(st.st_size); diff --git a/src/libutil/posix-source-accessor.hh b/src/libutil/posix-source-accessor.hh index 639fdaaaf..717c8f017 100644 --- a/src/libutil/posix-source-accessor.hh +++ b/src/libutil/posix-source-accessor.hh @@ -25,8 +25,6 @@ struct PosixSourceAccessor : virtual SourceAccessor */ time_t mtime = 0; - std::string readFile(const CanonPath & path) override; - void readFile( const CanonPath & path, Sink & sink,