cmGeneratedFileStream.cxx 5.6 KB

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