cmGeneratedFileStream.cxx 6.9 KB

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