aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/compression.cc60
-rw-r--r--src/libutil/compression.hh6
-rw-r--r--src/libutil/local.mk2
-rw-r--r--src/libutil/tarfile.cc37
-rw-r--r--src/libutil/tarfile.hh12
5 files changed, 68 insertions, 49 deletions
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 <lzma.h>
-#include <bzlib.h>
#include <archive.h>
#include <archive_entry.h>
#include <cstdio>
@@ -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<TarArchive>(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<FinishSink> makeDecompressionSink(const std::string & method, Si
else if (method == "br")
return std::make_unique<BrotliDecompressionSink>(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<Source> makeDecompressionSource(Source & prev) {
return std::unique_ptr<Source>(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<Source> 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<unsigned char> 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();