"nix-store -l": support compressed logs

This commit is contained in:
Eelco Dolstra 2012-05-30 00:00:02 -04:00
parent 4bc4da331a
commit 881beb170d
2 changed files with 34 additions and 9 deletions

View File

@ -5,7 +5,7 @@ nix_store_SOURCES = \
xmlgraph.cc xmlgraph.hh xmlgraph.cc xmlgraph.hh
nix_store_LDADD = ../libmain/libmain.la ../libstore/libstore.la ../libutil/libutil.la \ 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 nix-store.o: help.txt.hh

View File

@ -10,6 +10,13 @@
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <bzlib.h>
using namespace nix; using namespace nix;
@ -419,20 +426,38 @@ static void opReadLog(Strings opFlags, Strings opArgs)
{ {
if (!opFlags.empty()) throw UsageError("unknown flag"); if (!opFlags.empty()) throw UsageError("unknown flag");
for (Strings::iterator i = opArgs.begin(); foreach (Strings::iterator, i, opArgs) {
i != opArgs.end(); ++i)
{
Path path = useDeriver(followLinksToStorePath(*i)); Path path = useDeriver(followLinksToStorePath(*i));
Path logPath = (format("%1%/%2%/%3%") % Path logPath = (format("%1%/%2%/%3%") %
nixLogDir % drvsLogDir % baseNameOf(path)).str(); nixLogDir % drvsLogDir % baseNameOf(path)).str();
Path logBz2Path = logPath + ".bz2";
if (!pathExists(logPath)) if (pathExists(logPath)) {
throw Error(format("build log of derivation `%1%' is not available") % path); /* !!! 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. */ else if (pathExists(logBz2Path)) {
string log = readFile(logPath); AutoCloseFD fd = open(logBz2Path.c_str(), O_RDONLY);
writeFull(STDOUT_FILENO, (const unsigned char *) log.data(), log.size()); 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);
} }
} }