Clean up reader and writer
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Armin Friedl 2020-02-23 01:27:43 +01:00
parent a04a654055
commit 86ab4a5050
Signed by: armin
GPG key ID: 48C726EEE7FBCBC8
5 changed files with 31 additions and 21 deletions

View file

@ -3,4 +3,3 @@ project('xwim', 'cpp',
default_options: ['cpp_std=c++17'])
subdir('src')
subdir('doc')

View file

@ -80,10 +80,6 @@ static void _spec_has_single_root(ArchiveSpec* spec,
Archive::Archive(std::filesystem::path path) : path{path} {}
Archive::~Archive() {
archive_read_free(this->xwim_archive);
}
ArchiveSpec Archive::check() {
logger::trace("Creating archive spec for {}", this->path.string());
@ -109,18 +105,18 @@ ArchiveSpec Archive::check() {
void Archive::extract(ExtractSpec extract_spec) {
std::filesystem::path abs_path = std::filesystem::absolute(this->path);
ArchiveReaderSys reader{abs_path};
ArchiveExtractorSys extractor;
if(extract_spec.make_dir) {
ArchiveExtractorSys extractor{ArchiveExtractorSys{extract_spec.dirname}};
logger::trace("Creating extract directory {}", extract_spec.dirname.string());
extractor = ArchiveExtractorSys{extract_spec.dirname};
ArchiveReaderSys reader{abs_path};
extractor.extract_all(reader);
} else {
extractor = ArchiveExtractorSys{};
ArchiveExtractorSys extractor{};
logger::trace("Creating extract directory {}", extract_spec.dirname.string());
ArchiveReaderSys reader{abs_path};
extractor.extract_all(reader);
}
extractor.extract_all(reader);
}
} // namespace xwim

View file

@ -16,11 +16,9 @@ namespace xwim {
class Archive {
private:
std::filesystem::path path;
archive* xwim_archive;
public:
explicit Archive(std::filesystem::path path);
~Archive();
ArchiveSpec check();
void extract(ExtractSpec extract_spec);

View file

@ -49,7 +49,10 @@ xwim::ArchiveReaderSys::ArchiveReaderSys(std::filesystem::path& path) {
}
xwim::ArchiveReaderSys::~ArchiveReaderSys() {
archive_free(this->ar);
logger::trace("Destructing ArchiveReaderSys");
if (this->ae) archive_entry_free(this->ae);
if (this->ar) archive_read_free(this->ar);
}
bool xwim::ArchiveReaderSys::advance() {
@ -69,14 +72,18 @@ const xwim::ArchiveEntryView xwim::ArchiveReaderSys::cur() {
}
xwim::ArchiveExtractorSys::ArchiveExtractorSys(std::filesystem::path& root) {
std::filesystem::create_directories(root);
std::filesystem::current_path(root);
logger::trace("Constructing ArchiveExtractorSys with path {}", root.string());
this->writer = archive_write_disk_new();
archive_write_disk_set_standard_lookup(this->writer);
std::filesystem::create_directories(root);
std::filesystem::current_path(root);
this->writer = archive_write_disk_new();
archive_write_disk_set_standard_lookup(this->writer);
}
xwim::ArchiveExtractorSys::ArchiveExtractorSys() {
logger::trace("Construction ArchiveExtractorSys without root");
this->writer = archive_write_disk_new();
archive_write_disk_set_standard_lookup(this->writer);
}
@ -90,7 +97,7 @@ void xwim::ArchiveExtractorSys::extract_all(xwim::ArchiveReaderSys& reader) {
// forward declared
static int copy_data(struct archive* ar, struct archive* aw);
void xwim:: ArchiveExtractorSys::extract_entry(xwim::ArchiveReaderSys& reader) {
void xwim::ArchiveExtractorSys::extract_entry(xwim::ArchiveReaderSys& reader) {
int r;
r = archive_write_header(this->writer, reader.ae);
if (r != ARCHIVE_OK) {
@ -103,6 +110,14 @@ void xwim:: ArchiveExtractorSys::extract_entry(xwim::ArchiveReaderSys& reader) {
}
}
xwim::ArchiveExtractorSys::~ArchiveExtractorSys(){
logger::trace("Destructing ArchiveExtractorSys");
if(this->writer) {
archive_write_close(this->writer);
archive_write_free(this->writer);
}
}
static int copy_data(struct archive* ar, struct archive* aw) {
int r;
const void* buff;
@ -111,8 +126,9 @@ static int copy_data(struct archive* ar, struct archive* aw) {
for (;;) {
r = archive_read_data_block(ar, &buff, &size, &offset);
if (r == ARCHIVE_EOF)
if (r == ARCHIVE_EOF) {
return (ARCHIVE_OK);
}
if (r != ARCHIVE_OK) {
return (r);
}

View file

@ -68,6 +68,7 @@ class ArchiveExtractorSys {
public:
ArchiveExtractorSys(std::filesystem::path& root);
ArchiveExtractorSys();
~ArchiveExtractorSys();
void extract_all(ArchiveReaderSys& reader);
void extract_entry(ArchiveReaderSys& reader);