CMake/Source/cmGeneratedFileStream.cxx

217 lines
5.4 KiB
C++
Raw Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 22:01:08 +03:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
#include <stdio.h>
2005-01-27 18:14:24 +03:00
#if defined(CMAKE_BUILD_WITH_CMAKE)
#include <cm_zlib.h>
2005-01-27 18:14:24 +03:00
#endif
cmGeneratedFileStream::cmGeneratedFileStream()
: cmGeneratedFileStreamBase()
, Stream()
{
}
cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet)
: cmGeneratedFileStreamBase(name)
, Stream(TempName.c_str())
{
// Check if the file opened.
if (!*this && !quiet) {
cmSystemTools::Error("Cannot open file for write: ",
2006-05-10 23:56:00 +04:00
this->TempName.c_str());
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.
this->Okay = !this->fail();
}
cmGeneratedFileStream& cmGeneratedFileStream::Open(const char* name,
bool quiet, bool binaryFlag)
{
// Store the file name and construct the temporary file name.
this->cmGeneratedFileStreamBase::Open(name);
// Open the temporary output file.
if (binaryFlag) {
this->Stream::open(this->TempName.c_str(),
2006-05-10 23:56:00 +04:00
std::ios::out | std::ios::binary);
} else {
this->Stream::open(this->TempName.c_str());
}
// Check if the file opened.
if (!*this && !quiet) {
cmSystemTools::Error("Cannot open file for write: ",
2006-05-10 23:56:00 +04:00
this->TempName.c_str());
cmSystemTools::ReportLastSystemError("");
}
return *this;
}
bool cmGeneratedFileStream::Close()
{
// Save whether the temporary output file is valid before closing.
this->Okay = !this->fail();
// Close the temporary output file.
this->Stream::close();
// Remove the temporary file (possibly by renaming to the real file).
return this->cmGeneratedFileStreamBase::Close();
}
void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
{
2006-03-15 19:02:08 +03:00
this->CopyIfDifferent = copy_if_different;
}
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
}
void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
{
2006-03-15 19:02:08 +03:00
this->CompressExtraExtension = ext;
}
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase()
: Name()
, TempName()
, CopyIfDifferent(false)
, Okay(false)
, Compress(false)
, CompressExtraExtension(true)
{
}
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name)
: Name()
, TempName()
, CopyIfDifferent(false)
, Okay(false)
, Compress(false)
, CompressExtraExtension(true)
{
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;
// Create the name of the temporary file.
2006-03-15 19:02:08 +03:00
this->TempName = name;
#if defined(__VMS)
this->TempName += "_tmp";
#else
2006-03-15 19:02:08 +03:00
this->TempName += ".tmp";
#endif
// Make sure the temporary file that will be used is not present.
cmSystemTools::RemoveFile(this->TempName);
2006-03-15 19:02:08 +03:00
std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
cmSystemTools::MakeDirectory(dir.c_str());
}
bool cmGeneratedFileStreamBase::Close()
{
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";
}
2005-01-27 18:14:24 +03:00
// Only consider replacing the destination file if no error
// occurred.
if (!this->Name.empty() && this->Okay &&
(!this->CopyIfDifferent ||
cmSystemTools::FilesDiffer(this->TempName, resname))) {
// The destination is to be replaced. Rename the temporary to the
// destination atomically.
if (this->Compress) {
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);
} else {
2006-03-15 19:02:08 +03:00
this->RenameFile(this->TempName.c_str(), resname.c_str());
}
replaced = true;
}
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.
cmSystemTools::RemoveFile(this->TempName);
return replaced;
}
#ifdef CMAKE_BUILD_WITH_CMAKE
2005-01-27 18:14:24 +03:00
int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
const char* newname)
{
gzFile gf = gzopen(newname, "w");
if (!gf) {
2005-01-27 18:14:24 +03:00
return 0;
}
FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
if (!ifs) {
2005-01-27 18:14:24 +03:00
return 0;
}
2005-01-27 18:14:24 +03:00
size_t res;
const size_t BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
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);
gzclose(gf);
2005-01-27 18:14:24 +03:00
return 0;
}
}
2005-01-27 18:14:24 +03:00
fclose(ifs);
gzclose(gf);
2005-01-27 18:14:24 +03:00
return 1;
}
2005-01-27 18:14:24 +03:00
#else
int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
{
2005-01-27 18:14:24 +03:00
return 0;
}
#endif
2005-01-27 18:14:24 +03:00
int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
const char* newname)
{
return cmSystemTools::RenameFile(oldname, newname);
}
2005-05-10 19:11:28 +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
}