cmGeneratedFileStream.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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(m_TempName.c_str())
  34. {
  35. // Check if the file opened.
  36. if(!*this && !quiet)
  37. {
  38. cmSystemTools::Error("Cannot open file for write: ", m_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. m_Okay = (*this)?true:false;
  51. }
  52. //----------------------------------------------------------------------------
  53. cmGeneratedFileStream&
  54. cmGeneratedFileStream::Open(const char* name, bool quiet)
  55. {
  56. // Store the file name and construct the temporary file name.
  57. this->cmGeneratedFileStreamBase::Open(name);
  58. // Open the temporary output file.
  59. this->Stream::open(m_TempName.c_str());
  60. // Check if the file opened.
  61. if(!*this && !quiet)
  62. {
  63. cmSystemTools::Error("Cannot open file for write: ", m_TempName.c_str());
  64. cmSystemTools::ReportLastSystemError("");
  65. }
  66. return *this;
  67. }
  68. //----------------------------------------------------------------------------
  69. cmGeneratedFileStream&
  70. cmGeneratedFileStream::Close()
  71. {
  72. // Save whether the temporary output file is valid before closing.
  73. m_Okay = (*this)?true:false;
  74. // Close the temporary output file.
  75. this->Stream::close();
  76. // Remove the temporary file (possibly by renaming to the real file).
  77. this->cmGeneratedFileStreamBase::Close();
  78. return *this;
  79. }
  80. //----------------------------------------------------------------------------
  81. void cmGeneratedFileStream::SetCopyIfDifferent(bool copy_if_different)
  82. {
  83. m_CopyIfDifferent = copy_if_different;
  84. }
  85. //----------------------------------------------------------------------------
  86. void cmGeneratedFileStream::SetCompression(bool compression)
  87. {
  88. m_Compress = compression;
  89. }
  90. //----------------------------------------------------------------------------
  91. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase():
  92. m_Name(),
  93. m_TempName(),
  94. m_CopyIfDifferent(false),
  95. m_Okay(false),
  96. m_Compress(false)
  97. {
  98. }
  99. //----------------------------------------------------------------------------
  100. cmGeneratedFileStreamBase::cmGeneratedFileStreamBase(const char* name):
  101. m_Name(),
  102. m_TempName(),
  103. m_CopyIfDifferent(false),
  104. m_Okay(false),
  105. m_Compress(false)
  106. {
  107. this->Open(name);
  108. }
  109. //----------------------------------------------------------------------------
  110. cmGeneratedFileStreamBase::~cmGeneratedFileStreamBase()
  111. {
  112. this->Close();
  113. }
  114. //----------------------------------------------------------------------------
  115. void cmGeneratedFileStreamBase::Open(const char* name)
  116. {
  117. // Save the original name of the file.
  118. m_Name = name;
  119. // Create the name of the temporary file.
  120. m_TempName = name;
  121. m_TempName += ".tmp";
  122. // Make sure the temporary file that will be used is not present.
  123. cmSystemTools::RemoveFile(m_TempName.c_str());
  124. std::string dir = cmSystemTools::GetFilenamePath(m_TempName);
  125. cmSystemTools::MakeDirectory(dir.c_str());
  126. }
  127. //----------------------------------------------------------------------------
  128. void cmGeneratedFileStreamBase::Close()
  129. {
  130. std::string resname = m_Name;
  131. if ( m_Compress )
  132. {
  133. resname += ".gz";
  134. }
  135. // Only consider replacing the destination file if no error
  136. // occurred.
  137. if(m_Okay &&
  138. (!m_CopyIfDifferent ||
  139. cmSystemTools::FilesDiffer(m_TempName.c_str(), resname.c_str())))
  140. {
  141. // The destination is to be replaced. Rename the temporary to the
  142. // destination atomically.
  143. if ( m_Compress )
  144. {
  145. std::string gzname = m_TempName + ".temp.gz";
  146. if ( this->CompressFile(m_TempName.c_str(), gzname.c_str()) )
  147. {
  148. this->RenameFile(gzname.c_str(), resname.c_str());
  149. }
  150. cmSystemTools::RemoveFile(gzname.c_str());
  151. }
  152. else
  153. {
  154. this->RenameFile(m_TempName.c_str(), resname.c_str());
  155. }
  156. }
  157. // Else, the destination was not replaced.
  158. //
  159. // Always delete the temporary file. We never want it to stay around.
  160. cmSystemTools::RemoveFile(m_TempName.c_str());
  161. }
  162. //----------------------------------------------------------------------------
  163. #ifdef CMAKE_BUILD_WITH_CMAKE
  164. int cmGeneratedFileStreamBase::CompressFile(const char* oldname,
  165. const char* newname)
  166. {
  167. gzFile gf = cm_zlib_gzopen(newname, "w");
  168. if ( !gf )
  169. {
  170. return 0;
  171. }
  172. FILE* ifs = fopen(oldname, "r");
  173. if ( !ifs )
  174. {
  175. return 0;
  176. }
  177. size_t res;
  178. const size_t BUFFER_SIZE = 1024;
  179. char buffer[BUFFER_SIZE];
  180. while ( (res = fread(buffer, 1, BUFFER_SIZE, ifs)) > 0 )
  181. {
  182. if ( !cm_zlib_gzwrite(gf, buffer, res) )
  183. {
  184. fclose(ifs);
  185. cm_zlib_gzclose(gf);
  186. return 0;
  187. }
  188. }
  189. fclose(ifs);
  190. cm_zlib_gzclose(gf);
  191. return 1;
  192. }
  193. #else
  194. int cmGeneratedFileStreamBase::CompressFile(const char*, const char*)
  195. {
  196. return 0;
  197. }
  198. #endif
  199. //----------------------------------------------------------------------------
  200. int cmGeneratedFileStreamBase::RenameFile(const char* oldname,
  201. const char* newname)
  202. {
  203. #ifdef _WIN32
  204. /* On Windows the move functions will not replace existing files.
  205. Check if the destination exists. */
  206. struct stat newFile;
  207. if(stat(newname, &newFile) == 0)
  208. {
  209. /* The destination exists. We have to replace it carefully. The
  210. MoveFileEx function does what we need but is not available on
  211. Win9x. */
  212. OSVERSIONINFO osv;
  213. DWORD attrs;
  214. /* Make sure the destination is not read only. */
  215. attrs = GetFileAttributes(newname);
  216. if(attrs & FILE_ATTRIBUTE_READONLY)
  217. {
  218. SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
  219. }
  220. /* Check the windows version number. */
  221. osv.dwOSVersionInfoSize = sizeof(osv);
  222. GetVersionEx(&osv);
  223. if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  224. {
  225. /* This is Win9x. There is no MoveFileEx implementation. We
  226. cannot quite rename the file atomically. Just delete the
  227. destination and then move the file. */
  228. DeleteFile(newname);
  229. return MoveFile(oldname, newname);
  230. }
  231. else
  232. {
  233. /* This is not Win9x. Use the MoveFileEx implementation. */
  234. return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING);
  235. }
  236. }
  237. else
  238. {
  239. /* The destination does not exist. Just move the file. */
  240. return MoveFile(oldname, newname);
  241. }
  242. #else
  243. /* On UNIX we have an OS-provided call to do this atomically. */
  244. return rename(oldname, newname) == 0;
  245. #endif
  246. }