cmGeneratedFileStream.cxx 5.6 KB

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