cmGeneratedFileStream.cxx 6.2 KB

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