2004-11-03 15:23:18 +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.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE. See the above copyright notices for more information.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
#include "cmGeneratedFileStream.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
|
2005-01-27 18:14:24 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2006-10-19 23:00:10 +04:00
|
|
|
# include <cm_zlib.h>
|
2005-01-27 18:14:24 +03:00
|
|
|
#endif
|
|
|
|
|
2005-01-26 22:25:16 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmGeneratedFileStream::cmGeneratedFileStream():
|
|
|
|
cmGeneratedFileStreamBase(), Stream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-11-03 15:23:18 +03:00
|
|
|
//----------------------------------------------------------------------------
|
2004-11-03 15:51:51 +03:00
|
|
|
cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet):
|
|
|
|
cmGeneratedFileStreamBase(name),
|
2006-03-15 19:02:08 +03:00
|
|
|
Stream(TempName.c_str())
|
2004-11-03 15:23:18 +03:00
|
|
|
{
|
|
|
|
// Check if the file opened.
|
|
|
|
if(!*this && !quiet)
|
|
|
|
{
|
2006-05-10 23:56:00 +04:00
|
|
|
cmSystemTools::Error("Cannot open file for write: ",
|
|
|
|
this->TempName.c_str());
|
2004-11-03 15:23:18 +03:00
|
|
|
cmSystemTools::ReportLastSystemError("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmGeneratedFileStream::~cmGeneratedFileStream()
|
|
|
|
{
|
|
|
|
// This is the first destructor called. Check the status of the
|
|
|
|
// stream and give the information to the private base. Next the
|
|
|
|
// stream will be destroyed which will close the temporary file.
|
|
|
|
// Finally the base destructor will be called to replace the
|
|
|
|
// destination file.
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Okay = (*this)?true:false;
|
2004-11-03 15:23:18 +03:00
|
|
|
}
|
|
|
|
|
2005-01-26 22:25:16 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmGeneratedFileStream&
|
2006-01-12 22:21:37 +03:00
|
|
|
cmGeneratedFileStream::Open(const char* name, bool quiet, bool binaryFlag)
|
2005-01-26 22:25:16 +03:00
|
|
|
{
|
|
|
|
// Store the file name and construct the temporary file name.
|
|
|
|
this->cmGeneratedFileStreamBase::Open(name);
|
|
|
|
|
|
|
|
// Open the temporary output file.
|
2006-01-12 22:21:37 +03:00
|
|
|
if ( binaryFlag )
|
2006-01-12 17:20:53 +03:00
|
|
|
{
|
2006-05-10 23:56:00 +04:00
|
|
|
this->Stream::open(this->TempName.c_str(),
|
|
|
|
std::ios::out | std::ios::binary);
|
2006-01-12 17:20:53 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Stream::open(this->TempName.c_str(), std::ios::out);
|
2006-01-12 17:20:53 +03:00
|
|
|
}
|
2005-01-26 22:25:16 +03:00
|
|
|
|
|
|
|
// Check if the file opened.
|
|
|
|
if(!*this && !quiet)
|
|
|
|
{
|
2006-05-10 23:56:00 +04:00
|
|
|
cmSystemTools::Error("Cannot open file for write: ",
|
|
|
|
this->TempName.c_str());
|
2005-01-26 22:25:16 +03:00
|
|
|
cmSystemTools::ReportLastSystemError("");
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-04-05 16:25:21 +04:00
|
|
|
//----------------------------------------------------------------------------
|
2007-11-16 15:01:58 +03:00
|
|
|
bool
|
2005-04-05 16:25:21 +04:00
|
|
|
cmGeneratedFileStream::Close()
|
|
|
|
{
|
|
|
|
// Save whether the temporary output file is valid before closing.
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Okay = (*this)?true:false;
|
2005-04-05 16:25:21 +04:00
|
|
|
|
|
|
|
// Close the temporary output file.
|
|
|
|
this->Stream::close();
|
|
|
|
|
|
|
|
// Remove the temporary file (possibly by renaming to the real file).
|
2007-11-16 15:01:58 +03:00
|
|
|
return this->cmGeneratedFileStreamBase::Close();
|
2005-04-05 16:25:21 +04:00
|
|
|
}
|
|
|
|
|
2004-11-03 15:23:18 +03:00
|
|
|
//----------------------------------------------------------------------------
|
2004-11-03 15:51:51 +03:00
|
|
|
void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->CopyIfDifferent = copy_if_different;
|
2004-11-03 15:51:51 +03:00
|
|
|
}
|
|
|
|
|
2005-01-27 18:14:24 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmGeneratedFileStream::SetCompression(bool compression)
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Compress = compression;
|
2005-01-27 18:14:24 +03:00
|
|
|
}
|
|
|
|
|
2006-01-02 15:52:54 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->CompressExtraExtension = ext;
|
2006-01-02 15:52:54 +03:00
|
|
|
}
|
|
|
|
|
2005-01-26 22:25:16 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
|
2006-03-15 19:02:08 +03:00
|
|
|
Name(),
|
|
|
|
TempName(),
|
|
|
|
CopyIfDifferent(false),
|
|
|
|
Okay(false),
|
|
|
|
Compress(false),
|
|
|
|
CompressExtraExtension(true)
|
2005-01-26 22:25:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-11-03 15:51:51 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
|
2006-03-15 19:02:08 +03:00
|
|
|
Name(),
|
|
|
|
TempName(),
|
|
|
|
CopyIfDifferent(false),
|
|
|
|
Okay(false),
|
|
|
|
Compress(false),
|
|
|
|
CompressExtraExtension(true)
|
2004-11-03 15:23:18 +03:00
|
|
|
{
|
2005-04-05 16:25:21 +04:00
|
|
|
this->Open(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
|
|
|
|
{
|
|
|
|
this->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmGeneratedFileStreamBase::Open(const char* name)
|
|
|
|
{
|
|
|
|
// Save the original name of the file.
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Name = name;
|
2005-04-05 16:25:21 +04:00
|
|
|
|
2004-11-03 15:23:18 +03:00
|
|
|
// Create the name of the temporary file.
|
2006-03-15 19:02:08 +03:00
|
|
|
this->TempName = name;
|
|
|
|
this->TempName += ".tmp";
|
2004-11-03 15:23:18 +03:00
|
|
|
|
|
|
|
// Make sure the temporary file that will be used is not present.
|
2006-03-15 19:02:08 +03:00
|
|
|
cmSystemTools::RemoveFile(this->TempName.c_str());
|
2005-04-14 02:13:09 +04:00
|
|
|
|
2006-03-15 19:02:08 +03:00
|
|
|
std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
|
2005-04-14 02:13:09 +04:00
|
|
|
cmSystemTools::MakeDirectory(dir.c_str());
|
2004-11-03 15:23:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2007-11-16 15:01:58 +03:00
|
|
|
bool cmGeneratedFileStreamBase::Close()
|
2004-11-03 15:23:18 +03:00
|
|
|
{
|
2007-11-16 15:01:58 +03:00
|
|
|
bool replaced = false;
|
|
|
|
|
2006-03-15 19:02:08 +03:00
|
|
|
std::string resname = this->Name;
|
|
|
|
if ( this->Compress && this->CompressExtraExtension )
|
2005-01-27 18:14:24 +03:00
|
|
|
{
|
|
|
|
resname += ".gz";
|
|
|
|
}
|
|
|
|
|
2004-11-03 15:23:18 +03:00
|
|
|
// Only consider replacing the destination file if no error
|
|
|
|
// occurred.
|
2006-03-15 19:02:08 +03:00
|
|
|
if(!this->Name.empty() &&
|
|
|
|
this->Okay &&
|
|
|
|
(!this->CopyIfDifferent ||
|
|
|
|
cmSystemTools::FilesDiffer(this->TempName.c_str(), resname.c_str())))
|
2004-11-03 15:23:18 +03:00
|
|
|
{
|
|
|
|
// The destination is to be replaced. Rename the temporary to the
|
|
|
|
// destination atomically.
|
2006-03-15 19:02:08 +03:00
|
|
|
if ( this->Compress )
|
2005-01-27 18:14:24 +03:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
std::string gzname = this->TempName + ".temp.gz";
|
|
|
|
if ( this->CompressFile(this->TempName.c_str(), gzname.c_str()) )
|
2005-01-27 18:14:24 +03:00
|
|
|
{
|
|
|
|
this->RenameFile(gzname.c_str(), resname.c_str());
|
|
|
|
}
|
|
|
|
cmSystemTools::RemoveFile(gzname.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->RenameFile(this->TempName.c_str(), resname.c_str());
|
2005-01-27 18:14:24 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
replaced = true;
|
2004-11-03 15:23:18 +03:00
|
|
|
}
|
2005-01-27 18:14:24 +03:00
|
|
|
|
|
|
|
// Else, the destination was not replaced.
|
|
|
|
//
|
|
|
|
// Always delete the temporary file. We never want it to stay around.
|
2006-03-15 19:02:08 +03:00
|
|
|
cmSystemTools::RemoveFile(this->TempName.c_str());
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
return replaced;
|
2004-11-03 15:23:18 +03:00
|
|
|
}
|
|
|
|
|
2005-01-27 18:14:24 +03:00
|
|
|
//----------------------------------------------------------------------------
|
2005-01-29 01:25:51 +03:00
|
|
|
#ifdef CMAKE_BUILD_WITH_CMAKE
|
2005-01-27 18:14:24 +03:00
|
|
|
int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
|
|
|
|
const char* newname)
|
|
|
|
{
|
2006-10-19 23:00:10 +04:00
|
|
|
gzFile gf = gzopen(newname, "w");
|
2005-01-27 18:14:24 +03:00
|
|
|
if ( !gf )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
FILE* ifs = fopen(oldname, "r");
|
|
|
|
if ( !ifs )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size_t res;
|
|
|
|
const size_t BUFFER_SIZE = 1024;
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
while ( (res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0 )
|
|
|
|
{
|
2007-09-27 22:44:11 +04:00
|
|
|
if ( !gzwrite(gf, buffer, static_cast<int>(res)) )
|
2005-01-27 18:14:24 +03:00
|
|
|
{
|
|
|
|
fclose(ifs);
|
2006-10-19 23:00:10 +04:00
|
|
|
gzclose(gf);
|
2005-01-27 18:14:24 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(ifs);
|
2006-10-19 23:00:10 +04:00
|
|
|
gzclose(gf);
|
2005-01-27 18:14:24 +03:00
|
|
|
return 1;
|
2005-01-29 01:25:51 +03:00
|
|
|
}
|
2005-01-27 18:14:24 +03:00
|
|
|
#else
|
2005-01-29 01:25:51 +03:00
|
|
|
int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
|
|
|
|
{
|
2005-01-27 18:14:24 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2005-01-29 01:25:51 +03:00
|
|
|
#endif
|
2005-01-27 18:14:24 +03:00
|
|
|
|
2004-11-03 15:23:18 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
|
|
|
|
const char* newname)
|
|
|
|
{
|
2009-04-15 17:57:57 +04:00
|
|
|
return cmSystemTools::RenameFile(oldname, newname);
|
2004-11-03 15:23:18 +03:00
|
|
|
}
|
2005-05-10 19:11:28 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmGeneratedFileStream::SetName(const char* fname)
|
|
|
|
{
|
|
|
|
if ( !fname )
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Name = "";
|
2005-05-10 19:11:28 +04:00
|
|
|
return;
|
|
|
|
}
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Name = fname;
|
2005-05-10 19:11:28 +04:00
|
|
|
}
|