cmGeneratedFileStream.cxx 5.8 KB

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