cmGeneratedFileStream.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGeneratedFileStream.h"
  4. #include <cstdio>
  5. #include "cmStringAlgorithms.h"
  6. #include "cmSystemTools.h"
  7. #if !defined(CMAKE_BOOTSTRAP)
  8. # include <cm3p/zlib.h>
  9. # include "cm_codecvt.hxx"
  10. #endif
  11. cmGeneratedFileStream::cmGeneratedFileStream(Encoding encoding)
  12. : OriginalLocale(this->getloc())
  13. {
  14. #ifndef CMAKE_BOOTSTRAP
  15. if (encoding != codecvt::None) {
  16. this->imbue(std::locale(this->OriginalLocale, new codecvt(encoding)));
  17. }
  18. #else
  19. static_cast<void>(encoding);
  20. #endif
  21. }
  22. cmGeneratedFileStream::cmGeneratedFileStream(std::string const& name,
  23. bool quiet, Encoding encoding)
  24. : cmGeneratedFileStreamBase(name)
  25. , Stream(this->TempName.c_str())
  26. {
  27. // Check if the file opened.
  28. if (!*this && !quiet) {
  29. cmSystemTools::Error("Cannot open file for write: " + this->TempName);
  30. cmSystemTools::ReportLastSystemError("");
  31. }
  32. #ifndef CMAKE_BOOTSTRAP
  33. if (encoding != codecvt::None) {
  34. this->imbue(std::locale(this->getloc(), new codecvt(encoding)));
  35. }
  36. #else
  37. static_cast<void>(encoding);
  38. #endif
  39. if (encoding == codecvt::UTF8_WITH_BOM) {
  40. // Write the BOM encoding header into the file
  41. char magic[] = { char(0xEF), char(0xBB), char(0xBF) };
  42. this->write(magic, 3);
  43. }
  44. }
  45. cmGeneratedFileStream::~cmGeneratedFileStream()
  46. {
  47. // This is the first destructor called. Check the status of the
  48. // stream and give the information to the private base. Next the
  49. // stream will be destroyed which will close the temporary file.
  50. // Finally the base destructor will be called to replace the
  51. // destination file.
  52. this->Okay = !this->fail();
  53. }
  54. cmGeneratedFileStream& cmGeneratedFileStream::Open(std::string const& name,
  55. bool quiet, bool binaryFlag)
  56. {
  57. // Store the file name and construct the temporary file name.
  58. this->cmGeneratedFileStreamBase::Open(name);
  59. // Open the temporary output file.
  60. if (binaryFlag) {
  61. this->Stream::open(this->TempName.c_str(),
  62. std::ios::out | std::ios::binary);
  63. } else {
  64. this->Stream::open(this->TempName.c_str());
  65. }
  66. // Check if the file opened.
  67. if (!*this && !quiet) {
  68. cmSystemTools::Error("Cannot open file for write: " + this->TempName);
  69. cmSystemTools::ReportLastSystemError("");
  70. }
  71. return *this;
  72. }
  73. bool cmGeneratedFileStream::Close()
  74. {
  75. // Save whether the temporary output file is valid before closing.
  76. this->Okay = !this->fail();
  77. // Close the temporary output file.
  78. this->Stream::close();
  79. // Remove the temporary file (possibly by renaming to the real file).
  80. return this->cmGeneratedFileStreamBase::Close();
  81. }
  82. void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
  83. {
  84. this->CopyIfDifferent = copy_if_different;
  85. }
  86. void cmGeneratedFileStream::SetCompression(bool compression)
  87. {
  88. this->Compress = compression;
  89. }
  90. void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
  91. {
  92. this->CompressExtraExtension = ext;
  93. }
  94. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase() = default;
  95. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(std::string const& name)
  96. {
  97. this->Open(name);
  98. }
  99. cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
  100. {
  101. this->Close();
  102. }
  103. void cmGeneratedFileStreamBase::Open(std::string const& name)
  104. {
  105. // Save the original name of the file.
  106. this->Name = name;
  107. // Create the name of the temporary file.
  108. this->TempName = name;
  109. #if defined(__VMS)
  110. this->TempName += "_";
  111. #else
  112. this->TempName += ".";
  113. #endif
  114. if (!this->TempExt.empty()) {
  115. this->TempName += this->TempExt;
  116. } else {
  117. char buf[64];
  118. sprintf(buf, "tmp%05x", cmSystemTools::RandomSeed() & 0xFFFFF);
  119. this->TempName += buf;
  120. }
  121. // Make sure the temporary file that will be used is not present.
  122. cmSystemTools::RemoveFile(this->TempName);
  123. std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
  124. cmSystemTools::MakeDirectory(dir);
  125. }
  126. bool cmGeneratedFileStreamBase::Close()
  127. {
  128. bool replaced = false;
  129. std::string resname = this->Name;
  130. if (this->Compress && this->CompressExtraExtension) {
  131. resname += ".gz";
  132. }
  133. // Only consider replacing the destination file if no error
  134. // occurred.
  135. if (!this->Name.empty() && this->Okay &&
  136. (!this->CopyIfDifferent ||
  137. cmSystemTools::FilesDiffer(this->TempName, resname))) {
  138. // The destination is to be replaced. Rename the temporary to the
  139. // destination atomically.
  140. if (this->Compress) {
  141. std::string gzname = cmStrCat(this->TempName, ".temp.gz");
  142. if (this->CompressFile(this->TempName, gzname)) {
  143. this->RenameFile(gzname, resname);
  144. }
  145. cmSystemTools::RemoveFile(gzname);
  146. } else {
  147. this->RenameFile(this->TempName, resname);
  148. }
  149. replaced = true;
  150. }
  151. // Else, the destination was not replaced.
  152. //
  153. // Always delete the temporary file. We never want it to stay around.
  154. cmSystemTools::RemoveFile(this->TempName);
  155. return replaced;
  156. }
  157. #ifndef CMAKE_BOOTSTRAP
  158. int cmGeneratedFileStreamBase::CompressFile(std::string const& oldname,
  159. std::string const& newname)
  160. {
  161. gzFile gf = gzopen(newname.c_str(), "w");
  162. if (!gf) {
  163. return 0;
  164. }
  165. FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
  166. if (!ifs) {
  167. gzclose(gf);
  168. return 0;
  169. }
  170. size_t res;
  171. const size_t BUFFER_SIZE = 1024;
  172. char buffer[BUFFER_SIZE];
  173. while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
  174. if (!gzwrite(gf, buffer, static_cast<int>(res))) {
  175. fclose(ifs);
  176. gzclose(gf);
  177. return 0;
  178. }
  179. }
  180. fclose(ifs);
  181. gzclose(gf);
  182. return 1;
  183. }
  184. #else
  185. int cmGeneratedFileStreamBase::CompressFile(std::string const&,
  186. std::string const&)
  187. {
  188. return 0;
  189. }
  190. #endif
  191. int cmGeneratedFileStreamBase::RenameFile(std::string const& oldname,
  192. std::string const& newname)
  193. {
  194. return cmSystemTools::RenameFile(oldname, newname);
  195. }
  196. void cmGeneratedFileStream::SetName(const std::string& fname)
  197. {
  198. this->Name = fname;
  199. }
  200. void cmGeneratedFileStream::SetTempExt(std::string const& ext)
  201. {
  202. this->TempExt = ext;
  203. }
  204. void cmGeneratedFileStream::WriteRaw(std::string const& data)
  205. {
  206. #ifndef CMAKE_BOOTSTRAP
  207. std::locale activeLocale = this->imbue(this->OriginalLocale);
  208. this->write(data.data(), data.size());
  209. this->imbue(activeLocale);
  210. #else
  211. this->write(data.data(), data.size());
  212. #endif
  213. }