tarfile.cc: Style fixes
This commit is contained in:
parent
ee235e764c
commit
be32da0ed0
|
@ -6,66 +6,76 @@
|
||||||
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;
|
||||||
|
|
||||||
void check(int err, const char *reason = "Failed to extract archive (%s)") {
|
void check(int err, const char * reason = "failed to extract archive: %s")
|
||||||
|
{
|
||||||
if (err == ARCHIVE_EOF)
|
if (err == ARCHIVE_EOF)
|
||||||
throw EndOfFile("reached end of archive");
|
throw EndOfFile("reached end of archive");
|
||||||
else if (err != ARCHIVE_OK)
|
else if (err != ARCHIVE_OK)
|
||||||
throw Error(reason, archive_error_string(this->archive));
|
throw Error(reason, archive_error_string(this->archive));
|
||||||
}
|
}
|
||||||
|
|
||||||
TarArchive(Source& source) : buffer(4096) {
|
TarArchive(Source & source) : buffer(4096)
|
||||||
|
{
|
||||||
this->archive = archive_read_new();
|
this->archive = archive_read_new();
|
||||||
this->source = &source;
|
this->source = &source;
|
||||||
|
|
||||||
archive_read_support_filter_all(archive);
|
archive_read_support_filter_all(archive);
|
||||||
archive_read_support_format_all(archive);
|
archive_read_support_format_all(archive);
|
||||||
check(archive_read_open(archive, (void *)this, TarArchive::callback_open, TarArchive::callback_read, TarArchive::callback_close), "Failed to open archive (%s)");
|
check(archive_read_open(archive,
|
||||||
|
(void *)this,
|
||||||
|
TarArchive::callback_open,
|
||||||
|
TarArchive::callback_read,
|
||||||
|
TarArchive::callback_close),
|
||||||
|
"failed to open archive: %s");
|
||||||
}
|
}
|
||||||
|
|
||||||
TarArchive(const Path &path) {
|
TarArchive(const Path & path)
|
||||||
|
{
|
||||||
this->archive = archive_read_new();
|
this->archive = archive_read_new();
|
||||||
|
|
||||||
archive_read_support_filter_all(archive);
|
archive_read_support_filter_all(archive);
|
||||||
archive_read_support_format_all(archive);
|
archive_read_support_format_all(archive);
|
||||||
check(archive_read_open_filename(archive, path.c_str(), 16384), "Failed to open archive (%s)");
|
check(archive_read_open_filename(archive, path.c_str(), 16384), "failed to open archive: %s");
|
||||||
}
|
}
|
||||||
|
|
||||||
// disable copy constructor
|
TarArchive(const TarArchive &) = delete;
|
||||||
TarArchive(const TarArchive&) = delete;
|
|
||||||
|
|
||||||
void close() {
|
void close()
|
||||||
check(archive_read_close(archive), "Failed to close archive (%s)");
|
{
|
||||||
|
check(archive_read_close(archive), "failed to close archive: %s");
|
||||||
}
|
}
|
||||||
|
|
||||||
~TarArchive() {
|
~TarArchive()
|
||||||
|
{
|
||||||
if (this->archive) archive_read_free(this->archive);
|
if (this->archive) archive_read_free(this->archive);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static int callback_open(struct archive *, void *self) {
|
|
||||||
|
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)
|
||||||
TarArchive *self = (TarArchive *)_self;
|
{
|
||||||
|
auto self = (TarArchive *)_self;
|
||||||
*buffer = self->buffer.data();
|
*buffer = self->buffer.data();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return self->source->read(self->buffer.data(), 4096);
|
return self->source->read(self->buffer.data(), 4096);
|
||||||
} catch (EndOfFile &) {
|
} catch (EndOfFile &) {
|
||||||
return 0;
|
return 0;
|
||||||
} catch (std::exception &err) {
|
} catch (std::exception & err) {
|
||||||
archive_set_error(archive, EIO, "Source threw exception: %s", err.what());
|
archive_set_error(archive, EIO, "source threw exception: %s", err.what());
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int callback_close(struct archive *, void *self) {
|
static int callback_close(struct archive *, void * self) {
|
||||||
return ARCHIVE_OK;
|
return ARCHIVE_OK;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -84,26 +94,27 @@ struct PushD {
|
||||||
int r = chdir(oldDir);
|
int r = chdir(oldDir);
|
||||||
free(oldDir);
|
free(oldDir);
|
||||||
if (r != 0)
|
if (r != 0)
|
||||||
std::cerr << "warning: failed to change directory back after tar extraction";
|
warn("failed to change directory back after tar extraction");
|
||||||
/* can't throw out of a destructor */
|
/* can't throw out of a destructor */
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static void extract_archive(TarArchive &archive, const Path & destDir) {
|
static void extract_archive(TarArchive & archive, const Path & destDir)
|
||||||
|
{
|
||||||
// need to chdir back *after* archive closing
|
// need to chdir back *after* archive closing
|
||||||
PushD newDir(destDir);
|
PushD newDir(destDir);
|
||||||
struct archive_entry *entry;
|
|
||||||
int flags = ARCHIVE_EXTRACT_FFLAGS
|
int flags = ARCHIVE_EXTRACT_FFLAGS
|
||||||
| ARCHIVE_EXTRACT_PERM
|
| ARCHIVE_EXTRACT_PERM
|
||||||
| ARCHIVE_EXTRACT_SECURE_SYMLINKS
|
| ARCHIVE_EXTRACT_SECURE_SYMLINKS
|
||||||
| ARCHIVE_EXTRACT_SECURE_NODOTDOT
|
| ARCHIVE_EXTRACT_SECURE_NODOTDOT
|
||||||
| ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
|
| ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
|
||||||
|
|
||||||
for(;;) {
|
for (;;) {
|
||||||
|
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;
|
||||||
else if (r == ARCHIVE_WARN)
|
else if (r == ARCHIVE_WARN)
|
||||||
std::cerr << "warning: " << archive_error_string(archive.archive) << std::endl;
|
warn(archive_error_string(archive.archive));
|
||||||
else
|
else
|
||||||
archive.check(r);
|
archive.check(r);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue