cmGeneratedFileStream.cxx 8.8 KB

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