2019-12-07 15:35:14 +00:00
|
|
|
#include <archive.h>
|
|
|
|
#include <archive_entry.h>
|
2019-12-09 10:21:46 +00:00
|
|
|
|
2024-08-05 05:03:35 +00:00
|
|
|
#include "charptr-cast.hh"
|
2024-05-28 09:52:13 +00:00
|
|
|
#include "file-system.hh"
|
2024-05-28 14:53:21 +00:00
|
|
|
#include "logging.hh"
|
2019-12-09 10:21:46 +00:00
|
|
|
#include "serialise.hh"
|
2019-12-10 08:47:38 +00:00
|
|
|
#include "tarfile.hh"
|
2019-03-27 13:12:20 +00:00
|
|
|
|
2019-09-11 11:10:46 +00:00
|
|
|
namespace nix {
|
2021-04-15 11:51:00 +00:00
|
|
|
|
|
|
|
static int callback_open(struct archive *, void * self)
|
|
|
|
{
|
2019-12-10 08:47:38 +00:00
|
|
|
return ARCHIVE_OK;
|
|
|
|
}
|
|
|
|
|
2021-04-15 11:51:00 +00:00
|
|
|
static ssize_t callback_read(struct archive * archive, void * _self, const void * * buffer)
|
|
|
|
{
|
2024-08-05 03:19:58 +00:00
|
|
|
auto self = static_cast<TarArchive *>(_self);
|
2019-12-10 08:47:38 +00:00
|
|
|
*buffer = self->buffer.data();
|
|
|
|
|
|
|
|
try {
|
2024-08-05 05:03:35 +00:00
|
|
|
return self->source->read(charptr_cast<char *>(self->buffer.data()), self->buffer.size());
|
2019-12-10 08:47:38 +00:00
|
|
|
} catch (EndOfFile &) {
|
|
|
|
return 0;
|
2021-04-15 11:51:00 +00:00
|
|
|
} catch (std::exception & err) {
|
2019-12-10 08:47:38 +00:00
|
|
|
archive_set_error(archive, EIO, "Source threw exception: %s", err.what());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 11:10:46 +00:00
|
|
|
|
2021-04-15 11:51:00 +00:00
|
|
|
static int callback_close(struct archive *, void * self)
|
|
|
|
{
|
2019-12-10 08:47:38 +00:00
|
|
|
return ARCHIVE_OK;
|
|
|
|
}
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2021-04-15 11:51:00 +00:00
|
|
|
void TarArchive::check(int err, const std::string & reason)
|
2019-12-10 08:47:38 +00:00
|
|
|
{
|
2021-04-15 11:51:00 +00:00
|
|
|
if (err == ARCHIVE_EOF)
|
|
|
|
throw EndOfFile("reached end of archive");
|
|
|
|
else if (err != ARCHIVE_OK)
|
|
|
|
throw Error(reason, archive_error_string(this->archive));
|
|
|
|
}
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2023-01-31 22:14:48 +00:00
|
|
|
TarArchive::TarArchive(Source & source, bool raw) : buffer(65536)
|
2019-12-10 08:47:38 +00:00
|
|
|
{
|
2022-02-06 13:47:07 +00:00
|
|
|
this->archive = archive_read_new();
|
|
|
|
this->source = &source;
|
|
|
|
|
|
|
|
if (!raw) {
|
|
|
|
archive_read_support_filter_all(archive);
|
2019-12-07 18:08:33 +00:00
|
|
|
archive_read_support_format_all(archive);
|
2022-02-06 13:47:07 +00:00
|
|
|
} else {
|
|
|
|
archive_read_support_filter_all(archive);
|
2019-12-10 08:47:38 +00:00
|
|
|
archive_read_support_format_raw(archive);
|
2022-02-06 13:47:07 +00:00
|
|
|
archive_read_support_format_empty(archive);
|
|
|
|
}
|
2024-07-23 19:06:55 +00:00
|
|
|
archive_read_set_option(archive, nullptr, "mac-ext", nullptr);
|
2019-12-10 08:47:38 +00:00
|
|
|
check(archive_read_open(archive, (void *)this, callback_open, callback_read, callback_close), "Failed to open archive (%s)");
|
|
|
|
}
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2022-02-06 13:47:07 +00:00
|
|
|
|
2021-04-15 11:51:00 +00:00
|
|
|
TarArchive::TarArchive(const Path & path)
|
2019-12-10 08:47:38 +00:00
|
|
|
{
|
2022-02-06 13:47:07 +00:00
|
|
|
this->archive = archive_read_new();
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2021-08-30 13:41:19 +00:00
|
|
|
archive_read_support_filter_all(archive);
|
2022-02-06 13:47:07 +00:00
|
|
|
archive_read_support_format_all(archive);
|
2024-07-23 19:06:55 +00:00
|
|
|
archive_read_set_option(archive, nullptr, "mac-ext", nullptr);
|
2022-02-06 13:47:07 +00:00
|
|
|
check(archive_read_open_filename(archive, path.c_str(), 16384), "failed to open archive: %s");
|
2021-08-30 13:41:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 11:51:00 +00:00
|
|
|
void TarArchive::close()
|
|
|
|
{
|
2019-12-10 08:47:38 +00:00
|
|
|
check(archive_read_close(this->archive), "Failed to close archive (%s)");
|
|
|
|
}
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2021-04-15 11:51:00 +00:00
|
|
|
TarArchive::~TarArchive()
|
|
|
|
{
|
2019-12-10 08:47:38 +00:00
|
|
|
if (this->archive) archive_read_free(this->archive);
|
|
|
|
}
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2019-12-19 14:01:58 +00:00
|
|
|
static void extract_archive(TarArchive & archive, const Path & destDir)
|
|
|
|
{
|
2022-11-01 14:46:30 +00:00
|
|
|
int flags = ARCHIVE_EXTRACT_TIME
|
2019-12-09 10:21:46 +00:00
|
|
|
| ARCHIVE_EXTRACT_SECURE_SYMLINKS
|
2019-12-19 14:08:16 +00:00
|
|
|
| ARCHIVE_EXTRACT_SECURE_NODOTDOT;
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2019-12-19 14:01:58 +00:00
|
|
|
for (;;) {
|
|
|
|
struct archive_entry * entry;
|
2019-12-07 18:08:33 +00:00
|
|
|
int r = archive_read_next_header(archive.archive, &entry);
|
2019-12-07 15:35:14 +00:00
|
|
|
if (r == ARCHIVE_EOF) break;
|
2021-08-30 13:30:34 +00:00
|
|
|
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)
|
2019-12-19 14:01:58 +00:00
|
|
|
warn(archive_error_string(archive.archive));
|
2019-12-07 18:08:33 +00:00
|
|
|
else
|
|
|
|
archive.check(r);
|
|
|
|
|
2021-12-10 16:00:32 +00:00
|
|
|
archive_entry_copy_pathname(entry,
|
2021-08-30 13:30:34 +00:00
|
|
|
(destDir + "/" + name).c_str());
|
2019-12-19 14:08:16 +00:00
|
|
|
|
2022-11-01 14:46:30 +00:00
|
|
|
// 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);
|
|
|
|
|
2021-12-10 16:03:51 +00:00
|
|
|
// Patch hardlink path
|
|
|
|
const char *original_hardlink = archive_entry_hardlink(entry);
|
|
|
|
if (original_hardlink) {
|
|
|
|
archive_entry_copy_hardlink(entry,
|
|
|
|
(destDir + "/" + original_hardlink).c_str());
|
|
|
|
}
|
|
|
|
|
2019-12-07 18:08:33 +00:00
|
|
|
archive.check(archive_read_extract(archive.archive, entry, flags));
|
2019-12-07 15:35:14 +00:00
|
|
|
}
|
2019-12-07 18:08:33 +00:00
|
|
|
|
|
|
|
archive.close();
|
2019-12-07 15:35:14 +00:00
|
|
|
}
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2019-12-07 15:35:14 +00:00
|
|
|
void unpackTarfile(Source & source, const Path & destDir)
|
|
|
|
{
|
2019-12-07 18:08:33 +00:00
|
|
|
auto archive = TarArchive(source);
|
2019-12-07 16:23:11 +00:00
|
|
|
|
2019-12-07 15:35:14 +00:00
|
|
|
createDirs(destDir);
|
2019-12-07 18:08:33 +00:00
|
|
|
extract_archive(archive, destDir);
|
2019-12-07 15:35:14 +00:00
|
|
|
}
|
2019-12-07 18:08:33 +00:00
|
|
|
|
2019-12-07 15:35:14 +00:00
|
|
|
void unpackTarfile(const Path & tarFile, const Path & destDir)
|
|
|
|
{
|
2019-12-07 18:08:33 +00:00
|
|
|
auto archive = TarArchive(tarFile);
|
2019-12-07 16:23:11 +00:00
|
|
|
|
2019-12-07 15:35:14 +00:00
|
|
|
createDirs(destDir);
|
2019-12-07 18:08:33 +00:00
|
|
|
extract_archive(archive, destDir);
|
2019-09-11 13:25:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 11:10:46 +00:00
|
|
|
}
|