Cleaned up the uses of the libarchive structs

This commit is contained in:
Joshua Hildred 2024-02-12 13:33:35 -08:00
parent 4c1ac01e25
commit 48ad506fe3
3 changed files with 22 additions and 19 deletions

View File

@ -121,7 +121,7 @@ public:
close(archive); close(archive);
); );
struct archive_entry * entry = nullptr; Entry entry = nullptr;
std::vector<std::string> files; std::vector<std::string> files;
int error = readNextHeader(archive, &entry); int error = readNextHeader(archive, &entry);
@ -132,7 +132,7 @@ public:
if (!filter || filter(name)) if (!filter || filter(name))
files.push_back(std::move(name)); files.push_back(std::move(name));
error = readNextHeader(current_archive, &entry); error = readNextHeader(archive, &entry);
} }
checkError(error); checkError(error);

View File

@ -57,7 +57,7 @@ public:
: WriteBufferFromFileBase(DBMS_DEFAULT_BUFFER_SIZE, nullptr, 0), archive_writer(archive_writer_), filename(filename_), size(size_) : WriteBufferFromFileBase(DBMS_DEFAULT_BUFFER_SIZE, nullptr, 0), archive_writer(archive_writer_), filename(filename_), size(size_)
{ {
startWritingFile(); startWritingFile();
a = archive_writer_->getArchive(); archive = archive_writer_->getArchive();
entry = nullptr; entry = nullptr;
} }
@ -94,7 +94,7 @@ private:
if (entry == nullptr) if (entry == nullptr)
writeEntry(); writeEntry();
ssize_t to_write = offset(); ssize_t to_write = offset();
ssize_t written = archive_write_data(a, working_buffer.begin(), offset()); ssize_t written = archive_write_data(archive, working_buffer.begin(), offset());
if (written != to_write) if (written != to_write)
{ {
throw Exception( throw Exception(
@ -115,7 +115,7 @@ private:
archive_entry_set_size(entry, expected_size); archive_entry_set_size(entry, expected_size);
archive_entry_set_filetype(entry, static_cast<__LA_MODE_T>(0100000)); archive_entry_set_filetype(entry, static_cast<__LA_MODE_T>(0100000));
archive_entry_set_perm(entry, 0644); archive_entry_set_perm(entry, 0644);
checkResult(archive_write_header(a, entry)); checkResult(archive_write_header(archive, entry));
} }
size_t getSize() const size_t getSize() const
@ -155,8 +155,8 @@ private:
std::weak_ptr<LibArchiveWriter> archive_writer; std::weak_ptr<LibArchiveWriter> archive_writer;
const String filename; const String filename;
struct archive_entry * entry; Entry entry;
struct archive * a; Archive archive;
size_t size; size_t size;
size_t expected_size; size_t expected_size;
}; };
@ -169,18 +169,18 @@ LibArchiveWriter::LibArchiveWriter(const String & path_to_archive_) : path_to_ar
LibArchiveWriter::LibArchiveWriter(const String & path_to_archive_, std::unique_ptr<WriteBuffer> archive_write_buffer_) LibArchiveWriter::LibArchiveWriter(const String & path_to_archive_, std::unique_ptr<WriteBuffer> archive_write_buffer_)
: path_to_archive(path_to_archive_) : path_to_archive(path_to_archive_)
{ {
a = archive_write_new(); archive = archive_write_new();
archive_write_set_format_pax_restricted(a); archive_write_set_format_pax_restricted(archive);
//this allows use to write directly to a writer buffer rather than an intermediate buffer in LibArchive //this allows use to write directly to a writer buffer rather than an intermediate buffer in LibArchive
//archive_write_set_bytes_per_block(a, 0); //archive_write_set_bytes_per_block(a, 0);
if (archive_write_buffer_) if (archive_write_buffer_)
{ {
stream_info = std::make_unique<StreamInfo>(std::move(archive_write_buffer_)); stream_info = std::make_unique<StreamInfo>(std::move(archive_write_buffer_));
archive_write_open2(a, stream_info.get(), nullptr, &StreamInfo::memory_write, nullptr, nullptr); archive_write_open2(archive, stream_info.get(), nullptr, &StreamInfo::memory_write, nullptr, nullptr);
} }
else else
{ {
archive_write_open_filename(a, path_to_archive.c_str()); archive_write_open_filename(archive, path_to_archive.c_str());
} }
} }
@ -189,8 +189,8 @@ LibArchiveWriter::~LibArchiveWriter()
{ {
if (!finalized && !std::uncaught_exceptions() && !std::current_exception()) if (!finalized && !std::uncaught_exceptions() && !std::current_exception())
chassert(false && "TarArchiveWriter is not finalized in destructor."); chassert(false && "TarArchiveWriter is not finalized in destructor.");
if (a) if (archive)
archive_write_free(a); archive_write_free(archive);
} }
std::unique_ptr<WriteBufferFromFileBase> LibArchiveWriter::writeFile(const String & filename, size_t size) std::unique_ptr<WriteBufferFromFileBase> LibArchiveWriter::writeFile(const String & filename, size_t size)
@ -228,8 +228,8 @@ void LibArchiveWriter::finalize()
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
if (finalized) if (finalized)
return; return;
if (a) if (archive)
archive_write_close(a); archive_write_close(archive);
if (stream_info) if (stream_info)
{ {
stream_info->archive_write_buffer->finalize(); stream_info->archive_write_buffer->finalize();
@ -255,10 +255,10 @@ void LibArchiveWriter::setPassword([[maybe_unused]] const String & password_)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Setting a password is not currently supported for tar archives"); throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Setting a password is not currently supported for tar archives");
} }
struct archive * LibArchiveWriter::getArchive() LibArchiveWriter::Archive LibArchiveWriter::getArchive()
{ {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
return a; return archive;
} }
} }
#endif #endif

View File

@ -59,13 +59,16 @@ private:
class WriteBufferFromLibArchive; class WriteBufferFromLibArchive;
class StreamInfo; class StreamInfo;
struct archive * getArchive(); using Archive = struct archive *;
using Entry = struct archive_entry *;
Archive getArchive();
void startWritingFile(); void startWritingFile();
void endWritingFile(); void endWritingFile();
String path_to_archive; String path_to_archive;
std::unique_ptr<StreamInfo> stream_info TSA_GUARDED_BY(mutex) = nullptr; std::unique_ptr<StreamInfo> stream_info TSA_GUARDED_BY(mutex) = nullptr;
struct archive * a TSA_GUARDED_BY(mutex) = nullptr; Archive archive TSA_GUARDED_BY(mutex) = nullptr;
bool is_writing_file TSA_GUARDED_BY(mutex) = false; bool is_writing_file TSA_GUARDED_BY(mutex) = false;
bool finalized TSA_GUARDED_BY(mutex) = false; bool finalized TSA_GUARDED_BY(mutex) = false;
mutable std::mutex mutex; mutable std::mutex mutex;