From ffcd87fb2b7886aac179594df96e0654ef7f32b0 Mon Sep 17 00:00:00 2001 From: ThinkChaos Date: Wed, 14 Feb 2024 20:34:23 -0500 Subject: [PATCH] fix `builtins.readFile` for non regular files like `/dev/stdin` --- src/libutil/posix-source-accessor.cc | 10 ++++++++++ src/libutil/posix-source-accessor.hh | 2 ++ tests/functional/local.mk | 1 + 3 files changed, 13 insertions(+) diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index 0300de01e..e1eaeb012 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -33,6 +33,13 @@ 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, @@ -50,6 +57,9 @@ 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"); + sizeCallback(st.st_size); off_t left = st.st_size; diff --git a/src/libutil/posix-source-accessor.hh b/src/libutil/posix-source-accessor.hh index 717c8f017..639fdaaaf 100644 --- a/src/libutil/posix-source-accessor.hh +++ b/src/libutil/posix-source-accessor.hh @@ -25,6 +25,8 @@ struct PosixSourceAccessor : virtual SourceAccessor */ time_t mtime = 0; + std::string readFile(const CanonPath & path) override; + void readFile( const CanonPath & path, Sink & sink, diff --git a/tests/functional/local.mk b/tests/functional/local.mk index 18eb887cd..d8b7ae354 100644 --- a/tests/functional/local.mk +++ b/tests/functional/local.mk @@ -60,6 +60,7 @@ nix_tests = \ fetchGitVerification.sh \ flakes/search-root.sh \ readfile-context.sh \ + readfile-stdin.sh \ nix-channel.sh \ recursive.sh \ dependencies.sh \