ENH: Implement TarCompress generator using compress library

This commit is contained in:
Andy Cedilnik 2006-08-21 12:37:40 -04:00
parent ba9c97ccec
commit 07fa9ac09c
3 changed files with 181 additions and 100 deletions

View File

@ -19,6 +19,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_ZLIB_INCLUDES} ${CMAKE_ZLIB_INCLUDES}
${CMAKE_EXPAT_INCLUDES} ${CMAKE_EXPAT_INCLUDES}
${CMAKE_TAR_INCLUDES} ${CMAKE_TAR_INCLUDES}
${CMAKE_COMPRESS_INCLUDES}
) )
# let cmake know it is supposed to use it # let cmake know it is supposed to use it
@ -217,7 +218,9 @@ ENDIF (WIN32)
# create a library used by the command line and the GUI # create a library used by the command line and the GUI
ADD_LIBRARY(CMakeLib ${SRCS}) ADD_LIBRARY(CMakeLib ${SRCS})
TARGET_LINK_LIBRARIES(CMakeLib cmsys ${CMAKE_EXPAT_LIBRARIES} ${CMAKE_ZLIB_LIBRARIES} ${CMAKE_TAR_LIBRARIES}) TARGET_LINK_LIBRARIES(CMakeLib cmsys
${CMAKE_EXPAT_LIBRARIES} ${CMAKE_ZLIB_LIBRARIES}
${CMAKE_TAR_LIBRARIES} ${CMAKE_COMPRESS_LIBRARIES})
# On Apple we need Carbon # On Apple we need Carbon
IF(APPLE) IF(APPLE)

View File

