mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 14:06:16 +02:00
nfc(libutil): reformat files
Run clang-format on compression.{cc,hh} and tarfile{cc,hh}. This way follow-up patches will be formatted properly and have easier to read diffs.
This commit is contained in:
parent
b72e1c79da
commit
6d9bafb3b8
4 changed files with 63 additions and 49 deletions
|
@ -12,8 +12,6 @@
|
||||||
#include <brotli/decode.h>
|
#include <brotli/decode.h>
|
||||||
#include <brotli/encode.h>
|
#include <brotli/encode.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
static const int COMPRESSION_LEVEL_DEFAULT = -1;
|
static const int COMPRESSION_LEVEL_DEFAULT = -1;
|
||||||
|
@ -40,20 +38,24 @@ struct ArchiveDecompressionSource : Source
|
||||||
{
|
{
|
||||||
std::unique_ptr<TarArchive> archive = 0;
|
std::unique_ptr<TarArchive> archive = 0;
|
||||||
Source & src;
|
Source & src;
|
||||||
ArchiveDecompressionSource(Source & src) : src(src) {}
|
ArchiveDecompressionSource(Source & src)
|
||||||
|
: src(src)
|
||||||
|
{
|
||||||
|
}
|
||||||
~ArchiveDecompressionSource() override {}
|
~ArchiveDecompressionSource() override {}
|
||||||
size_t read(char * data, size_t len) override {
|
size_t read(char * data, size_t len) override
|
||||||
|
{
|
||||||
struct archive_entry * ae;
|
struct archive_entry * ae;
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
archive = std::make_unique<TarArchive>(src, true);
|
archive = std::make_unique<TarArchive>(src, true);
|
||||||
this->archive->check(archive_read_next_header(this->archive->archive, &ae),
|
this->archive->check(archive_read_next_header(this->archive->archive, &ae), "failed to read header (%s)");
|
||||||
"failed to read header (%s)");
|
|
||||||
if (archive_filter_count(this->archive->archive) < 2) {
|
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);
|
ssize_t result = archive_read_data(this->archive->archive, data, len);
|
||||||
if (result > 0) return result;
|
if (result > 0)
|
||||||
|
return result;
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
throw EndOfFile("reached end of compressed file");
|
throw EndOfFile("reached end of compressed file");
|
||||||
}
|
}
|
||||||
|
@ -67,16 +69,19 @@ struct ArchiveCompressionSink : CompressionSink
|
||||||
Sink & nextSink;
|
Sink & nextSink;
|
||||||
struct archive * archive;
|
struct archive * archive;
|
||||||
|
|
||||||
ArchiveCompressionSink(Sink & nextSink, std::string format, bool parallel, int level = COMPRESSION_LEVEL_DEFAULT) : nextSink(nextSink)
|
ArchiveCompressionSink(Sink & nextSink, std::string format, bool parallel, int level = COMPRESSION_LEVEL_DEFAULT)
|
||||||
|
: nextSink(nextSink)
|
||||||
{
|
{
|
||||||
archive = archive_write_new();
|
archive = archive_write_new();
|
||||||
if (!archive) throw Error("failed to initialize libarchive");
|
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));
|
check(archive_write_set_format_raw(archive));
|
||||||
if (parallel)
|
if (parallel)
|
||||||
check(archive_write_set_filter_option(archive, format.c_str(), "threads", "0"));
|
check(archive_write_set_filter_option(archive, format.c_str(), "threads", "0"));
|
||||||
if (level != COMPRESSION_LEVEL_DEFAULT)
|
if (level != COMPRESSION_LEVEL_DEFAULT)
|
||||||
check(archive_write_set_filter_option(archive, format.c_str(), "compression-level", std::to_string(level).c_str()));
|
check(archive_write_set_filter_option(
|
||||||
|
archive, format.c_str(), "compression-level", std::to_string(level).c_str()));
|
||||||
// disable internal buffering
|
// disable internal buffering
|
||||||
check(archive_write_set_bytes_per_block(archive, 0));
|
check(archive_write_set_bytes_per_block(archive, 0));
|
||||||
// disable output padding
|
// disable output padding
|
||||||
|
@ -86,7 +91,8 @@ struct ArchiveCompressionSink : CompressionSink
|
||||||
|
|
||||||
~ArchiveCompressionSink() override
|
~ArchiveCompressionSink() override
|
||||||
{
|
{
|
||||||
if (archive) archive_write_free(archive);
|
if (archive)
|
||||||
|
archive_write_free(archive);
|
||||||
}
|
}
|
||||||
|
|
||||||
void finish() override
|
void finish() override
|
||||||
|
@ -106,7 +112,8 @@ struct ArchiveCompressionSink : CompressionSink
|
||||||
void writeUnbuffered(std::string_view data) override
|
void writeUnbuffered(std::string_view data) override
|
||||||
{
|
{
|
||||||
ssize_t result = archive_write_data(archive, data.data(), data.length());
|
ssize_t result = archive_write_data(archive, data.data(), data.length());
|
||||||
if (result <= 0) check(result);
|
if (result <= 0)
|
||||||
|
check(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -130,13 +137,20 @@ private:
|
||||||
struct NoneSink : CompressionSink
|
struct NoneSink : CompressionSink
|
||||||
{
|
{
|
||||||
Sink & nextSink;
|
Sink & nextSink;
|
||||||
NoneSink(Sink & nextSink, int level = COMPRESSION_LEVEL_DEFAULT) : nextSink(nextSink)
|
NoneSink(Sink & nextSink, int level = COMPRESSION_LEVEL_DEFAULT)
|
||||||
|
: nextSink(nextSink)
|
||||||
{
|
{
|
||||||
if (level != COMPRESSION_LEVEL_DEFAULT)
|
if (level != COMPRESSION_LEVEL_DEFAULT)
|
||||||
warn("requested compression level '%d' not supported by compression method 'none'", level);
|
warn("requested compression level '%d' not supported by compression method 'none'", level);
|
||||||
}
|
}
|
||||||
void finish() override { flush(); }
|
void finish() override
|
||||||
void writeUnbuffered(std::string_view data) override { nextSink(data); }
|
{
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
void writeUnbuffered(std::string_view data) override
|
||||||
|
{
|
||||||
|
nextSink(data);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BrotliDecompressionSink : ChunkedCompressionSink
|
struct BrotliDecompressionSink : ChunkedCompressionSink
|
||||||
|
@ -145,7 +159,8 @@ struct BrotliDecompressionSink : ChunkedCompressionSink
|
||||||
BrotliDecoderState * state;
|
BrotliDecoderState * state;
|
||||||
bool finished = false;
|
bool finished = false;
|
||||||
|
|
||||||
BrotliDecompressionSink(Sink & nextSink) : nextSink(nextSink)
|
BrotliDecompressionSink(Sink & nextSink)
|
||||||
|
: nextSink(nextSink)
|
||||||
{
|
{
|
||||||
state = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
|
state = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
|
||||||
if (!state)
|
if (!state)
|
||||||
|
@ -173,10 +188,7 @@ struct BrotliDecompressionSink : ChunkedCompressionSink
|
||||||
while (!finished && (!data.data() || avail_in)) {
|
while (!finished && (!data.data() || avail_in)) {
|
||||||
checkInterrupt();
|
checkInterrupt();
|
||||||
|
|
||||||
if (!BrotliDecoderDecompressStream(state,
|
if (!BrotliDecoderDecompressStream(state, &avail_in, &next_in, &avail_out, &next_out, nullptr))
|
||||||
&avail_in, &next_in,
|
|
||||||
&avail_out, &next_out,
|
|
||||||
nullptr))
|
|
||||||
throw CompressionError("error while decompressing brotli file");
|
throw CompressionError("error while decompressing brotli file");
|
||||||
|
|
||||||
if (avail_out < sizeof(outbuf) || avail_in == 0) {
|
if (avail_out < sizeof(outbuf) || avail_in == 0) {
|
||||||
|
@ -219,7 +231,8 @@ struct BrotliCompressionSink : ChunkedCompressionSink
|
||||||
BrotliEncoderState * state;
|
BrotliEncoderState * state;
|
||||||
bool finished = false;
|
bool finished = false;
|
||||||
|
|
||||||
BrotliCompressionSink(Sink & nextSink) : nextSink(nextSink)
|
BrotliCompressionSink(Sink & nextSink)
|
||||||
|
: nextSink(nextSink)
|
||||||
{
|
{
|
||||||
state = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr);
|
state = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr);
|
||||||
if (!state)
|
if (!state)
|
||||||
|
@ -247,11 +260,9 @@ struct BrotliCompressionSink : ChunkedCompressionSink
|
||||||
while (!finished && (!data.data() || avail_in)) {
|
while (!finished && (!data.data() || avail_in)) {
|
||||||
checkInterrupt();
|
checkInterrupt();
|
||||||
|
|
||||||
if (!BrotliEncoderCompressStream(state,
|
if (!BrotliEncoderCompressStream(
|
||||||
data.data() ? BROTLI_OPERATION_PROCESS : BROTLI_OPERATION_FINISH,
|
state, data.data() ? BROTLI_OPERATION_PROCESS : BROTLI_OPERATION_FINISH, &avail_in, &next_in,
|
||||||
&avail_in, &next_in,
|
&avail_out, &next_out, nullptr))
|
||||||
&avail_out, &next_out,
|
|
||||||
nullptr))
|
|
||||||
throw CompressionError("error while compressing brotli compression");
|
throw CompressionError("error while compressing brotli compression");
|
||||||
|
|
||||||
if (avail_out < sizeof(outbuf) || avail_in == 0) {
|
if (avail_out < sizeof(outbuf) || avail_in == 0) {
|
||||||
|
@ -267,9 +278,8 @@ struct BrotliCompressionSink : ChunkedCompressionSink
|
||||||
|
|
||||||
ref<CompressionSink> makeCompressionSink(const std::string & method, Sink & nextSink, const bool parallel, int level)
|
ref<CompressionSink> makeCompressionSink(const std::string & method, Sink & nextSink, const bool parallel, int level)
|
||||||
{
|
{
|
||||||
std::vector<std::string> la_supports = {
|
std::vector<std::string> la_supports = {"bzip2", "compress", "grzip", "gzip", "lrzip", "lz4",
|
||||||
"bzip2", "compress", "grzip", "gzip", "lrzip", "lz4", "lzip", "lzma", "lzop", "xz", "zstd"
|
"lzip", "lzma", "lzop", "xz", "zstd"};
|
||||||
};
|
|
||||||
if (std::find(la_supports.begin(), la_supports.end(), method) != la_supports.end()) {
|
if (std::find(la_supports.begin(), la_supports.end(), method) != la_supports.end()) {
|
||||||
return make_ref<ArchiveCompressionSink>(nextSink, method, parallel, level);
|
return make_ref<ArchiveCompressionSink>(nextSink, method, parallel, level);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace nix {
|
||||||
|
|
||||||
struct CompressionSink : BufferedSink, FinishSink
|
struct CompressionSink : BufferedSink, FinishSink
|
||||||
{
|
{
|
||||||
using BufferedSink::operator ();
|
using BufferedSink::operator();
|
||||||
using BufferedSink::writeUnbuffered;
|
using BufferedSink::writeUnbuffered;
|
||||||
using FinishSink::finish;
|
using FinishSink::finish;
|
||||||
};
|
};
|
||||||
|
@ -22,7 +22,8 @@ std::unique_ptr<FinishSink> makeDecompressionSink(const std::string & method, Si
|
||||||
|
|
||||||
std::string compress(const std::string & method, std::string_view in, const bool parallel = false, int level = -1);
|
std::string compress(const std::string & method, std::string_view in, const bool parallel = false, int level = -1);
|
||||||
|
|
||||||
ref<CompressionSink> makeCompressionSink(const std::string & method, Sink & nextSink, const bool parallel = false, int level = -1);
|
ref<CompressionSink>
|
||||||
|
makeCompressionSink(const std::string & method, Sink & nextSink, const bool parallel = false, int level = -1);
|
||||||
|
|
||||||
MakeError(UnknownCompressionMethod, Error);
|
MakeError(UnknownCompressionMethod, Error);
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ static int callback_open(struct archive *, void * self)
|
||||||
return ARCHIVE_OK;
|
return ARCHIVE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t callback_read(struct archive * archive, void * _self, const void * * buffer)
|
static ssize_t callback_read(struct archive * archive, void * _self, const void ** buffer)
|
||||||
{
|
{
|
||||||
auto self = (TarArchive *) _self;
|
auto self = (TarArchive *) _self;
|
||||||
*buffer = self->buffer.data();
|
*buffer = self->buffer.data();
|
||||||
|
@ -40,7 +40,8 @@ void TarArchive::check(int err, const std::string & reason)
|
||||||
throw Error(reason, archive_error_string(this->archive));
|
throw Error(reason, archive_error_string(this->archive));
|
||||||
}
|
}
|
||||||
|
|
||||||
TarArchive::TarArchive(Source & source, bool raw) : buffer(65536)
|
TarArchive::TarArchive(Source & source, bool raw)
|
||||||
|
: buffer(65536)
|
||||||
{
|
{
|
||||||
this->archive = archive_read_new();
|
this->archive = archive_read_new();
|
||||||
this->source = &source;
|
this->source = &source;
|
||||||
|
@ -54,10 +55,11 @@ TarArchive::TarArchive(Source & source, bool raw) : buffer(65536)
|
||||||
archive_read_support_format_empty(archive);
|
archive_read_support_format_empty(archive);
|
||||||
}
|
}
|
||||||
archive_read_set_option(archive, NULL, "mac-ext", NULL);
|
archive_read_set_option(archive, NULL, "mac-ext", NULL);
|
||||||
check(archive_read_open(archive, (void *)this, callback_open, callback_read, callback_close), "Failed to open archive (%s)");
|
check(
|
||||||
|
archive_read_open(archive, (void *) this, callback_open, callback_read, callback_close),
|
||||||
|
"Failed to open archive (%s)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TarArchive::TarArchive(const Path & path)
|
TarArchive::TarArchive(const Path & path)
|
||||||
{
|
{
|
||||||
this->archive = archive_read_new();
|
this->archive = archive_read_new();
|
||||||
|
@ -75,19 +77,19 @@ void TarArchive::close()
|
||||||
|
|
||||||
TarArchive::~TarArchive()
|
TarArchive::~TarArchive()
|
||||||
{
|
{
|
||||||
if (this->archive) archive_read_free(this->archive);
|
if (this->archive)
|
||||||
|
archive_read_free(this->archive);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void extract_archive(TarArchive & archive, const Path & destDir)
|
static void extract_archive(TarArchive & archive, const Path & destDir)
|
||||||
{
|
{
|
||||||
int flags = ARCHIVE_EXTRACT_TIME
|
int flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_SECURE_SYMLINKS | ARCHIVE_EXTRACT_SECURE_NODOTDOT;
|
||||||
| ARCHIVE_EXTRACT_SECURE_SYMLINKS
|
|
||||||
| ARCHIVE_EXTRACT_SECURE_NODOTDOT;
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
struct archive_entry * entry;
|
struct archive_entry * entry;
|
||||||
int r = archive_read_next_header(archive.archive, &entry);
|
int r = archive_read_next_header(archive.archive, &entry);
|
||||||
if (r == ARCHIVE_EOF) break;
|
if (r == ARCHIVE_EOF)
|
||||||
|
break;
|
||||||
auto name = archive_entry_pathname(entry);
|
auto name = archive_entry_pathname(entry);
|
||||||
if (!name)
|
if (!name)
|
||||||
throw Error("cannot get archive member name: %s", archive_error_string(archive.archive));
|
throw Error("cannot get archive member name: %s", archive_error_string(archive.archive));
|
||||||
|
@ -96,18 +98,16 @@ static void extract_archive(TarArchive & archive, const Path & destDir)
|
||||||
else
|
else
|
||||||
archive.check(r);
|
archive.check(r);
|
||||||
|
|
||||||
archive_entry_copy_pathname(entry,
|
archive_entry_copy_pathname(entry, (destDir + "/" + name).c_str());
|
||||||
(destDir + "/" + name).c_str());
|
|
||||||
|
|
||||||
// sources can and do contain dirs with no rx bits
|
// sources can and do contain dirs with no rx bits
|
||||||
if (archive_entry_filetype(entry) == AE_IFDIR && (archive_entry_mode(entry) & 0500) != 0500)
|
if (archive_entry_filetype(entry) == AE_IFDIR && (archive_entry_mode(entry) & 0500) != 0500)
|
||||||
archive_entry_set_mode(entry, archive_entry_mode(entry) | 0500);
|
archive_entry_set_mode(entry, archive_entry_mode(entry) | 0500);
|
||||||
|
|
||||||
// Patch hardlink path
|
// Patch hardlink path
|
||||||
const char *original_hardlink = archive_entry_hardlink(entry);
|
const char * original_hardlink = archive_entry_hardlink(entry);
|
||||||
if (original_hardlink) {
|
if (original_hardlink) {
|
||||||
archive_entry_copy_hardlink(entry,
|
archive_entry_copy_hardlink(entry, (destDir + "/" + original_hardlink).c_str());
|
||||||
(destDir + "/" + original_hardlink).c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
archive.check(archive_read_extract(archive.archive, entry, flags));
|
archive.check(archive_read_extract(archive.archive, entry, flags));
|
||||||
|
@ -140,7 +140,8 @@ time_t unpackTarfileToSink(TarArchive & archive, FileSystemObjectSink & parseSin
|
||||||
// FIXME: merge with extract_archive
|
// FIXME: merge with extract_archive
|
||||||
struct archive_entry * entry;
|
struct archive_entry * entry;
|
||||||
int r = archive_read_next_header(archive.archive, &entry);
|
int r = archive_read_next_header(archive.archive, &entry);
|
||||||
if (r == ARCHIVE_EOF) break;
|
if (r == ARCHIVE_EOF)
|
||||||
|
break;
|
||||||
auto path = archive_entry_pathname(entry);
|
auto path = archive_entry_pathname(entry);
|
||||||
if (!path)
|
if (!path)
|
||||||
throw Error("cannot get archive member name: %s", archive_error_string(archive.archive));
|
throw Error("cannot get archive member name: %s", archive_error_string(archive.archive));
|
||||||
|
@ -167,8 +168,9 @@ time_t unpackTarfileToSink(TarArchive & archive, FileSystemObjectSink & parseSin
|
||||||
auto n = archive_read_data(archive.archive, buf.data(), buf.size());
|
auto n = archive_read_data(archive.archive, buf.data(), buf.size());
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
throw Error("cannot read file '%s' from tarball", path);
|
throw Error("cannot read file '%s' from tarball", path);
|
||||||
if (n == 0) break;
|
if (n == 0)
|
||||||
crf(std::string_view {
|
break;
|
||||||
|
crf(std::string_view{
|
||||||
(const char *) buf.data(),
|
(const char *) buf.data(),
|
||||||
(size_t) n,
|
(size_t) n,
|
||||||
});
|
});
|
||||||
|
|
|
@ -7,7 +7,8 @@
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
struct TarArchive {
|
struct TarArchive
|
||||||
|
{
|
||||||
struct archive * archive;
|
struct archive * archive;
|
||||||
Source * source;
|
Source * source;
|
||||||
std::vector<unsigned char> buffer;
|
std::vector<unsigned char> buffer;
|
||||||
|
|
Loading…
Reference in a new issue