cmGeneratedFileStream.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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()
  90. {
  91. }
  92. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(std::string const& name)
  93. {
  94. this->Open(name);
  95. }
  96. cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
  97. {
  98. this->Close();
  99. }
  100. void cmGeneratedFileStreamBase::Open(std::string const& name)
  101. {
  102. // Save the original name of the file.
  103. this->Name = name;
  104. // Create the name of the temporary file.
  105. this->TempName = name;
  106. #if defined(__VMS)
  107. this->TempName += "_tmp";
  108. #else
  109. this->TempName += ".tmp";
  110. #endif
  111. // Make sure the temporary file that will be used is not present.
  112. cmSystemTools::RemoveFile(this->TempName);
  113. std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
  114. cmSystemTools::MakeDirectory(dir);
  115. }
  116. bool cmGeneratedFileStreamBase::Close()
  117. {
  118. bool replaced = false;
  119. std::string resname = this->Name;
  120. if (this->Compress && this->CompressExtraExtension) {
  121. resname += ".gz";
  122. }
  123. // Only consider replacing the destination file if no error
  124. // occurred.
  125. if (!this->Name.empty() && this->Okay &&
  126. (!this->CopyIfDifferent ||
  127. cmSystemTools::FilesDiffer(this->TempName, resname))) {
  128. // The destination is to be replaced. Rename the temporary to the
  129. // destination atomically.
  130. if (this->Compress) {
  131. std::string gzname = this->TempName + ".temp.gz";
  132. if (this->CompressFile(this->TempName, gzname)) {
  133. this->RenameFile(gzname, resname);
  134. }
  135. cmSystemTools::RemoveFile(gzname);
  136. } else {
  137. this->RenameFile(this->TempName, resname);
  138. }
  139. replaced = true;
  140. }
  141. // Else, the destination was not replaced.
  142. //
  143. // Always delete the temporary file. We never want it to stay around.
  144. cmSystemTools::RemoveFile(this->TempName);
  145. return replaced;
  146. }
  147. #ifdef CMAKE_BUILD_WITH_CMAKE
  148. int cmGeneratedFileStreamBase::CompressFile(std::string const& oldname,
  149. std::string const& newname)
  150. {
  151. gzFile gf = gzopen(newname.c_str(), "w");
  152. if (!gf) {
  153. return 0;
  154. }
  155. FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
  156. if (!ifs) {
  157. return 0;
  158. }
  159. size_t res;
  160. const size_t BUFFER_SIZE = 1024;
  161. char buffer[BUFFER_SIZE];
  162. while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
  163. if (!gzwrite(gf, buffer, static_cast<int>(res))) {
  164. fclose(ifs);
  165. gzclose(gf);
  166. return 0;
  167. }
  168. }
  169. fclose(ifs);
  170. gzclose(gf);
  171. return 1;
  172. }
  173. #else
  174. int cmGeneratedFileStreamBase::CompressFile(std::string const&,
  175. std::string const&)
  176. {
  177. return 0;
  178. }
  179. #endif
  180. int cmGeneratedFileStreamBase::RenameFile(std::string const& oldname,
  181. std::string const& newname)
  182. {
  183. return cmSystemTools::RenameFile(oldname.c_str(), newname.c_str());
  184. }
  185. void cmGeneratedFileStream::SetName(const std::string& fname)
  186. {
  187. this->Name = fname;
  188. }