Expand archive reader

This commit is contained in:
Georg Hagen
2025-02-09 02:13:09 +01:00
parent afd2c8043e
commit ba475e6696
2 changed files with 47 additions and 22 deletions

View File

@@ -66,6 +66,15 @@ namespace OpenVulkano
m_open = true;
}
bool ArchiveReader::Open(const std::filesystem::path& archiveFile)
{
PrepOpen();
ChkErr(archive_read_open_filename(m_archive, archiveFile.c_str(), BUFFER_SIZE));
ReadNextHeader();
return HasNext();
}
bool ArchiveReader::Open(const char* archiveFile)
{
if (archiveFile[0] == '\0')
@@ -176,20 +185,14 @@ namespace OpenVulkano
ArchiveType::Type type = *m_archiveType;
switch (type)
{
case ArchiveType::ZIP:
return archive_read_support_format_zip;
case ArchiveType::SEVEN_ZIP:
return archive_read_support_format_7zip;
case ArchiveType::CPIO:
return archive_read_support_format_cpio;
case ArchiveType::ISO:
return archive_read_support_format_iso9660;
case ArchiveType::TAR:
return archive_read_support_format_tar;
case ArchiveType::WARC:
return archive_read_support_format_warc;
case ArchiveType::XAR:
return archive_read_support_format_xar;
case ArchiveType::SEVEN_ZIP: return archive_read_support_format_7zip;
case ArchiveType::ZIP: return archive_read_support_format_zip;
case ArchiveType::CPIO: return archive_read_support_format_cpio;
case ArchiveType::ISO: return archive_read_support_format_iso9660;
case ArchiveType::TAR: return archive_read_support_format_tar;
case ArchiveType::WARC: return archive_read_support_format_warc;
case ArchiveType::XAR: return archive_read_support_format_xar;
case ArchiveType::SHAR: break; // nothing to do
}
return archive_read_support_format_all;
}
@@ -204,6 +207,16 @@ namespace OpenVulkano
return count;
}
size_t ArchiveReader::ExtractRemaining(const std::filesystem::path &targetDir)
{
size_t count = 0;
while (ExtractNext(targetDir))
{
count++;
}
return count;
}
bool ArchiveReader::HasNext() const
{
return m_archiveEntry != nullptr;
@@ -246,19 +259,21 @@ namespace OpenVulkano
return std::nullopt;
}
std::optional<FileDescription> ArchiveReader::ExtractNext(std::string_view targetDir)
std::optional<FileDescription> ArchiveReader::ExtractNext(const std::filesystem::path& targetDir)
{
if (SkipTill(std::filesystem::file_type::regular))
{
FileDescription fileDescription = GetNextDescription();
std::string outputFileName;
outputFileName.reserve(targetDir.size() + 1 + fileDescription.path.size());
outputFileName.append(targetDir).append("/").append(fileDescription.path);
std::filesystem::path outputFilePath;
if (m_filePathRewrite)
outputFilePath = targetDir / m_filePathRewrite(fileDescription.path);
else
outputFilePath = targetDir / fileDescription.path;
std::unique_ptr<archive, decltype(archive_write_free)*> a(archive_write_disk_new(), archive_write_free);
std::unique_ptr<archive_entry, decltype(archive_entry_free)*> entry(archive_entry_clone(m_archiveEntry), archive_entry_free);
archive_entry_set_pathname(entry.get(), outputFileName.c_str());
archive_entry_set_pathname(entry.get(), outputFilePath.c_str());
ChkErr(archive_write_disk_set_options(a.get(), ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS));

View File

@@ -12,7 +12,6 @@
#include <string_view>
#include <optional>
#include <functional>
#include <ostream>
#include <queue>
namespace OpenVulkano
@@ -22,6 +21,7 @@ namespace OpenVulkano
std::optional<ArchiveType::Type> m_archiveType;
std::queue<std::string> m_archivesToRead;
std::function<const char*()> m_passwordCallback;
std::function<std::string(std::string)> m_filePathRewrite;
bool m_open = false;
bool m_eof = false;
@@ -38,6 +38,8 @@ namespace OpenVulkano
void SetPasswordCallback(const decltype(m_passwordCallback)& callback);
bool Open(const std::filesystem::path& archiveFile);
bool Open(const char* archiveFile);
bool Open(const std::string& archiveFile);
@@ -50,7 +52,9 @@ namespace OpenVulkano
[[nodiscard]] bool IsOpen() const { return m_open; }
size_t ExtractRemaining(std::string_view targetDir);
[[deprecated]] size_t ExtractRemaining(std::string_view targetDir);
size_t ExtractRemaining(const std::filesystem::path& targetDir);
// Element wise operations
[[nodiscard]] bool HasNext() const;
@@ -63,7 +67,9 @@ namespace OpenVulkano
std::optional<FileDescription> GetNextDirectory();
std::optional<FileDescription> ExtractNext(std::string_view targetDir);
[[deprecated]] std::optional<FileDescription> ExtractNext(std::string_view targetDir) { return ExtractNext(std::filesystem::path(targetDir)); }
std::optional<FileDescription> ExtractNext(const std::filesystem::path& targetDir);
std::optional<std::pair<FileDescription, Array<char>>> GetNextFile();
@@ -77,6 +83,10 @@ namespace OpenVulkano
const decltype(m_passwordCallback)& GetPasswordCallback() const { return m_passwordCallback; }
void SetPathRewriteFunction(const decltype(m_filePathRewrite)& rewriteFunc) { m_filePathRewrite = rewriteFunc; }
const decltype(m_filePathRewrite)& GetPathRewriteFunction() const { return m_filePathRewrite; }
private:
void ReadNextHeader();
std::function<int(archive*)> GetCurrentFormatReadFunc() const;