src/archive.h
browsing at commit = 118f7b3050759438e47061859b2ee856af44ebfc
/*
MIT License
Copyright (c) 2026 Poul Sander
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <stdexcept>
#include <string>
// Thrown for any tar/gzip writing failure.
class ArchiveError : public std::runtime_error {
public:
explicit ArchiveError(const std::string &what) : std::runtime_error(what) {}
};
// Streams a gzip-compressed tar archive to a file. Entry paths longer than the
// ustar limit are emitted via the GNU long-name extension, which modern tar
// implementations understand. Not thread safe: use one writer per archive.
class TarGzWriter {
public:
explicit TarGzWriter(const std::string &path);
~TarGzWriter();
TarGzWriter(const TarGzWriter &) = delete;
TarGzWriter &operator=(const TarGzWriter &) = delete;
void add_file(const std::string &path, uint32_t mode, std::time_t mtime,
const char *data, uint64_t size);
void add_symlink(const std::string &path, const std::string &target,
std::time_t mtime);
void add_dir(const std::string &path, std::time_t mtime);
// Writes the terminating zero blocks and flushes the gzip stream. Must be
// called for the archive to be valid; the destructor does not call it so a
// half-built archive is never silently completed.
void finish();
private:
void write_header(const std::string &path, uint32_t mode, std::time_t mtime,
uint64_t size, char typeflag, const std::string &linkname);
void write_gnu_long(const std::string &value, char typeflag);
void write_padding(uint64_t size); // pads body to a 512-byte boundary
void deflate_chunk(const void *data, size_t size, int flush);
FILE *file_ = nullptr;
void *stream_ = nullptr; // z_stream*, type-erased to keep zlib out of the header
bool finished_ = false;
};