cmake/Source/cmGeneratedFileStream.cxx

255 lines
6.8 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01: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"
2020-02-01 23:06:01 +01:00
#include <cstdio>
2016-10-30 18:24:19 +01:00
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
#if !defined(CMAKE_BOOTSTRAP)
2020-08-30 11:54:41 +02:00
# include <cm3p/zlib.h>
2018-08-09 18:06:22 +02:00
# include "cm_codecvt.hxx"
#endif
2017-04-14 19:02:05 +02:00
cmGeneratedFileStream::cmGeneratedFileStream(Encoding encoding)
{
2020-02-01 23:06:01 +01:00
#ifndef CMAKE_BOOTSTRAP
2017-04-14 19:02:05 +02:00
if (encoding != codecvt::None) {
2022-11-16 20:14:03 +01:00
this->imbue(std::locale(this->getloc(), new codecvt(encoding)));
2017-04-14 19:02:05 +02:00
}
#else
static_cast<void>(encoding);
#endif
}
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream::cmGeneratedFileStream(std::string const& name,
bool quiet, Encoding encoding)
2016-07-09 11:21:54 +02:00
: cmGeneratedFileStreamBase(name)
2023-05-23 16:38:00 +02:00
, Stream(this->TempName.c_str()) // NOLINT(cmake-use-cmsys-fstream)
{
// Check if the file opened.
2016-07-09 11:21:54 +02:00
if (!*this && !quiet) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Cannot open file for write: " + this->TempName);
cmSystemTools::ReportLastSystemError("");
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
#ifndef CMAKE_BOOTSTRAP
2017-04-14 19:02:05 +02:00
if (encoding != codecvt::None) {
2021-09-14 00:13:48 +02:00
this->imbue(std::locale(this->getloc(), new codecvt(encoding)));
2017-04-14 19:02:05 +02:00
}
#else
static_cast<void>(encoding);
#endif
2021-09-14 00:13:48 +02:00
if (encoding == codecvt::UTF8_WITH_BOM) {
// Write the BOM encoding header into the file
2022-08-04 22:12:04 +02:00
char magic[] = { static_cast<char>(0xEF), static_cast<char>(0xBB),
static_cast<char>(0xBF) };
2021-09-14 00:13:48 +02:00
this->write(magic, 3);
}
}
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-10-30 18:24:19 +01:00
this->Okay = !this->fail();
}
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream& cmGeneratedFileStream::Open(std::string const& name,
2016-07-09 11:21:54 +02:00
bool quiet, bool binaryFlag)
{
// Store the file name and construct the temporary file name.
this->cmGeneratedFileStreamBase::Open(name);
// Open the temporary output file.
2016-07-09 11:21:54 +02:00
if (binaryFlag) {
2023-05-23 16:38:00 +02:00
this->Stream::open( // NOLINT(cmake-use-cmsys-fstream)
this->TempName.c_str(), std::ios::out | std::ios::binary);
2016-07-09 11:21:54 +02:00
} else {
2023-05-23 16:38:00 +02:00
this->Stream::open( // NOLINT(cmake-use-cmsys-fstream)
this->TempName.c_str());
2016-07-09 11:21:54 +02:00
}
// Check if the file opened.
2016-07-09 11:21:54 +02:00
if (!*this && !quiet) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Cannot open file for write: " + this->TempName);
cmSystemTools::ReportLastSystemError("");
2016-07-09 11:21:54 +02:00
}
return *this;
}
2016-07-09 11:21:54 +02:00
bool cmGeneratedFileStream::Close()
{
// Save whether the temporary output file is valid before closing.
2016-10-30 18:24:19 +01:00
this->Okay = !this->fail();
// Close the temporary output file.
2023-05-23 16:38:00 +02:00
this->Stream::close(); // NOLINT(cmake-use-cmsys-fstream)
// Remove the temporary file (possibly by renaming to the real file).
return this->cmGeneratedFileStreamBase::Close();
}
void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
{
this->CopyIfDifferent = copy_if_different;
}
void cmGeneratedFileStream::SetCompression(bool compression)
{
this->Compress = compression;
}
void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
{
this->CompressExtraExtension = ext;
}
2019-11-11 23:01:05 +01:00
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase() = default;
2018-10-28 12:09:07 +01:00
cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(std::string const& name)
{
this->Open(name);
}
cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
{
this->Close();
}
2018-10-28 12:09:07 +01:00
void cmGeneratedFileStreamBase::Open(std::string const& name)
{
// Save the original name of the file.
2022-11-16 20:14:03 +01:00
this->Name = cmSystemTools::CollapseFullPath(name);
// Create the name of the temporary file.
2022-11-16 20:14:03 +01:00
this->TempName = this->Name;
2009-10-04 10:30:41 +03:00
#if defined(__VMS)
2021-09-14 00:13:48 +02:00
this->TempName += "_";
2009-10-04 10:30:41 +03:00
#else
2021-09-14 00:13:48 +02:00
this->TempName += ".";
2009-10-04 10:30:41 +03:00
#endif
2021-09-14 00:13:48 +02:00
if (!this->TempExt.empty()) {
this->TempName += this->TempExt;
} else {
char buf[64];
2022-03-29 21:10:50 +02:00
snprintf(buf, sizeof(buf), "tmp%05x",
cmSystemTools::RandomSeed() & 0xFFFFF);
2021-09-14 00:13:48 +02:00
this->TempName += buf;
}
// Make sure the temporary file that will be used is not present.
2015-04-27 22:25:09 +02:00
cmSystemTools::RemoveFile(this->TempName);
std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
2018-04-23 21:13:27 +02:00
cmSystemTools::MakeDirectory(dir);
}
bool cmGeneratedFileStreamBase::Close()
{
bool replaced = false;
std::string resname = this->Name;
2016-07-09 11:21:54 +02:00
if (this->Compress && this->CompressExtraExtension) {
resname += ".gz";
2016-07-09 11:21:54 +02:00
}
// Only consider replacing the destination file if no error
// occurred.
2016-07-09 11:21:54 +02:00
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.
2016-07-09 11:21:54 +02:00
if (this->Compress) {
2020-02-01 23:06:01 +01:00
std::string gzname = cmStrCat(this->TempName, ".temp.gz");
2018-10-28 12:09:07 +01:00
if (this->CompressFile(this->TempName, gzname)) {
this->RenameFile(gzname, resname);
}
2016-07-09 11:21:54 +02:00
cmSystemTools::RemoveFile(gzname);
} else {
2018-10-28 12:09:07 +01:00
this->RenameFile(this->TempName, resname);
2016-07-09 11:21:54 +02:00
}
replaced = true;
2016-07-09 11:21:54 +02:00
}
// Else, the destination was not replaced.
//
// Always delete the temporary file. We never want it to stay around.
2022-05-25 20:56:39 +02:00
if (!this->TempName.empty()) {
cmSystemTools::RemoveFile(this->TempName);
}
return replaced;
}
2020-02-01 23:06:01 +01:00
#ifndef CMAKE_BOOTSTRAP
2018-10-28 12:09:07 +01:00
int cmGeneratedFileStreamBase::CompressFile(std::string const& oldname,
std::string const& newname)
{
2018-10-28 12:09:07 +01:00
gzFile gf = gzopen(newname.c_str(), "w");
2016-07-09 11:21:54 +02:00
if (!gf) {
return 0;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
2016-07-09 11:21:54 +02:00
if (!ifs) {
2020-08-30 11:54:41 +02:00
gzclose(gf);
return 0;
2016-07-09 11:21:54 +02:00
}
size_t res;
const size_t BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
2016-07-09 11:21:54 +02:00
while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
if (!gzwrite(gf, buffer, static_cast<int>(res))) {
fclose(ifs);
gzclose(gf);
return 0;
}
2016-07-09 11:21:54 +02:00
}
fclose(ifs);
gzclose(gf);
return 1;
}
#else
2018-10-28 12:09:07 +01:00
int cmGeneratedFileStreamBase::CompressFile(std::string const&,
std::string const&)
{
return 0;
}
#endif
2018-10-28 12:09:07 +01:00
int cmGeneratedFileStreamBase::RenameFile(std::string const& oldname,
std::string const& newname)
{
2019-11-11 23:01:05 +01:00
return cmSystemTools::RenameFile(oldname, newname);
}
2015-04-27 22:25:09 +02:00
void cmGeneratedFileStream::SetName(const std::string& fname)
{
2022-11-16 20:14:03 +01:00
this->Name = cmSystemTools::CollapseFullPath(fname);
}
2021-09-14 00:13:48 +02:00
void cmGeneratedFileStream::SetTempExt(std::string const& ext)
{
this->TempExt = ext;
}
2022-11-16 20:14:03 +01:00
void cmGeneratedFileStream::WriteAltEncoding(std::string const& data,
Encoding encoding)
2021-09-14 00:13:48 +02:00
{
#ifndef CMAKE_BOOTSTRAP
2022-11-16 20:14:03 +01:00
std::locale prevLocale =
this->imbue(std::locale(this->getloc(), new codecvt(encoding)));
2021-09-14 00:13:48 +02:00
this->write(data.data(), data.size());
2022-11-16 20:14:03 +01:00
this->imbue(prevLocale);
2021-09-14 00:13:48 +02:00
#else
2022-11-16 20:14:03 +01:00
static_cast<void>(encoding);
2021-09-14 00:13:48 +02:00
this->write(data.data(), data.size());
#endif
}