further clean up libarchive code

This commit is contained in:
Yorick 2019-12-09 17:21:46 +07:00
parent 28ee687adf
commit eba82b7c88
No known key found for this signature in database
GPG key ID: A36E70F9DC014A15

View file

@ -1,15 +1,14 @@
#include "rust-ffi.hh"
#include "compression.hh"
#include <archive.h> #include <archive.h>
#include <archive_entry.h> #include <archive_entry.h>
#include "finally.hh"
#include "serialise.hh"
namespace nix { namespace nix {
struct TarArchive { struct TarArchive {
struct archive *archive; struct archive *archive;
Source *source; Source *source;
unsigned char buffer[4096]; 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)
@ -18,7 +17,7 @@ struct TarArchive {
throw Error(reason, archive_error_string(this->archive)); throw Error(reason, archive_error_string(this->archive));
} }
TarArchive(Source& source) { TarArchive(Source& source) : buffer(4096) {
this->archive = archive_read_new(); this->archive = archive_read_new();
this->source = &source; this->source = &source;
@ -35,6 +34,9 @@ struct TarArchive {
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;
void close() { void close() {
check(archive_read_close(archive), "Failed to close archive (%s)"); check(archive_read_close(archive), "Failed to close archive (%s)");
} }
@ -47,13 +49,13 @@ 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; TarArchive *self = (TarArchive *)_self;
*buffer = self->buffer; *buffer = self->buffer.data();
try { try {
return self->source->read(self->buffer, 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) {
@ -82,7 +84,7 @@ struct PushD {
int r = chdir(oldDir); int r = chdir(oldDir);
free(oldDir); free(oldDir);
if (r != 0) if (r != 0)
std::cerr << "warning: popd failed to chdir"; std::cerr << "warning: failed to change directory back after tar extraction";
/* can't throw out of a destructor */ /* can't throw out of a destructor */
} }
}; };
@ -91,7 +93,11 @@ 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; struct archive_entry *entry;
int flags = ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_PERM; int flags = ARCHIVE_EXTRACT_FFLAGS
| ARCHIVE_EXTRACT_PERM
| ARCHIVE_EXTRACT_SECURE_SYMLINKS
| ARCHIVE_EXTRACT_SECURE_NODOTDOT
| ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
for(;;) { for(;;) {
int r = archive_read_next_header(archive.archive, &entry); int r = archive_read_next_header(archive.archive, &entry);