fixup! libarchive proof of concept

This commit is contained in:
Yorick 2019-12-07 23:00:37 +07:00
parent 9ff5f6492f
commit 232b390766
No known key found for this signature in database
GPG key ID: A36E70F9DC014A15

View file

@ -61,11 +61,11 @@ static void copy_data(std::shared_ptr<struct archive> ar, std::shared_ptr<struct
for (;;) { for (;;) {
r = archive_read_data_block(ar.get(), &buff, &size, &offset); r = archive_read_data_block(ar.get(), &buff, &size, &offset);
if (r == ARCHIVE_EOF) return; if (r == ARCHIVE_EOF) return;
if (r < ARCHIVE_OK) { if (r != ARCHIVE_OK) {
throw Error("archive is corrupt (%s)", archive_error_string(ar.get())); throw Error("archive is corrupt (%s)", archive_error_string(ar.get()));
} }
r = archive_write_data_block(aw.get(), buff, size, offset); r = archive_write_data_block(aw.get(), buff, size, offset);
if (r < ARCHIVE_OK) { if (r != ARCHIVE_OK) {
throw Error("could not write archive output (%s)", archive_error_string(aw.get())); throw Error("could not write archive output (%s)", archive_error_string(aw.get()));
} }
} }
@ -74,14 +74,10 @@ static void copy_data(std::shared_ptr<struct archive> ar, std::shared_ptr<struct
static void extract_archive(std::shared_ptr<struct archive> a, const Path & destDir) { static void extract_archive(std::shared_ptr<struct archive> a, const Path & destDir) {
char * cwd = getcwd(0, 0); char * cwd = getcwd(0, 0);
if (!cwd) throw SysError("getting current directory"); if (!cwd) throw SysError("getting current directory");
Finally freeCwd([&]() { free(cwd); }); Finally freeCwd([&]() { chdir(cwd); free(cwd); });
int r = chdir(destDir.c_str()); int r = chdir(destDir.c_str());
if (r != 0) throw SysError("setting directory to tar output path"); if (r != 0) throw SysError("setting directory to tar output path");
struct archive_entry *entry; struct archive_entry *entry;
r = archive_read_next_header(a.get(), &entry);
if (r != ARCHIVE_OK) {
throw Error("archive is corrupt (%s)", archive_error_string(a.get()));
}
int flags = 0; int flags = 0;
auto ext = archive_write_ptr(); auto ext = archive_write_ptr();
flags |= ARCHIVE_EXTRACT_PERM; flags |= ARCHIVE_EXTRACT_PERM;
@ -100,13 +96,10 @@ static void extract_archive(std::shared_ptr<struct archive> a, const Path & dest
if (r != ARCHIVE_OK) { if (r != ARCHIVE_OK) {
throw Error("could not write archive output (%s)", archive_error_string(ext.get())); throw Error("could not write archive output (%s)", archive_error_string(ext.get()));
} }
if (archive_entry_size(entry) > 0) { copy_data(a, ext);
copy_data(a, ext);
}
archive_write_finish_entry(ext.get()); archive_write_finish_entry(ext.get());
} }
r = chdir(cwd); //if (r != 0) throw SysError("resetting directory after archive extraction");
if (r != 0) throw SysError("resetting directory after archive extraction");
} }
void unpackTarfile(Source & source, const Path & destDir) void unpackTarfile(Source & source, const Path & destDir)
{ {