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 <stdio.h>
  5. #include "cmSystemTools.h"
  6. #if defined(CMAKE_BUILD_WITH_CMAKE)
  7. # include "cm_codecvt.hxx"
  8. # include "cm_zlib.h"
  9. #endif
  10. cmGeneratedFileStream::cmGeneratedFileStream(Encoding encoding)
  11. {
  12. #ifdef CMAKE_BUILD_WITH_CMAKE
  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: ",
  28. this->TempName.c_str());
  29. cmSystemTools::ReportLastSystemError("");
  30. }
  31. #ifdef CMAKE_BUILD_WITH_CMAKE
  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: ",
  63. this->TempName.c_str());
  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 += "_tmp";
  106. #else
  107. this->TempName += ".tmp";
  108. #endif
  109. // Make sure the temporary file that will be used is not present.
  110. cmSystemTools::RemoveFile(this->TempName);
  111. std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
  112. cmSystemTools::MakeDirectory(dir);
  113. }
  114. bool cmGeneratedFileStreamBase::Close()
  115. {
  116. bool replaced = false;
  117. std::string resname = this->Name;
  118. if (this->Compress && this->CompressExtraExtension) {
  119. resname += ".gz";
  120. }
  121. // Only consider replacing the destination file if no error
  122. // occurred.
  123. if (!this->Name.empty() && this->Okay &&
  124. (!this->CopyIfDifferent ||
  125. cmSystemTools::FilesDiffer(this->TempName, resname))) {
  126. // The destination is to be replaced. Rename the temporary to the
  127. // destination atomically.
  128. if (this->Compress) {
  129. std::string gzname = this->TempName + ".temp.gz";
  130. if (this->CompressFile(this->TempName, gzname)) {
  131. this->RenameFile(gzname, resname);
  132. }
  133. cmSystemTools::RemoveFile(gzname);
  134. } else {
  135. this->RenameFile(this->TempName, resname);
  136. }
  137. replaced = true;
  138. }
  139. // Else, the destination was not replaced.
  140. //
  141. // Always delete the temporary file. We never want it to stay around.
  142. cmSystemTools::RemoveFile(this->TempName);
  143. return replaced;
  144. }
  145. #ifdef CMAKE_BUILD_WITH_CMAKE
  146. int cmGeneratedFileStreamBase::CompressFile(std::string const& oldname,
  147. std::string const& newname)
  148. {
  149. gzFile gf = gzopen(newname.c_str(), "w");
  150. if (!gf) {
  151. return 0;
  152. }
  153. FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
  154. if (!ifs) {
  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. }