cmCPackDebGenerator.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 "cmCPackDebGenerator.h"
  11. #include "cmArchiveWrite.h"
  12. #include "cmCPackLog.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmMakefile.h"
  15. #include "cmSystemTools.h"
  16. #include <cmsys/Glob.hxx>
  17. #include <cmsys/SystemTools.hxx>
  18. #include <limits.h> // USHRT_MAX
  19. #include <sys/stat.h>
  20. // NOTE:
  21. // A debian package .deb is simply an 'ar' archive. The only subtle difference
  22. // is that debian uses the BSD ar style archive whereas most Linux distro have
  23. // a GNU ar.
  24. // See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=161593 for more info
  25. // Therefore we provide our own implementation of a BSD-ar:
  26. static int ar_append(const char*archive,const std::vector<std::string>& files);
  27. cmCPackDebGenerator::cmCPackDebGenerator()
  28. {
  29. }
  30. cmCPackDebGenerator::~cmCPackDebGenerator()
  31. {
  32. }
  33. int cmCPackDebGenerator::InitializeInternal()
  34. {
  35. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  36. if (cmSystemTools::IsOff(this->GetOption("CPACK_SET_DESTDIR")))
  37. {
  38. this->SetOption("CPACK_SET_DESTDIR", "I_ON");
  39. }
  40. return this->Superclass::InitializeInternal();
  41. }
  42. int cmCPackDebGenerator::PackageOnePack(std::string initialTopLevel,
  43. std::string packageName)
  44. {
  45. int retval = 1;
  46. // Begin the archive for this pack
  47. std::string localToplevel(initialTopLevel);
  48. std::string packageFileName(
  49. cmSystemTools::GetParentDirectory(toplevel)
  50. );
  51. std::string outputFileName(
  52. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  53. +"-"+packageName + this->GetOutputExtension()
  54. );
  55. localToplevel += "/"+ packageName;
  56. /* replace the TEMP DIRECTORY with the component one */
  57. this->SetOption("CPACK_TEMPORARY_DIRECTORY",localToplevel.c_str());
  58. packageFileName += "/"+ outputFileName;
  59. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  60. this->SetOption("CPACK_OUTPUT_FILE_NAME",outputFileName.c_str());
  61. /* replace the TEMPORARY package file name */
  62. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  63. packageFileName.c_str());
  64. // Tell CPackDeb.cmake the name of the component GROUP.
  65. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT",packageName.c_str());
  66. // Tell CPackDeb.cmake the path where the component is.
  67. std::string component_path = "/";
  68. component_path += packageName;
  69. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  70. component_path.c_str());
  71. if (!this->ReadListFile("CPackDeb.cmake"))
  72. {
  73. cmCPackLogger(cmCPackLog::LOG_ERROR,
  74. "Error while execution CPackDeb.cmake" << std::endl);
  75. retval = 0;
  76. return retval;
  77. }
  78. cmsys::Glob gl;
  79. std::string findExpr(this->GetOption("GEN_WDIR"));
  80. findExpr += "/*";
  81. gl.RecurseOn();
  82. gl.SetRecurseListDirs(true);
  83. if ( !gl.FindFiles(findExpr) )
  84. {
  85. cmCPackLogger(cmCPackLog::LOG_ERROR,
  86. "Cannot find any files in the installed directory" << std::endl);
  87. return 0;
  88. }
  89. packageFiles = gl.GetFiles();
  90. int res = createDeb();
  91. if (res != 1)
  92. {
  93. retval = 0;
  94. }
  95. // add the generated package to package file names list
  96. packageFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  97. packageFileName += "/";
  98. packageFileName += this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  99. packageFileNames.push_back(packageFileName);
  100. return retval;
  101. }
  102. int cmCPackDebGenerator::PackageComponents(bool ignoreGroup)
  103. {
  104. int retval = 1;
  105. /* Reset package file name list it will be populated during the
  106. * component packaging run*/
  107. packageFileNames.clear();
  108. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  109. // The default behavior is to have one package by component group
  110. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  111. if (!ignoreGroup)
  112. {
  113. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  114. for (compGIt=this->ComponentGroups.begin();
  115. compGIt!=this->ComponentGroups.end(); ++compGIt)
  116. {
  117. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  118. << compGIt->first
  119. << std::endl);
  120. // Begin the archive for this group
  121. retval &= PackageOnePack(initialTopLevel,compGIt->first);
  122. }
  123. // Handle Orphan components (components not belonging to any groups)
  124. std::map<std::string, cmCPackComponent>::iterator compIt;
  125. for (compIt=this->Components.begin();
  126. compIt!=this->Components.end(); ++compIt )
  127. {
  128. // Does the component belong to a group?
  129. if (compIt->second.Group==NULL)
  130. {
  131. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  132. "Component <"
  133. << compIt->second.Name
  134. << "> does not belong to any group, package it separately."
  135. << std::endl);
  136. // Begin the archive for this orphan component
  137. retval &= PackageOnePack(initialTopLevel,compIt->first);
  138. }
  139. }
  140. }
  141. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  142. // We build 1 package per component
  143. else
  144. {
  145. std::map<std::string, cmCPackComponent>::iterator compIt;
  146. for (compIt=this->Components.begin();
  147. compIt!=this->Components.end(); ++compIt )
  148. {
  149. retval &= PackageOnePack(initialTopLevel,compIt->first);
  150. }
  151. }
  152. return retval;
  153. }
  154. int cmCPackDebGenerator::PackageComponentsAllInOne()
  155. {
  156. int retval = 1;
  157. std::string compInstDirName;
  158. /* Reset package file name list it will be populated during the
  159. * component packaging run*/
  160. packageFileNames.clear();
  161. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  162. compInstDirName = "ALL_COMPONENTS_IN_ONE";
  163. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  164. "Packaging all groups in one package..."
  165. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  166. << std::endl);
  167. // The ALL GROUPS in ONE package case
  168. std::string localToplevel(initialTopLevel);
  169. std::string packageFileName(
  170. cmSystemTools::GetParentDirectory(toplevel)
  171. );
  172. std::string outputFileName(
  173. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  174. + this->GetOutputExtension()
  175. );
  176. // all GROUP in one vs all COMPONENT in one
  177. localToplevel += "/"+compInstDirName;
  178. /* replace the TEMP DIRECTORY with the component one */
  179. this->SetOption("CPACK_TEMPORARY_DIRECTORY",localToplevel.c_str());
  180. packageFileName += "/"+ outputFileName;
  181. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  182. this->SetOption("CPACK_OUTPUT_FILE_NAME",outputFileName.c_str());
  183. /* replace the TEMPORARY package file name */
  184. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  185. packageFileName.c_str());
  186. // Tell CPackDeb.cmake the path where the component is.
  187. std::string component_path = "/";
  188. component_path += compInstDirName;
  189. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  190. component_path.c_str());
  191. if (!this->ReadListFile("CPackDeb.cmake"))
  192. {
  193. cmCPackLogger(cmCPackLog::LOG_ERROR,
  194. "Error while execution CPackDeb.cmake" << std::endl);
  195. retval = 0;
  196. return retval;
  197. }
  198. cmsys::Glob gl;
  199. std::string findExpr(this->GetOption("GEN_WDIR"));
  200. findExpr += "/*";
  201. gl.RecurseOn();
  202. gl.SetRecurseListDirs(true);
  203. if ( !gl.FindFiles(findExpr) )
  204. {
  205. cmCPackLogger(cmCPackLog::LOG_ERROR,
  206. "Cannot find any files in the installed directory" << std::endl);
  207. return 0;
  208. }
  209. packageFiles = gl.GetFiles();
  210. int res = createDeb();
  211. if (res != 1)
  212. {
  213. retval = 0;
  214. }
  215. // add the generated package to package file names list
  216. packageFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  217. packageFileName += "/";
  218. packageFileName += this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  219. packageFileNames.push_back(packageFileName);
  220. return retval;
  221. }
  222. int cmCPackDebGenerator::PackageFiles()
  223. {
  224. int retval = -1;
  225. /* Are we in the component packaging case */
  226. if (WantsComponentInstallation()) {
  227. // CASE 1 : COMPONENT ALL-IN-ONE package
  228. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  229. // then the package file is unique and should be open here.
  230. if (componentPackageMethod == ONE_PACKAGE)
  231. {
  232. return PackageComponentsAllInOne();
  233. }
  234. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  235. // There will be 1 package for each component group
  236. // however one may require to ignore component group and
  237. // in this case you'll get 1 package for each component.
  238. else
  239. {
  240. return PackageComponents(componentPackageMethod ==
  241. ONE_PACKAGE_PER_COMPONENT);
  242. }
  243. }
  244. // CASE 3 : NON COMPONENT package.
  245. else
  246. {
  247. if (!this->ReadListFile("CPackDeb.cmake"))
  248. {
  249. cmCPackLogger(cmCPackLog::LOG_ERROR,
  250. "Error while execution CPackDeb.cmake" << std::endl);
  251. retval = 0;
  252. }
  253. else
  254. {
  255. packageFiles = files;
  256. return createDeb();
  257. }
  258. }
  259. return retval;
  260. }
  261. int cmCPackDebGenerator::createDeb()
  262. {
  263. // debian-binary file
  264. const std::string strGenWDIR(this->GetOption("GEN_WDIR"));
  265. const std::string dbfilename = strGenWDIR + "/debian-binary";
  266. { // the scope is needed for cmGeneratedFileStream
  267. cmGeneratedFileStream out(dbfilename.c_str());
  268. out << "2.0";
  269. out << std::endl; // required for valid debian package
  270. }
  271. // control file
  272. std::string ctlfilename = strGenWDIR + "/control";
  273. // debian policy enforce lower case for package name
  274. // mandatory entries:
  275. std::string debian_pkg_name = cmsys::SystemTools::LowerCase(
  276. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME") );
  277. const char* debian_pkg_version =
  278. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_VERSION");
  279. const char* debian_pkg_section =
  280. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SECTION");
  281. const char* debian_pkg_priority =
  282. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PRIORITY");
  283. const char* debian_pkg_arch =
  284. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  285. const char* maintainer =
  286. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MAINTAINER");
  287. const char* desc =
  288. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  289. // optional entries
  290. const char* debian_pkg_dep =
  291. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DEPENDS");
  292. const char* debian_pkg_rec =
  293. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  294. const char* debian_pkg_sug =
  295. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SUGGESTS");
  296. const char* debian_pkg_url =
  297. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  298. const char* debian_pkg_predep =
  299. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PREDEPENDS");
  300. const char* debian_pkg_enhances =
  301. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ENHANCES");
  302. const char* debian_pkg_breaks =
  303. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_BREAKS");
  304. const char* debian_pkg_conflicts =
  305. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONFLICTS");
  306. const char* debian_pkg_provides =
  307. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PROVIDES");
  308. const char* debian_pkg_replaces =
  309. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_REPLACES");
  310. const char* debian_pkg_source =
  311. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SOURCE");
  312. { // the scope is needed for cmGeneratedFileStream
  313. cmGeneratedFileStream out(ctlfilename.c_str());
  314. out << "Package: " << debian_pkg_name << "\n";
  315. out << "Version: " << debian_pkg_version << "\n";
  316. out << "Section: " << debian_pkg_section << "\n";
  317. out << "Priority: " << debian_pkg_priority << "\n";
  318. out << "Architecture: " << debian_pkg_arch << "\n";
  319. if(debian_pkg_source && *debian_pkg_source)
  320. {
  321. out << "Source: " << debian_pkg_source << "\n";
  322. }
  323. if(debian_pkg_dep && *debian_pkg_dep)
  324. {
  325. out << "Depends: " << debian_pkg_dep << "\n";
  326. }
  327. if(debian_pkg_rec && *debian_pkg_rec)
  328. {
  329. out << "Recommends: " << debian_pkg_rec << "\n";
  330. }
  331. if(debian_pkg_sug && *debian_pkg_sug)
  332. {
  333. out << "Suggests: " << debian_pkg_sug << "\n";
  334. }
  335. if(debian_pkg_url && *debian_pkg_url)
  336. {
  337. out << "Homepage: " << debian_pkg_url << "\n";
  338. }
  339. if (debian_pkg_predep && *debian_pkg_predep)
  340. {
  341. out << "Pre-Depends: " << debian_pkg_predep << "\n";
  342. }
  343. if (debian_pkg_enhances && *debian_pkg_enhances)
  344. {
  345. out << "Enhances: " << debian_pkg_enhances << "\n";
  346. }
  347. if (debian_pkg_breaks && *debian_pkg_breaks)
  348. {
  349. out << "Breaks: " << debian_pkg_breaks << "\n";
  350. }
  351. if (debian_pkg_conflicts && *debian_pkg_conflicts)
  352. {
  353. out << "Conflicts: " << debian_pkg_conflicts << "\n";
  354. }
  355. if (debian_pkg_provides && *debian_pkg_provides)
  356. {
  357. out << "Provides: " << debian_pkg_provides << "\n";
  358. }
  359. if (debian_pkg_replaces && *debian_pkg_replaces)
  360. {
  361. out << "Replaces: " << debian_pkg_replaces << "\n";
  362. }
  363. unsigned long totalSize = 0;
  364. {
  365. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  366. dirName += '/';
  367. for (std::vector<std::string>::const_iterator fileIt =
  368. packageFiles.begin();
  369. fileIt != packageFiles.end(); ++ fileIt )
  370. {
  371. totalSize += cmSystemTools::FileLength(*fileIt);
  372. }
  373. }
  374. out << "Installed-Size: " << (totalSize + 1023) / 1024 << "\n";
  375. out << "Maintainer: " << maintainer << "\n";
  376. out << "Description: " << desc << "\n";
  377. out << std::endl;
  378. }
  379. const std::string shlibsfilename = strGenWDIR + "/shlibs";
  380. const char* debian_pkg_shlibs = this->GetOption(
  381. "GEN_CPACK_DEBIAN_PACKAGE_SHLIBS");
  382. const bool gen_shibs = this->IsOn("CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS")
  383. && debian_pkg_shlibs && *debian_pkg_shlibs;
  384. if( gen_shibs )
  385. {
  386. cmGeneratedFileStream out(shlibsfilename.c_str());
  387. out << debian_pkg_shlibs;
  388. out << std::endl;
  389. }
  390. const std::string postinst = strGenWDIR + "/postinst";
  391. const std::string postrm = strGenWDIR + "/postrm";
  392. if(this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST"))
  393. {
  394. cmGeneratedFileStream out(postinst.c_str());
  395. out <<
  396. "#!/bin/sh\n\n"
  397. "set -e\n\n"
  398. "if [ \"$1\" = \"configure\" ]; then\n"
  399. "\tldconfig\n"
  400. "fi\n";
  401. }
  402. if(this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM"))
  403. {
  404. cmGeneratedFileStream out(postrm.c_str());
  405. out <<
  406. "#!/bin/sh\n\n"
  407. "set -e\n\n"
  408. "if [ \"$1\" = \"remove\" ]; then\n"
  409. "\tldconfig\n"
  410. "fi\n";
  411. }
  412. cmArchiveWrite::Compress tar_compression_type = cmArchiveWrite::CompressGZip;
  413. const char* debian_compression_type =
  414. this->GetOption("GEN_CPACK_DEBIAN_COMPRESSION_TYPE");
  415. if(!debian_compression_type)
  416. {
  417. debian_compression_type = "gzip";
  418. }
  419. std::string compression_suffix;
  420. if(!strcmp(debian_compression_type, "lzma")) {
  421. compression_suffix = ".lzma";
  422. tar_compression_type = cmArchiveWrite::CompressLZMA;
  423. } else if(!strcmp(debian_compression_type, "xz")) {
  424. compression_suffix = ".xz";
  425. tar_compression_type = cmArchiveWrite::CompressXZ;
  426. } else if(!strcmp(debian_compression_type, "bzip2")) {
  427. compression_suffix = ".bz2";
  428. tar_compression_type = cmArchiveWrite::CompressBZip2;
  429. } else if(!strcmp(debian_compression_type, "gzip")) {
  430. compression_suffix = ".gz";
  431. tar_compression_type = cmArchiveWrite::CompressGZip;
  432. } else if(!strcmp(debian_compression_type, "none")) {
  433. compression_suffix = "";
  434. tar_compression_type = cmArchiveWrite::CompressNone;
  435. } else {
  436. cmCPackLogger(cmCPackLog::LOG_ERROR,
  437. "Error unrecognized compression type: "
  438. << debian_compression_type << std::endl);
  439. }
  440. std::string filename_data_tar = strGenWDIR
  441. + "/data.tar" + compression_suffix;
  442. // atomic file generation for data.tar
  443. {
  444. cmGeneratedFileStream fileStream_data_tar;
  445. fileStream_data_tar.Open(filename_data_tar.c_str(), false, true);
  446. if(!fileStream_data_tar)
  447. {
  448. cmCPackLogger(cmCPackLog::LOG_ERROR,
  449. "Error opening the file \"" << filename_data_tar << "\" for writing"
  450. << std::endl);
  451. return 0;
  452. }
  453. cmArchiveWrite data_tar(fileStream_data_tar, tar_compression_type, "paxr");
  454. // uid/gid should be the one of the root user, and this root user has
  455. // always uid/gid equal to 0.
  456. data_tar.SetUIDAndGID(0u, 0u);
  457. data_tar.SetUNAMEAndGNAME("root", "root");
  458. // now add all directories which have to be compressed
  459. // collect all top level install dirs for that
  460. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would
  461. // give /usr and /opt
  462. size_t topLevelLength = strGenWDIR.length();
  463. cmCPackLogger(cmCPackLog::LOG_DEBUG, "WDIR: \""
  464. << strGenWDIR
  465. << "\", length = " << topLevelLength
  466. << std::endl);
  467. std::set<std::string> orderedFiles;
  468. // we have to reconstruct the parent folders as well
  469. for (std::vector<std::string>::const_iterator fileIt =
  470. packageFiles.begin();
  471. fileIt != packageFiles.end(); ++ fileIt )
  472. {
  473. std::string currentPath = *fileIt;
  474. while(currentPath != strGenWDIR)
  475. {
  476. // the last one IS strGenWDIR, but we do not want this one:
  477. // XXX/application/usr/bin/myprogram with GEN_WDIR=XXX/application
  478. // should not add XXX/application
  479. orderedFiles.insert(currentPath);
  480. currentPath = cmSystemTools::CollapseCombinedPath(currentPath, "..");
  481. }
  482. }
  483. for (std::set<std::string>::const_iterator fileIt =
  484. orderedFiles.begin();
  485. fileIt != orderedFiles.end(); ++ fileIt )
  486. {
  487. cmCPackLogger(cmCPackLog::LOG_DEBUG, "FILEIT: \"" << *fileIt << "\""
  488. << std::endl);
  489. std::string::size_type slashPos = fileIt->find('/', topLevelLength+1);
  490. std::string relativeDir = fileIt->substr(topLevelLength,
  491. slashPos - topLevelLength);
  492. cmCPackLogger(cmCPackLog::LOG_DEBUG, "RELATIVEDIR: \"" << relativeDir
  493. << "\"" << std::endl);
  494. // do not recurse because the loop will do it
  495. if(!data_tar.Add(*fileIt, topLevelLength, ".", false))
  496. {
  497. cmCPackLogger(cmCPackLog::LOG_ERROR,
  498. "Problem adding file to tar:" << std::endl
  499. << "#top level directory: "
  500. << strGenWDIR << std::endl
  501. << "#file: " << *fileIt << std::endl
  502. << "#error:" << data_tar.GetError() << std::endl);
  503. return 0;
  504. }
  505. }
  506. } // scope for file generation
  507. std::string md5filename = strGenWDIR + "/md5sums";
  508. {
  509. // the scope is needed for cmGeneratedFileStream
  510. cmGeneratedFileStream out(md5filename.c_str());
  511. std::string topLevelWithTrailingSlash =
  512. this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  513. topLevelWithTrailingSlash += '/';
  514. for (std::vector<std::string>::const_iterator fileIt =
  515. packageFiles.begin();
  516. fileIt != packageFiles.end(); ++ fileIt )
  517. {
  518. // hash only regular files
  519. if( cmSystemTools::FileIsDirectory(*fileIt)
  520. || cmSystemTools::FileIsSymlink(*fileIt))
  521. {
  522. continue;
  523. }
  524. char md5sum[33];
  525. if(!cmSystemTools::ComputeFileMD5(*fileIt, md5sum))
  526. {
  527. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem computing the md5 of "
  528. << *fileIt << std::endl);
  529. }
  530. md5sum[32] = 0;
  531. std::string output(md5sum);
  532. output += " " + *fileIt + "\n";
  533. // debian md5sums entries are like this:
  534. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  535. // thus strip the full path (with the trailing slash)
  536. cmSystemTools::ReplaceString(output,
  537. topLevelWithTrailingSlash.c_str(), "");
  538. out << output;
  539. }
  540. // each line contains a eol.
  541. // Do not end the md5sum file with yet another (invalid)
  542. }
  543. std::string filename_control_tar = strGenWDIR + "/control.tar.gz";
  544. // atomic file generation for control.tar
  545. {
  546. cmGeneratedFileStream fileStream_control_tar;
  547. fileStream_control_tar.Open(filename_control_tar.c_str(), false, true);
  548. if(!fileStream_control_tar)
  549. {
  550. cmCPackLogger(cmCPackLog::LOG_ERROR,
  551. "Error opening the file \"" << filename_control_tar
  552. << "\" for writing" << std::endl);
  553. return 0;
  554. }
  555. cmArchiveWrite control_tar(fileStream_control_tar,
  556. cmArchiveWrite::CompressGZip,
  557. "paxr");
  558. // sets permissions and uid/gid for the files
  559. control_tar.SetUIDAndGID(0u, 0u);
  560. control_tar.SetUNAMEAndGNAME("root", "root");
  561. /* permissions are set according to
  562. https://www.debian.org/doc/debian-policy/ch-files.html#s-permissions-owners
  563. and
  564. https://lintian.debian.org/tags/control-file-has-bad-permissions.html
  565. */
  566. const mode_t permission644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  567. const mode_t permissionExecute = S_IXUSR | S_IXGRP | S_IXOTH;
  568. const mode_t permission755 = permission644 | permissionExecute;
  569. // for md5sum and control (that we have generated here), we use 644
  570. // (RW-R--R--)
  571. // so that deb lintian doesn't warn about it
  572. control_tar.SetPermissions(permission644);
  573. // adds control and md5sums
  574. if( !control_tar.Add(md5filename, strGenWDIR.length(), ".")
  575. || !control_tar.Add(strGenWDIR + "/control", strGenWDIR.length(), "."))
  576. {
  577. cmCPackLogger(cmCPackLog::LOG_ERROR,
  578. "Error adding file to tar:" << std::endl
  579. << "#top level directory: "
  580. << strGenWDIR << std::endl
  581. << "#file: \"control\" or \"md5sums\"" << std::endl
  582. << "#error:" << control_tar.GetError() << std::endl);
  583. return 0;
  584. }
  585. // adds generated shlibs file
  586. if( gen_shibs )
  587. {
  588. if( !control_tar.Add(shlibsfilename, strGenWDIR.length(), ".") )
  589. {
  590. cmCPackLogger(cmCPackLog::LOG_ERROR,
  591. "Error adding file to tar:" << std::endl
  592. << "#top level directory: "
  593. << strGenWDIR << std::endl
  594. << "#file: \"shlibs\"" << std::endl
  595. << "#error:" << control_tar.GetError() << std::endl);
  596. return 0;
  597. }
  598. }
  599. // adds LDCONFIG related files
  600. if(this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST"))
  601. {
  602. control_tar.SetPermissions(permission755);
  603. if(!control_tar.Add(postinst, strGenWDIR.length(), "."))
  604. {
  605. cmCPackLogger(cmCPackLog::LOG_ERROR,
  606. "Error adding file to tar:" << std::endl
  607. << "#top level directory: "
  608. << strGenWDIR << std::endl
  609. << "#file: \"postinst\"" << std::endl
  610. << "#error:" << control_tar.GetError() << std::endl);
  611. return 0;
  612. }
  613. control_tar.SetPermissions(permission644);
  614. }
  615. if(this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM"))
  616. {
  617. control_tar.SetPermissions(permission755);
  618. if(!control_tar.Add(postrm, strGenWDIR.length(), "."))
  619. {
  620. cmCPackLogger(cmCPackLog::LOG_ERROR,
  621. "Error adding file to tar:" << std::endl
  622. << "#top level directory: "
  623. << strGenWDIR << std::endl
  624. << "#file: \"postinst\"" << std::endl
  625. << "#error:" << control_tar.GetError() << std::endl);
  626. return 0;
  627. }
  628. control_tar.SetPermissions(permission644);
  629. }
  630. // for the other files, we use
  631. // -either the original permission on the files
  632. // -either a permission strictly defined by the Debian policies
  633. const char* controlExtra =
  634. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA");
  635. if( controlExtra )
  636. {
  637. // permissions are now controlled by the original file permissions
  638. const bool permissionStrictPolicy =
  639. this->IsSet("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION");
  640. static const char* strictFiles[] = {
  641. "config", "postinst", "postrm", "preinst", "prerm"
  642. };
  643. std::set<std::string> setStrictFiles(
  644. strictFiles,
  645. strictFiles + sizeof(strictFiles)/sizeof(strictFiles[0]));
  646. // default
  647. control_tar.ClearPermissions();
  648. std::vector<std::string> controlExtraList;
  649. cmSystemTools::ExpandListArgument(controlExtra, controlExtraList);
  650. for(std::vector<std::string>::iterator i = controlExtraList.begin();
  651. i != controlExtraList.end(); ++i)
  652. {
  653. std::string filenamename =
  654. cmsys::SystemTools::GetFilenameName(*i);
  655. std::string localcopy = strGenWDIR + "/" + filenamename;
  656. if(permissionStrictPolicy)
  657. {
  658. control_tar.SetPermissions(setStrictFiles.count(filenamename) ?
  659. permission755 : permission644);
  660. }
  661. // if we can copy the file, it means it does exist, let's add it:
  662. if( cmsys::SystemTools::CopyFileIfDifferent(*i, localcopy) )
  663. {
  664. control_tar.Add(localcopy, strGenWDIR.length(), ".");
  665. }
  666. }
  667. }
  668. }
  669. // ar -r your-package-name.deb debian-binary control.tar.* data.tar.*
  670. // since debian packages require BSD ar (most Linux distros and even
  671. // FreeBSD and NetBSD ship GNU ar) we use a copy of OpenBSD ar here.
  672. std::vector<std::string> arFiles;
  673. std::string topLevelString = strGenWDIR + "/";
  674. arFiles.push_back(topLevelString + "debian-binary");
  675. arFiles.push_back(topLevelString + "control.tar.gz");
  676. arFiles.push_back(topLevelString + "data.tar" + compression_suffix);
  677. std::string outputFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  678. outputFileName += "/";
  679. outputFileName += this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  680. int res = ar_append(outputFileName.c_str(), arFiles);
  681. if ( res!=0 )
  682. {
  683. std::string tmpFile = this->GetOption(
  684. "GEN_CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  685. tmpFile += "/Deb.log";
  686. cmGeneratedFileStream ofs(tmpFile.c_str());
  687. ofs << "# Problem creating archive using: " << res << std::endl;
  688. return 0;
  689. }
  690. return 1;
  691. }
  692. bool cmCPackDebGenerator::SupportsComponentInstallation() const
  693. {
  694. if (IsOn("CPACK_DEB_COMPONENT_INSTALL"))
  695. {
  696. return true;
  697. }
  698. else
  699. {
  700. return false;
  701. }
  702. }
  703. std::string cmCPackDebGenerator::GetComponentInstallDirNameSuffix(
  704. const std::string& componentName)
  705. {
  706. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  707. return componentName;
  708. }
  709. if (componentPackageMethod == ONE_PACKAGE) {
  710. return std::string("ALL_COMPONENTS_IN_ONE");
  711. }
  712. // We have to find the name of the COMPONENT GROUP
  713. // the current COMPONENT belongs to.
  714. std::string groupVar = "CPACK_COMPONENT_" +
  715. cmSystemTools::UpperCase(componentName) + "_GROUP";
  716. if (NULL != GetOption(groupVar))
  717. {
  718. return std::string(GetOption(groupVar));
  719. }
  720. else
  721. {
  722. return componentName;
  723. }
  724. }
  725. // The following code is taken from OpenBSD ar:
  726. // http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ar/
  727. // It has been slightly modified:
  728. // -return error codes instead exit() in functions
  729. // -use the stdio file I/O functions instead the file descriptor based ones
  730. // -merged into one cxx file
  731. // -no additional options supported
  732. // The coding style hasn't been modified.
  733. /*-
  734. * Copyright (c) 1990, 1993, 1994
  735. * The Regents of the University of California. All rights reserved.
  736. *
  737. * This code is derived from software contributed to Berkeley by
  738. * Hugh Smith at The University of Guelph.
  739. *
  740. * Redistribution and use in source and binary forms, with or without
  741. * modification, are permitted provided that the following conditions
  742. * are met:
  743. * 1. Redistributions of source code must retain the above copyright
  744. * notice, this list of conditions and the following disclaimer.
  745. * 2. Redistributions in binary form must reproduce the above copyright
  746. * notice, this list of conditions and the following disclaimer in the
  747. * documentation and/or other materials provided with the distribution.
  748. * 3. Neither the name of the University nor the names of its contributors
  749. * may be used to endorse or promote products derived from this software
  750. * without specific prior written permission.
  751. *
  752. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  753. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  754. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  755. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  756. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  757. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  758. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  759. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  760. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  761. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  762. * SUCH DAMAGE.
  763. */
  764. #include <sys/types.h>
  765. // include sys/stat.h after sys/types.h
  766. #include <sys/stat.h>
  767. #include <stdio.h>
  768. #include <stdlib.h>
  769. #include <string.h>
  770. #define ARMAG "!<arch>\n" /* ar "magic number" */
  771. #define SARMAG 8 /* strlen(ARMAG); */
  772. #define AR_EFMT1 "#1/" /* extended format #1 */
  773. #define ARFMAG "`\n"
  774. /* Header format strings. */
  775. #define HDR1 "%s%-13d%-12ld%-6u%-6u%-8o%-10lld%2s"
  776. #define HDR2 "%-16.16s%-12ld%-6u%-6u%-8o%-10lld%2s"
  777. struct ar_hdr {
  778. char ar_name[16]; /* name */
  779. char ar_date[12]; /* modification time */
  780. char ar_uid[6]; /* user id */
  781. char ar_gid[6]; /* group id */
  782. char ar_mode[8]; /* octal file permissions */
  783. char ar_size[10]; /* size in bytes */
  784. char ar_fmag[2]; /* consistency check */
  785. };
  786. /* Set up file copy. */
  787. #define SETCF(from, fromname, to, toname, pad) { \
  788. cf.rFile = from; \
  789. cf.rname = fromname; \
  790. cf.wFile = to; \
  791. cf.wname = toname; \
  792. cf.flags = pad; \
  793. }
  794. /* File copy structure. */
  795. typedef struct {
  796. FILE* rFile; /* read file descriptor */
  797. const char *rname; /* read name */
  798. FILE* wFile; /* write file descriptor */
  799. const char *wname; /* write name */
  800. #define NOPAD 0x00 /* don't pad */
  801. #define WPAD 0x02 /* pad on writes */
  802. unsigned int flags; /* pad flags */
  803. } CF;
  804. /* misc.c */
  805. static const char * ar_rname(const char *path)
  806. {
  807. const char *ind = strrchr(path, '/');
  808. return (ind ) ? ind + 1 : path;
  809. }
  810. /* archive.c */
  811. typedef struct ar_hdr HDR;
  812. static char ar_hb[sizeof(HDR) + 1]; /* real header */
  813. static size_t ar_already_written;
  814. /* copy_ar --
  815. * Copy size bytes from one file to another - taking care to handle the
  816. * extra byte (for odd size files) when reading archives and writing an
  817. * extra byte if necessary when adding files to archive. The length of
  818. * the object is the long name plus the object itself; the variable
  819. * already_written gets set if a long name was written.
  820. *
  821. * The padding is really unnecessary, and is almost certainly a remnant
  822. * of early archive formats where the header included binary data which
  823. * a PDP-11 required to start on an even byte boundary. (Or, perhaps,
  824. * because 16-bit word addressed copies were faster?) Anyhow, it should
  825. * have been ripped out long ago.
  826. */
  827. static int copy_ar(CF *cfp, off_t size)
  828. {
  829. static char pad = '\n';
  830. off_t sz = size;
  831. size_t nr, nw;
  832. char buf[8*1024];
  833. if (sz == 0)
  834. return 0;
  835. FILE* from = cfp->rFile;
  836. FILE* to = cfp->wFile;
  837. while (sz &&
  838. (nr = fread(buf, 1, sz < static_cast<off_t>(sizeof(buf))
  839. ? static_cast<size_t>(sz) : sizeof(buf), from ))
  840. > 0) {
  841. sz -= nr;
  842. for (size_t off = 0; off < nr; nr -= off, off += nw)
  843. if ((nw = fwrite(buf + off, 1, nr, to)) < nr)
  844. return -1;
  845. }
  846. if (sz)
  847. return -2;
  848. if (cfp->flags & WPAD && (size + ar_already_written) & 1
  849. && fwrite(&pad, 1, 1, to) != 1)
  850. return -4;
  851. return 0;
  852. }
  853. /* put_arobj -- Write an archive member to a file. */
  854. static int put_arobj(CF *cfp, struct stat *sb)
  855. {
  856. int result = 0;
  857. struct ar_hdr *hdr;
  858. /* If passed an sb structure, reading a file from disk. Get stat(2)
  859. * information, build a name and construct a header. (Files are named
  860. * by their last component in the archive.) */
  861. const char* name = ar_rname(cfp->rname);
  862. (void)stat(cfp->rname, sb);
  863. /* If not truncating names and the name is too long or contains
  864. * a space, use extended format 1. */
  865. size_t lname = strlen(name);
  866. uid_t uid = sb->st_uid;
  867. gid_t gid = sb->st_gid;
  868. if (uid > USHRT_MAX) {
  869. uid = USHRT_MAX;
  870. }
  871. if (gid > USHRT_MAX) {
  872. gid = USHRT_MAX;
  873. }
  874. if (lname > sizeof(hdr->ar_name) || strchr(name, ' '))
  875. (void)sprintf(ar_hb, HDR1, AR_EFMT1, (int)lname,
  876. (long int)sb->st_mtime, (unsigned)uid, (unsigned)gid,
  877. (unsigned)sb->st_mode, (long long)sb->st_size + lname,
  878. ARFMAG);
  879. else {
  880. lname = 0;
  881. (void)sprintf(ar_hb, HDR2, name,
  882. (long int)sb->st_mtime, (unsigned)uid, (unsigned)gid,
  883. (unsigned)sb->st_mode, (long long)sb->st_size,
  884. ARFMAG);
  885. }
  886. off_t size = sb->st_size;
  887. if (fwrite(ar_hb, 1, sizeof(HDR), cfp->wFile) != sizeof(HDR))
  888. return -1;
  889. if (lname) {
  890. if (fwrite(name, 1, lname, cfp->wFile) != lname)
  891. return -2;
  892. ar_already_written = lname;
  893. }
  894. result = copy_ar(cfp, size);
  895. ar_already_written = 0;
  896. return result;
  897. }
  898. /* append.c */
  899. /* append --
  900. * Append files to the archive - modifies original archive or creates
  901. * a new archive if named archive does not exist.
  902. */
  903. static int ar_append(const char* archive,const std::vector<std::string>& files)
  904. {
  905. int eval = 0;
  906. FILE* aFile = cmSystemTools::Fopen(archive, "wb+");
  907. if (aFile!=NULL) {
  908. fwrite(ARMAG, SARMAG, 1, aFile);
  909. if (fseek(aFile, 0, SEEK_END) != -1) {
  910. CF cf;
  911. struct stat sb;
  912. /* Read from disk, write to an archive; pad on write. */
  913. SETCF(NULL, 0, aFile, archive, WPAD);
  914. for(std::vector<std::string>::const_iterator fileIt = files.begin();
  915. fileIt!=files.end(); ++fileIt) {
  916. const char* filename = fileIt->c_str();
  917. FILE* file = cmSystemTools::Fopen(filename, "rb");
  918. if (file == NULL) {
  919. eval = -1;
  920. continue;
  921. }
  922. cf.rFile = file;
  923. cf.rname = filename;
  924. int result = put_arobj(&cf, &sb);
  925. (void)fclose(file);
  926. if (result!=0) {
  927. eval = -2;
  928. break;
  929. }
  930. }
  931. }
  932. else {
  933. eval = -3;
  934. }
  935. fclose(aFile);
  936. }
  937. else {
  938. eval = -4;
  939. }
  940. return eval;
  941. }