cmCPackDebGenerator.cxx 34 KB

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