cmCPackTarCompressGenerator.cxx 6.5 KB

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