Skip to content

Commit

Permalink
Initial gzip support
Browse files Browse the repository at this point in the history
Closes NixOS#3256

(cherry picked from commit c6295a3)
  • Loading branch information
Tom Bereknyei authored and dtzWill committed Dec 15, 2019
1 parent 95a14b9 commit 3db6fb4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ PKG_CHECK_MODULES([LIBLZMA], [liblzma], [CXXFLAGS="$LIBLZMA_CFLAGS $CXXFLAGS"])
AC_CHECK_LIB([lzma], [lzma_stream_encoder_mt],
[AC_DEFINE([HAVE_LZMA_MT], [1], [xz multithreaded compression support])])

# Look for zlib, a required dependency.
PKG_CHECK_MODULES([ZLIB], [zlib], [CXXFLAGS="$ZLIB_CFLAGS $CXXFLAGS"])
AC_CHECK_HEADER([zlib.h],[:],[AC_MSG_ERROR([could not find the zlib.h header])])
LDFLAGS="-lz $LDFLAGS"

# Look for libbrotli{enc,dec}.
PKG_CHECK_MODULES([LIBBROTLI], [libbrotlienc libbrotlidec], [CXXFLAGS="$LIBBROTLI_CFLAGS $CXXFLAGS"])
Expand Down
4 changes: 2 additions & 2 deletions release-common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ rec {
});

buildDeps =
[ curl
bzip2 xz brotli editline
[ curl
bzip2 xz brotli zlib editline
openssl pkgconfig sqlite boehmgc
boost
nlohmann_json
Expand Down
63 changes: 63 additions & 0 deletions src/libutil/compression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <brotli/decode.h>
#include <brotli/encode.h>

#include <zlib.h>

#include <iostream>

namespace nix {
Expand Down Expand Up @@ -64,6 +66,65 @@ struct NoneSink : CompressionSink
void write(const unsigned char * data, size_t len) override { nextSink(data, len); }
};

struct GzipDecompressionSink : CompressionSink
{
Sink & nextSink;
z_stream strm;
bool finished = false;
uint8_t outbuf[BUFSIZ];

GzipDecompressionSink(Sink & nextSink) : nextSink(nextSink)
{
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
strm.next_out = outbuf;
strm.avail_out = sizeof(outbuf);

// Enable gzip and zlib decoding (+32) with 15 windowBits
int ret = inflateInit2(&strm,15+32);
if (ret != Z_OK)
throw CompressionError("unable to initialise gzip encoder");
}

~GzipDecompressionSink()
{
inflateEnd(&strm);
}

void finish() override
{
CompressionSink::flush();
write(nullptr, 0);
}

void write(const unsigned char * data, size_t len) override
{
assert(len <= std::numeric_limits<decltype(strm.avail_in)>::max());

strm.next_in = (Bytef *) data;
strm.avail_in = len;

while (!finished && (!data || strm.avail_in)) {
checkInterrupt();

int ret = inflate(&strm,Z_SYNC_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END)
throw CompressionError("error while decompressing gzip file: %d: %d: %d",ret, len, strm.avail_in);


finished = ret == Z_STREAM_END;

if (strm.avail_out < sizeof(outbuf) || strm.avail_in == 0) {
nextSink(outbuf, sizeof(outbuf) - strm.avail_out);
strm.next_out = (Bytef *) outbuf;
strm.avail_out = sizeof(outbuf);
}
}
}
};

struct XzDecompressionSink : CompressionSink
{
Expand Down Expand Up @@ -238,6 +299,8 @@ ref<CompressionSink> makeDecompressionSink(const std::string & method, Sink & ne
return make_ref<XzDecompressionSink>(nextSink);
else if (method == "bzip2")
return make_ref<BzipDecompressionSink>(nextSink);
else if (method == "gzip")
return make_ref<GzipDecompressionSink>(nextSink);
else if (method == "br")
return make_ref<BrotliDecompressionSink>(nextSink);
else
Expand Down
17 changes: 17 additions & 0 deletions tests/tarball.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ nix-instantiate --eval -E 'with <fnord/xyzzy>; 1 + 2' -I fnord=file://no-such-ta
(! nix-instantiate --eval -E '<fnord/xyzzy> 1' -I fnord=file://no-such-tarball.tar.xz)

nix-instantiate --eval -E '<fnord/config.nix>' -I fnord=file://no-such-tarball.tar.xz -I fnord=.

tarball=$TEST_ROOT/tarball.tar.gz
(cd $TEST_ROOT && tar c tarball) | gzip > $tarball

nix-env -f file://$tarball -qa --out-path | grep -q dependencies

nix-build -o $TEST_ROOT/result file://$tarball

nix-build -o $TEST_ROOT/result '<foo>' -I foo=file://$tarball

nix-build -o $TEST_ROOT/result -E "import (fetchTarball file://$tarball)"

nix-instantiate --eval -E '1 + 2' -I fnord=file://no-such-tarball.tar.gz
nix-instantiate --eval -E 'with <fnord/xyzzy>; 1 + 2' -I fnord=file://no-such-tarball.tar.gz
(! nix-instantiate --eval -E '<fnord/xyzzy> 1' -I fnord=file://no-such-tarball.tar.gz)

nix-instantiate --eval -E '<fnord/config.nix>' -I fnord=file://no-such-tarball.tar.gz -I fnord=.

0 comments on commit 3db6fb4

Please sign in to comment.