cmCPackDebGenerator.cxx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. packageFileNames.push_back(packageFileName);
  97. return retval;
  98. }
  99. int cmCPackDebGenerator::PackageComponents(bool ignoreGroup)
  100. {
  101. int retval = 1;
  102. /* Reset package file name list it will be populated during the
  103. * component packaging run*/
  104. packageFileNames.clear();
  105. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  106. // The default behavior is to have one package by component group
  107. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  108. if (!ignoreGroup)
  109. {
  110. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  111. for (compGIt=this->ComponentGroups.begin();
  112. compGIt!=this->ComponentGroups.end(); ++compGIt)
  113. {
  114. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  115. << compGIt->first
  116. << std::endl);
  117. // Begin the archive for this group
  118. retval &= PackageOnePack(initialTopLevel,compGIt->first);
  119. }
  120. // Handle Orphan components (components not belonging to any groups)
  121. std::map<std::string, cmCPackComponent>::iterator compIt;
  122. for (compIt=this->Components.begin();
  123. compIt!=this->Components.end(); ++compIt )
  124. {
  125. // Does the component belong to a group?
  126. if (compIt->second.Group==NULL)
  127. {
  128. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  129. "Component <"
  130. << compIt->second.Name
  131. << "> does not belong to any group, package it separately."
  132. << std::endl);
  133. // Begin the archive for this orphan component
  134. retval &= PackageOnePack(initialTopLevel,compIt->first);
  135. }
  136. }
  137. }
  138. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  139. // We build 1 package per component
  140. else
  141. {
  142. std::map<std::string, cmCPackComponent>::iterator compIt;
  143. for (compIt=this->Components.begin();
  144. compIt!=this->Components.end(); ++compIt )
  145. {
  146. retval &= PackageOnePack(initialTopLevel,compIt->first);
  147. }
  148. }
  149. return retval;
  150. }
  151. int cmCPackDebGenerator::PackageComponentsAllInOne()
  152. {
  153. int retval = 1;
  154. std::string compInstDirName;
  155. /* Reset package file name list it will be populated during the
  156. * component packaging run*/
  157. packageFileNames.clear();
  158. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  159. compInstDirName = "ALL_COMPONENTS_IN_ONE";
  160. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  161. "Packaging all groups in one package..."
  162. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  163. << std::endl);
  164. // The ALL GROUPS in ONE package case
  165. std::string localToplevel(initialTopLevel);
  166. std::string packageFileName(
  167. cmSystemTools::GetParentDirectory(toplevel)
  168. );
  169. std::string outputFileName(
  170. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  171. + this->GetOutputExtension()
  172. );
  173. // all GROUP in one vs all COMPONENT in one
  174. localToplevel += "/"+compInstDirName;
  175. /* replace the TEMP DIRECTORY with the component one */
  176. this->SetOption("CPACK_TEMPORARY_DIRECTORY",localToplevel.c_str());
  177. packageFileName += "/"+ outputFileName;
  178. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  179. this->SetOption("CPACK_OUTPUT_FILE_NAME",outputFileName.c_str());
  180. /* replace the TEMPORARY package file name */
  181. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  182. packageFileName.c_str());
  183. // Tell CPackDeb.cmake the path where the component is.
  184. std::string component_path = "/";
  185. component_path += compInstDirName;
  186. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  187. component_path.c_str());
  188. if (!this->ReadListFile("CPackDeb.cmake"))
  189. {
  190. cmCPackLogger(cmCPackLog::LOG_ERROR,
  191. "Error while execution CPackDeb.cmake" << std::endl);
  192. retval = 0;
  193. return retval;
  194. }
  195. cmsys::Glob gl;
  196. std::string findExpr(this->GetOption("GEN_WDIR"));
  197. findExpr += "/*";
  198. gl.RecurseOn();
  199. gl.SetRecurseListDirs(true);
  200. if ( !gl.FindFiles(findExpr) )
  201. {
  202. cmCPackLogger(cmCPackLog::LOG_ERROR,
  203. "Cannot find any files in the installed directory" << std::endl);
  204. return 0;
  205. }
  206. packageFiles = gl.GetFiles();
  207. int res = createDeb();
  208. if (res != 1)
  209. {
  210. retval = 0;
  211. }
  212. // add the generated package to package file names list
  213. packageFileNames.push_back(packageFileName);
  214. return retval;
  215. }
  216. int cmCPackDebGenerator::PackageFiles()
  217. {
  218. int retval = -1;
  219. /* Are we in the component packaging case */
  220. if (WantsComponentInstallation()) {
  221. // CASE 1 : COMPONENT ALL-IN-ONE package
  222. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  223. // then the package file is unique and should be open here.
  224. if (componentPackageMethod == ONE_PACKAGE)
  225. {
  226. return PackageComponentsAllInOne();
  227. }
  228. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  229. // There will be 1 package for each component group
  230. // however one may require to ignore component group and
  231. // in this case you'll get 1 package for each component.
  232. else
  233. {
  234. return PackageComponents(componentPackageMethod ==
  235. ONE_PACKAGE_PER_COMPONENT);
  236. }
  237. }
  238. // CASE 3 : NON COMPONENT package.
  239. else
  240. {
  241. if (!this->ReadListFile("CPackDeb.cmake"))
  242. {
  243. cmCPackLogger(cmCPackLog::LOG_ERROR,
  244. "Error while execution CPackDeb.cmake" << std::endl);
  245. retval = 0;
  246. }
  247. else
  248. {
  249. packageFiles = files;
  250. return createDeb();
  251. }
  252. }
  253. return retval;
  254. }
  255. int cmCPackDebGenerator::createDeb()
  256. {
  257. // debian-binary file
  258. const std::string strGenWDIR(this->GetOption("GEN_WDIR"));
  259. const std::string dbfilename = strGenWDIR + "/debian-binary";
  260. { // the scope is needed for cmGeneratedFileStream
  261. cmGeneratedFileStream out(dbfilename.c_str());
  262. out << "2.0";
  263. out << std::endl; // required for valid debian package
  264. }
  265. // control file
  266. std::string ctlfilename = strGenWDIR + "/control";
  267. // debian policy enforce lower case for package name
  268. // mandatory entries:
  269. std::string debian_pkg_name = cmsys::SystemTools::LowerCase(
  270. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME") );
  271. const char* debian_pkg_version =
  272. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_VERSION");
  273. const char* debian_pkg_section =
  274. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SECTION");
  275. const char* debian_pkg_priority =
  276. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PRIORITY");
  277. const char* debian_pkg_arch =
  278. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  279. const char* maintainer =
  280. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MAINTAINER");
  281. const char* desc =
  282. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  283. // optional entries
  284. const char* debian_pkg_dep =
  285. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DEPENDS");
  286. const char* debian_pkg_rec =
  287. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  288. const char* debian_pkg_sug =
  289. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SUGGESTS");
  290. const char* debian_pkg_url =
  291. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  292. const char* debian_pkg_predep =
  293. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PREDEPENDS");
  294. const char* debian_pkg_enhances =
  295. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ENHANCES");
  296. const char* debian_pkg_breaks =
  297. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_BREAKS");
  298. const char* debian_pkg_conflicts =
  299. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONFLICTS");
  300. const char* debian_pkg_provides =
  301. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PROVIDES");
  302. const char* debian_pkg_replaces =
  303. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_REPLACES");
  304. const char* debian_pkg_source =
  305. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SOURCE");
  306. { // the scope is needed for cmGeneratedFileStream
  307. cmGeneratedFileStream out(ctlfilename.c_str());
  308. out << "Package: " << debian_pkg_name << "\n";
  309. out << "Version: " << debian_pkg_version << "\n";
  310. out << "Section: " << debian_pkg_section << "\n";
  311. out << "Priority: " << debian_pkg_priority << "\n";
  312. out << "Architecture: " << debian_pkg_arch << "\n";
  313. if(debian_pkg_source && *debian_pkg_source)
  314. {
  315. out << "Source: " << debian_pkg_source << "\n";
  316. }
  317. if(debian_pkg_dep && *debian_pkg_dep)
  318. {
  319. out << "Depends: " << debian_pkg_dep << "\n";
  320. }
  321. if(debian_pkg_rec && *debian_pkg_rec)
  322. {
  323. out << "Recommends: " << debian_pkg_rec << "\n";
  324. }
  325. if(debian_pkg_sug && *debian_pkg_sug)
  326. {
  327. out << "Suggests: " << debian_pkg_sug << "\n";
  328. }
  329. if(debian_pkg_url && *debian_pkg_url)
  330. {
  331. out << "Homepage: " << debian_pkg_url << "\n";
  332. }
  333. if (debian_pkg_predep && *debian_pkg_predep)
  334. {
  335. out << "Pre-Depends: " << debian_pkg_predep << "\n";
  336. }
  337. if (debian_pkg_enhances && *debian_pkg_enhances)
  338. {
  339. out << "Enhances: " << debian_pkg_enhances << "\n";
  340. }
  341. if (debian_pkg_breaks && *debian_pkg_breaks)
  342. {
  343. out << "Breaks: " << debian_pkg_breaks << "\n";
  344. }
  345. if (debian_pkg_conflicts && *debian_pkg_conflicts)
  346. {
  347. out << "Conflicts: " << debian_pkg_conflicts << "\n";
  348. }
  349. if (debian_pkg_provides && *debian_pkg_provides)
  350. {
  351. out << "Provides: " << debian_pkg_provides << "\n";
  352. }
  353. if (debian_pkg_replaces && *debian_pkg_replaces)
  354. {
  355. out << "Replaces: " << debian_pkg_replaces << "\n";
  356. }
  357. unsigned long totalSize = 0;
  358. {
  359. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  360. dirName += '/';
  361. for (std::vector<std::string>::const_iterator fileIt =
  362. packageFiles.begin();
  363. fileIt != packageFiles.end(); ++ fileIt )
  364. {
  365. totalSize += cmSystemTools::FileLength(*fileIt);
  366. }
  367. }
  368. out << "Installed-Size: " << (totalSize + 1023) / 1024 << "\n";
  369. out << "Maintainer: " << maintainer << "\n";
  370. out << "Description: " << desc << "\n";
  371. out << std::endl;
  372. }
  373. const std::string shlibsfilename = strGenWDIR + "/shlibs";
  374. const char* debian_pkg_shlibs = this->GetOption(
  375. "GEN_CPACK_DEBIAN_PACKAGE_SHLIBS");
  376. const bool gen_shibs = this->IsOn("CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS")
  377. && debian_pkg_shlibs && *debian_pkg_shlibs;
  378. if( gen_shibs )
  379. {
  380. cmGeneratedFileStream out(shlibsfilename.c_str());
  381. out << debian_pkg_shlibs;
  382. out << std::endl;
  383. }
  384. const std::string postinst = strGenWDIR + "/postinst";
  385. const std::string postrm = strGenWDIR + "/postrm";
  386. if(this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST"))
  387. {
  388. cmGeneratedFileStream out(postinst.c_str());
  389. out <<
  390. "#!/bin/sh\n\n"
  391. "set -e\n\n"
  392. "if [ \"$1\" = \"configure\" ]; then\n"
  393. "\tldconfig\n"
  394. "fi\n";
  395. }
  396. if(this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM"))
  397. {
  398. cmGeneratedFileStream out(postrm.c_str());
  399. out <<
  400. "#!/bin/sh\n\n"
  401. "set -e\n\n"
  402. "if [ \"$1\" = \"remove\" ]; then\n"
  403. "\tldconfig\n"
  404. "fi\n";
  405. }
  406. cmArchiveWrite::Compress tar_compression_type = cmArchiveWrite::CompressGZip;
  407. const char* debian_compression_type =
  408. this->GetOption("GEN_CPACK_DEBIAN_COMPRESSION_TYPE");
  409. if(!debian_compression_type)
  410. {
  411. debian_compression_type = "gzip";
  412. }
  413. std::string compression_suffix;
  414. if(!strcmp(debian_compression_type, "lzma")) {
  415. compression_suffix = ".lzma";
  416. tar_compression_type = cmArchiveWrite::CompressLZMA;
  417. } else if(!strcmp(debian_compression_type, "xz")) {
  418. compression_suffix = ".xz";
  419. tar_compression_type = cmArchiveWrite::CompressXZ;
  420. } else if(!strcmp(debian_compression_type, "bzip2")) {
  421. compression_suffix = ".bz2";
  422. tar_compression_type = cmArchiveWrite::CompressBZip2;
  423. } else if(!strcmp(debian_compression_type, "gzip")) {
  424. compression_suffix = ".gz";
  425. tar_compression_type = cmArchiveWrite::CompressGZip;
  426. } else if(!strcmp(debian_compression_type, "none")) {
  427. compression_suffix = "";
  428. tar_compression_type = cmArchiveWrite::CompressNone;
  429. } else {
  430. cmCPackLogger(cmCPackLog::LOG_ERROR,
  431. "Error unrecognized compression type: "
  432. << debian_compression_type << std::endl);
  433. }
  434. std::string filename_data_tar = strGenWDIR
  435. + "/data.tar" + compression_suffix;
  436. // atomic file generation for data.tar
  437. {
  438. cmGeneratedFileStream fileStream_data_tar;
  439. fileStream_data_tar.Open(filename_data_tar.c_str(), false, true);
  440. if(!fileStream_data_tar)
  441. {
  442. cmCPackLogger(cmCPackLog::LOG_ERROR,
  443. "Error opening the file \"" << filename_data_tar << "\" for writing"
  444. << std::endl);
  445. return 0;
  446. }
  447. cmArchiveWrite data_tar(fileStream_data_tar, tar_compression_type, "paxr");
  448. // uid/gid should be the one of the root user, and this root user has
  449. // always uid/gid equal to 0.
  450. data_tar.SetUIDAndGID(0u, 0u);
  451. data_tar.SetUNAMEAndGNAME("root", "root");
  452. // now add all directories which have to be compressed
  453. // collect all top level install dirs for that
  454. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would
  455. // give /usr and /opt
  456. size_t topLevelLength = strGenWDIR.length();
  457. cmCPackLogger(cmCPackLog::LOG_DEBUG, "WDIR: \""
  458. << strGenWDIR
  459. << "\", length = " << topLevelLength
  460. << std::endl);
  461. std::set<std::string> orderedFiles;
  462. // we have to reconstruct the parent folders as well
  463. for (std::vector<std::string>::const_iterator fileIt =
  464. packageFiles.begin();
  465. fileIt != packageFiles.end(); ++ fileIt )
  466. {
  467. std::string currentPath = *fileIt;
  468. while(currentPath != strGenWDIR)
  469. {
  470. // the last one IS strGenWDIR, but we do not want this one:
  471. // XXX/application/usr/bin/myprogram with GEN_WDIR=XXX/application
  472. // should not add XXX/application
  473. orderedFiles.insert(currentPath);
  474. currentPath = cmSystemTools::CollapseCombinedPath(currentPath, "..");
  475. }
  476. }
  477. for (std::set<std::string>::const_iterator fileIt =
  478. orderedFiles.begin();
  479. fileIt != orderedFiles.end(); ++ fileIt )
  480. {
  481. cmCPackLogger(cmCPackLog::LOG_DEBUG, "FILEIT: \"" << *fileIt << "\""
  482. << std::endl);
  483. std::string::size_type slashPos = fileIt->find('/', topLevelLength+1);
  484. std::string relativeDir = fileIt->substr(topLevelLength,
  485. slashPos - topLevelLength);
  486. cmCPackLogger(cmCPackLog::LOG_DEBUG, "RELATIVEDIR: \"" << relativeDir
  487. << "\"" << std::endl);
  488. // do not recurse because the loop will do it
  489. if(!data_tar.Add(*fileIt, topLevelLength, ".", false))
  490. {
  491. cmCPackLogger(cmCPackLog::LOG_ERROR,
  492. "Problem adding file to tar:" << std::endl
  493. << "#top level directory: "
  494. << strGenWDIR << std::endl
  495. << "#file: " << *fileIt << std::endl
  496. << "#error:" << data_tar.GetError() << std::endl);
  497. return 0;
  498. }
  499. }
  500. } // scope for file generation
  501. std::string md5filename = strGenWDIR + "/md5sums";
  502. {
  503. // the scope is needed for cmGeneratedFileStream
  504. cmGeneratedFileStream out(md5filename.c_str());
  505. std::string topLevelWithTrailingSlash =
  506. this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  507. topLevelWithTrailingSlash += '/';
  508. for (std::vector<std::string>::const_iterator fileIt =
  509. packageFiles.begin();
  510. fileIt != packageFiles.end(); ++ fileIt )
  511. {
  512. // hash only regular files
  513. if( cmSystemTools::FileIsDirectory(*fileIt)
  514. || cmSystemTools::FileIsSymlink(*fileIt))
  515. {
  516. continue;
  517. }
  518. char md5sum[33];
  519. if(!cmSystemTools::ComputeFileMD5(*fileIt, md5sum))
  520. {
  521. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem computing the md5 of "
  522. << *fileIt << std::endl);
  523. }
  524. md5sum[32] = 0;
  525. std::string output(md5sum);
  526. output += " " + *fileIt + "\n";
  527. // debian md5sums entries are like this:
  528. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  529. // thus strip the full path (with the trailing slash)
  530. cmSystemTools::ReplaceString(output,
  531. topLevelWithTrailingSlash.c_str(), "");
  532. out << output;
  533. }
  534. // each line contains a eol.
  535. // Do not end the md5sum file with yet another (invalid)
  536. }
  537. std::string filename_control_tar = strGenWDIR + "/control.tar.gz";
  538. // atomic file generation for control.tar
  539. {
  540. cmGeneratedFileStream fileStream_control_tar;
  541. fileStream_control_tar.Open(filename_control_tar.c_str(), false, true);
  542. if(!fileStream_control_tar)
  543. {
  544. cmCPackLogger(cmCPackLog::LOG_ERROR,
  545. "Error opening the file \"" << filename_control_tar
  546. << "\" for writing" << std::endl);
  547. return 0;
  548. }
  549. cmArchiveWrite control_tar(fileStream_control_tar,
  550. cmArchiveWrite::CompressGZip,
  551. "paxr");
  552. // sets permissions and uid/gid for the files
  553. control_tar.SetUIDAndGID(0u, 0u);
  554. control_tar.SetUNAMEAndGNAME("root", "root");
  555. /* permissions are set according to
  556. https://www.debian.org/doc/debian-policy/ch-files.html#s-permissions-owners
  557. and
  558. https://lintian.debian.org/tags/control-file-has-bad-permissions.html
  559. */
  560. const mode_t permission644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  561. const mode_t permissionExecute = S_IXUSR | S_IXGRP | S_IXOTH;
  562. const mode_t permission755 = permission644 | permissionExecute;
  563. // for md5sum and control (that we have generated here), we use 644
  564. // (RW-R--R--)
  565. // so that deb lintian doesn't warn about it
  566. control_tar.SetPermissions(permission644);
  567. // adds control and md5sums
  568. if( !control_tar.Add(md5filename, strGenWDIR.length(), ".")
  569. || !control_tar.Add(strGenWDIR + "/control", strGenWDIR.length(), "."))
  570. {
  571. cmCPackLogger(cmCPackLog::LOG_ERROR,
  572. "Error adding file to tar:" << std::endl
  573. << "#top level directory: "
  574. << strGenWDIR << std::endl
  575. << "#file: \"control\" or \"md5sums\"" << std::endl
  576. << "#error:" << control_tar.GetError() << std::endl);
  577. return 0;
  578. }
  579. // adds generated shlibs file
  580. if( gen_shibs )
  581. {
  582. if( !control_tar.Add(shlibsfilename, strGenWDIR.length(), ".") )
  583. {
  584. cmCPackLogger(cmCPackLog::LOG_ERROR,
  585. "Error adding file to tar:" << std::endl
  586. << "#top level directory: "
  587. << strGenWDIR << std::endl
  588. << "#file: \"shlibs\"" << std::endl
  589. << "#error:" << control_tar.GetError() << std::endl);
  590. return 0;
  591. }
  592. }
  593. // adds LDCONFIG related files
  594. if(this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST"))
  595. {
  596. control_tar.SetPermissions(permission755);
  597. if(!control_tar.Add(postinst, strGenWDIR.length(), "."))
  598. {
  599. cmCPackLogger(cmCPackLog::LOG_ERROR,
  600. "Error adding file to tar:" << std::endl
  601. << "#top level directory: "
  602. << strGenWDIR << std::endl
  603. << "#file: \"postinst\"" << std::endl
  604. << "#error:" << control_tar.GetError() << std::endl);
  605. return 0;
  606. }
  607. control_tar.SetPermissions(permission644);
  608. }
  609. if(this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM"))
  610. {
  611. control_tar.SetPermissions(permission755);
  612. if(!control_tar.Add(postrm, strGenWDIR.length(), "."))
  613. {
  614. cmCPackLogger(cmCPackLog::LOG_ERROR,
  615. "Error adding file to tar:" << std::endl
  616. << "#top level directory: "
  617. << strGenWDIR << std::endl
  618. << "#file: \"postinst\"" << std::endl
  619. << "#error:" << control_tar.GetError() << std::endl);
  620. return 0;
  621. }
  622. control_tar.SetPermissions(permission644);
  623. }
  624. // for the other files, we use
  625. // -either the original permission on the files
  626. // -either a permission strictly defined by the Debian policies
  627. const char* controlExtra =
  628. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA");
  629. if( controlExtra )
  630. {
  631. // permissions are now controlled by the original file permissions
  632. const bool permissionStrictPolicy =
  633. this->IsSet("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION");
  634. static const char* strictFiles[] = {
  635. "config", "postinst", "postrm", "preinst", "prerm"
  636. };
  637. std::set<std::string> setStrictFiles(
  638. strictFiles,
  639. strictFiles + sizeof(strictFiles)/sizeof(strictFiles[0]));
  640. // default
  641. control_tar.ClearPermissions();
  642. std::vector<std::string> controlExtraList;
  643. cmSystemTools::ExpandListArgument(controlExtra, controlExtraList);
  644. for(std::vector<std::string>::iterator i = controlExtraList.begin();
  645. i != controlExtraList.end(); ++i)
  646. {
  647. std::string filenamename =
  648. cmsys::SystemTools::GetFilenameName(*i);
  649. std::string localcopy = strGenWDIR + "/" + filenamename;
  650. if(permissionStrictPolicy)
  651. {
  652. control_tar.SetPermissions(setStrictFiles.count(filenamename) ?
  653. permission755 : permission644);
  654. }
  655. // if we can copy the file, it means it does exist, let's add it:
  656. if( cmsys::SystemTools::CopyFileIfDifferent(*i, localcopy) )
  657. {
  658. control_tar.Add(localcopy, strGenWDIR.length(), ".");
  659. }
  660. }
  661. }
  662. }
  663. // ar -r your-package-name.deb debian-binary control.tar.* data.tar.*
  664. // since debian packages require BSD ar (most Linux distros and even
  665. // FreeBSD and NetBSD ship GNU ar) we use a copy of OpenBSD ar here.
  666. std::vector<std::string> arFiles;
  667. std::string topLevelString = strGenWDIR + "/";
  668. arFiles.push_back(topLevelString + "debian-binary");
  669. arFiles.push_back(topLevelString + "control.tar.gz");
  670. arFiles.push_back(topLevelString + "data.tar" + compression_suffix);
  671. std::string outputFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  672. outputFileName += "/";
  673. outputFileName += this->GetOption("CPACK_OUTPUT_FILE_NAME");
  674. int res = ar_append(outputFileName.c_str(), arFiles);
  675. if ( res!=0 )
  676. {
  677. std::string tmpFile = this->GetOption("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. }