cmGeneratedFileStream.cxx 7.5 KB

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