cmake/Source/cmArchiveWrite.cxx

336 lines
9.6 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"
2015-04-27 22:25:09 +02:00
#include "cmLocale.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
#include "cm_get_date.h"
2017-07-20 19:35:53 +02:00
#include "cm_libarchive.h"
#include "cmsys/Directory.hxx"
#include "cmsys/Encoding.hxx"
#include "cmsys/FStream.hxx"
2016-10-30 18:24:19 +01:00
#include <iostream>
#include <string.h>
#include <time.h>
2010-11-13 01:00:53 +02:00
2016-03-13 13:35:51 +01:00
#ifndef __LA_SSIZE_T
2016-07-09 11:21:54 +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); }
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,
std::string const& format)
: 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) {
2015-11-17 17:22:37 +01:00
this->Error = "archive_write_add_filter_none: ";
2014-08-03 19:52:23 +02:00
this->Error += 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) {
2015-11-17 17:22:37 +01:00
this->Error = "archive_write_add_filter_compress: ";
2014-08-03 19:52:23 +02:00
this->Error += 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 CompressGZip:
2016-07-09 11:21:54 +02:00
if (archive_write_add_filter_gzip(this->Archive) != ARCHIVE_OK) {
2015-11-17 17:22:37 +01:00
this->Error = "archive_write_add_filter_gzip: ";
2014-08-03 19:52:23 +02:00
this->Error += 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 CompressBZip2:
2016-07-09 11:21:54 +02:00
if (archive_write_add_filter_bzip2(this->Archive) != ARCHIVE_OK) {
2015-11-17 17:22:37 +01:00
this->Error = "archive_write_add_filter_bzip2: ";
2014-08-03 19:52:23 +02:00
this->Error += 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) {
2015-11-17 17:22:37 +01:00
this->Error = "archive_write_add_filter_lzma: ";
2014-08-03 19:52:23 +02:00
this->Error += 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) {
2015-11-17 17:22:37 +01:00
this->Error = "archive_write_add_filter_xz: ";
2014-08-03 19:52:23 +02:00
this->Error += 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;
2016-07-09 11:21:54 +02:00
};
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) {
2010-11-13 01:00:53 +02:00
this->Error = "archive_read_disk_set_standard_lookup: ";
2014-08-03 19:52:23 +02:00
this->Error += 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) {
2015-08-17 11:37:30 +02:00
this->Error = "archive_write_set_format_by_name: ";
this->Error += cm_archive_error_string(this->Archive);
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)) {
2010-11-13 01:00:53 +02:00
this->Error = "archive_write_set_bytes_in_last_block: ";
2014-08-03 19:52:23 +02:00
this->Error += 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
2016-07-09 11:21:54 +02:00
if (archive_write_open(
2016-10-30 18:24:19 +01:00
this->Archive, this, CM_NULLPTR,
2016-07-09 11:21:54 +02:00
reinterpret_cast<archive_write_callback*>(&Callback::Write),
2016-10-30 18:24:19 +01:00
CM_NULLPTR) != ARCHIVE_OK) {
2010-11-13 01:00:53 +02:00
this->Error = "archive_write_open: ";
2014-08-03 19:52:23 +02:00
this->Error += 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
}
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
{
2016-07-09 11:21:54 +02:00
if (this->Okay()) {
if (!path.empty() && path[path.size() - 1] == '/') {
path.erase(path.size() - 1);
2010-11-13 01:00:53 +02:00
}
2016-07-09 11:21:54 +02: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
{
2016-07-09 11:21:54 +02:00
if (!this->AddFile(path, skip, prefix)) {
2010-11-13 01:00:53 +02:00
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)) {
2010-11-13 01:00:53 +02:00
std::string next = path;
next += "/";
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
{
// 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.
2016-07-09 11:21:54 +02:00
std::string dest = prefix ? prefix : "";
2010-11-13 01:00:53 +02:00
dest += 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);
2016-10-30 18:24:19 +01:00
if (archive_read_disk_entry_from_file(this->Disk, e, -1, CM_NULLPTR) !=
ARCHIVE_OK) {
2015-04-27 22:25:09 +02:00
this->Error = "archive_read_disk_entry_from_file '";
this->Error += file;
this->Error += "': ";
2014-08-03 19:52:23 +02:00
this->Error += 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) {
2015-04-27 22:25:09 +02:00
this->Error = "unable to parse mtime '";
this->Error += this->MTime;
this->Error += "'";
return false;
}
2016-07-09 11:21:54 +02:00
archive_entry_set_mtime(e, t, 0);
}
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) {
2010-11-13 01:00:53 +02:00
this->Error = "archive_write_header: ";
2014-08-03 19:52:23 +02:00
this->Error += 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) {
2010-11-13 01:00:53 +02:00
this->Error = "Error opening \"";
this->Error += file;
this->Error += "\": ";
this->Error += cmSystemTools::GetLastSystemError();
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) {
2015-11-17 17:22:37 +01:00
typedef std::streamsize ssize_type;
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) {
2010-11-13 01:00:53 +02:00
this->Error = "archive_write_data: ";
2014-08-03 19:52:23 +02:00
this->Error += 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) {
2010-11-13 01:00:53 +02:00
this->Error = "Error reading \"";
this->Error += file;
this->Error += "\": ";
this->Error += cmSystemTools::GetLastSystemError();
return false;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
return true;
}