cmGeneratedFileStream.cxx 6.8 KB

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