cmGeneratedFileStream.cxx 8.5 KB

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