Clean up reader and writer
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a04a654055
commit
86ab4a5050
5 changed files with 31 additions and 21 deletions
|
@ -3,4 +3,3 @@ project('xwim', 'cpp',
|
||||||
default_options: ['cpp_std=c++17'])
|
default_options: ['cpp_std=c++17'])
|
||||||
|
|
||||||
subdir('src')
|
subdir('src')
|
||||||
subdir('doc')
|
|
||||||
|
|
|
@ -80,10 +80,6 @@ static void _spec_has_single_root(ArchiveSpec* spec,
|
||||||
|
|
||||||
Archive::Archive(std::filesystem::path path) : path{path} {}
|
Archive::Archive(std::filesystem::path path) : path{path} {}
|
||||||
|
|
||||||
Archive::~Archive() {
|
|
||||||
archive_read_free(this->xwim_archive);
|
|
||||||
}
|
|
||||||
|
|
||||||
ArchiveSpec Archive::check() {
|
ArchiveSpec Archive::check() {
|
||||||
logger::trace("Creating archive spec for {}", this->path.string());
|
logger::trace("Creating archive spec for {}", this->path.string());
|
||||||
|
|
||||||
|
@ -109,18 +105,18 @@ ArchiveSpec Archive::check() {
|
||||||
|
|
||||||
void Archive::extract(ExtractSpec extract_spec) {
|
void Archive::extract(ExtractSpec extract_spec) {
|
||||||
std::filesystem::path abs_path = std::filesystem::absolute(this->path);
|
std::filesystem::path abs_path = std::filesystem::absolute(this->path);
|
||||||
ArchiveReaderSys reader{abs_path};
|
|
||||||
|
|
||||||
ArchiveExtractorSys extractor;
|
|
||||||
|
|
||||||
if(extract_spec.make_dir) {
|
if(extract_spec.make_dir) {
|
||||||
|
ArchiveExtractorSys extractor{ArchiveExtractorSys{extract_spec.dirname}};
|
||||||
logger::trace("Creating extract directory {}", extract_spec.dirname.string());
|
logger::trace("Creating extract directory {}", extract_spec.dirname.string());
|
||||||
extractor = ArchiveExtractorSys{extract_spec.dirname};
|
ArchiveReaderSys reader{abs_path};
|
||||||
} else {
|
|
||||||
extractor = ArchiveExtractorSys{};
|
|
||||||
}
|
|
||||||
|
|
||||||
extractor.extract_all(reader);
|
extractor.extract_all(reader);
|
||||||
|
} else {
|
||||||
|
ArchiveExtractorSys extractor{};
|
||||||
|
logger::trace("Creating extract directory {}", extract_spec.dirname.string());
|
||||||
|
ArchiveReaderSys reader{abs_path};
|
||||||
|
extractor.extract_all(reader);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace xwim
|
} // namespace xwim
|
||||||
|
|
|
@ -16,11 +16,9 @@ namespace xwim {
|
||||||
class Archive {
|
class Archive {
|
||||||
private:
|
private:
|
||||||
std::filesystem::path path;
|
std::filesystem::path path;
|
||||||
archive* xwim_archive;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Archive(std::filesystem::path path);
|
explicit Archive(std::filesystem::path path);
|
||||||
~Archive();
|
|
||||||
|
|
||||||
ArchiveSpec check();
|
ArchiveSpec check();
|
||||||
void extract(ExtractSpec extract_spec);
|
void extract(ExtractSpec extract_spec);
|
||||||
|
|
|
@ -49,7 +49,10 @@ xwim::ArchiveReaderSys::ArchiveReaderSys(std::filesystem::path& path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
xwim::ArchiveReaderSys::~ArchiveReaderSys() {
|
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() {
|
bool xwim::ArchiveReaderSys::advance() {
|
||||||
|
@ -69,6 +72,8 @@ const xwim::ArchiveEntryView xwim::ArchiveReaderSys::cur() {
|
||||||
}
|
}
|
||||||
|
|
||||||
xwim::ArchiveExtractorSys::ArchiveExtractorSys(std::filesystem::path& root) {
|
xwim::ArchiveExtractorSys::ArchiveExtractorSys(std::filesystem::path& root) {
|
||||||
|
logger::trace("Constructing ArchiveExtractorSys with path {}", root.string());
|
||||||
|
|
||||||
std::filesystem::create_directories(root);
|
std::filesystem::create_directories(root);
|
||||||
std::filesystem::current_path(root);
|
std::filesystem::current_path(root);
|
||||||
|
|
||||||
|
@ -77,6 +82,8 @@ xwim::ArchiveExtractorSys::ArchiveExtractorSys(std::filesystem::path& root) {
|
||||||
}
|
}
|
||||||
|
|
||||||
xwim::ArchiveExtractorSys::ArchiveExtractorSys() {
|
xwim::ArchiveExtractorSys::ArchiveExtractorSys() {
|
||||||
|
logger::trace("Construction ArchiveExtractorSys without root");
|
||||||
|
|
||||||
this->writer = archive_write_disk_new();
|
this->writer = archive_write_disk_new();
|
||||||
archive_write_disk_set_standard_lookup(this->writer);
|
archive_write_disk_set_standard_lookup(this->writer);
|
||||||
}
|
}
|
||||||
|
@ -90,7 +97,7 @@ void xwim::ArchiveExtractorSys::extract_all(xwim::ArchiveReaderSys& reader) {
|
||||||
// forward declared
|
// forward declared
|
||||||
static int copy_data(struct archive* ar, struct archive* aw);
|
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;
|
int r;
|
||||||
r = archive_write_header(this->writer, reader.ae);
|
r = archive_write_header(this->writer, reader.ae);
|
||||||
if (r != ARCHIVE_OK) {
|
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) {
|
static int copy_data(struct archive* ar, struct archive* aw) {
|
||||||
int r;
|
int r;
|
||||||
const void* buff;
|
const void* buff;
|
||||||
|
@ -111,8 +126,9 @@ static int copy_data(struct archive* ar, struct archive* aw) {
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
r = archive_read_data_block(ar, &buff, &size, &offset);
|
r = archive_read_data_block(ar, &buff, &size, &offset);
|
||||||
if (r == ARCHIVE_EOF)
|
if (r == ARCHIVE_EOF) {
|
||||||
return (ARCHIVE_OK);
|
return (ARCHIVE_OK);
|
||||||
|
}
|
||||||
if (r != ARCHIVE_OK) {
|
if (r != ARCHIVE_OK) {
|
||||||
return (r);
|
return (r);
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,6 +68,7 @@ class ArchiveExtractorSys {
|
||||||
public:
|
public:
|
||||||
ArchiveExtractorSys(std::filesystem::path& root);
|
ArchiveExtractorSys(std::filesystem::path& root);
|
||||||
ArchiveExtractorSys();
|
ArchiveExtractorSys();
|
||||||
|
~ArchiveExtractorSys();
|
||||||
|
|
||||||
void extract_all(ArchiveReaderSys& reader);
|
void extract_all(ArchiveReaderSys& reader);
|
||||||
void extract_entry(ArchiveReaderSys& reader);
|
void extract_entry(ArchiveReaderSys& reader);
|
||||||
|
|
Loading…
Reference in a new issue