CPackArchiveGenerator use cmArchiveWrite wrapper

This commit is contained in:
Eric NOULARD 2010-08-24 21:41:32 +02:00
parent d5e3e04a23
commit 36a550ae7a
6 changed files with 47 additions and 208 deletions

View File

@ -25,8 +25,8 @@
#include <cm_libarchive.h> #include <cm_libarchive.h>
//---------------------------------------------------------------------- //----------------------------------------------------------------------
cmCPackArchiveGenerator::cmCPackArchiveGenerator(CompressType t, cmCPackArchiveGenerator::cmCPackArchiveGenerator(cmArchiveWrite::Compress t,
ArchiveType at) cmArchiveWrite::Type at)
{ {
this->Compress = t; this->Compress = t;
this->Archive = at; this->Archive = at;
@ -37,126 +37,6 @@ 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)
{
int res = 0;
// pick the archive type
switch(at)
{
case cmCPackArchiveGenerator::TAR:
// maybe this:
res = archive_write_set_format_pax_restricted(a);
break;
case cmCPackArchiveGenerator::ZIP:
res = archive_write_set_format_zip(a);
break;
}
if(res != ARCHIVE_OK)
{
return false;
}
// pick a compression type
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);
}
if(res != ARCHIVE_OK)
{
return false;
}
// do not pad the last block!!
res = archive_write_set_bytes_in_last_block(a, 1);
if(res != ARCHIVE_OK)
{
return false;
}
return true;
}
struct StreamData
{
StreamData(cmGeneratedFileStream* gfs,
cmCPackArchiveGenerator* ag)
{
this->GeneratedFileStream = gfs;
this->Generator = ag;
}
cmGeneratedFileStream* GeneratedFileStream;
cmCPackArchiveGenerator* Generator;
};
extern "C"
{
int OpenArchive(struct archive *, void *client_data)
{
struct StreamData *data = (StreamData*)client_data;
if(data->GeneratedFileStream &&
*data->GeneratedFileStream)
{
if(data->Generator->
GenerateHeader(data->GeneratedFileStream))
{
return ARCHIVE_OK;
}
}
return (ARCHIVE_FATAL);
}
__LA_SSIZE_T WriteArchive(struct archive *,
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;
}
int CloseArchive(struct archive *, void *client_data)
{
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() int cmCPackArchiveGenerator::InitializeInternal()
{ {
@ -166,93 +46,51 @@ int cmCPackArchiveGenerator::InitializeInternal()
int cmCPackArchiveGenerator::PackageFiles() int cmCPackArchiveGenerator::PackageFiles()
{ {
int res = ARCHIVE_OK;
#define CHECK_ARCHIVE_ERROR(res, msg) \
if(res != ARCHIVE_OK) \
{\
cmCPackLogger(cmCPackLog::LOG_ERROR, msg \
<< archive_error_string(a) \
<< cmSystemTools::GetLastSystemError() \
<< " " << res \
<< "\n"); \
}
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
<< toplevel << std::endl); << toplevel << 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 // Open binary stream
cmGeneratedFileStream* gf = new cmGeneratedFileStream; cmGeneratedFileStream gf;
gf->Open(packageFileNames[0].c_str(), false, true); gf.Open(packageFileNames[0].c_str(), false, true);
StreamData data(gf, this); // Add an eventual header to the archive
// pass callbacks to archive_write_open to handle stream if (!GenerateHeader(&gf))
res = archive_write_open(a, {
&data, cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to generate Header for archive < "
OpenArchive, << packageFileNames[0]
WriteArchive, << ">." << std::endl);
CloseArchive); }
CHECK_ARCHIVE_ERROR(res, "archive_write_open:"); // create a new archive
// create a new disk struct cmArchiveWrite archive(gf,this->Compress, this->Archive);
struct archive* disk = archive_read_disk_new(); if (!archive)
#if !defined(_WIN32) || defined(__CYGWIN__) {
res = archive_read_disk_set_standard_lookup(disk); cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to create archive < "
#endif << packageFileNames[0]
CHECK_ARCHIVE_ERROR(res, "archive_read_disk_set_standard_lookup:"); << ">. ERROR ="
<< archive.GetError()
<< std::endl);
return 0;
}
std::vector<std::string>::const_iterator fileIt; std::vector<std::string>::const_iterator fileIt;
std::string dir = cmSystemTools::GetCurrentWorkingDirectory(); std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
cmSystemTools::ChangeDirectory(toplevel.c_str()); cmSystemTools::ChangeDirectory(toplevel.c_str());
for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt ) 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 // Get the relative path to the file
std::string rp = cmSystemTools::RelativePath(toplevel.c_str(), fileIt->c_str()); std::string rp = cmSystemTools::RelativePath(toplevel.c_str(), fileIt->c_str());
// Set the name of the entry to the file name archive.Add(rp);
archive_entry_set_pathname(entry, rp.c_str()); if(!archive)
res = archive_read_disk_entry_from_file(disk, entry, -1, 0);
CHECK_ARCHIVE_ERROR(res, "archive_read_disk_entry_from_file:");
// write entry header
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)
{ {
// now copy contents of file into archive a cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem while adding file< "
FILE* file = fopen(fileIt->c_str(), "rb"); << *fileIt
if(!file) << "> to archive <"
{ << packageFileNames[0] << "> .ERROR ="
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with fopen(): " << archive.GetError()
<< fileIt->c_str() << std::endl);
<< strerror(errno) return 0;
<< 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);
} }
archive_entry_free(entry);
} }
cmSystemTools::ChangeDirectory(dir.c_str()); cmSystemTools::ChangeDirectory(dir.c_str());
// close the archive and finish the write // The destructor of cmArchiveWrite will close and finish the write
archive_write_close(a);
archive_write_finish(a);
archive_read_finish(disk);
return 1; return 1;
} }

