2022-06-16 15:42:08 +02:00
|
|
|
#include <hex/helpers/tar.hpp>
|
|
|
|
#include <hex/helpers/literals.hpp>
|
2023-05-05 21:57:37 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2023-06-21 20:07:36 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2022-06-16 15:42:08 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
#include <microtar.h>
|
|
|
|
|
2022-06-16 15:42:08 +02:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
using namespace hex::literals;
|
|
|
|
|
|
|
|
Tar::Tar(const std::fs::path &path, Mode mode) {
|
2023-12-22 23:39:38 +01:00
|
|
|
int tarError = MTAR_ESUCCESS;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-01-01 21:46:41 +01:00
|
|
|
// Explicitly create file so a short path gets generated
|
|
|
|
if (mode == Mode::Create) {
|
2023-03-12 18:27:29 +01:00
|
|
|
wolv::io::File file(path, wolv::io::File::Mode::Create);
|
2023-01-01 21:46:41 +01:00
|
|
|
file.flush();
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
m_ctx = std::make_unique<mtar_t>();
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
auto shortPath = wolv::io::fs::toShortPath(path);
|
2023-01-01 21:46:41 +01:00
|
|
|
if (mode == Tar::Mode::Read)
|
2023-12-22 23:39:38 +01:00
|
|
|
tarError = mtar_open(m_ctx.get(), shortPath.string().c_str(), "r");
|
2023-01-01 21:46:41 +01:00
|
|
|
else if (mode == Tar::Mode::Write)
|
2023-12-22 23:39:38 +01:00
|
|
|
tarError = mtar_open(m_ctx.get(), shortPath.string().c_str(), "a");
|
2023-01-01 21:46:41 +01:00
|
|
|
else if (mode == Tar::Mode::Create)
|
2023-12-22 23:39:38 +01:00
|
|
|
tarError = mtar_open(m_ctx.get(), shortPath.string().c_str(), "w");
|
2023-01-01 21:46:41 +01:00
|
|
|
else
|
2023-12-22 23:39:38 +01:00
|
|
|
tarError = MTAR_EFAILURE;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_path = path;
|
2023-12-22 23:39:38 +01:00
|
|
|
m_valid = (tarError == MTAR_ESUCCESS);
|
2023-06-21 20:07:36 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
if (!m_valid) {
|
2023-12-22 23:39:38 +01:00
|
|
|
m_tarOpenErrno = tarError;
|
2023-06-21 20:07:36 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Hopefully this errno corresponds to the file open call in mtar_open
|
2023-12-19 13:10:25 +01:00
|
|
|
m_fileOpenErrno = errno;
|
2023-06-21 20:07:36 +02:00
|
|
|
}
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Tar::~Tar() {
|
2022-08-08 21:23:52 +02:00
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tar::Tar(hex::Tar &&other) noexcept {
|
2023-12-22 23:39:38 +01:00
|
|
|
m_ctx = std::move(other.m_ctx);
|
2023-12-19 13:10:25 +01:00
|
|
|
m_path = other.m_path;
|
|
|
|
m_valid = other.m_valid;
|
|
|
|
m_tarOpenErrno = other.m_tarOpenErrno;
|
|
|
|
m_fileOpenErrno = other.m_fileOpenErrno;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
other.m_ctx = { };
|
|
|
|
other.m_valid = false;
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
Tar &Tar::operator=(Tar &&other) noexcept {
|
2023-12-22 23:39:38 +01:00
|
|
|
m_ctx = std::move(other.m_ctx);
|
|
|
|
m_path = std::move(other.m_path);
|
2023-05-05 21:57:37 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_valid = other.m_valid;
|
2022-08-08 21:23:52 +02:00
|
|
|
other.m_valid = false;
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_tarOpenErrno = other.m_tarOpenErrno;
|
|
|
|
m_fileOpenErrno = other.m_fileOpenErrno;
|
2022-08-08 21:23:52 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
std::vector<std::fs::path> Tar::listEntries(const std::fs::path &basePath) const {
|
2022-06-16 15:42:08 +02:00
|
|
|
std::vector<std::fs::path> result;
|
|
|
|
|
|
|
|
const std::string PaxHeaderName = "@PaxHeader";
|
|
|
|
mtar_header_t header;
|
2023-12-22 23:39:38 +01:00
|
|
|
while (mtar_read_header(m_ctx.get(), &header) != MTAR_ENULLRECORD) {
|
2022-08-08 21:23:52 +02:00
|
|
|
std::fs::path path = header.name;
|
2023-03-12 18:27:29 +01:00
|
|
|
if (header.name != PaxHeaderName && wolv::io::fs::isSubPath(basePath, path)) {
|
2022-06-16 15:42:08 +02:00
|
|
|
result.emplace_back(header.name);
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
mtar_next(m_ctx.get());
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
bool Tar::contains(const std::fs::path &path) const {
|
2022-08-08 21:23:52 +02:00
|
|
|
mtar_header_t header;
|
2023-08-06 21:33:15 +02:00
|
|
|
|
|
|
|
auto fixedPath = path.string();
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/');
|
|
|
|
#endif
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
return mtar_find(m_ctx.get(), fixedPath.c_str(), &header) == MTAR_ESUCCESS;
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
std::string Tar::getOpenErrorString() const {
|
2023-12-19 13:10:25 +01:00
|
|
|
return hex::format("{}: {}", mtar_strerror(m_tarOpenErrno), std::strerror(m_fileOpenErrno));
|
2023-06-21 20:07:36 +02:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
void Tar::close() {
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_valid) {
|
2023-12-22 23:39:38 +01:00
|
|
|
mtar_finalize(m_ctx.get());
|
|
|
|
mtar_close(m_ctx.get());
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
m_ctx.reset();
|
2023-12-19 13:10:25 +01:00
|
|
|
m_valid = false;
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
std::vector<u8> Tar::readVector(const std::fs::path &path) const {
|
2022-06-16 15:42:08 +02:00
|
|
|
mtar_header_t header;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
auto fixedPath = path.string();
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/');
|
|
|
|
#endif
|
2023-12-22 23:39:38 +01:00
|
|
|
int ret = mtar_find(m_ctx.get(), fixedPath.c_str(), &header);
|
2023-11-10 20:47:08 +01:00
|
|
|
if (ret != MTAR_ESUCCESS){
|
2023-05-05 21:57:37 +02:00
|
|
|
log::debug("Failed to read vector from path {} in tarred file {}: {}",
|
2023-12-19 13:10:25 +01:00
|
|
|
path.string(), m_path.string(), mtar_strerror(ret));
|
2023-05-05 21:57:37 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:42:08 +02:00
|
|
|
std::vector<u8> result(header.size, 0x00);
|
2023-12-22 23:39:38 +01:00
|
|
|
mtar_read_data(m_ctx.get(), result.data(), result.size());
|
2022-06-16 15:42:08 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
std::string Tar::readString(const std::fs::path &path) const {
|
2023-03-23 11:23:07 +01:00
|
|
|
auto result = this->readVector(path);
|
2022-08-08 21:23:52 +02:00
|
|
|
return { result.begin(), result.end() };
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
void Tar::writeVector(const std::fs::path &path, const std::vector<u8> &data) const {
|
2022-06-16 15:42:08 +02:00
|
|
|
if (path.has_parent_path()) {
|
|
|
|
std::fs::path pathPart;
|
|
|
|
for (const auto &part : path.parent_path()) {
|
|
|
|
pathPart /= part;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
auto fixedPath = pathPart.string();
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/');
|
|
|
|
#endif
|
2023-12-22 23:39:38 +01:00
|
|
|
mtar_write_dir_header(m_ctx.get(), fixedPath.c_str());
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
auto fixedPath = path.string();
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
std::replace(fixedPath.begin(), fixedPath.end(), '\\', '/');
|
|
|
|
#endif
|
2023-12-22 23:39:38 +01:00
|
|
|
mtar_write_file_header(m_ctx.get(), fixedPath.c_str(), data.size());
|
|
|
|
mtar_write_data(m_ctx.get(), data.data(), data.size());
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
void Tar::writeString(const std::fs::path &path, const std::string &data) const {
|
2023-03-23 11:23:07 +01:00
|
|
|
this->writeVector(path, { data.begin(), data.end() });
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
static void writeFile(mtar_t *ctx, const mtar_header_t *header, const std::fs::path &path) {
|
2022-06-16 15:42:08 +02:00
|
|
|
constexpr static u64 BufferSize = 1_MiB;
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
wolv::io::File outputFile(path, wolv::io::File::Mode::Create);
|
2022-06-16 15:42:08 +02:00
|
|
|
|
2022-06-27 14:58:40 +02:00
|
|
|
std::vector<u8> buffer;
|
|
|
|
for (u64 offset = 0; offset < header->size; offset += BufferSize) {
|
|
|
|
buffer.resize(std::min<u64>(BufferSize, header->size - offset));
|
|
|
|
|
|
|
|
mtar_read_data(ctx, buffer.data(), buffer.size());
|
2023-03-23 11:23:07 +01:00
|
|
|
outputFile.writeVector(buffer);
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
void Tar::extract(const std::fs::path &path, const std::fs::path &outputPath) const {
|
2022-06-27 14:58:40 +02:00
|
|
|
mtar_header_t header;
|
2023-12-22 23:39:38 +01:00
|
|
|
mtar_find(m_ctx.get(), path.string().c_str(), &header);
|
2022-06-16 15:42:08 +02:00
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
writeFile(m_ctx.get(), &header, outputPath);
|
2022-06-27 14:58:40 +02:00
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
void Tar::extractAll(const std::fs::path &outputPath) const {
|
2022-06-16 15:42:08 +02:00
|
|
|
mtar_header_t header;
|
2023-12-22 23:39:38 +01:00
|
|
|
while (mtar_read_header(m_ctx.get(), &header) != MTAR_ENULLRECORD) {
|
2022-06-16 15:42:08 +02:00
|
|
|
const auto filePath = std::fs::absolute(outputPath / std::fs::path(header.name));
|
|
|
|
|
|
|
|
if (filePath.filename() != "@PaxHeader") {
|
|
|
|
|
|
|
|
std::fs::create_directories(filePath.parent_path());
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
writeFile(m_ctx.get(), &header, filePath);
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
|
2023-12-22 23:39:38 +01:00
|
|
|
mtar_next(m_ctx.get());
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|