cmGeneratedFileStream.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. // Includes needed for implementation of RenameFile. This is not in
  16. // system tools because it is not implemented robustly enough to move
  17. // files across directories.
  18. #ifdef _WIN32
  19. # include <windows.h>
  20. # include <sys/stat.h>
  21. #endif
  22. #if defined(CMAKE_BUILD_WITH_CMAKE)
  23. # include <cmzlib/zlib.h>
  24. #endif
  25. //----------------------------------------------------------------------------
  26. cmGeneratedFileStream::cmGeneratedFileStream():
  27. cmGeneratedFileStreamBase(), Stream()
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. cmGeneratedFileStream::cmGeneratedFileStream(const char* name, bool quiet):
  32. cmGeneratedFileStreamBase(name),
  33. Stream(TempName.c_str())
  34. {
  35. // Check if the file opened.
  36. if(!*this && !quiet)
  37. {
  38. cmSystemTools::Error("Cannot open file for write: ", this->TempName.c_str());
  39. cmSystemTools::ReportLastSystemError("");
  40. }
  41. }
  42. //----------------------------------------------------------------------------
  43. cmGeneratedFileStream::~cmGeneratedFileStream()
  44. {
  45. // This is the first destructor called. Check the status of the
  46. // stream and give the information to the private base. Next the
  47. // stream will be destroyed which will close the temporary file.
  48. // Finally the base destructor will be called to replace the
  49. // destination file.
  50. this->Okay = (*this)?true:false;
  51. }
  52. //----------------------------------------------------------------------------
  53. cmGeneratedFileStream&
  54. cmGeneratedFileStream::Open(const char* name, bool quiet, bool binaryFlag)
  55. {
  56. // Store the file name and construct the temporary file name.
  57. this->cmGeneratedFileStreamBase::Open(name);
  58. // Open the temporary output file.
  59. if ( binaryFlag )
  60. {
  61. this->Stream::open(this->TempName.c_str(), std::ios::out | std::ios::binary);
  62. }
  63. else
  64. {
  65. this->Stream::open(this->TempName.c_str(), std::ios::out);
  66. }
  67. // Check if the file opened.
  68. if(!*this && !quiet)
  69. {
  70. cmSystemTools::Error("Cannot open file for write: ", this->TempName.c_str());
  71. cmSystemTools::ReportLastSystemError("");
  72. }
  73. return *this;
  74. }
  75. //----------------------------------------------------------------------------
  76. cmGeneratedFileStream&
  77. cmGeneratedFileStream::Close()
  78. {
  79. // Save whether the temporary output file is valid before closing.
  80. this->Okay = (*this)?true:false;
  81. // Close the temporary output file.
  82. this->Stream::close();
  83. // Remove the temporary file (possibly by renaming to the real file).
  84. this->cmGeneratedFileStreamBase::Close();
  85. return *this;
  86. }
  87. //----------------------------------------------------------------------------
  88. void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
  89. {
  90. this->CopyIfDifferent = copy_if_different;
  91. }
  92. //----------------------------------------------------------------------------
  93. void cmGeneratedFileStream::SetCompression(bool compression)
  94. {
  95. this->Compress = compression;
  96. }
  97. //----------------------------------------------------------------------------
  98. void cmGeneratedFileStream::SetCompressionExtraExtension(bool ext)
  99. {
  100. this->CompressExtraExtension = ext;
  101. }
  102. //----------------------------------------------------------------------------
  103. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
  104. Name(),
  105. TempName(),
  106. CopyIfDifferent(false),
  107. Okay(false),
  108. Compress(false),
  109. CompressExtraExtension(true)
  110. {
  111. }
  112. //----------------------------------------------------------------------------
  113. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
  114. Name(),
  115. TempName(),
  116. CopyIfDifferent(false),
  117. Okay(false),
  118. Compress(false),
  119. CompressExtraExtension(true)
  120. {
  121. this->Open(name);
  122. }
  123. //----------------------------------------------------------------------------
  124. cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
  125. {
  126. this->Close();
  127. }
  128. //----------------------------------------------------------------------------
  129. void cmGeneratedFileStreamBase::Open(const char* name)
  130. {
  131. // Save the original name of the file.
  132. this->Name = name;
  133. // Create the name of the temporary file.
  134. this->TempName = name;
  135. this->TempName += ".tmp";
  136. // Make sure the temporary file that will be used is not present.
  137. cmSystemTools::RemoveFile(this->TempName.c_str());
  138. std::string dir = cmSystemTools::GetFilenamePath(this->TempName);
  139. cmSystemTools::MakeDirectory(dir.c_str());
  140. }
  141. //----------------------------------------------------------------------------
  142. void cmGeneratedFileStreamBase::Close()
  143. {
  144. std::string resname = this->Name;
  145. if ( this->Compress && this->CompressExtraExtension )
  146. {
  147. resname += ".gz";
  148. }
  149. // Only consider replacing the destination file if no error
  150. // occurred.
  151. if(!this->Name.empty() &&
  152. this->Okay &&
  153. (!this->CopyIfDifferent ||
  154. cmSystemTools::FilesDiffer(this->TempName.c_str(), resname.c_str())))
  155. {
  156. // The destination is to be replaced. Rename the temporary to the
  157. // destination atomically.
  158. if ( this->Compress )
  159. {
  160. std::string gzname = this->TempName + ".temp.gz";
  161. if ( this->CompressFile(this->TempName.c_str(), gzname.c_str()) )
  162. {
  163. this->RenameFile(gzname.c_str(), resname.c_str());
  164. }
  165. cmSystemTools::RemoveFile(gzname.c_str());
  166. }
  167. else
  168. {
  169. this->RenameFile(this->TempName.c_str(), resname.c_str());
  170. }
  171. }
  172. // Else, the destination was not replaced.
  173. //
  174. // Always delete the temporary file. We never want it to stay around.
  175. cmSystemTools::RemoveFile(this->TempName.c_str());
  176. }
  177. //----------------------------------------------------------------------------
  178. #ifdef CMAKE_BUILD_WITH_CMAKE
  179. int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
  180. const char* newname)
  181. {
  182. gzFile gf = cm_zlib_gzopen(newname, "w");
  183. if ( !gf )
  184. {
  185. return 0;
  186. }
  187. FILE* ifs = fopen(oldname, "r");
  188. if ( !ifs )
  189. {
  190. return 0;
  191. }
  192. size_t res;
  193. const size_t BUFFER_SIZE = 1024;
  194. char buffer[BUFFER_SIZE];
  195. while ( (res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0 )
  196. {
  197. if ( !cm_zlib_gzwrite(gf, buffer, res) )
  198. {
  199. fclose(ifs);
  200. cm_zlib_gzclose(gf);
  201. return 0;
  202. }
  203. }
  204. fclose(ifs);
  205. cm_zlib_gzclose(gf);
  206. return 1;
  207. }
  208. #else
  209. int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
  210. {
  211. return 0;
  212. }
  213. #endif
  214. //----------------------------------------------------------------------------
  215. int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
  216. const char* newname)
  217. {
  218. #ifdef _WIN32
  219. /* On Windows the move functions will not replace existing files.
  220. Check if the destination exists. */
  221. struct stat newFile;
  222. if(stat(newname, &newFile) == 0)
  223. {
  224. /* The destination exists. We have to replace it carefully. The
  225. MoveFileEx function does what we need but is not available on
  226. Win9x. */
  227. OSVERSIONINFO osv;
  228. DWORD attrs;
  229. /* Make sure the destination is not read only. */
  230. attrs = GetFileAttributes(newname);
  231. if(attrs & FILE_ATTRIBUTE_READONLY)
  232. {
  233. SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
  234. }
  235. /* Check the windows version number. */
  236. osv.dwOSVersionInfoSize = sizeof(osv);
  237. GetVersionEx(&osv);
  238. if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  239. {
  240. /* This is Win9x. There is no MoveFileEx implementation. We
  241. cannot quite rename the file atomically. Just delete the
  242. destination and then move the file. */
  243. DeleteFile(newname);
  244. return MoveFile(oldname, newname);
  245. }
  246. else
  247. {
  248. /* This is not Win9x. Use the MoveFileEx implementation. */
  249. return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING);
  250. }
  251. }
  252. else
  253. {
  254. /* The destination does not exist. Just move the file. */
  255. return MoveFile(oldname, newname);
  256. }
  257. #else
  258. /* On UNIX we have an OS-provided call to do this atomically. */
  259. return rename(oldname, newname) == 0;
  260. #endif
  261. }
  262. //----------------------------------------------------------------------------
  263. void cmGeneratedFileStream::SetName(const char* fname)
  264. {
  265. if ( !fname )
  266. {
  267. this->Name = "";
  268. return;
  269. }
  270. this->Name = fname;
  271. }