cmake/Source/cmArchiveWrite.cxx

456 lines
14 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2010-11-13 01:00:53 +02:00
#include "cmArchiveWrite.h"
2021-09-14 00:13:48 +02:00
#include <cstdlib>
2020-02-01 23:06:01 +01:00
#include <cstring>
#include <ctime>
#include <iostream>
2021-09-14 00:13:48 +02:00
#include <limits>
2020-02-01 23:06:01 +01:00
#include <sstream>
2021-09-14 00:13:48 +02:00
#include <string>
#include <thread>
#include <cm/algorithm>
2020-02-01 23:06:01 +01:00
2020-08-30 11:54:41 +02:00
#include <cm3p/archive.h>
#include <cm3p/archive_entry.h>
2017-07-20 19:35:53 +02:00
#include "cmsys/Directory.hxx"
#include "cmsys/Encoding.hxx"
#include "cmsys/FStream.hxx"
2020-02-01 23:06:01 +01:00
#include "cm_get_date.h"
#include "cmLocale.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
2010-11-13 01:00:53 +02:00
2016-03-13 13:35:51 +01:00
#ifndef __LA_SSIZE_T
2018-08-09 18:06:22 +02:00
# define __LA_SSIZE_T la_ssize_t
2016-03-13 13:35:51 +01:00
#endif
2014-08-03 19:52:23 +02:00
static std::string cm_archive_error_string(struct archive* a)
{
const char* e = archive_error_string(a);
2016-07-09 11:21:54 +02:00
return e ? e : "unknown error";
2014-08-03 19:52:23 +02:00
}
2015-04-27 22:25:09 +02:00
static void cm_archive_entry_copy_pathname(struct archive_entry* e,
2016-07-09 11:21:54 +02:00
const std::string& dest)
2015-04-27 22:25:09 +02:00
{
#if cmsys_STL_HAS_WSTRING
archive_entry_copy_pathname_w(e, cmsys::Encoding::ToWide(dest).c_str());
#else
archive_entry_copy_pathname(e, dest.c_str());
#endif
}
static void cm_archive_entry_copy_sourcepath(struct archive_entry* e,
2016-07-09 11:21:54 +02:00
const std::string& file)
2015-04-27 22:25:09 +02:00
{
#if cmsys_STL_HAS_WSTRING
archive_entry_copy_sourcepath_w(e, cmsys::Encoding::ToWide(file).c_str());
#else
archive_entry_copy_sourcepath(e, file.c_str());
#endif
}
2010-11-13 01:00:53 +02:00
class cmArchiveWrite::Entry
{
struct archive_entry* Object;
2016-07-09 11:21:54 +02:00
2010-11-13 01:00:53 +02:00
public:
2016-07-09 11:21:54 +02:00
Entry()
: Object(archive_entry_new())
{
}
2010-11-13 01:00:53 +02:00
~Entry() { archive_entry_free(this->Object); }
2019-11-11 23:01:05 +01:00
Entry(const Entry&) = delete;
Entry& operator=(const Entry&) = delete;
2010-11-13 01:00:53 +02:00
operator struct archive_entry*() { return this->Object; }
};
struct cmArchiveWrite::Callback
{
// archive_write_callback
2016-10-30 18:24:19 +01:00
static __LA_SSIZE_T Write(struct archive* /*unused*/, void* cd,
const void* b, size_t n)
2016-07-09 11:21:54 +02:00
{
2010-11-13 01:00:53 +02:00
cmArchiveWrite* self = static_cast<cmArchiveWrite*>(cd);
2016-07-09 11:21:54 +02:00
if (self->Stream.write(static_cast<const char*>(b),
static_cast<std::streamsize>(n))) {
2010-11-13 01:00:53 +02:00
return static_cast<__LA_SSIZE_T>(n);
}
2016-10-30 18:24:19 +01:00
return static_cast<__LA_SSIZE_T>(-1);
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
};
2016-07-09 11:21:54 +02:00
cmArchiveWrite::cmArchiveWrite(std::ostream& os, Compress c,
2021-09-14 00:13:48 +02:00
std::string const& format, int compressionLevel,
int numThreads)
2016-07-09 11:21:54 +02:00
: Stream(os)
, Archive(archive_write_new())
, Disk(archive_read_disk_new())
, Verbose(false)
, Format(format)
2010-11-13 01:00:53 +02:00
{
2016-07-09 11:21:54 +02:00
switch (c) {
2010-11-13 01:00:53 +02:00
case CompressNone:
2016-07-09 11:21:54 +02:00
if (archive_write_add_filter_none(this->Archive) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_add_filter_none: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
break;
case CompressCompress:
2016-07-09 11:21:54 +02:00
if (archive_write_add_filter_compress(this->Archive) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_add_filter_compress: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
break;
2018-10-28 12:09:07 +01:00
case CompressGZip: {
2016-07-09 11:21:54 +02:00
if (archive_write_add_filter_gzip(this->Archive) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_add_filter_gzip: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2018-10-28 12:09:07 +01:00
std::string source_date_epoch;
cmSystemTools::GetEnv("SOURCE_DATE_EPOCH", source_date_epoch);
if (!source_date_epoch.empty()) {
// We're not able to specify an arbitrary timestamp for gzip.
// The next best thing is to omit the timestamp entirely.
if (archive_write_set_filter_option(this->Archive, "gzip", "timestamp",
nullptr) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_set_filter_option: ",
cm_archive_error_string(this->Archive));
2018-10-28 12:09:07 +01:00
return;
}
}
} break;
2010-11-13 01:00:53 +02:00
case CompressBZip2:
2016-07-09 11:21:54 +02:00
if (archive_write_add_filter_bzip2(this->Archive) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_add_filter_bzip2: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
break;
case CompressLZMA:
2016-07-09 11:21:54 +02:00
if (archive_write_add_filter_lzma(this->Archive) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_add_filter_lzma: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
break;
case CompressXZ:
2016-07-09 11:21:54 +02:00
if (archive_write_add_filter_xz(this->Archive) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_add_filter_xz: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
{
#if ARCHIVE_VERSION_NUMBER >= 3004000
// Upstream fixed an issue with their integer parsing in 3.4.0
// which would cause spurious errors to be raised from `strtoull`.
if (numThreads < 1) {
int upperLimit = (numThreads == 0) ? std::numeric_limits<int>::max()
: std::abs(numThreads);
numThreads =
cm::clamp<int>(std::thread::hardware_concurrency(), 1, upperLimit);
}
# ifdef _AIX
// FIXME: Using more than 2 threads creates an empty archive.
// Enforce this limit pending further investigation.
numThreads = std::min(numThreads, 2);
# endif
std::string sNumThreads = std::to_string(numThreads);
if (archive_write_set_filter_option(this->Archive, "xz", "threads",
sNumThreads.c_str()) !=
ARCHIVE_OK) {
this->Error = cmStrCat("archive_compressor_xz_options: ",
cm_archive_error_string(this->Archive));
return;
}
#endif
}
2010-11-13 01:00:53 +02:00
break;
2019-11-11 23:01:05 +01:00
case CompressZstd:
if (archive_write_add_filter_zstd(this->Archive) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_add_filter_zstd: ",
cm_archive_error_string(this->Archive));
2019-11-11 23:01:05 +01:00
return;
}
break;
}
2021-09-14 00:13:48 +02:00
if (compressionLevel != 0) {
std::string compressionLevelStr = std::to_string(compressionLevel);
std::string archiveFilterName;
switch (c) {
case CompressNone:
case CompressCompress:
break;
case CompressGZip:
archiveFilterName = "gzip";
break;
case CompressBZip2:
archiveFilterName = "bzip2";
break;
case CompressLZMA:
archiveFilterName = "lzma";
break;
case CompressXZ:
archiveFilterName = "xz";
break;
case CompressZstd:
archiveFilterName = "zstd";
break;
}
if (!archiveFilterName.empty()) {
if (archive_write_set_filter_option(
this->Archive, archiveFilterName.c_str(), "compression-level",
compressionLevelStr.c_str()) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_set_filter_option: ",
cm_archive_error_string(this->Archive));
return;
}
}
}
2010-11-13 01:00:53 +02:00
#if !defined(_WIN32) || defined(__CYGWIN__)
2016-07-09 11:21:54 +02:00
if (archive_read_disk_set_standard_lookup(this->Disk) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_read_disk_set_standard_lookup: ",
cm_archive_error_string(this->Archive));
2015-08-17 11:37:30 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
#endif
2015-08-17 11:37:30 +02:00
2016-07-09 11:21:54 +02:00
if (archive_write_set_format_by_name(this->Archive, format.c_str()) !=
ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_set_format_by_name: ",
cm_archive_error_string(this->Archive));
2015-08-17 11:37:30 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
// do not pad the last block!!
2016-07-09 11:21:54 +02:00
if (archive_write_set_bytes_in_last_block(this->Archive, 1)) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_set_bytes_in_last_block: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
}
2010-11-13 01:00:53 +02:00
2020-08-30 11:54:41 +02:00
bool cmArchiveWrite::Open()
{
2021-11-20 13:41:27 +01:00
if (!this->Error.empty()) {
return false;
}
2016-07-09 11:21:54 +02:00
if (archive_write_open(
2018-01-26 17:06:56 +01:00
this->Archive, this, nullptr,
2016-07-09 11:21:54 +02:00
reinterpret_cast<archive_write_callback*>(&Callback::Write),
2018-01-26 17:06:56 +01:00
nullptr) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error =
cmStrCat("archive_write_open: ", cm_archive_error_string(this->Archive));
2020-08-30 11:54:41 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
return true;
2010-11-13 01:00:53 +02:00
}
cmArchiveWrite::~cmArchiveWrite()
{
2015-11-17 17:22:37 +01:00
archive_read_free(this->Disk);
archive_write_free(this->Archive);
2010-11-13 01:00:53 +02:00
}
2016-07-09 11:21:54 +02:00
bool cmArchiveWrite::Add(std::string path, size_t skip, const char* prefix,
2015-11-17 17:22:37 +01:00
bool recursive)
2010-11-13 01:00:53 +02:00
{
2019-11-11 23:01:05 +01:00
if (!path.empty() && path.back() == '/') {
path.erase(path.size() - 1);
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
this->AddPath(path.c_str(), skip, prefix, recursive);
2010-11-13 01:00:53 +02:00
return this->Okay();
}
2016-07-09 11:21:54 +02:00
bool cmArchiveWrite::AddPath(const char* path, size_t skip, const char* prefix,
2015-11-17 17:22:37 +01:00
bool recursive)
2010-11-13 01:00:53 +02:00
{
2020-08-30 11:54:41 +02:00
if (strcmp(path, ".") != 0 ||
(this->Format != "zip" && this->Format != "7zip")) {
if (!this->AddFile(path, skip, prefix)) {
return false;
}
2016-07-09 11:21:54 +02:00
}
if ((!cmSystemTools::FileIsDirectory(path) || !recursive) ||
cmSystemTools::FileIsSymlink(path)) {
2010-11-13 01:00:53 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
cmsys::Directory d;
2016-07-09 11:21:54 +02:00
if (d.Load(path)) {
2020-02-01 23:06:01 +01:00
std::string next = cmStrCat(path, '/');
2020-08-30 11:54:41 +02:00
if (next == "./" && (this->Format == "zip" || this->Format == "7zip")) {
next.clear();
}
2010-11-13 01:00:53 +02:00
std::string::size_type end = next.size();
unsigned long n = d.GetNumberOfFiles();
2016-07-09 11:21:54 +02:00
for (unsigned long i = 0; i < n; ++i) {
2010-11-13 01:00:53 +02:00
const char* file = d.GetFile(i);
2016-07-09 11:21:54 +02:00
if (strcmp(file, ".") != 0 && strcmp(file, "..") != 0) {
2010-11-13 01:00:53 +02:00
next.erase(end);
next += file;
2016-07-09 11:21:54 +02:00
if (!this->AddPath(next.c_str(), skip, prefix)) {
2010-11-13 01:00:53 +02:00
return false;
}
}
}
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
bool cmArchiveWrite::AddFile(const char* file, size_t skip, const char* prefix)
2010-11-13 01:00:53 +02:00
{
2019-11-11 23:01:05 +01:00
this->Error = "";
2010-11-13 01:00:53 +02:00
// Skip the file if we have no name for it. This may happen on a
// top-level directory, which does not need to be included anyway.
2016-07-09 11:21:54 +02:00
if (skip >= strlen(file)) {
2010-11-13 01:00:53 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
const char* out = file + skip;
2015-04-27 22:25:09 +02:00
cmLocaleRAII localeRAII;
static_cast<void>(localeRAII);
2010-11-13 01:00:53 +02:00
// Meta-data.
2020-02-01 23:06:01 +01:00
std::string dest = cmStrCat(prefix ? prefix : "", out);
2016-07-09 11:21:54 +02:00
if (this->Verbose) {
2010-11-13 01:00:53 +02:00
std::cout << dest << "\n";
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
Entry e;
2015-04-27 22:25:09 +02:00
cm_archive_entry_copy_sourcepath(e, file);
cm_archive_entry_copy_pathname(e, dest);
2018-01-26 17:06:56 +01:00
if (archive_read_disk_entry_from_file(this->Disk, e, -1, nullptr) !=
2016-10-30 18:24:19 +01:00
ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("Unable to read from file '", file,
"': ", cm_archive_error_string(this->Disk));
2010-11-13 01:00:53 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
if (!this->MTime.empty()) {
2015-04-27 22:25:09 +02:00
time_t now;
time(&now);
time_t t = cm_get_date(now, this->MTime.c_str());
2016-07-09 11:21:54 +02:00
if (t == -1) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("unable to parse mtime '", this->MTime, '\'');
2015-04-27 22:25:09 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
archive_entry_set_mtime(e, t, 0);
2018-10-28 12:09:07 +01:00
} else {
std::string source_date_epoch;
cmSystemTools::GetEnv("SOURCE_DATE_EPOCH", source_date_epoch);
if (!source_date_epoch.empty()) {
std::istringstream iss(source_date_epoch);
time_t epochTime;
iss >> epochTime;
if (iss.eof() && !iss.fail()) {
2020-08-30 11:54:41 +02:00
// Set all of the file times to the epoch time to handle archive
// formats that include creation/access time.
2018-10-28 12:09:07 +01:00
archive_entry_set_mtime(e, epochTime, 0);
2020-08-30 11:54:41 +02:00
archive_entry_set_atime(e, epochTime, 0);
archive_entry_set_ctime(e, epochTime, 0);
archive_entry_set_birthtime(e, epochTime, 0);
2018-10-28 12:09:07 +01:00
}
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// manages the uid/guid of the entry (if any)
2016-07-09 11:21:54 +02:00
if (this->Uid.IsSet() && this->Gid.IsSet()) {
2015-11-17 17:22:37 +01:00
archive_entry_set_uid(e, this->Uid.Get());
archive_entry_set_gid(e, this->Gid.Get());
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2016-10-30 18:24:19 +01:00
if (!this->Uname.empty() && !this->Gname.empty()) {
2015-11-17 17:22:37 +01:00
archive_entry_set_uname(e, this->Uname.c_str());
archive_entry_set_gname(e, this->Gname.c_str());
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// manages the permissions
2016-07-09 11:21:54 +02:00
if (this->Permissions.IsSet()) {
2015-11-17 17:22:37 +01:00
archive_entry_set_perm(e, this->Permissions.Get());
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
if (this->PermissionsMask.IsSet()) {
2016-10-30 18:24:19 +01:00
int perm = archive_entry_perm(e);
2015-11-17 17:22:37 +01:00
archive_entry_set_perm(e, perm & this->PermissionsMask.Get());
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2011-06-19 15:41:06 +03:00
// Clear acl and xattr fields not useful for distribution.
archive_entry_acl_clear(e);
archive_entry_xattr_clear(e);
2012-06-27 20:52:58 +03:00
archive_entry_set_fflags(e, 0, 0);
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
if (this->Format == "pax" || this->Format == "paxr") {
2015-11-17 17:22:37 +01:00
// Sparse files are a GNU tar extension.
// Do not use them in standard tar files.
archive_entry_sparse_clear(e);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
if (archive_write_header(this->Archive, e) != ARCHIVE_OK) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_header: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
2012-04-19 19:04:21 +03:00
// do not copy content of symlink
2016-07-09 11:21:54 +02:00
if (!archive_entry_symlink(e)) {
2012-04-19 19:04:21 +03:00
// Content.
2016-07-09 11:21:54 +02:00
if (size_t size = static_cast<size_t>(archive_entry_size(e))) {
2012-04-19 19:04:21 +03:00
return this->AddData(file, size);
2010-11-13 01:00:53 +02:00
}
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
return true;
}
bool cmArchiveWrite::AddData(const char* file, size_t size)
{
2015-11-17 17:22:37 +01:00
cmsys::ifstream fin(file, std::ios::in | std::ios::binary);
2016-07-09 11:21:54 +02:00
if (!fin) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("Error opening \"", file,
"\": ", cmSystemTools::GetLastSystemError());
2010-11-13 01:00:53 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
char buffer[16384];
size_t nleft = size;
2016-07-09 11:21:54 +02:00
while (nleft > 0) {
2020-02-01 23:06:01 +01:00
using ssize_type = std::streamsize;
2016-07-09 11:21:54 +02:00
size_t const nnext = nleft > sizeof(buffer) ? sizeof(buffer) : nleft;
2010-11-13 01:00:53 +02:00
ssize_type const nnext_s = static_cast<ssize_type>(nnext);
fin.read(buffer, nnext_s);
// Some stream libraries (older HPUX) return failure at end of
// file on the last read even if some data were read. Check
// gcount instead of trusting the stream error status.
2016-07-09 11:21:54 +02:00
if (static_cast<size_t>(fin.gcount()) != nnext) {
2010-11-13 01:00:53 +02:00
break;
2016-07-09 11:21:54 +02:00
}
if (archive_write_data(this->Archive, buffer, nnext) != nnext_s) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("archive_write_data: ",
cm_archive_error_string(this->Archive));
2010-11-13 01:00:53 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
nleft -= nnext;
}
if (nleft > 0) {
2020-02-01 23:06:01 +01:00
this->Error = cmStrCat("Error reading \"", file,
"\": ", cmSystemTools::GetLastSystemError());
2010-11-13 01:00:53 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
return true;
}