cmCPackTarCompressGenerator.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCPackTarCompressGenerator.h"
  11. #include "cmake.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmSystemTools.h"
  15. #include "cmMakefile.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmCPackLog.h"
  18. #include <cmsys/SystemTools.hxx>
  19. #include <cmcompress/cmcompress.h>
  20. #include <libtar/libtar.h>
  21. #include <memory> // auto_ptr
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. //----------------------------------------------------------------------
  25. class cmCPackTarCompressGeneratorForward
  26. {
  27. public:
  28. static int GenerateHeader(cmCPackTarCompressGenerator* gg, std::ostream* os)
  29. {
  30. return gg->GenerateHeader(os);
  31. }
  32. };
  33. //----------------------------------------------------------------------
  34. cmCPackTarCompressGenerator::cmCPackTarCompressGenerator()
  35. {
  36. }
  37. //----------------------------------------------------------------------
  38. cmCPackTarCompressGenerator::~cmCPackTarCompressGenerator()
  39. {
  40. }
  41. //----------------------------------------------------------------------
  42. class cmCPackTarCompress_Data
  43. {
  44. public:
  45. cmCPackTarCompress_Data(cmCPackTarCompressGenerator* gen) :
  46. OutputStream(0), Generator(gen) {}
  47. std::ostream* OutputStream;
  48. cmCPackTarCompressGenerator* Generator;
  49. cmcompress_stream CMCompressStream;
  50. };
  51. //----------------------------------------------------------------------
  52. extern "C" {
  53. // For cmTar
  54. int cmCPackTarCompress_Data_Open(void *client_data, const char* name,
  55. int oflags, mode_t mode);
  56. ssize_t cmCPackTarCompress_Data_Write(void *client_data, void *buff,
  57. size_t n);
  58. int cmCPackTarCompress_Data_Close(void *client_data);
  59. // For cmCompress
  60. int cmCPackTarCompress_Compress_Output(void* cdata, const char* data,
  61. int len);
  62. }
  63. //----------------------------------------------------------------------
  64. int cmCPackTarCompress_Data_Open(void *client_data, const char* pathname,
  65. int, mode_t)
  66. {
  67. cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;
  68. if ( !cmcompress_compress_initialize(&mydata->CMCompressStream) )
  69. {
  70. return -1;
  71. }
  72. mydata->CMCompressStream.client_data = mydata;
  73. mydata->CMCompressStream.output_stream = cmCPackTarCompress_Compress_Output;
  74. cmGeneratedFileStream* gf = new cmGeneratedFileStream;
  75. // Open binary
  76. gf->Open(pathname, false, true);
  77. mydata->OutputStream = gf;
  78. if ( !*mydata->OutputStream )
  79. {
  80. return -1;
  81. }
  82. if ( !cmcompress_compress_start(&mydata->CMCompressStream) )
  83. {
  84. return -1;
  85. }
  86. if ( !cmCPackTarCompressGeneratorForward::GenerateHeader(
  87. mydata->Generator,gf))
  88. {
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. //----------------------------------------------------------------------
  94. ssize_t cmCPackTarCompress_Data_Write(void *client_data, void *buff, size_t n)
  95. {
  96. cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;
  97. if ( !cmcompress_compress(&mydata->CMCompressStream, buff, n) )
  98. {
  99. return 0;
  100. }
  101. return n;
  102. }
  103. //----------------------------------------------------------------------
  104. int cmCPackTarCompress_Data_Close(void *client_data)
  105. {
  106. cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;
  107. if ( !cmcompress_compress_finalize(&mydata->CMCompressStream) )
  108. {
  109. delete mydata->OutputStream;
  110. return -1;
  111. }
  112. delete mydata->OutputStream;
  113. mydata->OutputStream = 0;
  114. return (0);
  115. }
  116. //----------------------------------------------------------------------
  117. int cmCPackTarCompressGenerator::InitializeInternal()
  118. {
  119. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  120. return this->Superclass::InitializeInternal();
  121. }
  122. //----------------------------------------------------------------------
  123. int cmCPackTarCompressGenerator::CompressFiles(const char* outFileName,
  124. const char* toplevel, const std::vector<std::string>& files)
  125. {
  126. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
  127. << (toplevel ? toplevel : "(NULL)") << std::endl);
  128. cmCPackTarCompress_Data mydata(this);
  129. TAR *t;
  130. char buf[TAR_MAXPATHLEN];
  131. char pathname[TAR_MAXPATHLEN];
  132. tartype_t compressType = {
  133. (openfunc_t)cmCPackTarCompress_Data_Open,
  134. (closefunc_t)cmCPackTarCompress_Data_Close,
  135. (readfunc_t)0,
  136. (writefunc_t)cmCPackTarCompress_Data_Write,
  137. &mydata
  138. };
  139. // Ok, this libtar is not const safe. for now use auto_ptr hack
  140. char* realName = new char[ strlen(outFileName) + 1 ];
  141. std::auto_ptr<char> realNamePtr(realName);
  142. strcpy(realName, outFileName);
  143. int flags = O_WRONLY | O_CREAT;
  144. int options = 0;
  145. if(this->GeneratorVerbose)
  146. {
  147. options |= TAR_VERBOSE;
  148. }
  149. #ifdef __CYGWIN__
  150. options |= TAR_GNU;
  151. #endif
  152. if (tar_open(&t, realName,
  153. &compressType,
  154. flags, 0644,
  155. options) == -1)
  156. {
  157. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_open(): "
  158. << strerror(errno) << std::endl);
  159. return 0;
  160. }
  161. std::vector<std::string>::const_iterator fileIt;
  162. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  163. {
  164. std::string rp = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
  165. strncpy(pathname, fileIt->c_str(), sizeof(pathname));
  166. pathname[sizeof(pathname)-1] = 0;
  167. strncpy(buf, rp.c_str(), sizeof(buf));
  168. buf[sizeof(buf)-1] = 0;
  169. if (tar_append_tree(t, pathname, buf) != 0)
  170. {
  171. cmCPackLogger(cmCPackLog::LOG_ERROR,
  172. "Problem with tar_append_tree(\"" << buf << "\", \""
  173. << pathname << "\"): "
  174. << strerror(errno) << std::endl);
  175. tar_close(t);
  176. return 0;
  177. }
  178. }
  179. if (tar_append_eof(t) != 0)
  180. {
  181. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_append_eof(): "
  182. << strerror(errno) << std::endl);
  183. tar_close(t);
  184. return 0;
  185. }
  186. if (tar_close(t) != 0)
  187. {
  188. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_close(): "
  189. << strerror(errno) << std::endl);
  190. return 0;
  191. }
  192. return 1;
  193. }
  194. //----------------------------------------------------------------------
  195. int cmCPackTarCompress_Compress_Output(void* client_data,
  196. const char* data, int data_length)
  197. {
  198. if(!client_data)
  199. {
  200. return 0;
  201. }
  202. cmcompress_stream *cstream = static_cast<cmcompress_stream*>(client_data);
  203. cmCPackTarCompress_Data *mydata
  204. = static_cast<cmCPackTarCompress_Data*>(cstream->client_data);
  205. if ( !mydata->OutputStream )
  206. {
  207. return 0;
  208. }
  209. mydata->OutputStream->write(data, data_length);
  210. return data_length;
  211. }
  212. //----------------------------------------------------------------------
  213. int cmCPackTarCompressGenerator::GenerateHeader(std::ostream* os)
  214. {
  215. (void)os;
  216. return 1;
  217. }