cmGeneratedFileStream.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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())
  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(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 = cmSystemTools::CollapseFullPath(name);
  107. // Create the name of the temporary file.
  108. this->TempName = this->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. snprintf(buf, sizeof(buf), "tmp%05x",
  119. cmSystemTools::RandomSeed() & 0xFFFFF);
  120. this->TempName += buf;
  121. }
  122. // Make sure the temporary file that will be used is not present.
  123. cmSystemTools::RemoveFile(this->TempName);
  124. std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
  125. cmSystemTools::MakeDirectory(dir);
  126. }
  127. bool cmGeneratedFileStreamBase::Close()
  128. {
  129. bool replaced = false;
  130. std::string resname = this->Name;
  131. if (this->Compress && this->CompressExtraExtension) {
  132. resname += ".gz";
  133. }
  134. // Only consider replacing the destination file if no error
  135. // occurred.
  136. if (!this->Name.empty() && this->Okay &&
  137. (!this->CopyIfDifferent ||
  138. cmSystemTools::FilesDiffer(this->TempName, resname))) {
  139. // The destination is to be replaced. Rename the temporary to the
  140. // destination atomically.
  141. if (this->Compress) {
  142. std::string gzname = cmStrCat(this->TempName, ".temp.gz");
  143. if (this->CompressFile(this->TempName, gzname)) {
  144. this->RenameFile(gzname, resname);
  145. }
  146. cmSystemTools::RemoveFile(gzname);
  147. } else {
  148. this->RenameFile(this->TempName, resname);
  149. }
  150. replaced = true;
  151. }
  152. // Else, the destination was not replaced.
  153. //
  154. // Always delete the temporary file. We never want it to stay around.
  155. if (!this->TempName.empty()) {
  156. cmSystemTools::RemoveFile(this->TempName);
  157. }
  158. return replaced;
  159. }
  160. #ifndef CMAKE_BOOTSTRAP
  161. int cmGeneratedFileStreamBase::CompressFile(std::string const& oldname,
  162. std::string const& newname)
  163. {
  164. gzFile gf = gzopen(newname.c_str(), "w");
  165. if (!gf) {
  166. return 0;
  167. }
  168. FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
  169. if (!ifs) {
  170. gzclose(gf);
  171. return 0;
  172. }
  173. size_t res;
  174. const size_t BUFFER_SIZE = 1024;
  175. char buffer[BUFFER_SIZE];
  176. while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
  177. if (!gzwrite(gf, buffer, static_cast<int>(res))) {
  178. fclose(ifs);
  179. gzclose(gf);
  180. return 0;
  181. }
  182. }
  183. fclose(ifs);
  184. gzclose(gf);
  185. return 1;
  186. }
  187. #else
  188. int cmGeneratedFileStreamBase::CompressFile(std::string const&,
  189. std::string const&)
  190. {
  191. return 0;
  192. }
  193. #endif
  194. int cmGeneratedFileStreamBase::RenameFile(std::string const& oldname,
  195. std::string const& newname)
  196. {
  197. return cmSystemTools::RenameFile(oldname, newname);
  198. }
  199. void cmGeneratedFileStream::SetName(const std::string& fname)
  200. {
  201. this->Name = cmSystemTools::CollapseFullPath(fname);
  202. }
  203. void cmGeneratedFileStream::SetTempExt(std::string const& ext)
  204. {
  205. this->TempExt = ext;
  206. }
  207. void cmGeneratedFileStream::WriteAltEncoding(std::string const& data,
  208. Encoding encoding)
  209. {
  210. #ifndef CMAKE_BOOTSTRAP
  211. std::locale prevLocale =
  212. this->imbue(std::locale(this->getloc(), new codecvt(encoding)));
  213. this->write(data.data(), data.size());
  214. this->imbue(prevLocale);
  215. #else
  216. static_cast<void>(encoding);
  217. this->write(data.data(), data.size());
  218. #endif
  219. }