diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2012-05-30T04·00-0400 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2012-05-30T04·00-0400 |
commit | 881beb170d324f392a53168e6a76e2c5cfd24789 (patch) | |
tree | 49f24fca0ed34da41e613c2a5d92aea0c6a5c57c /src | |
parent | 4bc4da331aae13be8a39e768524a854597addd8a (diff) |
"nix-store -l": support compressed logs
Diffstat (limited to 'src')
-rw-r--r-- | src/nix-store/Makefile.am | 2 | ||||
-rw-r--r-- | src/nix-store/nix-store.cc | 41 |
2 files changed, 34 insertions, 9 deletions
diff --git a/src/nix-store/Makefile.am b/src/nix-store/Makefile.am index 44ff54674ade..ef8d760d6f5d 100644 --- a/src/nix-store/Makefile.am +++ b/src/nix-store/Makefile.am @@ -5,7 +5,7 @@ nix_store_SOURCES = \ xmlgraph.cc xmlgraph.hh nix_store_LDADD = ../libmain/libmain.la ../libstore/libstore.la ../libutil/libutil.la \ - ../boost/format/libformat.la + ../boost/format/libformat.la -lbz2 nix-store.o: help.txt.hh diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 36c04168259c..4768c2e7a257 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -10,6 +10,13 @@ #include <iostream> #include <algorithm> +#include <cstdio> + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#include <bzlib.h> using namespace nix; @@ -419,20 +426,38 @@ static void opReadLog(Strings opFlags, Strings opArgs) { if (!opFlags.empty()) throw UsageError("unknown flag"); - for (Strings::iterator i = opArgs.begin(); - i != opArgs.end(); ++i) - { + foreach (Strings::iterator, i, opArgs) { Path path = useDeriver(followLinksToStorePath(*i)); Path logPath = (format("%1%/%2%/%3%") % nixLogDir % drvsLogDir % baseNameOf(path)).str(); + Path logBz2Path = logPath + ".bz2"; - if (!pathExists(logPath)) - throw Error(format("build log of derivation `%1%' is not available") % path); + if (pathExists(logPath)) { + /* !!! Make this run in O(1) memory. */ + string log = readFile(logPath); + writeFull(STDOUT_FILENO, (const unsigned char *) log.data(), log.size()); + } - /* !!! Make this run in O(1) memory. */ - string log = readFile(logPath); - writeFull(STDOUT_FILENO, (const unsigned char *) log.data(), log.size()); + else if (pathExists(logBz2Path)) { + AutoCloseFD fd = open(logBz2Path.c_str(), O_RDONLY); + FILE * f = 0; + if (fd == -1 || (f = fdopen(fd.borrow(), "r")) == 0) + throw SysError(format("opening file `%1%'") % logBz2Path); + int err; + BZFILE * bz = BZ2_bzReadOpen(&err, f, 0, 0, 0, 0); + if (!bz) throw Error(format("cannot open bzip2 file `%1%'") % logBz2Path); + unsigned char buf[128 * 1024]; + do { + int n = BZ2_bzRead(&err, bz, buf, sizeof(buf)); + if (err != BZ_OK && err != BZ_STREAM_END) + throw Error(format("error reading bzip2 file `%1%'") % logBz2Path); + writeFull(STDOUT_FILENO, buf, n); + } while (err != BZ_STREAM_END); + BZ2_bzReadClose(&err, bz); + } + + else throw Error(format("build log of derivation `%1%' is not available") % path); } } |