2022-06-16 15:42:08 +02:00
|
|
|
#include <hex/helpers/tar.hpp>
|
|
|
|
#include <hex/helpers/literals.hpp>
|
2023-01-01 21:46:41 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2022-06-16 15:42:08 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
|
2022-06-16 15:42:08 +02:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
using namespace hex::literals;
|
|
|
|
|
|
|
|
Tar::Tar(const std::fs::path &path, Mode mode) {
|
2022-08-08 21:23:52 +02:00
|
|
|
int error = MTAR_ESUCCESS;
|
|
|
|
|
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-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)
|
|
|
|
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "r");
|
|
|
|
else if (mode == Tar::Mode::Write)
|
|
|
|
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "a");
|
|
|
|
else if (mode == Tar::Mode::Create)
|
|
|
|
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "w");
|
|
|
|
else
|
|
|
|
error = MTAR_EFAILURE;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
this->m_valid = (error == MTAR_ESUCCESS);
|
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 {
|
|
|
|
this->m_ctx = other.m_ctx;
|
|
|
|
this->m_valid = other.m_valid;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
this->m_ctx = other.m_ctx;
|
|
|
|
other.m_ctx = { };
|
|
|
|
|
|
|
|
this->m_valid = other.m_valid;
|
|
|
|
other.m_valid = false;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::fs::path> Tar::listEntries(const std::fs::path &basePath) {
|
2022-06-16 15:42:08 +02:00
|
|
|
std::vector<std::fs::path> result;
|
|
|
|
|
|
|
|
const std::string PaxHeaderName = "@PaxHeader";
|
|
|
|
mtar_header_t header;
|
|
|
|
while (mtar_read_header(&this->m_ctx, &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);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtar_next(&this->m_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
bool Tar::contains(const std::fs::path &path) {
|
|
|
|
mtar_header_t header;
|
|
|
|
return mtar_find(&this->m_ctx, path.string().c_str(), &header) == MTAR_ESUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tar::close() {
|
|
|
|
if (this->m_valid) {
|
|
|
|
mtar_finalize(&this->m_ctx);
|
|
|
|
mtar_close(&this->m_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_ctx = { };
|
|
|
|
this->m_valid = false;
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
std::vector<u8> Tar::readVector(const std::fs::path &path) {
|
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
|
|
|
|
mtar_find(&this->m_ctx, fixedPath.c_str(), &header);
|
2022-06-16 15:42:08 +02:00
|
|
|
|
|
|
|
std::vector<u8> result(header.size, 0x00);
|
|
|
|
mtar_read_data(&this->m_ctx, result.data(), result.size());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
std::string Tar::readString(const std::fs::path &path) {
|
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-03-23 11:23:07 +01:00
|
|
|
void Tar::writeVector(const std::fs::path &path, const std::vector<u8> &data) {
|
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
|
|
|
|
mtar_write_dir_header(&this->m_ctx, 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
|
|
|
|
mtar_write_file_header(&this->m_ctx, fixedPath.c_str(), data.size());
|
2022-06-16 15:42:08 +02:00
|
|
|
mtar_write_data(&this->m_ctx, data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
void Tar::writeString(const std::fs::path &path, const std::string &data) {
|
|
|
|
this->writeVector(path, { data.begin(), data.end() });
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 14:58:40 +02:00
|
|
|
static void writeFile(mtar_t *ctx, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 14:58:40 +02:00
|
|
|
void Tar::extract(const std::fs::path &path, const std::fs::path &outputPath) {
|
|
|
|
mtar_header_t header;
|
|
|
|
mtar_find(&this->m_ctx, path.string().c_str(), &header);
|
2022-06-16 15:42:08 +02:00
|
|
|
|
2022-06-27 14:58:40 +02:00
|
|
|
writeFile(&this->m_ctx, &header, outputPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tar::extractAll(const std::fs::path &outputPath) {
|
2022-06-16 15:42:08 +02:00
|
|
|
mtar_header_t header;
|
|
|
|
while (mtar_read_header(&this->m_ctx, &header) != MTAR_ENULLRECORD) {
|
|
|
|
const auto filePath = std::fs::absolute(outputPath / std::fs::path(header.name));
|
|
|
|
|
|
|
|
if (filePath.filename() != "@PaxHeader") {
|
|
|
|
|
|
|
|
std::fs::create_directories(filePath.parent_path());
|
|
|
|
|
2022-06-27 14:58:40 +02:00
|
|
|
writeFile(&this->m_ctx, &header, filePath);
|
2022-06-16 15:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mtar_next(&this->m_ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|