@ -26,19 +26,25 @@
#include "cmCPackLog.h" #include "cmCPackLog.h"
#include <cmsys/SystemTools.hxx> #include <cmsys/SystemTools.hxx>
#include <cmcompress/cmcompress.h>
#include <libtar/libtar.h>
#include <memory> // auto_ptr
#include <fcntl.h>
#include <errno.h>
// Includes needed for implementation of RenameFile. This is not in //----------------------------------------------------------------------
// system tools because it is not implemented robustly enough to move class cmCPackTarCompressGeneratorForward
// files across directories. {
#ifdef _WIN32 public:
# include <windows.h> static int GenerateHeader(cmCPackTarCompressGenerator* gg, std::ostream* os)
# include <sys/stat.h> {
#endif return gg->GenerateHeader(os);
}
};
//---------------------------------------------------------------------- //----------------------------------------------------------------------
cmCPackTarCompressGenerator::cmCPackTarCompressGenerator() cmCPackTarCompressGenerator::cmCPackTarCompressGenerator()
{ {
this->Compress = false;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -46,22 +52,104 @@ cmCPackTarCompressGenerator::~cmCPackTarCompressGenerator()
{ {
} }
static const size_t cmCPackTarCompress_Data_BlockSize = 16384;
//----------------------------------------------------------------------
class cmCPackTarCompress_Data
{
public:
cmCPackTarCompress_Data(cmCPackTarCompressGenerator* gen) :
OutputStream(0), Generator(gen) {}
std::ostream* OutputStream;
cmCPackTarCompressGenerator* Generator;
cmcompress_stream CMCompressStream;
};
//----------------------------------------------------------------------
extern "C" {
// For cmTar
int cmCPackTarCompress_Data_Open(void *client_data, const char* name,
int oflags, mode_t mode);
ssize_t cmCPackTarCompress_Data_Write(void *client_data, void *buff,
size_t n);
int cmCPackTarCompress_Data_Close(void *client_data);
// For cmCompress
int cmCPackTarCompress_Compress_Output(void* cdata, const char* data,
int len);
}
//----------------------------------------------------------------------
int cmCPackTarCompress_Data_Open(void *client_data, const char* pathname,
int, mode_t)
{
cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;
if ( !cmcompress_compress_initialize(&mydata->CMCompressStream) )
{
return -1;
}
mydata->CMCompressStream.client_data = mydata;
mydata->CMCompressStream.output_stream = cmCPackTarCompress_Compress_Output;
cmGeneratedFileStream* gf = new cmGeneratedFileStream;
// Open binary
gf->Open(pathname, false, true);
mydata->OutputStream = gf;
if ( !*mydata->OutputStream )
{
return -1;
}
if ( !cmcompress_compress_start(&mydata->CMCompressStream) )
{
return -1;
}
if ( !cmCPackTarCompressGeneratorForward::GenerateHeader(
mydata->Generator,gf))
{
return -1;
}
return 0;
}
//----------------------------------------------------------------------
ssize_t cmCPackTarCompress_Data_Write(void *client_data, void *buff, size_t n)
{
cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;
if ( !cmcompress_compress(&mydata->CMCompressStream, buff, n) )
{
return 0;
}
return n;
}
//----------------------------------------------------------------------
int cmCPackTarCompress_Data_Close(void *client_data)
{
cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;
if ( !cmcompress_compress_finalize(&mydata->CMCompressStream) )
{
delete mydata->OutputStream;
return -1;
}
delete mydata->OutputStream;
mydata->OutputStream = 0;
return (0);
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int cmCPackTarCompressGenerator::InitializeInternal() int cmCPackTarCompressGenerator::InitializeInternal()
{ {
this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1"); this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
std::vector<std::string> path;
std::string pkgPath = cmSystemTools::FindProgram("compress", path, false);
if ( pkgPath.empty() )
{
cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find Compress" << std::endl);
return 0;
}
this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Found Compress program: "
<< pkgPath.c_str()
<< std::endl);
return this->Superclass::InitializeInternal(); return this->Superclass::InitializeInternal();
} }
@ -69,101 +157,90 @@ int cmCPackTarCompressGenerator::InitializeInternal()
int cmCPackTarCompressGenerator::CompressFiles(const char* outFileName, int cmCPackTarCompressGenerator::CompressFiles(const char* outFileName,
const char* toplevel, const std::vector<std::string>& files) const char* toplevel, const std::vector<std::string>& files)
{ {
std::string packageDirFileName cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
= this->GetOption("CPACK_TEMPORARY_DIRECTORY"); cmCPackTarCompress_Data mydata(this);
packageDirFileName += ".tar"; TAR *t;
std::string output; char buf[TAR_MAXPATHLEN];
int retVal = -1; char pathname[TAR_MAXPATHLEN];
if ( !this->Superclass::CompressFiles(packageDirFileName.c_str(),
toplevel, files) ) tartype_t compressType = {
(openfunc_t)cmCPackTarCompress_Data_Open,
(closefunc_t)cmCPackTarCompress_Data_Close,
(readfunc_t)0,
(writefunc_t)cmCPackTarCompress_Data_Write,
&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,
&compressType,
flags, 0644,
(this->GeneratorVerbose?TAR_VERBOSE:0)
| 0) == -1)
{ {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_open(): "
<< strerror(errno) << std::endl);
return 0; return 0;
} }
cmOStringStream dmgCmd1; std::vector<std::string>::const_iterator fileIt;
dmgCmd1 << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM") for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
<< "\" \"" << packageDirFileName
<< "\"";
retVal = -1;
int res = cmSystemTools::RunSingleCommand(dmgCmd1.str().c_str(), &output,
&retVal, toplevel, this->GeneratorVerbose, 0);
if ( !res || retVal )
{ {
std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY"); std::string rp = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
tmpFile += "/CompressCompress.log"; strncpy(pathname, fileIt->c_str(), sizeof(pathname));
cmGeneratedFileStream ofs(tmpFile.c_str()); pathname[sizeof(pathname)-1] = 0;
ofs << "# Run command: " << dmgCmd1.str().c_str() << std::endl strncpy(buf, rp.c_str(), sizeof(buf));
<< "# Output:" << std::endl buf[sizeof(buf)-1] = 0;
<< output.c_str() << std::endl; if (tar_append_tree(t, pathname, buf) != 0)
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running Compress command: "
<< dmgCmd1.str().c_str() << std::endl
<< "Please check " << tmpFile.c_str() << " for errors" << std::endl);
return 0;
}
std::string compressOutFile = packageDirFileName + ".Z";
if ( !cmSystemTools::SameFile(compressOutFile.c_str(), outFileName ) )
{
if ( !this->RenameFile(compressOutFile.c_str(), outFileName) )
{ {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem renaming: \"" cmCPackLogger(cmCPackLog::LOG_ERROR,
<< compressOutFile.c_str() << "\" to \"" "Problem with tar_append_tree(\"" << buf << "\", \""
<< outFileName << std::endl); << pathname << "\"): "
<< strerror(errno) << std::endl);
tar_close(t);
return 0; return 0;
} }
} }
if (tar_append_eof(t) != 0)
{
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_append_eof(): "
<< strerror(errno) << std::endl);
tar_close(t);
return 0;
}
if (tar_close(t) != 0)
{
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_close(): "
<< strerror(errno) << std::endl);
return 0;
}
return 1; return 1;
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------
int cmCPackTarCompressGenerator::RenameFile(const char* oldname, int cmCPackTarCompress_Compress_Output(void* client_data,
const char* newname) const char* data, int data_length)
{ {
#ifdef _WIN32 cmcompress_stream *cstream = static_cast<cmcompress_stream*>(client_data);
/* On Windows the move functions will not replace existing files. cmCPackTarCompress_Data *mydata
Check if the destination exists. */ = static_cast<cmCPackTarCompress_Data*>(cstream->client_data);
struct stat newFile; mydata->OutputStream->write(data, data_length);
if(stat(newname, &newFile) == 0)
{
/* The destination exists. We have to replace it carefully. The
MoveFileEx function does what we need but is not available on
Win9x. */
OSVERSIONINFO osv;
DWORD attrs;
/* Make sure the destination is not read only. */ if ( !mydata->OutputStream )
attrs = GetFileAttributes(newname);
if(attrs & FILE_ATTRIBUTE_READONLY)
{
SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
}
/* Check the windows version number. */
osv.dwOSVersionInfoSize = sizeof(osv);
GetVersionEx(&osv);
if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
/* This is Win9x. There is no MoveFileEx implementation. We
cannot quite rename the file atomically. Just delete the
destination and then move the file. */
DeleteFile(newname);
return MoveFile(oldname, newname);
}
else
{
/* This is not Win9x. Use the MoveFileEx implementation. */
return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING);
}
}
else
{ {
/* The destination does not exist. Just move the file. */ return 0;
return MoveFile(oldname, newname);
} }
#else return data_length;
/* On UNIX we have an OS-provided call to do this atomically. */
return rename(oldname, newname) == 0;
#endif
} }
//----------------------------------------------------------------------
int cmCPackTarCompressGenerator::GenerateHeader(std::ostream* os)
{
(void)os;
return 1;
}

View File

@ -42,6 +42,7 @@ protected:
virtual const char* GetOutputExtension() { return "tar.Z"; } virtual const char* GetOutputExtension() { return "tar.Z"; }
int RenameFile(const char* oldname, const char* newname); int RenameFile(const char* oldname, const char* newname);
int GenerateHeader(std::ostream* os);
}; };
#endif #endif