2009-10-30 20:10:56 +03:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 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 "cmCPackArchiveGenerator.h"
|
|
|
|
|
|
|
|
#include "cmake.h"
|
|
|
|
#include "cmGlobalGenerator.h"
|
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmMakefile.h"
|
|
|
|
#include "cmGeneratedFileStream.h"
|
|
|
|
#include "cmCPackLog.h"
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <cmsys/SystemTools.hxx>
|
|
|
|
#include <cmlibarchive/libarchive/archive.h>
|
|
|
|
#include <cmlibarchive/libarchive/archive_entry.h>
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
cmCPackArchiveGenerator::cmCPackArchiveGenerator(CompressType t,
|
|
|
|
ArchiveType at)
|
|
|
|
{
|
|
|
|
this->Compress = t;
|
|
|
|
this->Archive = at;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
cmCPackArchiveGenerator::~cmCPackArchiveGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static const size_t cmCPackTGZ_Data_BlockSize = 16384;
|
|
|
|
|
|
|
|
// make this an anonymous namespace so that archive.h does not
|
|
|
|
// have to be included in the .h file for this class
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool SetArchiveType(struct archive* a,
|
|
|
|
cmCPackArchiveGenerator::CompressType ct,
|
|
|
|
cmCPackArchiveGenerator::ArchiveType at)
|
|
|
|
{
|
2009-11-04 01:21:25 +03:00
|
|
|
int res = 0;
|
2009-11-03 17:40:57 +03:00
|
|
|
// pick the archive type
|
|
|
|
switch(at)
|
|
|
|
{
|
|
|
|
case cmCPackArchiveGenerator::TAR:
|
|
|
|
// maybe this:
|
2009-11-14 16:48:32 +03:00
|
|
|
res = archive_write_set_format_pax_restricted(a);
|
2009-11-03 17:40:57 +03:00
|
|
|
break;
|
|
|
|
case cmCPackArchiveGenerator::ZIP:
|
|
|
|
res = archive_write_set_format_zip(a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(res != ARCHIVE_OK)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pick a compression type
|
2009-10-30 20:10:56 +03:00
|
|
|
switch(ct)
|
|
|
|
{
|
|
|
|
case cmCPackArchiveGenerator::GZIP:
|
|
|
|
res = archive_write_set_compression_gzip(a);
|
|
|
|
break;
|
|
|
|
case cmCPackArchiveGenerator::BZIP2:
|
|
|
|
res = archive_write_set_compression_bzip2(a);
|
|
|
|
break;
|
|
|
|
case cmCPackArchiveGenerator::COMPRESS:
|
|
|
|
res = archive_write_set_compression_compress(a);
|
|
|
|
break;
|
|
|
|
case cmCPackArchiveGenerator::LZMA:
|
|
|
|
res = archive_write_set_compression_lzma(a);
|
|
|
|
break;
|
|
|
|
case cmCPackArchiveGenerator::NONE:
|
|
|
|
default:
|
|
|
|
res = archive_write_set_compression_none(a);
|
2009-11-03 17:40:57 +03:00
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
if(res != ARCHIVE_OK)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-03 17:40:57 +03:00
|
|
|
// do not pad the last block!!
|
|
|
|
res = archive_write_set_bytes_in_last_block(a, 1);
|
2009-10-30 20:10:56 +03:00
|
|
|
if(res != ARCHIVE_OK)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-03 17:40:57 +03:00
|
|
|
|
2009-10-30 20:10:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StreamData
|
|
|
|
{
|
|
|
|
StreamData(cmGeneratedFileStream* gfs,
|
|
|
|
cmCPackArchiveGenerator* ag)
|
|
|
|
{
|
|
|
|
this->GeneratedFileStream = gfs;
|
|
|
|
this->Generator = ag;
|
|
|
|
}
|
|
|
|
cmGeneratedFileStream* GeneratedFileStream;
|
|
|
|
cmCPackArchiveGenerator* Generator;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2009-11-04 00:50:49 +03:00
|
|
|
int OpenArchive(struct archive *, void *client_data)
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
|
|
|
struct StreamData *data = (StreamData*)client_data;
|
|
|
|
if(data->GeneratedFileStream &&
|
2009-11-02 17:20:53 +03:00
|
|
|
*data->GeneratedFileStream)
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
|
|
|
if(data->Generator->
|
|
|
|
GenerateHeader(data->GeneratedFileStream))
|
|
|
|
{
|
|
|
|
return ARCHIVE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
|
2009-11-04 00:50:49 +03:00
|
|
|
__LA_SSIZE_T WriteArchive(struct archive *,
|
2009-10-30 20:10:56 +03:00
|
|
|
void *client_data,
|
|
|
|
const void *buff,
|
|
|
|
size_t n)
|
|
|
|
{
|
|
|
|
struct StreamData *data = (StreamData*)client_data;
|
|
|
|
data->GeneratedFileStream->
|
|
|
|
write(reinterpret_cast<const char*>(buff),n);
|
|
|
|
if(!data->GeneratedFileStream->bad())
|
|
|
|
{
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 00:50:49 +03:00
|
|
|
int CloseArchive(struct archive *, void *client_data)
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
|
|
|
struct StreamData *data = (StreamData*)client_data;
|
|
|
|
if(data->GeneratedFileStream->Close())
|
|
|
|
{
|
|
|
|
delete data->GeneratedFileStream;
|
|
|
|
return ARCHIVE_OK;
|
|
|
|
}
|
|
|
|
return ARCHIVE_FATAL;
|
|
|
|
}
|
|
|
|
} //extern C
|
|
|
|
} // anon name space
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int cmCPackArchiveGenerator::InitializeInternal()
|
|
|
|
{
|
|
|
|
this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
|
|
|
|
return this->Superclass::InitializeInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmCPackArchiveGenerator::CompressFiles(const char* outFileName,
|
|
|
|
const char* toplevel, const std::vector<std::string>& files)
|
|
|
|
{
|
2009-11-09 04:54:13 +03:00
|
|
|
int res = ARCHIVE_OK;
|
|
|
|
#define CHECK_ARCHIVE_ERROR(res, msg) \
|
2009-11-09 17:58:03 +03:00
|
|
|
if(res != ARCHIVE_OK) \
|
2009-11-09 04:54:13 +03:00
|
|
|
{\
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, msg \
|
|
|
|
<< archive_error_string(a) \
|
2009-11-09 17:58:03 +03:00
|
|
|
<< cmSystemTools::GetLastSystemError() \
|
|
|
|
<< " " << res \
|
2009-11-09 04:54:13 +03:00
|
|
|
<< "\n"); \
|
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
|
|
|
|
<< (toplevel ? toplevel : "(NULL)") << std::endl);
|
|
|
|
// create a new archive
|
|
|
|
struct archive* a = archive_write_new();
|
|
|
|
// Set the compress and archive types for the archive
|
|
|
|
SetArchiveType(a, this->Compress, this->Archive);
|
|
|
|
// Open binary stream
|
|
|
|
cmGeneratedFileStream* gf = new cmGeneratedFileStream;
|
|
|
|
gf->Open(outFileName, false, true);
|
|
|
|
StreamData data(gf, this);
|
|
|
|
// pass callbacks to archive_write_open to handle stream
|
2009-11-09 04:54:13 +03:00
|
|
|
res = archive_write_open(a,
|
|
|
|
&data,
|
|
|
|
OpenArchive,
|
|
|
|
WriteArchive,
|
|
|
|
CloseArchive);
|
|
|
|
CHECK_ARCHIVE_ERROR(res, "archive_write_open:");
|
2009-10-30 20:10:56 +03:00
|
|
|
// create a new disk struct
|
|
|
|
struct archive* disk = archive_read_disk_new();
|
2009-11-09 17:58:03 +03:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
2009-11-09 04:54:13 +03:00
|
|
|
res = archive_read_disk_set_standard_lookup(disk);
|
2009-11-09 17:58:03 +03:00
|
|
|
#endif
|
2009-11-09 04:54:13 +03:00
|
|
|
CHECK_ARCHIVE_ERROR(res, "archive_read_disk_set_standard_lookup:");
|
2009-10-30 20:10:56 +03:00
|
|
|
std::vector<std::string>::const_iterator fileIt;
|
2009-11-09 17:58:03 +03:00
|
|
|
std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
|
|
|
|
cmSystemTools::ChangeDirectory(toplevel);
|
2009-10-30 20:10:56 +03:00
|
|
|
for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
|
|
|
|
{
|
|
|
|
// create a new entry for each file
|
|
|
|
struct archive_entry *entry = archive_entry_new();
|
|
|
|
// Get the relative path to the file
|
|
|
|
std::string rp = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
|
|
|
|
// Set the name of the entry to the file name
|
2009-11-09 17:58:03 +03:00
|
|
|
archive_entry_set_pathname(entry, rp.c_str());
|
2009-11-09 04:54:13 +03:00
|
|
|
res = archive_read_disk_entry_from_file(disk, entry, -1, 0);
|
|
|
|
CHECK_ARCHIVE_ERROR(res, "archive_read_disk_entry_from_file:");
|
2009-10-30 20:10:56 +03:00
|
|
|
// write entry header
|
2009-11-09 04:54:13 +03:00
|
|
|
res = archive_write_header(a, entry);
|
|
|
|
CHECK_ARCHIVE_ERROR(res, "archive_write_header:");
|
|
|
|
// the entry size can be 0 if it is a symlink
|
|
|
|
if(archive_entry_size(entry) > 0)
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
2009-11-09 04:54:13 +03:00
|
|
|
// now copy contents of file into archive a
|
|
|
|
FILE* file = fopen(fileIt->c_str(), "rb");
|
|
|
|
if(!file)
|
|
|
|
{
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with fopen(): "
|
|
|
|
<< fileIt->c_str()
|
|
|
|
<< strerror(errno)
|
|
|
|
<< std::endl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char buff[cmCPackTGZ_Data_BlockSize];
|
|
|
|
size_t len = fread(buff, 1, sizeof(buff), file);
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
size_t wlen = archive_write_data(a, buff, len);
|
|
|
|
if(wlen != len)
|
|
|
|
{
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "archive_write_data(): "
|
|
|
|
<< "tried to write " << len << "\n"
|
|
|
|
<< "write " << wlen << "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len = fread(buff, 1, sizeof(buff), file);
|
|
|
|
}
|
|
|
|
// close the file and free the entry
|
|
|
|
fclose(file);
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
archive_entry_free(entry);
|
|
|
|
}
|
2009-11-09 17:58:03 +03:00
|
|
|
cmSystemTools::ChangeDirectory(dir.c_str());
|
2009-10-30 20:10:56 +03:00
|
|
|
// close the archive and finish the write
|
|
|
|
archive_write_close(a);
|
|
|
|
archive_write_finish(a);
|
2009-12-28 21:15:19 +03:00
|
|
|
archive_read_finish(disk);
|
2009-10-30 20:10:56 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int cmCPackArchiveGenerator::GenerateHeader(std::ostream*)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|