From 15f4d4fd437070e898982c510228b6e8eea17b7d Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 15 Apr 2021 13:51:00 +0200 Subject: [PATCH] Drop libbz2 / zlib / lzma dependency + style fixes --- Makefile.config.in | 1 - configure.ac | 15 ---------- flake.nix | 2 +- src/libutil/compression.cc | 60 +++++++++++++++++++++++--------------- src/libutil/compression.hh | 6 ++-- src/libutil/local.mk | 2 +- src/libutil/tarfile.cc | 37 +++++++++++++---------- src/libutil/tarfile.hh | 12 ++++---- 8 files changed, 69 insertions(+), 66 deletions(-) diff --git a/Makefile.config.in b/Makefile.config.in index 3c1f01d1e..fd92365eb 100644 --- a/Makefile.config.in +++ b/Makefile.config.in @@ -15,7 +15,6 @@ LDFLAGS = @LDFLAGS@ LIBARCHIVE_LIBS = @LIBARCHIVE_LIBS@ LIBBROTLI_LIBS = @LIBBROTLI_LIBS@ LIBCURL_LIBS = @LIBCURL_LIBS@ -LIBLZMA_LIBS = @LIBLZMA_LIBS@ OPENSSL_LIBS = @OPENSSL_LIBS@ LIBSECCOMP_LIBS = @LIBSECCOMP_LIBS@ PACKAGE_NAME = @PACKAGE_NAME@ diff --git a/configure.ac b/configure.ac index a24287ff6..c8d59a212 100644 --- a/configure.ac +++ b/configure.ac @@ -172,11 +172,6 @@ fi PKG_CHECK_MODULES([OPENSSL], [libcrypto], [CXXFLAGS="$OPENSSL_CFLAGS $CXXFLAGS"]) -# Look for libbz2, a required dependency. -AC_CHECK_LIB([bz2], [BZ2_bzWriteOpen], [true], - [AC_MSG_ERROR([Nix requires libbz2, which is part of bzip2. See https://sourceware.org/bzip2/.])]) -AC_CHECK_HEADERS([bzlib.h], [true], - [AC_MSG_ERROR([Nix requires libbz2, which is part of bzip2. See https://sourceware.org/bzip2/.])]) # Checks for libarchive PKG_CHECK_MODULES([LIBARCHIVE], [libarchive >= 3.1.2], [CXXFLAGS="$LIBARCHIVE_CFLAGS $CXXFLAGS"]) # Workaround until https://github.com/libarchive/libarchive/issues/1446 is fixed @@ -205,16 +200,6 @@ PKG_CHECK_MODULES([EDITLINE], [libeditline], [CXXFLAGS="$EDITLINE_CFLAGS $CXXFLA # Look for libsodium, an optional dependency. PKG_CHECK_MODULES([SODIUM], [libsodium], [CXXFLAGS="$SODIUM_CFLAGS $CXXFLAGS"]) -# Look for liblzma, a required dependency. -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"]) diff --git a/flake.nix b/flake.nix index 58dc5019d..b8d5e0087 100644 --- a/flake.nix +++ b/flake.nix @@ -82,7 +82,7 @@ buildDeps = [ curl - bzip2 xz brotli zlib editline + bzip2 xz brotli editline openssl sqlite libarchive boost diff --git a/src/libutil/compression.cc b/src/libutil/compression.cc index 8ba536000..8b50776b3 100644 --- a/src/libutil/compression.cc +++ b/src/libutil/compression.cc @@ -4,8 +4,6 @@ #include "finally.hh" #include "logging.hh" -#include -#include #include #include #include @@ -45,12 +43,13 @@ struct ArchiveDecompressionSource : Source ArchiveDecompressionSource(Source & src) : src(src) {} ~ArchiveDecompressionSource() override {} size_t read(char * data, size_t len) override { - struct archive_entry* ae; + struct archive_entry * ae; if (!archive) { archive = std::make_unique(src, true); - this->archive->check(archive_read_next_header(this->archive->archive, &ae), "Failed to read header (%s)"); + this->archive->check(archive_read_next_header(this->archive->archive, &ae), + "failed to read header (%s)"); if (archive_filter_count(this->archive->archive) < 2) { - throw CompressionError("Input compression not recognized."); + throw CompressionError("input compression not recognized"); } } ssize_t result = archive_read_data(this->archive->archive, data, len); @@ -58,18 +57,20 @@ struct ArchiveDecompressionSource : Source if (result == 0) { throw EndOfFile("reached end of compressed file"); } - this->archive->check(result, "Failed to read compressed data (%s)"); + this->archive->check(result, "failed to read compressed data (%s)"); return result; } }; + struct ArchiveCompressionSink : CompressionSink { Sink & nextSink; - struct archive* archive; + struct archive * archive; + ArchiveCompressionSink(Sink & nextSink, std::string format, bool parallel) : nextSink(nextSink) { archive = archive_write_new(); if (!archive) throw Error("failed to initialize libarchive"); - check(archive_write_add_filter_by_name(archive, format.c_str()), "Couldn't initialize compression (%s)"); + check(archive_write_add_filter_by_name(archive, format.c_str()), "couldn't initialize compression (%s)"); check(archive_write_set_format_raw(archive)); if (format == "xz" && parallel) { check(archive_write_set_filter_option(archive, format.c_str(), "threads", "0")); @@ -80,34 +81,46 @@ struct ArchiveCompressionSink : CompressionSink check(archive_write_set_bytes_in_last_block(archive, 1)); open(); } - ~ArchiveCompressionSink() override { + + ~ArchiveCompressionSink() override + { if (archive) archive_write_free(archive); } - void finish() override { + + void finish() override + { flush(); check(archive_write_close(archive)); } - void check(int err, const char *reason="Failed to compress (%s)") { + + void check(int err, const std::string & reason = "failed to compress (%s)") + { if (err == ARCHIVE_EOF) throw EndOfFile("reached end of archive"); else if (err != ARCHIVE_OK) throw Error(reason, archive_error_string(this->archive)); } - void write(std::string_view data) override { + + void write(std::string_view data) override + { ssize_t result = archive_write_data(archive, data.data(), data.length()); if (result <= 0) check(result); } + private: - void open() { - check(archive_write_open(archive, this, NULL, ArchiveCompressionSink::callback_write, NULL)); - struct archive_entry *ae = archive_entry_new(); + void open() + { + check(archive_write_open(archive, this, nullptr, ArchiveCompressionSink::callback_write, nullptr)); + auto ae = archive_entry_new(); archive_entry_set_filetype(ae, AE_IFREG); check(archive_write_header(archive, ae)); archive_entry_free(ae); } - static ssize_t callback_write(struct archive *archive, void *_self, const void *buffer, size_t length) { - ArchiveCompressionSink *self = (ArchiveCompressionSink *)_self; - self->nextSink({(const char*)buffer, length}); + + static ssize_t callback_write(struct archive * archive, void * _self, const void * buffer, size_t length) + { + auto self = (ArchiveCompressionSink *) _self; + self->nextSink({(const char *) buffer, length}); return length; } }; @@ -193,17 +206,17 @@ std::unique_ptr makeDecompressionSink(const std::string & method, Si else if (method == "br") return std::make_unique(nextSink); else - return sourceToSink([&](Source & source) { - auto decompressionSource = makeDecompressionSource(source); - decompressionSource->drainInto(nextSink); - }); + return sourceToSink([&](Source & source) { + auto decompressionSource = makeDecompressionSource(source); + decompressionSource->drainInto(nextSink); + }); } struct BrotliCompressionSink : ChunkedCompressionSink { Sink & nextSink; uint8_t outbuf[BUFSIZ]; - BrotliEncoderState *state; + BrotliEncoderState * state; bool finished = false; BrotliCompressionSink(Sink & nextSink) : nextSink(nextSink) @@ -251,6 +264,7 @@ struct BrotliCompressionSink : ChunkedCompressionSink } } }; + std::unique_ptr makeDecompressionSource(Source & prev) { return std::unique_ptr(new ArchiveDecompressionSource(prev)); } diff --git a/src/libutil/compression.hh b/src/libutil/compression.hh index 192cb3e91..a140f8be2 100644 --- a/src/libutil/compression.hh +++ b/src/libutil/compression.hh @@ -10,9 +10,9 @@ namespace nix { struct CompressionSink : BufferedSink, FinishSink { - using BufferedSink::operator (); - using BufferedSink::write; - using FinishSink::finish; + using BufferedSink::operator (); + using BufferedSink::write; + using FinishSink::finish; }; std::unique_ptr makeDecompressionSource(Source & prev); diff --git a/src/libutil/local.mk b/src/libutil/local.mk index 5341c58e6..3a6415ee3 100644 --- a/src/libutil/local.mk +++ b/src/libutil/local.mk @@ -6,7 +6,7 @@ libutil_DIR := $(d) libutil_SOURCES := $(wildcard $(d)/*.cc) -libutil_LDFLAGS = $(LIBLZMA_LIBS) -lbz2 -pthread $(OPENSSL_LIBS) $(LIBBROTLI_LIBS) $(LIBARCHIVE_LIBS) $(BOOST_LDFLAGS) -lboost_context +libutil_LDFLAGS = -pthread $(OPENSSL_LIBS) $(LIBBROTLI_LIBS) $(LIBARCHIVE_LIBS) $(BOOST_LDFLAGS) -lboost_context ifeq ($(HAVE_LIBCPUID), 1) libutil_LDFLAGS += -lcpuid diff --git a/src/libutil/tarfile.cc b/src/libutil/tarfile.cc index b5e1cb4c0..24905130d 100644 --- a/src/libutil/tarfile.cc +++ b/src/libutil/tarfile.cc @@ -5,38 +5,41 @@ #include "tarfile.hh" namespace nix { -static int callback_open(struct archive *, void *self) { + +static int callback_open(struct archive *, void * self) +{ return ARCHIVE_OK; } -static ssize_t callback_read(struct archive * archive, void * _self, const void * * buffer) { - TarArchive *self = (TarArchive *)_self; +static ssize_t callback_read(struct archive * archive, void * _self, const void * * buffer) +{ + auto self = (TarArchive *) _self; *buffer = self->buffer.data(); try { return self->source->read((char *) self->buffer.data(), 4096); } catch (EndOfFile &) { return 0; - } catch (std::exception &err) { + } catch (std::exception & err) { archive_set_error(archive, EIO, "Source threw exception: %s", err.what()); - return -1; } } -static int callback_close(struct archive *, void *self) { +static int callback_close(struct archive *, void * self) +{ return ARCHIVE_OK; } -void TarArchive::check(int err, const char *reason) +void TarArchive::check(int err, const std::string & reason) { - if (err == ARCHIVE_EOF) - throw EndOfFile("reached end of archive"); - else if (err != ARCHIVE_OK) - throw Error(reason, archive_error_string(this->archive)); - } + if (err == ARCHIVE_EOF) + throw EndOfFile("reached end of archive"); + else if (err != ARCHIVE_OK) + throw Error(reason, archive_error_string(this->archive)); +} -TarArchive::TarArchive(Source& source, bool raw) : buffer(4096) +TarArchive::TarArchive(Source & source, bool raw) : buffer(4096) { this->archive = archive_read_new(); this->source = &source; @@ -53,7 +56,7 @@ TarArchive::TarArchive(Source& source, bool raw) : buffer(4096) } -TarArchive::TarArchive(const Path &path) +TarArchive::TarArchive(const Path & path) { this->archive = archive_read_new(); @@ -62,11 +65,13 @@ TarArchive::TarArchive(const Path &path) check(archive_read_open_filename(archive, path.c_str(), 16384), "failed to open archive: %s"); } -void TarArchive::close() { +void TarArchive::close() +{ check(archive_read_close(this->archive), "Failed to close archive (%s)"); } -TarArchive::~TarArchive() { +TarArchive::~TarArchive() +{ if (this->archive) archive_read_free(this->archive); } diff --git a/src/libutil/tarfile.hh b/src/libutil/tarfile.hh index 18adf3490..4d9141fd4 100644 --- a/src/libutil/tarfile.hh +++ b/src/libutil/tarfile.hh @@ -4,18 +4,18 @@ namespace nix { struct TarArchive { - struct archive *archive; - Source *source; + struct archive * archive; + Source * source; std::vector buffer; - void check(int err, const char *reason = "Failed to extract archive (%s)"); + void check(int err, const std::string & reason = "failed to extract archive (%s)"); - TarArchive(Source& source, bool raw = false); + TarArchive(Source & source, bool raw = false); - TarArchive(const Path &path); + TarArchive(const Path & path); // disable copy constructor - TarArchive(const TarArchive&) = delete; + TarArchive(const TarArchive &) = delete; void close();