cmCPackTarBZip2Generator.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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::CompressFiles(const char* outFileName,
  57. const char* toplevel, const std::vector<std::string>& files)
  58. {
  59. std::string packageDirFileName
  60. = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  61. packageDirFileName += ".tar";
  62. std::string output;
  63. int retVal = -1;
  64. if ( !this->Superclass::CompressFiles(packageDirFileName.c_str(),
  65. toplevel, files) )
  66. {
  67. return 0;
  68. }
  69. cmOStringStream dmgCmd1;
  70. dmgCmd1 << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  71. << "\" \"" << packageDirFileName
  72. << "\"";
  73. retVal = -1;
  74. int res = cmSystemTools::RunSingleCommand(dmgCmd1.str().c_str(), &output,
  75. &retVal, toplevel, this->GeneratorVerbose, 0);
  76. if ( !res || retVal )
  77. {
  78. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  79. tmpFile += "/CompressBZip2.log";
  80. cmGeneratedFileStream ofs(tmpFile.c_str());
  81. ofs << "# Run command: " << dmgCmd1.str().c_str() << std::endl
  82. << "# Output:" << std::endl
  83. << output.c_str() << std::endl;
  84. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running BZip2 command: "
  85. << dmgCmd1.str().c_str() << std::endl
  86. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  87. return 0;
  88. }
  89. std::string compressOutFile = packageDirFileName + ".bz2";
  90. if ( !cmSystemTools::SameFile(compressOutFile.c_str(), outFileName ) )
  91. {
  92. if ( !this->RenameFile(compressOutFile.c_str(), outFileName) )
  93. {
  94. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem renaming: \""
  95. << compressOutFile.c_str() << "\" to \""
  96. << (outFileName ? outFileName : "(NULL)") << std::endl);
  97. return 0;
  98. }
  99. }
  100. return 1;
  101. }
  102. //----------------------------------------------------------------------------
  103. int cmCPackTarBZip2Generator::RenameFile(const char* oldname,
  104. const char* newname)
  105. {
  106. #ifdef _WIN32
  107. /* On Windows the move functions will not replace existing files.
  108. Check if the destination exists. */
  109. struct stat newFile;
  110. if(stat(newname, &newFile) == 0)
  111. {
  112. /* The destination exists. We have to replace it carefully. The
  113. MoveFileEx function does what we need but is not available on
  114. Win9x. */
  115. OSVERSIONINFO osv;
  116. DWORD attrs;
  117. /* Make sure the destination is not read only. */
  118. attrs = GetFileAttributes(newname);
  119. if(attrs & FILE_ATTRIBUTE_READONLY)
  120. {
  121. SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
  122. }
  123. /* Check the windows version number. */
  124. osv.dwOSVersionInfoSize = sizeof(osv);
  125. GetVersionEx(&osv);
  126. if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  127. {
  128. /* This is Win9x. There is no MoveFileEx implementation. We
  129. cannot quite rename the file atomically. Just delete the
  130. destination and then move the file. */
  131. DeleteFile(newname);
  132. return MoveFile(oldname, newname);
  133. }
  134. else
  135. {
  136. /* This is not Win9x. Use the MoveFileEx implementation. */
  137. return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING);
  138. }
  139. }
  140. else
  141. {
  142. /* The destination does not exist. Just move the file. */
  143. return MoveFile(oldname, newname);
  144. }
  145. #else
  146. /* On UNIX we have an OS-provided call to do this atomically. */
  147. return rename(oldname, newname) == 0;
  148. #endif
  149. }