cmGeneratedFileStream.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. {
  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 += "_";
  105. #else
  106. this->TempName += ".";
  107. #endif
  108. if (!this->TempExt.empty()) {
  109. this->TempName += this->TempExt;
  110. } else {
  111. char buf[64];
  112. sprintf(buf, "tmp%05x", cmSystemTools::RandomSeed() & 0xFFFFF);
  113. this->TempName += buf;
  114. }
  115. // Make sure the temporary file that will be used is not present.
  116. cmSystemTools::RemoveFile(this->TempName);
  117. std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
  118. cmSystemTools::MakeDirectory(dir);
  119. }
  120. bool cmGeneratedFileStreamBase::Close()
  121. {
  122. bool replaced = false;
  123. std::string resname = this->Name;
  124. if (this->Compress && this->CompressExtraExtension) {
  125. resname += ".gz";
  126. }
  127. // Only consider replacing the destination file if no error
  128. // occurred.
  129. if (!this->Name.empty() && this->Okay &&
  130. (!this->CopyIfDifferent ||
  131. cmSystemTools::FilesDiffer(this->TempName, resname))) {
  132. // The destination is to be replaced. Rename the temporary to the
  133. // destination atomically.
  134. if (this->Compress) {
  135. std::string gzname = cmStrCat(this->TempName, ".temp.gz");
  136. if (this->CompressFile(this->TempName, gzname)) {
  137. this->RenameFile(gzname, resname);
  138. }
  139. cmSystemTools::RemoveFile(gzname);
  140. } else {
  141. this->RenameFile(this->TempName, resname);
  142. }
  143. replaced = true;
  144. }
  145. // Else, the destination was not replaced.
  146. //
  147. // Always delete the temporary file. We never want it to stay around.
  148. cmSystemTools::RemoveFile(this->TempName);
  149. return replaced;
  150. }
  151. #ifndef CMAKE_BOOTSTRAP
  152. int cmGeneratedFileStreamBase::CompressFile(std::string const& oldname,
  153. std::string const& newname)
  154. {
  155. gzFile gf = gzopen(newname.c_str(), "w");
  156. if (!gf) {
  157. return 0;
  158. }
  159. FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
  160. if (!ifs) {
  161. gzclose(gf);
  162. return 0;
  163. }
  164. size_t res;
  165. const size_t BUFFER_SIZE = 1024;
  166. char buffer[BUFFER_SIZE];
  167. while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
  168. if (!gzwrite(gf, buffer, static_cast<int>(res))) {
  169. fclose(ifs);
  170. gzclose(gf);
  171. return 0;
  172. }
  173. }
  174. fclose(ifs);
  175. gzclose(gf);
  176. return 1;
  177. }
  178. #else
  179. int cmGeneratedFileStreamBase::CompressFile(std::string const&,
  180. std::string const&)
  181. {
  182. return 0;
  183. }
  184. #endif
  185. int cmGeneratedFileStreamBase::RenameFile(std::string const& oldname,
  186. std::string const& newname)
  187. {
  188. return cmSystemTools::RenameFile(oldname, newname);
  189. }
  190. void cmGeneratedFileStream::SetName(const std::string& fname)
  191. {
  192. this->Name = fname;
  193. }
  194. void cmGeneratedFileStream::SetTempExt(std::string const& ext)
  195. {
  196. this->TempExt = ext;
  197. }