lix/src/libutil/tarfile.cc

116 lines
3.1 KiB
C++
Raw Normal View History

2019-12-07 16:35:14 +01:00
#include <archive.h>
#include <archive_entry.h>
2019-12-09 11:21:46 +01:00
#include "serialise.hh"
2019-12-10 09:47:38 +01:00
#include "tarfile.hh"
2019-03-27 14:12:20 +01:00
2019-09-11 13:10:46 +02:00
namespace nix {
2019-12-10 09:47:38 +01:00
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;
*buffer = self->buffer.data();
try {
return self->source->read((char *) self->buffer.data(), 4096);
} catch (EndOfFile &) {
return 0;
} catch (std::exception &err) {
archive_set_error(archive, EIO, "Source threw exception: %s", err.what());
return -1;
}
}
2019-09-11 13:10:46 +02:00
2019-12-10 09:47:38 +01:00
static int callback_close(struct archive *, void *self) {
return ARCHIVE_OK;
}
2019-12-07 19:08:33 +01:00
2019-12-10 09:47:38 +01:00
void TarArchive::check(int err, const char *reason)
{
2019-12-07 19:08:33 +01:00
if (err == ARCHIVE_EOF)
throw EndOfFile("reached end of archive");
else if (err != ARCHIVE_OK)
throw Error(reason, archive_error_string(this->archive));
}
2019-12-07 19:08:33 +01:00
2019-12-10 09:47:38 +01:00
TarArchive::TarArchive(Source& source, bool raw) : buffer(4096)
{
this->archive = archive_read_new();
this->source = &source;
2019-12-07 19:08:33 +01:00
2019-12-10 09:47:38 +01:00
if (!raw) {
2019-12-07 19:08:33 +01:00
archive_read_support_filter_all(archive);
archive_read_support_format_all(archive);
2019-12-10 09:47:38 +01:00
} else {
2019-12-07 19:08:33 +01:00
archive_read_support_filter_all(archive);
2019-12-10 09:47:38 +01:00
archive_read_support_format_raw(archive);
archive_read_support_format_empty(archive);
2019-12-07 19:08:33 +01:00
}
2019-12-10 09:47:38 +01:00
check(archive_read_open(archive, (void *)this, callback_open, callback_read, callback_close), "Failed to open archive (%s)");
}
2019-12-07 19:08:33 +01:00
2019-12-19 15:01:58 +01:00
2019-12-10 09:47:38 +01:00
TarArchive::TarArchive(const Path &path)
{
this->archive = archive_read_new();
2019-12-09 11:21:46 +01:00
2019-12-10 09:47:38 +01:00
archive_read_support_filter_all(archive);
archive_read_support_format_all(archive);
check(archive_read_open_filename(archive, path.c_str(), 16384), "failed to open archive: %s");
}
2019-12-07 19:08:33 +01:00
2019-12-10 09:47:38 +01:00
void TarArchive::close() {
check(archive_read_close(this->archive), "Failed to close archive (%s)");
}
2019-12-07 19:08:33 +01:00
2019-12-10 09:47:38 +01:00
TarArchive::~TarArchive() {
if (this->archive) archive_read_free(this->archive);
}
2019-12-07 19:08:33 +01:00
2019-12-19 15:01:58 +01:00
static void extract_archive(TarArchive & archive, const Path & destDir)
{
2019-12-09 11:21:46 +01:00
int flags = ARCHIVE_EXTRACT_FFLAGS
| ARCHIVE_EXTRACT_PERM
| ARCHIVE_EXTRACT_TIME
2019-12-09 11:21:46 +01:00
| ARCHIVE_EXTRACT_SECURE_SYMLINKS
| ARCHIVE_EXTRACT_SECURE_NODOTDOT;
2019-12-07 19:08:33 +01:00
2019-12-19 15:01:58 +01:00
for (;;) {
struct archive_entry * entry;
2019-12-07 19:08:33 +01:00
int r = archive_read_next_header(archive.archive, &entry);
2019-12-07 16:35:14 +01:00
if (r == ARCHIVE_EOF) break;
2019-12-07 19:08:33 +01:00
else if (r == ARCHIVE_WARN)
2019-12-19 15:01:58 +01:00
warn(archive_error_string(archive.archive));
2019-12-07 19:08:33 +01:00
else
archive.check(r);
archive_entry_set_pathname(entry,
(destDir + "/" + archive_entry_pathname(entry)).c_str());
2019-12-07 19:08:33 +01:00
archive.check(archive_read_extract(archive.archive, entry, flags));
2019-12-07 16:35:14 +01:00
}
2019-12-07 19:08:33 +01:00
archive.close();
2019-12-07 16:35:14 +01:00
}
2019-12-07 19:08:33 +01:00
2019-12-07 16:35:14 +01:00
void unpackTarfile(Source & source, const Path & destDir)
{
2019-12-07 19:08:33 +01:00
auto archive = TarArchive(source);
2019-12-07 17:23:11 +01:00
2019-12-07 16:35:14 +01:00
createDirs(destDir);
2019-12-07 19:08:33 +01:00
extract_archive(archive, destDir);
2019-12-07 16:35:14 +01:00
}
2019-12-07 19:08:33 +01:00
2019-12-07 16:35:14 +01:00
void unpackTarfile(const Path & tarFile, const Path & destDir)
{
2019-12-07 19:08:33 +01:00
auto archive = TarArchive(tarFile);
2019-12-07 17:23:11 +01:00
2019-12-07 16:35:14 +01:00
createDirs(destDir);
2019-12-07 19:08:33 +01:00
extract_archive(archive, destDir);
}
2019-09-11 13:10:46 +02:00
}