2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2004-11-03 15:23:18 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2004-11-03 15:23:18 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
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.
|
|
|
|
============================================================================*/
|
2004-11-03 15:23:18 +03:00
|
|
|
#include "cmGeneratedFileStream.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
|
2005-01-27 18:14:24 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <cm_zlib.h>
|
2005-01-27 18:14:24 +03:00
|
|
|
#endif
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmGeneratedFileStream::cmGeneratedFileStream()
|
|
|
|
: cmGeneratedFileStreamBase()
|
|
|
|
, Stream()
|
2005-01-26 22:25:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet)
|
|
|
|
: cmGeneratedFileStreamBase(name)
|
|
|
|
, Stream(TempName.c_str())
|
2004-11-03 15:23:18 +03:00
|
|
|
{
|
|
|
|
// Check if the file opened.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!*this && !quiet) {
|
2012-08-13 21:42:58 +04:00
|
|
|
cmSystemTools::Error("Cannot open file for write: ",
|
2006-05-10 23:56:00 +04:00
|
|
|
this->TempName.c_str());
|
2004-11-03 15:23:18 +03:00
|
|
|
cmSystemTools::ReportLastSystemError("");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2004-11-03 15:23:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2016-05-16 17:34:04 +03:00
|
|
|
this->Okay = (*this) ? true : false;
|
2004-11-03 15:23:18 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmGeneratedFileStream& 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.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (binaryFlag) {
|
2012-08-13 21:42:58 +04:00
|
|
|
this->Stream::open(this->TempName.c_str(),
|
2006-05-10 23:56:00 +04:00
|
|
|
std::ios::out | std::ios::binary);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Stream::open(this->TempName.c_str(), std::ios::out);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-01-26 22:25:16 +03:00
|
|
|
|
|
|
|
// Check if the file opened.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!*this && !quiet) {
|
2012-08-13 21:42:58 +04:00
|
|
|
cmSystemTools::Error("Cannot open file for write: ",
|
2006-05-10 23:56:00 +04:00
|
|
|
this->TempName.c_str());
|
2005-01-26 22:25:16 +03:00
|
|
|
cmSystemTools::ReportLastSystemError("");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-01-26 22:25:16 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmGeneratedFileStream::Close()
|
2005-04-05 16:25:21 +04:00
|
|
|
{
|
|
|
|
// Save whether the temporary output file is valid before closing.
|
2016-05-16 17:34:04 +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: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
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase()
|
|
|
|
: Name()
|
|
|
|
, TempName()
|
|
|
|
, CopyIfDifferent(false)
|
|
|
|
, Okay(false)
|
|
|
|
, Compress(false)
|
|
|
|
, CompressExtraExtension(true)
|
2005-01-26 22:25:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name)
|
|
|
|
: 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;
|
2009-06-10 21:03:11 +04:00
|
|
|
#if defined(__VMS)
|
|
|
|
this->TempName += "_tmp";
|
|
|
|
#else
|
2006-03-15 19:02:08 +03:00
|
|
|
this->TempName += ".tmp";
|
2009-06-10 21:03:11 +04:00
|
|
|
#endif
|
2004-11-03 15:23:18 +03:00
|
|
|
|
|
|
|
// Make sure the temporary file that will be used is not present.
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::RemoveFile(this->TempName);
|
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;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Compress && this->CompressExtraExtension) {
|
2005-01-27 18:14:24 +03:00
|
|
|
resname += ".gz";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-01-27 18:14:24 +03:00
|
|
|
|
2004-11-03 15:23:18 +03:00
|
|
|
// Only consider replacing the destination file if no error
|
|
|
|
// occurred.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Name.empty() && this->Okay &&
|
|
|
|
(!this->CopyIfDifferent ||
|
|
|
|
cmSystemTools::FilesDiffer(this->TempName, resname))) {
|
2004-11-03 15:23:18 +03:00
|
|
|
// The destination is to be replaced. Rename the temporary to the
|
|
|
|
// destination atomically.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Compress) {
|
2006-03-15 19:02:08 +03:00
|
|
|
std::string gzname = this->TempName + ".temp.gz";
|
2016-05-16 17:34:04 +03:00
|
|
|
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());
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::RemoveFile(gzname);
|
|
|
|
} else {
|
2006-03-15 19:02:08 +03:00
|
|
|
this->RenameFile(this->TempName.c_str(), resname.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
replaced = true;
|
2016-05-16 17:34:04 +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.
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::RemoveFile(this->TempName);
|
2007-11-16 15:01:58 +03:00
|
|
|
|
|
|
|
return replaced;
|
2004-11-03 15:23:18 +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");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!gf) {
|
2005-01-27 18:14:24 +03:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-01-04 09:47:13 +04:00
|
|
|
FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!ifs) {
|
2005-01-27 18:14:24 +03:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-01-27 18:14:24 +03:00
|
|
|
size_t res;
|
|
|
|
const size_t BUFFER_SIZE = 1024;
|
|
|
|
char buffer[BUFFER_SIZE];
|
2016-05-16 17:34:04 +03:00
|
|
|
while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
|
|
|
|
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;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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 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
|
|
|
|
2014-02-08 00:37:02 +04:00
|
|
|
void cmGeneratedFileStream::SetName(const std::string& fname)
|
2005-05-10 19:11:28 +04:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Name = fname;
|
2005-05-10 19:11:28 +04:00
|
|
|
}
|