View File

@ -13,24 +13,25 @@
#ifndef cmCPackArchiveGenerator_h #ifndef cmCPackArchiveGenerator_h
#define cmCPackArchiveGenerator_h #define cmCPackArchiveGenerator_h
#include "cmArchiveWrite.h"
#include "cmCPackGenerator.h" #include "cmCPackGenerator.h"
/** \class cmCPackArchiveGenerator /** \class cmCPackArchiveGenerator
* \brief A generator base for libarchive generation * \brief A generator base for libarchive generation.
* The generator itself uses the libarchive wrapper
* \ref cmArchiveWrite.
* *
*/ */
class cmCPackArchiveGenerator : public cmCPackGenerator class cmCPackArchiveGenerator : public cmCPackGenerator
{ {
public: public:
enum CompressType{ GZIP, BZIP2, COMPRESS, LZMA, NONE};
enum ArchiveType{ TAR, ZIP};
cmTypeMacro(cmCPackArchiveGenerator, cmCPackGenerator); cmTypeMacro(cmCPackArchiveGenerator, cmCPackGenerator);
/** /**
* Construct generator * Construct generator
*/ */
cmCPackArchiveGenerator(CompressType, ArchiveType); cmCPackArchiveGenerator(cmArchiveWrite::Compress, cmArchiveWrite::Type);
virtual ~cmCPackArchiveGenerator(); virtual ~cmCPackArchiveGenerator();
// Used to add a header to the archive // Used to add a header to the archive
virtual int GenerateHeader(std::ostream* os); virtual int GenerateHeader(std::ostream* os);
@ -39,8 +40,8 @@ protected:
virtual int InitializeInternal(); virtual int InitializeInternal();
int PackageFiles(); int PackageFiles();
virtual const char* GetOutputExtension() = 0; virtual const char* GetOutputExtension() = 0;
CompressType Compress; cmArchiveWrite::Compress Compress;
ArchiveType Archive; cmArchiveWrite::Type Archive;
}; };
#endif #endif

View File

@ -14,8 +14,8 @@
//---------------------------------------------------------------------- //----------------------------------------------------------------------
cmCPackTGZGenerator::cmCPackTGZGenerator() cmCPackTGZGenerator::cmCPackTGZGenerator()
:cmCPackArchiveGenerator(cmCPackArchiveGenerator::GZIP, :cmCPackArchiveGenerator(cmArchiveWrite::CompressGZip,
cmCPackArchiveGenerator::TAR) cmArchiveWrite::TypeTAR)
{ {
} }

View File

@ -13,8 +13,8 @@
#include "cmCPackTarBZip2Generator.h" #include "cmCPackTarBZip2Generator.h"
//---------------------------------------------------------------------- //----------------------------------------------------------------------
cmCPackTarBZip2Generator::cmCPackTarBZip2Generator() cmCPackTarBZip2Generator::cmCPackTarBZip2Generator()
:cmCPackArchiveGenerator(cmCPackArchiveGenerator::BZIP2, :cmCPackArchiveGenerator(cmArchiveWrite::CompressBZip2,
cmCPackArchiveGenerator::TAR) cmArchiveWrite::TypeTAR)
{ {
} }

View File

@ -14,8 +14,8 @@
//---------------------------------------------------------------------- //----------------------------------------------------------------------
cmCPackTarCompressGenerator::cmCPackTarCompressGenerator() cmCPackTarCompressGenerator::cmCPackTarCompressGenerator()
:cmCPackArchiveGenerator(cmCPackArchiveGenerator::COMPRESS, :cmCPackArchiveGenerator(cmArchiveWrite::CompressCompress,
cmCPackArchiveGenerator::TAR) cmArchiveWrite::TypeTAR)
{ {
} }

View File

@ -14,8 +14,8 @@
//---------------------------------------------------------------------- //----------------------------------------------------------------------
cmCPackZIPGenerator::cmCPackZIPGenerator() cmCPackZIPGenerator::cmCPackZIPGenerator()
:cmCPackArchiveGenerator(cmCPackArchiveGenerator::NONE, :cmCPackArchiveGenerator(cmArchiveWrite::CompressNone,
cmCPackArchiveGenerator::ZIP) cmArchiveWrite::TypeZIP)
{ {
} }