cmCPackDebGenerator.cxx 35 KB

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