cmCPackTarBZip2Generator.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "cmCPackTarBZip2Generator.h"
  14. #include "cmake.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmSystemTools.h"
  18. #include "cmMakefile.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include "cmCPackLog.h"
  21. #include <cmsys/SystemTools.hxx>
  22. // Includes needed for implementation of RenameFile. This is not in
  23. // system tools because it is not implemented robustly enough to move
  24. // files across directories.
  25. #ifdef _WIN32
  26. # include <windows.h>
  27. # include <sys/stat.h>
  28. #endif
  29. //----------------------------------------------------------------------
  30. cmCPackTarBZip2Generator::cmCPackTarBZip2Generator()
  31. {
  32. this->Compress = false;
  33. }
  34. //----------------------------------------------------------------------
  35. cmCPackTarBZip2Generator::~cmCPackTarBZip2Generator()
  36. {
  37. }
  38. //----------------------------------------------------------------------
  39. int cmCPackTarBZip2Generator::InitializeInternal()
  40. {
  41. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  42. std::vector<std::string> path;
  43. std::string pkgPath = cmSystemTools::FindProgram("bzip2", path, false);
  44. if ( pkgPath.empty() )
  45. {
  46. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find BZip2" << std::endl);
  47. return 0;
  48. }
  49. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  50. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Found Compress program: "
  51. << pkgPath.c_str()
  52. << std::endl);
  53. return this->Superclass::InitializeInternal();
  54. }
  55. //----------------------------------------------------------------------
  56. int cmCPackTarBZip2Generator::BZip2File(const char* packageDirFileName)
  57. {
  58. int retVal = 0;
  59. cmOStringStream dmgCmd1;
  60. dmgCmd1 << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  61. << "\" \"" << packageDirFileName
  62. << "\"";
  63. retVal = -1;
  64. std::string output;
  65. int res = cmSystemTools::RunSingleCommand(dmgCmd1.str().c_str(), &output,
  66. &retVal, 0, this->GeneratorVerbose, 0);
  67. if ( !res || retVal )
  68. {
  69. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  70. tmpFile += "/CompressBZip2.log";
  71. cmGeneratedFileStream ofs(tmpFile.c_str());
  72. ofs << "# Run command: " << dmgCmd1.str().c_str() << std::endl
  73. << "# Output:" << std::endl
  74. << output.c_str() << std::endl;
  75. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running BZip2 command: "
  76. << dmgCmd1.str().c_str() << std::endl
  77. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  78. return 0;
  79. }
  80. return 1;
  81. }
  82. //----------------------------------------------------------------------
  83. int cmCPackTarBZip2Generator::CompressFiles(const char* outFileName,
  84. const char* toplevel, const std::vector<std::string>& files)
  85. {
  86. std::string packageDirFileName
  87. = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  88. packageDirFileName += ".tar";
  89. std::string output;
  90. if ( !this->Superclass::CompressFiles(packageDirFileName.c_str(),
  91. toplevel, files) )
  92. {
  93. return 0;
  94. }
  95. if(!this->BZip2File(packageDirFileName.c_str()))
  96. {
  97. return 0;
  98. }
  99. std::string compressOutFile = packageDirFileName + ".bz2";
  100. if ( !cmSystemTools::SameFile(compressOutFile.c_str(), outFileName ) )
  101. {
  102. if ( !this->RenameFile(compressOutFile.c_str(), outFileName) )
  103. {
  104. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem renaming: \""
  105. << compressOutFile.c_str() << "\" to \""
  106. << (outFileName ? outFileName : "(NULL)") << std::endl);
  107. return 0;
  108. }
  109. }
  110. return 1;
  111. }
  112. //----------------------------------------------------------------------------
  113. int cmCPackTarBZip2Generator::RenameFile(const char* oldname,
  114. const char* newname)
  115. {
  116. #ifdef _WIN32
  117. /* On Windows the move functions will not replace existing files.
  118. Check if the destination exists. */
  119. struct stat newFile;
  120. if(stat(newname, &newFile) == 0)
  121. {
  122. /* The destination exists. We have to replace it carefully. The
  123. MoveFileEx function does what we need but is not available on
  124. Win9x. */
  125. OSVERSIONINFO osv;
  126. DWORD attrs;
  127. /* Make sure the destination is not read only. */
  128. attrs = GetFileAttributes(newname);
  129. if(attrs & FILE_ATTRIBUTE_READONLY)
  130. {
  131. SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
  132. }
  133. /* Check the windows version number. */
  134. osv.dwOSVersionInfoSize = sizeof(osv);
  135. GetVersionEx(&osv);
  136. if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  137. {
  138. /* This is Win9x. There is no MoveFileEx implementation. We
  139. cannot quite rename the file atomically. Just delete the
  140. destination and then move the file. */
  141. DeleteFile(newname);
  142. return MoveFile(oldname, newname);
  143. }
  144. else
  145. {
  146. /* This is not Win9x. Use the MoveFileEx implementation. */
  147. return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING);
  148. }
  149. }
  150. else
  151. {
  152. /* The destination does not exist. Just move the file. */
  153. return MoveFile(oldname, newname);
  154. }
  155. #else
  156. /* On UNIX we have an OS-provided call to do this atomically. */
  157. return rename(oldname, newname) == 0;
  158. #endif
  159. }