2010-08-06 19:38:05 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2010 Kitware, Inc., Insight Software Consortium
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
|
|
|
#include "cmArchiveWrite.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
2015-02-06 21:23:07 +03:00
|
|
|
#include "cmLocale.h"
|
2010-08-06 19:38:05 +04:00
|
|
|
#include <cmsys/Directory.hxx>
|
2014-01-04 09:47:13 +04:00
|
|
|
#include <cmsys/FStream.hxx>
|
2010-08-06 19:38:05 +04:00
|
|
|
#include <cm_libarchive.h>
|
2015-01-14 21:22:29 +03:00
|
|
|
#include "cm_get_date.h"
|
2010-08-06 19:38:05 +04:00
|
|
|
|
2015-10-21 17:37:14 +03:00
|
|
|
#ifndef __LA_SSIZE_T
|
|
|
|
# define __LA_SSIZE_T la_ssize_t
|
|
|
|
#endif
|
|
|
|
|
2014-04-16 21:07:55 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
static std::string cm_archive_error_string(struct archive* a)
|
|
|
|
{
|
|
|
|
const char* e = archive_error_string(a);
|
|
|
|
return e? e : "unknown error";
|
|
|
|
}
|
|
|
|
|
2014-06-12 16:46:40 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
static void cm_archive_entry_copy_pathname(struct archive_entry* e,
|
|
|
|
const std::string& dest)
|
|
|
|
{
|
|
|
|
#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,
|
|
|
|
const std::string& file)
|
|
|
|
{
|
|
|
|
#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-08-06 19:38:05 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
class cmArchiveWrite::Entry
|
|
|
|
{
|
|
|
|
struct archive_entry* Object;
|
|
|
|
public:
|
|
|
|
Entry(): Object(archive_entry_new()) {}
|
|
|
|
~Entry() { archive_entry_free(this->Object); }
|
|
|
|
operator struct archive_entry*() { return this->Object; }
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
struct cmArchiveWrite::Callback
|
|
|
|
{
|
|
|
|
// archive_write_callback
|
|
|
|
static __LA_SSIZE_T Write(struct archive*, void *cd,
|
|
|
|
const void *b, size_t n)
|
|
|
|
{
|
|
|
|
cmArchiveWrite* self = static_cast<cmArchiveWrite*>(cd);
|
2010-08-09 18:30:41 +04:00
|
|
|
if(self->Stream.write(static_cast<const char*>(b),
|
2015-08-20 23:02:20 +03:00
|
|
|
static_cast<std::streamsize>(n)))
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
|
|
|
return static_cast<__LA_SSIZE_T>(n);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return static_cast<__LA_SSIZE_T>(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2015-04-07 13:36:52 +03:00
|
|
|
cmArchiveWrite::cmArchiveWrite(
|
|
|
|
std::ostream& os, Compress c, std::string const& format):
|
|
|
|
Stream(os),
|
|
|
|
Archive(archive_write_new()),
|
|
|
|
Disk(archive_read_disk_new()),
|
2015-07-06 17:54:19 +03:00
|
|
|
Verbose(false),
|
|
|
|
Format(format)
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case CompressNone:
|
2015-08-22 19:14:12 +03:00
|
|
|
if(archive_write_add_filter_none(this->Archive) != ARCHIVE_OK)
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
2015-08-22 19:14:12 +03:00
|
|
|
this->Error = "archive_write_add_filter_none: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-06 19:38:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2010-08-16 22:45:05 +04:00
|
|
|
case CompressCompress:
|
2015-08-22 19:14:12 +03:00
|
|
|
if(archive_write_add_filter_compress(this->Archive) != ARCHIVE_OK)
|
2010-08-16 22:45:05 +04:00
|
|
|
{
|
2015-08-22 19:14:12 +03:00
|
|
|
this->Error = "archive_write_add_filter_compress: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-16 22:45:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2010-08-06 19:38:05 +04:00
|
|
|
case CompressGZip:
|
2015-08-22 19:14:12 +03:00
|
|
|
if(archive_write_add_filter_gzip(this->Archive) != ARCHIVE_OK)
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
2015-08-22 19:14:12 +03:00
|
|
|
this->Error = "archive_write_add_filter_gzip: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-06 19:38:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CompressBZip2:
|
2015-08-22 19:14:12 +03:00
|
|
|
if(archive_write_add_filter_bzip2(this->Archive) != ARCHIVE_OK)
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
2015-08-22 19:14:12 +03:00
|
|
|
this->Error = "archive_write_add_filter_bzip2: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-06 19:38:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2010-08-13 19:49:47 +04:00
|
|
|
case CompressLZMA:
|
2015-08-22 19:14:12 +03:00
|
|
|
if(archive_write_add_filter_lzma(this->Archive) != ARCHIVE_OK)
|
2010-08-13 19:49:47 +04:00
|
|
|
{
|
2015-08-22 19:14:12 +03:00
|
|
|
this->Error = "archive_write_add_filter_lzma: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-13 19:49:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2010-08-13 19:51:27 +04:00
|
|
|
case CompressXZ:
|
2015-08-22 19:14:12 +03:00
|
|
|
if(archive_write_add_filter_xz(this->Archive) != ARCHIVE_OK)
|
2010-08-13 19:51:27 +04:00
|
|
|
{
|
2015-08-22 19:14:12 +03:00
|
|
|
this->Error = "archive_write_add_filter_xz: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-13 19:51:27 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2010-08-06 19:38:05 +04:00
|
|
|
};
|
2010-08-13 19:49:47 +04:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
if (archive_read_disk_set_standard_lookup(this->Disk) != ARCHIVE_OK)
|
|
|
|
{
|
|
|
|
this->Error = "archive_read_disk_set_standard_lookup: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2015-04-07 13:36:52 +03:00
|
|
|
return;
|
2010-08-13 19:49:47 +04:00
|
|
|
}
|
|
|
|
#endif
|
2015-04-07 13:36:52 +03:00
|
|
|
|
|
|
|
if(archive_write_set_format_by_name(this->Archive, format.c_str())
|
|
|
|
!= ARCHIVE_OK)
|
2010-08-13 19:49:47 +04:00
|
|
|
{
|
2015-04-07 13:36:52 +03:00
|
|
|
this->Error = "archive_write_set_format_by_name: ";
|
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
|
|
|
return;
|
2010-08-13 19:49:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// do not pad the last block!!
|
|
|
|
if (archive_write_set_bytes_in_last_block(this->Archive, 1))
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
2010-08-13 19:49:47 +04:00
|
|
|
this->Error = "archive_write_set_bytes_in_last_block: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-06 19:38:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(archive_write_open(
|
|
|
|
this->Archive, this, 0,
|
|
|
|
reinterpret_cast<archive_write_callback*>(&Callback::Write),
|
|
|
|
0) != ARCHIVE_OK)
|
|
|
|
{
|
|
|
|
this->Error = "archive_write_open: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-06 19:38:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmArchiveWrite::~cmArchiveWrite()
|
|
|
|
{
|
2015-08-22 19:14:12 +03:00
|
|
|
archive_read_free(this->Disk);
|
|
|
|
archive_write_free(this->Archive);
|
2010-08-06 19:38:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2015-09-11 21:00:50 +03:00
|
|
|
bool cmArchiveWrite::Add(std::string path,
|
|
|
|
size_t skip,
|
|
|
|
const char* prefix,
|
|
|
|
bool recursive)
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
|
|
|
if(this->Okay())
|
|
|
|
{
|
|
|
|
if(!path.empty() && path[path.size()-1] == '/')
|
|
|
|
{
|
|
|
|
path.erase(path.size()-1);
|
|
|
|
}
|
2015-09-11 21:00:50 +03:00
|
|
|
this->AddPath(path.c_str(), skip, prefix, recursive);
|
2010-08-06 19:38:05 +04:00
|
|
|
}
|
|
|
|
return this->Okay();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmArchiveWrite::AddPath(const char* path,
|
2015-09-11 21:00:50 +03:00
|
|
|
size_t skip, const char* prefix,
|
|
|
|
bool recursive)
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
|
|
|
if(!this->AddFile(path, skip, prefix))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-11 21:00:50 +03:00
|
|
|
if((!cmSystemTools::FileIsDirectory(path) || !recursive) ||
|
2011-08-27 21:35:08 +04:00
|
|
|
cmSystemTools::FileIsSymlink(path))
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
cmsys::Directory d;
|
|
|
|
if(d.Load(path))
|
|
|
|
{
|
|
|
|
std::string next = path;
|
|
|
|
next += "/";
|
|
|
|
std::string::size_type end = next.size();
|
|
|
|
unsigned long n = d.GetNumberOfFiles();
|
|
|
|
for(unsigned long i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
const char* file = d.GetFile(i);
|
|
|
|
if(strcmp(file, ".") != 0 && strcmp(file, "..") != 0)
|
|
|
|
{
|
|
|
|
next.erase(end);
|
|
|
|
next += file;
|
|
|
|
if(!this->AddPath(next.c_str(), skip, prefix))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmArchiveWrite::AddFile(const char* file,
|
|
|
|
size_t skip, const char* prefix)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
if(skip >= strlen(file))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const char* out = file + skip;
|
|
|
|
|
2015-02-06 21:23:07 +03:00
|
|
|
cmLocaleRAII localeRAII;
|
|
|
|
static_cast<void>(localeRAII);
|
|
|
|
|
2010-08-06 19:38:05 +04:00
|
|
|
// Meta-data.
|
|
|
|
std::string dest = prefix? prefix : "";
|
|
|
|
dest += out;
|
|
|
|
if(this->Verbose)
|
|
|
|
{
|
|
|
|
std::cout << dest << "\n";
|
|
|
|
}
|
|
|
|
Entry e;
|
2014-06-12 16:46:40 +04:00
|
|
|
cm_archive_entry_copy_sourcepath(e, file);
|
|
|
|
cm_archive_entry_copy_pathname(e, dest);
|
2010-08-06 19:38:05 +04:00
|
|
|
if(archive_read_disk_entry_from_file(this->Disk, e, -1, 0) != ARCHIVE_OK)
|
|
|
|
{
|
2015-01-19 22:14:18 +03:00
|
|
|
this->Error = "archive_read_disk_entry_from_file '";
|
|
|
|
this->Error += file;
|
|
|
|
this->Error += "': ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Disk);
|
2010-08-06 19:38:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-14 21:22:29 +03:00
|
|
|
if (!this->MTime.empty())
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
time_t t = cm_get_date(now, this->MTime.c_str());
|
|
|
|
if (t == -1)
|
|
|
|
{
|
|
|
|
this->Error = "unable to parse mtime '";
|
|
|
|
this->Error += this->MTime;
|
|
|
|
this->Error += "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
archive_entry_set_mtime(e, t, 0);
|
|
|
|
}
|
2015-09-11 21:00:50 +03:00
|
|
|
|
|
|
|
// manages the uid/guid of the entry (if any)
|
|
|
|
if (this->Uid.IsSet() && this->Gid.IsSet())
|
|
|
|
{
|
|
|
|
archive_entry_set_uid(e, this->Uid.Get());
|
|
|
|
archive_entry_set_gid(e, this->Gid.Get());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->Uname.size() && this->Gname.size())
|
|
|
|
{
|
|
|
|
archive_entry_set_uname(e, this->Uname.c_str());
|
|
|
|
archive_entry_set_gname(e, this->Gname.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// manages the permissions
|
|
|
|
if (this->Permissions.IsSet())
|
|
|
|
{
|
|
|
|
archive_entry_set_perm(e, this->Permissions.Get());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->PermissionsMask.IsSet())
|
|
|
|
{
|
|
|
|
mode_t perm = archive_entry_perm(e);
|
|
|
|
archive_entry_set_perm(e, perm & this->PermissionsMask.Get());
|
|
|
|
}
|
|
|
|
|
2011-04-07 16:27:56 +04:00
|
|
|
// Clear acl and xattr fields not useful for distribution.
|
|
|
|
archive_entry_acl_clear(e);
|
|
|
|
archive_entry_xattr_clear(e);
|
2012-06-06 15:29:19 +04:00
|
|
|
archive_entry_set_fflags(e, 0, 0);
|
2015-07-06 17:54:19 +03:00
|
|
|
|
|
|
|
if (this->Format == "pax" || this->Format == "paxr")
|
|
|
|
{
|
|
|
|
// Sparse files are a GNU tar extension.
|
|
|
|
// Do not use them in standard tar files.
|
|
|
|
archive_entry_sparse_clear(e);
|
|
|
|
}
|
|
|
|
|
2010-08-06 19:38:05 +04:00
|
|
|
if(archive_write_header(this->Archive, e) != ARCHIVE_OK)
|
|
|
|
{
|
|
|
|
this->Error = "archive_write_header: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-06 19:38:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-11 02:19:41 +04:00
|
|
|
// do not copy content of symlink
|
|
|
|
if (!archive_entry_symlink(e))
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
2012-01-11 02:19:41 +04:00
|
|
|
// Content.
|
|
|
|
if(size_t size = static_cast<size_t>(archive_entry_size(e)))
|
|
|
|
{
|
|
|
|
return this->AddData(file, size);
|
|
|
|
}
|
2010-08-06 19:38:05 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmArchiveWrite::AddData(const char* file, size_t size)
|
|
|
|
{
|
2015-08-20 23:02:20 +03:00
|
|
|
cmsys::ifstream fin(file, std::ios::in | std::ios::binary);
|
2010-08-06 19:38:05 +04:00
|
|
|
if(!fin)
|
|
|
|
{
|
|
|
|
this->Error = "Error opening \"";
|
|
|
|
this->Error += file;
|
|
|
|
this->Error += "\": ";
|
|
|
|
this->Error += cmSystemTools::GetLastSystemError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[16384];
|
|
|
|
size_t nleft = size;
|
|
|
|
while(nleft > 0)
|
|
|
|
{
|
2015-08-20 23:02:20 +03:00
|
|
|
typedef std::streamsize ssize_type;
|
2010-08-11 17:47:04 +04:00
|
|
|
size_t const nnext = nleft > sizeof(buffer)? sizeof(buffer) : nleft;
|
|
|
|
ssize_type const nnext_s = static_cast<ssize_type>(nnext);
|
|
|
|
fin.read(buffer, nnext_s);
|
2010-08-06 19:38:05 +04:00
|
|
|
// 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.
|
2010-08-11 17:47:04 +04:00
|
|
|
if(static_cast<size_t>(fin.gcount()) != nnext)
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-08-11 17:47:04 +04:00
|
|
|
if(archive_write_data(this->Archive, buffer, nnext) != nnext_s)
|
2010-08-06 19:38:05 +04:00
|
|
|
{
|
|
|
|
this->Error = "archive_write_data: ";
|
2014-04-16 21:07:55 +04:00
|
|
|
this->Error += cm_archive_error_string(this->Archive);
|
2010-08-06 19:38:05 +04:00
|
|
|
return false;
|
|
|
|
}
|
2010-08-11 17:47:04 +04:00
|
|
|
nleft -= nnext;
|
2010-08-06 19:38:05 +04:00
|
|
|
}
|
|
|
|
if(nleft > 0)
|
|
|
|
{
|
|
|
|
this->Error = "Error reading \"";
|
|
|
|
this->Error += file;
|
|
|
|
this->Error += "\": ";
|
|
|
|
this->Error += cmSystemTools::GetLastSystemError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|