cmCPackDebGenerator.cxx 34 KB

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