2006-01-02 07:21:05 +03:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: CMake - Cross-Platform Makefile Generator
|
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
|
|
|
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
|
|
|
|
|
2006-03-10 21:06:26 +03:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
2006-01-02 07:21:05 +03:00
|
|
|
PURPOSE. See the above copyright notices for more information.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
|
|
|
|
#include "cmCPackTGZGenerator.h"
|
|
|
|
|
|
|
|
#include "cmake.h"
|
|
|
|
#include "cmGlobalGenerator.h"
|
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmMakefile.h"
|
|
|
|
#include "cmGeneratedFileStream.h"
|
2006-01-03 00:14:21 +03:00
|
|
|
#include "cmCPackLog.h"
|
2006-01-02 07:21:05 +03:00
|
|
|
|
|
|
|
#include <cmsys/SystemTools.hxx>
|
2006-01-04 23:14:09 +03:00
|
|
|
#include <cmzlib/zutil.h>
|
2006-01-02 15:53:05 +03:00
|
|
|
#include <libtar/libtar.h>
|
|
|
|
#include <memory> // auto_ptr
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2006-01-02 07:21:05 +03:00
|
|
|
|
2006-01-02 18:37:00 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
class cmCPackTGZGeneratorForward
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static int GenerateHeader(cmCPackTGZGenerator* gg, std::ostream* os)
|
|
|
|
{
|
|
|
|
return gg->GenerateHeader(os);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-01-02 15:53:05 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
cmCPackTGZGenerator::cmCPackTGZGenerator()
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-01-02 15:53:05 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
cmCPackTGZGenerator::~cmCPackTGZGenerator()
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-01-04 23:14:09 +03:00
|
|
|
static const size_t cmCPackTGZ_Data_BlockSize = 16384;
|
|
|
|
|
2006-01-02 07:21:05 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
class cmCPackTGZ_Data
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cmCPackTGZ_Data(cmCPackTGZGenerator* gen) :
|
2006-03-09 00:33:39 +03:00
|
|
|
OutputStream(0), Generator(gen),
|
2006-03-10 21:06:26 +03:00
|
|
|
CompressionLevel(Z_DEFAULT_COMPRESSION) {}
|
2006-01-02 07:21:05 +03:00
|
|
|
std::ostream* OutputStream;
|
|
|
|
cmCPackTGZGenerator* Generator;
|
2006-03-10 21:06:26 +03:00
|
|
|
char CompressedBuffer[cmCPackTGZ_Data_BlockSize];
|
|
|
|
int CompressionLevel;
|
|
|
|
z_stream ZLibStream;
|
|
|
|
uLong CRC;
|
2006-01-02 07:21:05 +03:00
|
|
|
};
|
|
|
|
|
2006-01-03 00:14:21 +03:00
|
|
|
//----------------------------------------------------------------------
|
2006-01-02 18:37:00 +03:00
|
|
|
extern "C" {
|
2006-03-09 00:33:39 +03:00
|
|
|
int cmCPackTGZ_Data_Open(void *client_data, const char* name, int oflags,
|
|
|
|
mode_t mode);
|
2006-01-02 18:37:00 +03:00
|
|
|
ssize_t cmCPackTGZ_Data_Write(void *client_data, void *buff, size_t n);
|
|
|
|
int cmCPackTGZ_Data_Close(void *client_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-02 07:21:05 +03:00
|
|
|
//----------------------------------------------------------------------
|
2006-03-09 00:33:39 +03:00
|
|
|
int cmCPackTGZ_Data_Open(void *client_data, const char* pathname,
|
|
|
|
int, mode_t)
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
|
|
|
cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
|
|
|
|
|
2006-03-10 21:06:26 +03:00
|
|
|
mydata->ZLibStream.zalloc = Z_NULL;
|
|
|
|
mydata->ZLibStream.zfree = Z_NULL;
|
|
|
|
mydata->ZLibStream.opaque = Z_NULL;
|
2006-01-04 23:14:09 +03:00
|
|
|
int strategy = Z_DEFAULT_STRATEGY;
|
2006-03-10 21:06:26 +03:00
|
|
|
if ( deflateInit2(&mydata->ZLibStream, mydata->CompressionLevel,
|
2006-01-04 23:14:09 +03:00
|
|
|
Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy) != Z_OK )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-01-12 03:12:21 +03:00
|
|
|
cmGeneratedFileStream* gf = new cmGeneratedFileStream;
|
|
|
|
// Open binary
|
|
|
|
gf->Open(pathname, false, true);
|
2006-01-02 15:53:05 +03:00
|
|
|
mydata->OutputStream = gf;
|
|
|
|
if ( !*mydata->OutputStream )
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
2006-01-02 15:53:05 +03:00
|
|
|
return -1;
|
2006-01-02 07:21:05 +03:00
|
|
|
}
|
2006-03-10 21:06:26 +03:00
|
|
|
|
2006-01-03 00:14:21 +03:00
|
|
|
if ( !cmCPackTGZGeneratorForward::GenerateHeader(mydata->Generator,gf))
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
2006-01-02 15:53:05 +03:00
|
|
|
return -1;
|
2006-01-02 07:21:05 +03:00
|
|
|
}
|
2006-01-04 23:14:09 +03:00
|
|
|
|
2006-03-10 21:06:26 +03:00
|
|
|
mydata->CRC = crc32(0L, Z_NULL, 0);
|
2006-01-04 23:14:09 +03:00
|
|
|
|
2006-01-02 15:53:05 +03:00
|
|
|
return 0;
|
2006-01-02 07:21:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2006-01-02 18:37:00 +03:00
|
|
|
ssize_t cmCPackTGZ_Data_Write(void *client_data, void *buff, size_t n)
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
|
|
|
cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
|
|
|
|
|
2006-03-10 21:06:26 +03:00
|
|
|
mydata->ZLibStream.avail_in = n;
|
|
|
|
mydata->ZLibStream.next_in = reinterpret_cast<Bytef*>(buff);
|
2006-01-04 23:14:09 +03:00
|
|
|
|
|
|
|
do {
|
2006-03-10 21:06:26 +03:00
|
|
|
mydata->ZLibStream.avail_out = cmCPackTGZ_Data_BlockSize;
|
|
|
|
mydata->ZLibStream.next_out
|
|
|
|
= reinterpret_cast<Bytef*>(mydata->CompressedBuffer);
|
2006-03-09 00:33:39 +03:00
|
|
|
// no bad return value
|
2006-03-10 21:06:26 +03:00
|
|
|
int ret = deflate(&mydata->ZLibStream, (n?Z_NO_FLUSH:Z_FINISH));
|
2006-01-05 00:24:24 +03:00
|
|
|
if(ret == Z_STREAM_ERROR)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2006-01-04 23:14:09 +03:00
|
|
|
|
2006-03-09 00:33:39 +03:00
|
|
|
size_t compressedSize
|
2006-03-10 21:06:26 +03:00
|
|
|
= cmCPackTGZ_Data_BlockSize - mydata->ZLibStream.avail_out;
|
2006-01-04 23:14:09 +03:00
|
|
|
|
2006-03-09 00:33:39 +03:00
|
|
|
mydata->OutputStream->write(
|
2006-03-10 21:06:26 +03:00
|
|
|
reinterpret_cast<const char*>(mydata->CompressedBuffer),
|
2006-03-09 00:33:39 +03:00
|
|
|
compressedSize);
|
2006-03-10 21:06:26 +03:00
|
|
|
} while ( mydata->ZLibStream.avail_out == 0 );
|
2006-01-04 23:14:09 +03:00
|
|
|
|
2006-01-02 07:21:05 +03:00
|
|
|
if ( !*mydata->OutputStream )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2006-01-04 23:14:09 +03:00
|
|
|
if ( n )
|
|
|
|
{
|
2006-03-10 21:06:26 +03:00
|
|
|
mydata->CRC = crc32(mydata->CRC, reinterpret_cast<Bytef *>(buff), n);
|
2006-01-04 23:14:09 +03:00
|
|
|
}
|
2006-01-02 07:21:05 +03:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2006-01-02 18:37:00 +03:00
|
|
|
int cmCPackTGZ_Data_Close(void *client_data)
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
|
|
|
cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
|
|
|
|
|
2006-01-04 23:14:09 +03:00
|
|
|
cmCPackTGZ_Data_Write(client_data, 0, 0);
|
|
|
|
|
|
|
|
char buffer[8];
|
|
|
|
int n;
|
2006-03-10 21:06:26 +03:00
|
|
|
uLong x = mydata->CRC;
|
2006-01-04 23:14:09 +03:00
|
|
|
for (n = 0; n < 4; n++) {
|
|
|
|
buffer[n] = (int)(x & 0xff);
|
|
|
|
x >>= 8;
|
|
|
|
}
|
2006-03-10 21:06:26 +03:00
|
|
|
x = mydata->ZLibStream.total_in;
|
2006-01-04 23:14:09 +03:00
|
|
|
for (n = 0; n < 4; n++) {
|
|
|
|
buffer[n+4] = (int)(x & 0xff);
|
|
|
|
x >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
mydata->OutputStream->write(buffer, 8);
|
2006-03-10 21:06:26 +03:00
|
|
|
(void)deflateEnd(&mydata->ZLibStream);
|
2006-01-02 07:21:05 +03:00
|
|
|
delete mydata->OutputStream;
|
2006-01-02 15:53:05 +03:00
|
|
|
mydata->OutputStream = 0;
|
2006-01-02 07:21:05 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2006-03-09 00:33:39 +03:00
|
|
|
int cmCPackTGZGenerator::CompressFiles(const char* outFileName,
|
|
|
|
const char* toplevel, const std::vector<std::string>& files)
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
2006-01-03 00:14:21 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
|
2006-01-02 07:21:05 +03:00
|
|
|
cmCPackTGZ_Data mydata(this);
|
2006-01-02 15:53:05 +03:00
|
|
|
TAR *t;
|
|
|
|
char buf[TAR_MAXPATHLEN];
|
|
|
|
char pathname[TAR_MAXPATHLEN];
|
|
|
|
|
|
|
|
tartype_t gztype = {
|
2006-01-02 18:37:00 +03:00
|
|
|
(openfunc_t)cmCPackTGZ_Data_Open,
|
|
|
|
(closefunc_t)cmCPackTGZ_Data_Close,
|
2006-01-02 15:53:05 +03:00
|
|
|
(readfunc_t)0,
|
2006-01-02 18:37:00 +03:00
|
|
|
(writefunc_t)cmCPackTGZ_Data_Write,
|
2006-01-02 15:53:05 +03:00
|
|
|
&mydata
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ok, this libtar is not const safe. for now use auto_ptr hack
|
|
|
|
char* realName = new char[ strlen(outFileName) + 1 ];
|
|
|
|
std::auto_ptr<char> realNamePtr(realName);
|
|
|
|
strcpy(realName, outFileName);
|
|
|
|
int flags = O_WRONLY | O_CREAT;
|
|
|
|
if (tar_open(&t, realName,
|
|
|
|
&gztype,
|
|
|
|
flags, 0644,
|
2006-03-10 21:06:26 +03:00
|
|
|
(this->GeneratorVerbose?TAR_VERBOSE:0)
|
2006-01-02 15:53:05 +03:00
|
|
|
| 0) == -1)
|
|
|
|
{
|
2006-03-09 00:33:39 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_open(): "
|
|
|
|
<< strerror(errno) << std::endl);
|
2006-01-02 15:53:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2006-01-02 07:21:05 +03:00
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator fileIt;
|
|
|
|
for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
|
|
|
|
{
|
2006-01-04 23:14:09 +03:00
|
|
|
std::string rp = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
|
2006-01-02 15:53:05 +03:00
|
|
|
strncpy(pathname, fileIt->c_str(), sizeof(pathname));
|
|
|
|
pathname[sizeof(pathname)-1] = 0;
|
2006-01-04 23:14:09 +03:00
|
|
|
strncpy(buf, rp.c_str(), sizeof(buf));
|
2006-01-02 15:53:05 +03:00
|
|
|
buf[sizeof(buf)-1] = 0;
|
2006-01-04 23:14:09 +03:00
|
|
|
if (tar_append_tree(t, pathname, buf) != 0)
|
2006-01-02 07:21:05 +03:00
|
|
|
{
|
2006-01-03 00:14:21 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR,
|
2006-03-09 00:33:39 +03:00
|
|
|
"Problem with tar_append_tree(\"" << buf << "\", \""
|
|
|
|
<< pathname << "\"): "
|
2006-01-03 00:14:21 +03:00
|
|
|
<< strerror(errno) << std::endl);
|
2006-01-02 15:53:05 +03:00
|
|
|
tar_close(t);
|
|
|
|
return 0;
|
2006-01-02 07:21:05 +03:00
|
|
|
}
|
|
|
|
}
|
2006-01-02 15:53:05 +03:00
|
|
|
if (tar_append_eof(t) != 0)
|
|
|
|
{
|
2006-03-09 00:33:39 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_append_eof(): "
|
|
|
|
<< strerror(errno) << std::endl);
|
2006-01-02 15:53:05 +03:00
|
|
|
tar_close(t);
|
|
|
|
return 0;
|
2006-01-02 07:21:05 +03:00
|
|
|
}
|
|
|
|
|
2006-01-02 15:53:05 +03:00
|
|
|
if (tar_close(t) != 0)
|
|
|
|
{
|
2006-03-09 00:33:39 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_close(): "
|
|
|
|
<< strerror(errno) << std::endl);
|
2006-01-02 15:53:05 +03:00
|
|
|
return 0;
|
2006-01-02 07:21:05 +03:00
|
|
|
}
|
2006-01-02 15:53:05 +03:00
|
|
|
return 1;
|
2006-01-02 07:21:05 +03:00
|
|
|
}
|
2006-01-02 18:37:00 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int cmCPackTGZGenerator::GenerateHeader(std::ostream* os)
|
|
|
|
{
|
2006-01-04 23:14:09 +03:00
|
|
|
const int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
|
2006-01-09 21:14:41 +03:00
|
|
|
char header[11];
|
2006-01-04 23:14:09 +03:00
|
|
|
sprintf(header, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
|
|
|
|
Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
|
|
|
|
os->write(header, 10);
|
2006-01-02 18:37:00 +03:00
|
|
|
return 1;
|
|
|
|
}
|