cmGeneratedFileStream.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGeneratedFileStream.h"
  11. #include "cmSystemTools.h"
  12. #if defined(CMAKE_BUILD_WITH_CMAKE)
  13. #include <cm_zlib.h>
  14. #endif
  15. cmGeneratedFileStream::cmGeneratedFileStream()
  16. : cmGeneratedFileStreamBase()
  17. , Stream()
  18. {
  19. }
  20. cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet)
  21. : cmGeneratedFileStreamBase(name)
  22. , Stream(TempName.c_str())
  23. {
  24. // Check if the file opened.
  25. if (!*this && !quiet) {
  26. cmSystemTools::Error("Cannot open file for write: ",
  27. this->TempName.c_str());
  28. cmSystemTools::ReportLastSystemError("");
  29. }
  30. }
  31. cmGeneratedFileStream::~cmGeneratedFileStream()
  32. {
  33. // This is the first destructor called. Check the status of the
  34. // stream and give the information to the private base. Next the
  35. // stream will be destroyed which will close the temporary file.
  36. // Finally the base destructor will be called to replace the
  37. // destination file.
  38. this->Okay = !this->fail();
  39. }
  40. cmGeneratedFileStream& cmGeneratedFileStream::Open(const char* name,
  41. bool quiet, bool binaryFlag)
  42. {
  43. // Store the file name and construct the temporary file name.
  44. this->cmGeneratedFileStreamBase::Open(name);
  45. // Open the temporary output file.
  46. if (binaryFlag) {
  47. this->Stream::open(this->TempName.c_str(),
  48. std::ios::out | std::ios::binary);
  49. } else {
  50. this->Stream::open(this->TempName.c_str(), std::ios::out);
  51. }
  52. // Check if the file opened.
  53. if (!*this && !quiet) {
  54. cmSystemTools::Error("Cannot open file for write: ",
  55. this->TempName.c_str());
  56. cmSystemTools::ReportLastSystemError("");
  57. }
  58. return *this;
  59. }
  60. bool cmGeneratedFileStream::Close()
  61. {
  62. // Save whether the temporary output file is valid before closing.
  63. this->Okay = !this->fail();
  64. // Close the temporary output file.
  65. this->Stream::close();
  66. // Remove the temporary file (possibly by renaming to the real file).
  67. return this->cmGeneratedFileStreamBase::Close();
  68. }
  69. void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
  70. {
  71. this->CopyIfDifferent = copy_if_different;
  72. }
  73. void cmGeneratedFileStream::SetCompression(bool compression)
  74. {
  75. this->Compress = compression;
  76. }
  77. void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
  78. {
  79. this->CompressExtraExtension = ext;
  80. }
  81. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase()
  82. : Name()
  83. , TempName()
  84. , CopyIfDifferent(false)
  85. , Okay(false)
  86. , Compress(false)
  87. , CompressExtraExtension(true)
  88. {
  89. }
  90. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name)
  91. : Name()
  92. , TempName()
  93. , CopyIfDifferent(false)
  94. , Okay(false)
  95. , Compress(false)
  96. , CompressExtraExtension(true)
  97. {
  98. this->Open(name);
  99. }
  100. cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
  101. {
  102. this->Close();
  103. }
  104. void cmGeneratedFileStreamBase::Open(const char* name)
  105. {
  106. // Save the original name of the file.
  107. this->Name = name;
  108. // Create the name of the temporary file.
  109. this->TempName = name;
  110. #if defined(__VMS)
  111. this->TempName += "_tmp";
  112. #else
  113. this->TempName += ".tmp";
  114. #endif
  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.c_str());
  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 = this->TempName + ".temp.gz";
  136. if (this->CompressFile(this->TempName.c_str(), gzname.c_str())) {
  137. this->RenameFile(gzname.c_str(), resname.c_str());
  138. }
  139. cmSystemTools::RemoveFile(gzname);
  140. } else {
  141. this->RenameFile(this->TempName.c_str(), resname.c_str());
  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. #ifdef CMAKE_BUILD_WITH_CMAKE
  152. int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
  153. const char* newname)
  154. {
  155. gzFile gf = gzopen(newname, "w");
  156. if (!gf) {
  157. return 0;
  158. }
  159. FILE* ifs = cmsys::SystemTools::Fopen(oldname, "r");
  160. if (!ifs) {
  161. return 0;
  162. }
  163. size_t res;
  164. const size_t BUFFER_SIZE = 1024;
  165. char buffer[BUFFER_SIZE];
  166. while ((res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0) {
  167. if (!gzwrite(gf, buffer, static_cast<int>(res))) {
  168. fclose(ifs);
  169. gzclose(gf);
  170. return 0;
  171. }
  172. }
  173. fclose(ifs);
  174. gzclose(gf);
  175. return 1;
  176. }
  177. #else
  178. int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
  179. {
  180. return 0;
  181. }
  182. #endif
  183. int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
  184. const char* newname)
  185. {
  186. return cmSystemTools::RenameFile(oldname, newname);
  187. }
  188. void cmGeneratedFileStream::SetName(const std::string& fname)
  189. {
  190. this->Name = fname;
  191. }