libutil: reformat tarfile.{cc,hh}

Change-Id: I24670300157213fba787df62931776416a6a6964
This commit is contained in:
Rebecca Turner
2025-12-13 16:35:22 -08:00
parent 6bf187537a
commit 3e3573cb58
2 changed files with 31 additions and 20 deletions
+27 -17
View File
@@ -42,11 +42,12 @@ static int callback_close(struct archive *, void * self)
void TarArchive::check(int err, const std::string & reason)
{
if (err == ARCHIVE_EOF)
if (err == ARCHIVE_EOF) {
throw EndOfFile("reached end of archive");
else if (err != ARCHIVE_OK)
} else if (err != ARCHIVE_OK) {
throw Error(reason, archive_error_string(this->archive));
}
}
TarArchive::TarArchive(Source & source, bool raw) : buffer(65536)
{
@@ -62,10 +63,12 @@ TarArchive::TarArchive(Source & source, bool raw) : buffer(65536)
archive_read_support_format_empty(archive);
}
archive_read_set_option(archive, nullptr, "mac-ext", nullptr);
check(archive_read_open(archive, (void *)this, callback_open, callback_read, callback_close), "Failed to open archive (%s)");
check(
archive_read_open(archive, (void *) this, callback_open, callback_read, callback_close),
"Failed to open archive (%s)"
);
}
TarArchive::TarArchive(const Path & path)
{
this->archive = archive_read_new();
@@ -86,43 +89,50 @@ void TarArchive::close()
TarArchive::~TarArchive()
{
if (this->archive) archive_read_free(this->archive);
if (this->archive) {
archive_read_free(this->archive);
}
}
static void extract_archive(TarArchive & archive, const Path & destDir)
{
requireCString(destDir);
int flags = ARCHIVE_EXTRACT_TIME
| ARCHIVE_EXTRACT_SECURE_SYMLINKS
| ARCHIVE_EXTRACT_SECURE_NODOTDOT;
int flags =
ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_SECURE_SYMLINKS | ARCHIVE_EXTRACT_SECURE_NODOTDOT;
for (;;) {
struct archive_entry * entry;
int r = archive_read_next_header(archive.archive, &entry);
if (r == ARCHIVE_EOF) break;
if (r == ARCHIVE_EOF) {
break;
}
auto name = archive_entry_pathname(entry);
if (!name)
throw Error("cannot get archive member name: %s", archive_error_string(archive.archive));
if (r == ARCHIVE_WARN)
if (!name) {
throw Error(
"cannot get archive member name: %s", archive_error_string(archive.archive)
);
}
if (r == ARCHIVE_WARN) {
printTaggedWarning("%1%", Uncolored(archive_error_string(archive.archive)));
else
} else {
archive.check(r);
}
// NOLINTNEXTLINE(lix-unsafe-c-calls): destDir is checked, name is a c string
archive_entry_copy_pathname(entry,
(destDir + "/" + name).c_str());
archive_entry_copy_pathname(entry, (destDir + "/" + name).c_str());
// sources can and do contain dirs with no rx bits
if (archive_entry_filetype(entry) == AE_IFDIR && (archive_entry_mode(entry) & 0500) != 0500)
{
archive_entry_set_mode(entry, archive_entry_mode(entry) | 0500);
}
// Patch hardlink path
const char * original_hardlink = archive_entry_hardlink(entry);
if (original_hardlink) {
// NOLINTNEXTLINE(lix-unsafe-c-calls): destDir is checked, name is a c string
archive_entry_copy_hardlink(entry,
(destDir + "/" + original_hardlink).c_str());
archive_entry_copy_hardlink(entry, (destDir + "/" + original_hardlink).c_str());
}
archive.check(archive_read_extract(archive.archive, entry, flags));
+2 -1
View File
@@ -7,7 +7,8 @@
namespace nix {
struct TarArchive {
struct TarArchive
{
struct archive * archive;
Source * source;
std::vector<unsigned char> buffer;