cmGeneratedFileStream.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmSystemTools.h"
  15. #if defined(CMAKE_BUILD_WITH_CMAKE)
  16. # include <cm_zlib.h>
  17. #endif
  18. //----------------------------------------------------------------------------
  19. cmGeneratedFileStream::cmGeneratedFileStream():
  20. cmGeneratedFileStreamBase(), Stream()
  21. {
  22. }
  23. //----------------------------------------------------------------------------
  24. cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet):
  25. cmGeneratedFileStreamBase(name),
  26. Stream(TempName.c_str())
  27. {
  28. // Check if the file opened.
  29. if(!*this && !quiet)
  30. {
  31. cmSystemTools::Error("Cannot open file for write: ",
  32. this->TempName.c_str());
  33. cmSystemTools::ReportLastSystemError("");
  34. }
  35. }
  36. //----------------------------------------------------------------------------
  37. cmGeneratedFileStream::~cmGeneratedFileStream()
  38. {
  39. // This is the first destructor called. Check the status of the
  40. // stream and give the information to the private base. Next the
  41. // stream will be destroyed which will close the temporary file.
  42. // Finally the base destructor will be called to replace the
  43. // destination file.
  44. this->Okay = (*this)?true:false;
  45. }
  46. //----------------------------------------------------------------------------
  47. cmGeneratedFileStream&
  48. cmGeneratedFileStream::Open(const char* name, bool quiet, bool binaryFlag)
  49. {
  50. // Store the file name and construct the temporary file name.
  51. this->cmGeneratedFileStreamBase::Open(name);
  52. // Open the temporary output file.
  53. if ( binaryFlag )
  54. {
  55. this->Stream::open(this->TempName.c_str(),
  56. std::ios::out | std::ios::binary);
  57. }
  58. else
  59. {
  60. this->Stream::open(this->TempName.c_str(), std::ios::out);
  61. }
  62. // Check if the file opened.
  63. if(!*this && !quiet)
  64. {
  65. cmSystemTools::Error("Cannot open file for write: ",
  66. this->TempName.c_str());
  67. cmSystemTools::ReportLastSystemError("");
  68. }
  69. return *this;
  70. }
  71. //----------------------------------------------------------------------------
  72. bool
  73. cmGeneratedFileStream::Close()
  74. {
  75. // Save whether the temporary output file is valid before closing.
  76. this->Okay = (*this)?true:false;
  77. // Close the temporary output file.
  78. this->Stream::close();
  79. // Remove the temporary file (possibly by renaming to the real file).
  80. return this->cmGeneratedFileStreamBase::Close();
  81. }
  82. //----------------------------------------------------------------------------
  83. void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
  84. {
  85. this->CopyIfDifferent = copy_if_different;
  86. }
  87. //----------------------------------------------------------------------------
  88. void cmGeneratedFileStream::SetCompression(bool compression)
  89. {
  90. this->Compress = compression;
  91. }
  92. //----------------------------------------------------------------------------
  93. void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
  94. {
  95. this->CompressExtraExtension = ext;
  96. }
  97. //----------------------------------------------------------------------------
  98. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
  99. Name(),
  100. TempName(),
  101. CopyIfDifferent(false),
  102. Okay(false),
  103. Compress(false),
  104. CompressExtraExtension(true)
  105. {
  106. }
  107. //----------------------------------------------------------------------------
  108. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
  109. Name(),
  110. TempName(),
  111. CopyIfDifferent(false),
  112. Okay(false),
  113. Compress(false),
  114. CompressExtraExtension(true)
  115. {
  116. this->Open(name);
  117. }
  118. //----------------------------------------------------------------------------
  119. cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
  120. {
  121. this->Close();
  122. }
  123. //----------------------------------------------------------------------------
  124. void cmGeneratedFileStreamBase::Open(const char* name)
  125. {
  126. // Save the original name of the file.
  127. this->Name = name;
  128. // Create the name of the temporary file.
  129. this->TempName = name;
  130. this->TempName += ".tmp";
  131. // Make sure the temporary file that will be used is not present.
  132. cmSystemTools::RemoveFile(this->TempName.c_str());
  133. std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
  134. cmSystemTools::MakeDirectory(dir.c_str());
  135. }
  136. //----------------------------------------------------------------------------
  137. bool cmGeneratedFileStreamBase::Close()
  138. {
  139. bool replaced = false;
  140. std::string resname = this->Name;
  141. if ( this->Compress && this->CompressExtraExtension )
  142. {
  143. resname += ".gz";
  144. }
  145. // Only consider replacing the destination file if no error
  146. // occurred.
  147. if(!this->Name.empty() &&
  148. this->Okay &&
  149. (!this->CopyIfDifferent ||
  150. cmSystemTools::FilesDiffer(this->TempName.c_str(), resname.c_str())))
  151. {
  152. // The destination is to be replaced. Rename the temporary to the
  153. // destination atomically.
  154. if ( this->Compress )
  155. {
  156. std::string gzname = this->TempName + ".temp.gz";
  157. if ( this->CompressFile(this->TempName.c_str(), gzname.c_str()) )
  158. {
  159. this->RenameFile(gzname.c_str(), resname.c_str());
  160. }
  161. cmSystemTools::RemoveFile(gzname.c_str());
  162. }
  163. else
  164. {
  165. this->RenameFile(this->TempName.c_str(), resname.c_str());
  166. }
  167. replaced = true;
  168. }
  169. // Else, the destination was not replaced.
  170. //
  171. // Always delete the temporary file. We never want it to stay around.
  172. cmSystemTools::RemoveFile(this->TempName.c_str());
  173. return replaced;
  174. }
  175. //----------------------------------------------------------------------------
  176. #ifdef CMAKE_BUILD_WITH_CMAKE
  177. int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
  178. const char* newname)
  179. {
  180. gzFile gf = gzopen(newname, "w");
  181. if ( !gf )
  182. {
  183. return 0;
  184. }
  185. FILE* ifs = fopen(oldname, "r");
  186. if ( !ifs )
  187. {
  188. return 0;
  189. }
  190. size_t res;
  191. const size_t BUFFER_SIZE = 1024;
  192. char buffer[BUFFER_SIZE];
  193. while ( (res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0 )
  194. {
  195. if ( !gzwrite(gf, buffer, static_cast<int>(res)) )
  196. {
  197. fclose(ifs);
  198. gzclose(gf);
  199. return 0;
  200. }
  201. }
  202. fclose(ifs);
  203. gzclose(gf);
  204. return 1;
  205. }
  206. #else
  207. int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
  208. {
  209. return 0;
  210. }
  211. #endif
  212. //----------------------------------------------------------------------------
  213. int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
  214. const char* newname)
  215. {
  216. return cmSystemTools::RenameFile(oldname, newname);
  217. }
  218. //----------------------------------------------------------------------------
  219. void cmGeneratedFileStream::SetName(const char* fname)
  220. {
  221. if ( !fname )
  222. {
  223. this->Name = "";
  224. return;
  225. }
  226. this->Name = fname;
  227. }