2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2004-11-03 12:23:18 +00:00
|
|
|
#include "cmGeneratedFileStream.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
|
2016-09-01 18:59:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2005-01-27 15:14:24 +00:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2016-05-16 14:34:04 +00:00
|
|
|
#include <cm_zlib.h>
|
2005-01-27 15:14:24 +00:00
|
|
|
#endif
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmGeneratedFileStream::cmGeneratedFileStream()
|
|
|
|
: cmGeneratedFileStreamBase()
|
|
|
|
, Stream()
|
2005-01-26 19:25:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet)
|
|
|
|
: cmGeneratedFileStreamBase(name)
|
|
|
|
, Stream(TempName.c_str())
|
2004-11-03 12:23:18 +00:00
|
|
|
{
|
|
|
|
// Check if the file opened.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!*this && !quiet) {
|
2012-08-13 17:42:58 +00:00
|
|
|
cmSystemTools::Error("Cannot open file for write: ",
|
2006-05-10 19:56:00 +00:00
|
|
|
this->TempName.c_str());
|
2004-11-03 12:23:18 +00:00
|
|
|
cmSystemTools::ReportLastSystemError("");
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2004-11-03 12:23:18 +00: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-06-01 21:29:53 +00:00
|
|
|
this->Okay = !this->fail();
|
2004-11-03 12:23:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmGeneratedFileStream& cmGeneratedFileStream::Open(const char* name,
|
|
|
|
bool quiet, bool binaryFlag)
|
2005-01-26 19:25:16 +00:00
|
|
|
{
|
|
|
|
// Store the file name and construct the temporary file name.
|
|
|
|
this->cmGeneratedFileStreamBase::Open(name);
|
|
|
|
|
|
|
|
// Open the temporary output file.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (binaryFlag) {
|
2012-08-13 17:42:58 +00:00
|
|
|
this->Stream::open(this->TempName.c_str(),
|
2006-05-10 19:56:00 +00:00
|
|
|
std::ios::out | std::ios::binary);
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2016-06-13 20:56:48 +00:00
|
|
|
this->Stream::open(this->TempName.c_str());
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2005-01-26 19:25:16 +00:00
|
|
|
|
|
|
|
// Check if the file opened.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!*this && !quiet) {
|
2012-08-13 17:42:58 +00:00
|
|
|
cmSystemTools::Error("Cannot open file for write: ",
|
2006-05-10 19:56:00 +00:00
|
|
|
this->TempName.c_str());
|
2005-01-26 19:25:16 +00:00
|
|
|
cmSystemTools::ReportLastSystemError("");
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2005-01-26 19:25:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
bool cmGeneratedFileStream::Close()
|
2005-04-05 12:25:21 +00:00
|
|
|
{
|
|
|
|
// Save whether the temporary output file is valid before closing.
|
2016-06-01 21:29:53 +00:00
|
|
|
this->Okay = !this->fail();
|
2005-04-05 12:25:21 +00:00
|
|
|
|
|
|
|
// Close the temporary output file.
|
|
|
|
this->Stream::close();
|
|
|
|
|
|
|
|
// Remove the temporary file (possibly by renaming to the real file).
|
2007-11-16 12:01:58 +00:00
|
|
|
return this->cmGeneratedFileStreamBase::Close();
|
2005-04-05 12:25:21 +00:00
|
|
|
}
|
|
|
|
|
2004-11-03 12:51:51 +00:00
|
|
|
void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->CopyIfDifferent = copy_if_different;
|
2004-11-03 12:51:51 +00:00
|
|
|
}
|
|
|
|
|
2005-01-27 15:14:24 +00:00
|
|
|
void cmGeneratedFileStream::SetCompression(bool compression)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Compress = compression;
|
2005-01-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2006-01-02 12:52:54 +00:00
|
|
|
void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->CompressExtraExtension = ext;
|
2006-01-02 12:52:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase()
|
|
|
|
: Name()
|
|
|
|
, TempName()
|
|
|
|
, CopyIfDifferent(false)
|
|
|
|
, Okay(false)
|
|
|
|
, Compress(false)
|
|
|
|
, CompressExtraExtension(true)
|
2005-01-26 19:25:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name)
|
|
|
|
: Name()
|
|
|
|
, TempName()
|
|
|
|
, CopyIfDifferent(false)
|
|
|
|
, Okay(false)
|
|
|
|
, Compress(false)
|
|
|
|
, CompressExtraExtension(true)
|
2004-11-03 12:23:18 +00:00
|
|
|
{
|
2005-04-05 12:25:21 +00:00
|
|
|
this->Open(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
|
|
|
|
{
|
|
|
|
this->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmGeneratedFileStreamBase::Open(const char* name)
|
|
|
|
{
|
|
|
|
// Save the original name of the file.
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Name = name;
|
2005-04-05 12:25:21 +00:00
|
|
|
|
2004-11-03 12:23:18 +00:00
|
|
|
// Create the name of the temporary file.
|
2006-03-15 16:02:08 +00:00
|
|
|
this->TempName = name;
|
2009-06-10 17:03:11 +00:00
|
|
|
#if defined(__VMS)
|
|
|
|
this->TempName += "_tmp";
|
|
|
|
#else
|
2006-03-15 16:02:08 +00:00
|
|
|
this->TempName += ".tmp";
|
2009-06-10 17:03:11 +00:00
|
|
|
#endif
|
2004-11-03 12:23:18 +00:00
|
|
|
|
|
|
|
// Make sure the temporary file that will be used is not present.
|
2014-10-15 12:54:05 +00:00
|
|
|
cmSystemTools::RemoveFile(this->TempName);
|
2005-04-13 22:13:09 +00:00
|
|
|
|
2006-03-15 16:02:08 +00:00
|
|
|
std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
|
2005-04-13 22:13:09 +00:00
|
|
|
cmSystemTools::MakeDirectory(dir.c_str());
|
2004-11-03 12:23:18 +00:00
|
|
|
}
|
|
|
|
|
2007-11-16 12:01:58 +00:00
|
|
|
bool cmGeneratedFileStreamBase::Close()
|
2004-11-03 12:23:18 +00:00
|
|
|
{
|
2007-11-16 12:01:58 +00:00
|
|
|
bool replaced = false;
|
|
|
|
|
2006-03-15 16:02:08 +00:00
|
|
|
std::string resname = this->Name;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->Compress && this->CompressExtraExtension) {
|
2005-01-27 15:14:24 +00:00
|
|
|
resname += ".gz";
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2005-01-27 15:14:24 +00:00
|
|
|
|
2004-11-03 12:23:18 +00:00
|
|
|
// Only consider replacing the destination file if no error
|
|
|
|
// occurred.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->Name.empty() && this->Okay &&
|
|
|
|
(!this->CopyIfDifferent ||
|
|
|
|
cmSystemTools::FilesDiffer(this->TempName, resname))) {
|
2004-11-03 12:23:18 +00:00
|
|
|
// The destination is to be replaced. Rename the temporary to the
|
|
|
|
// destination atomically.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->Compress) {
|
2006-03-15 16:02:08 +00:00
|
|
|
std::string gzname = this->TempName + ".temp.gz";
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->CompressFile(this->TempName.c_str(), gzname.c_str())) {
|
2005-01-27 15:14:24 +00:00
|
|
|
this->RenameFile(gzname.c_str(), resname.c_str());
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
cmSystemTools::RemoveFile(gzname);
|
|
|
|
} else {
|
2006-03-15 16:02:08 +00:00
|
|
|
this->RenameFile(this->TempName.c_str(), resname.c_str());
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-11-16 12:01:58 +00:00
|
|
|
|
|
|
|
replaced = true;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2005-01-27 15:14:24 +00:00
|
|
|
|
|
|
|
// Else, the destination was not replaced.
|
|
|
|
//
|
|
|
|
// Always delete the temporary file. We never want it to stay around.
|
2014-10-15 12:54:05 +00:00
|
|
|
cmSystemTools::RemoveFile(this->TempName);
|
2007-11-16 12:01:58 +00:00
|
|
|
|
|
|
|
return replaced;
|
2004-11-03 12:23:18 +00:00
|
|
|
}
|
|
|
|
|
2005-01-28 22:25:51 +00:00
|
|
|
#ifdef CMAKE_BUILD_WITH_CMAKE
|
2005-01-27 15:14:24 +00:00
|
|
|
int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
|
|
|
|
const char* newname)
|
|
|
|
{
|
2006-10-19 19:00:10 +00:00
|
|
|
gzFile gf = gzopen(newname, "w");
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!gf) {
|
2005-01-27 15:14:24 +00:00
|
|
|
return 0;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-01-04 05:47:13 +00:00
|
|
|
FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!ifs) {
|
2005-01-27 15:14:24 +00:00
|
|
|
return 0;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2005-01-27 15:14:24 +00:00
|
|
|
size_t res;
|
|
|
|
const size_t BUFFER_SIZE = 1024;
|
|
|
|
char buffer[BUFFER_SIZE];
|
2016-05-16 14:34:04 +00:00
|
|
|
while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
|
|
|
|
if (!gzwrite(gf, buffer, static_cast<int>(res))) {
|
2005-01-27 15:14:24 +00:00
|
|
|
fclose(ifs);
|
2006-10-19 19:00:10 +00:00
|
|
|
gzclose(gf);
|
2005-01-27 15:14:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2005-01-27 15:14:24 +00:00
|
|
|
fclose(ifs);
|
2006-10-19 19:00:10 +00:00
|
|
|
gzclose(gf);
|
2005-01-27 15:14:24 +00:00
|
|
|
return 1;
|
2005-01-28 22:25:51 +00:00
|
|
|
}
|
2005-01-27 15:14:24 +00:00
|
|
|
#else
|
2005-01-28 22:25:51 +00:00
|
|
|
int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
|
|
|
|
{
|
2005-01-27 15:14:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-01-28 22:25:51 +00:00
|
|
|
#endif
|
2005-01-27 15:14:24 +00:00
|
|
|
|
2004-11-03 12:23:18 +00:00
|
|
|
int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
|
|
|
|
const char* newname)
|
|
|
|
{
|
2009-04-15 13:57:57 +00:00
|
|
|
return cmSystemTools::RenameFile(oldname, newname);
|
2004-11-03 12:23:18 +00:00
|
|
|
}
|
2005-05-10 15:11:28 +00:00
|
|
|
|
2014-02-07 20:37:02 +00:00
|
|
|
void cmGeneratedFileStream::SetName(const std::string& fname)
|
2005-05-10 15:11:28 +00:00
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Name = fname;
|
2005-05-10 15:11:28 +00:00
|
|
|
}
|