cmCPackDebGenerator.cxx 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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(
  50. std::string(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 == CM_NULLPTR) {
  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(
  159. std::string(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. return PackageComponents(componentPackageMethod ==
  222. ONE_PACKAGE_PER_COMPONENT);
  223. }
  224. // CASE 3 : NON COMPONENT package.
  225. return PackageComponentsAllInOne("");
  226. }
  227. int cmCPackDebGenerator::createDeb()
  228. {
  229. // debian-binary file
  230. const std::string strGenWDIR(this->GetOption("GEN_WDIR"));
  231. const std::string dbfilename = strGenWDIR + "/debian-binary";
  232. { // the scope is needed for cmGeneratedFileStream
  233. cmGeneratedFileStream out(dbfilename.c_str());
  234. out << "2.0";
  235. out << std::endl; // required for valid debian package
  236. }
  237. // control file
  238. std::string ctlfilename = strGenWDIR + "/control";
  239. // debian policy enforce lower case for package name
  240. // mandatory entries:
  241. std::string debian_pkg_name = cmsys::SystemTools::LowerCase(
  242. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME"));
  243. const char* debian_pkg_version =
  244. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_VERSION");
  245. const char* debian_pkg_section =
  246. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SECTION");
  247. const char* debian_pkg_priority =
  248. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PRIORITY");
  249. const char* debian_pkg_arch =
  250. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  251. const char* maintainer =
  252. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MAINTAINER");
  253. const char* desc = this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  254. // optional entries
  255. const char* debian_pkg_dep =
  256. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DEPENDS");
  257. const char* debian_pkg_rec =
  258. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  259. const char* debian_pkg_sug =
  260. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SUGGESTS");
  261. const char* debian_pkg_url =
  262. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  263. const char* debian_pkg_predep =
  264. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PREDEPENDS");
  265. const char* debian_pkg_enhances =
  266. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ENHANCES");
  267. const char* debian_pkg_breaks =
  268. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_BREAKS");
  269. const char* debian_pkg_conflicts =
  270. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONFLICTS");
  271. const char* debian_pkg_provides =
  272. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PROVIDES");
  273. const char* debian_pkg_replaces =
  274. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_REPLACES");
  275. const char* debian_pkg_source =
  276. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SOURCE");
  277. { // the scope is needed for cmGeneratedFileStream
  278. cmGeneratedFileStream out(ctlfilename.c_str());
  279. out << "Package: " << debian_pkg_name << "\n";
  280. out << "Version: " << debian_pkg_version << "\n";
  281. out << "Section: " << debian_pkg_section << "\n";
  282. out << "Priority: " << debian_pkg_priority << "\n";
  283. out << "Architecture: " << debian_pkg_arch << "\n";
  284. if (debian_pkg_source && *debian_pkg_source) {
  285. out << "Source: " << debian_pkg_source << "\n";
  286. }
  287. if (debian_pkg_dep && *debian_pkg_dep) {
  288. out << "Depends: " << debian_pkg_dep << "\n";
  289. }
  290. if (debian_pkg_rec && *debian_pkg_rec) {
  291. out << "Recommends: " << debian_pkg_rec << "\n";
  292. }
  293. if (debian_pkg_sug && *debian_pkg_sug) {
  294. out << "Suggests: " << debian_pkg_sug << "\n";
  295. }
  296. if (debian_pkg_url && *debian_pkg_url) {
  297. out << "Homepage: " << debian_pkg_url << "\n";
  298. }
  299. if (debian_pkg_predep && *debian_pkg_predep) {
  300. out << "Pre-Depends: " << debian_pkg_predep << "\n";
  301. }
  302. if (debian_pkg_enhances && *debian_pkg_enhances) {
  303. out << "Enhances: " << debian_pkg_enhances << "\n";
  304. }
  305. if (debian_pkg_breaks && *debian_pkg_breaks) {
  306. out << "Breaks: " << debian_pkg_breaks << "\n";
  307. }
  308. if (debian_pkg_conflicts && *debian_pkg_conflicts) {
  309. out << "Conflicts: " << debian_pkg_conflicts << "\n";
  310. }
  311. if (debian_pkg_provides && *debian_pkg_provides) {
  312. out << "Provides: " << debian_pkg_provides << "\n";
  313. }
  314. if (debian_pkg_replaces && *debian_pkg_replaces) {
  315. out << "Replaces: " << debian_pkg_replaces << "\n";
  316. }
  317. unsigned long totalSize = 0;
  318. {
  319. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  320. dirName += '/';
  321. for (std::vector<std::string>::const_iterator fileIt =
  322. packageFiles.begin();
  323. fileIt != packageFiles.end(); ++fileIt) {
  324. totalSize += cmSystemTools::FileLength(*fileIt);
  325. }
  326. }
  327. out << "Installed-Size: " << (totalSize + 1023) / 1024 << "\n";
  328. out << "Maintainer: " << maintainer << "\n";
  329. out << "Description: " << desc << "\n";
  330. out << std::endl;
  331. }
  332. const std::string shlibsfilename = strGenWDIR + "/shlibs";
  333. const char* debian_pkg_shlibs =
  334. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SHLIBS");
  335. const bool gen_shibs = this->IsOn("CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS") &&
  336. debian_pkg_shlibs && *debian_pkg_shlibs;
  337. if (gen_shibs) {
  338. cmGeneratedFileStream out(shlibsfilename.c_str());
  339. out << debian_pkg_shlibs;
  340. out << std::endl;
  341. }
  342. const std::string postinst = strGenWDIR + "/postinst";
  343. const std::string postrm = strGenWDIR + "/postrm";
  344. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST")) {
  345. cmGeneratedFileStream out(postinst.c_str());
  346. out << "#!/bin/sh\n\n"
  347. "set -e\n\n"
  348. "if [ \"$1\" = \"configure\" ]; then\n"
  349. "\tldconfig\n"
  350. "fi\n";
  351. }
  352. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM")) {
  353. cmGeneratedFileStream out(postrm.c_str());
  354. out << "#!/bin/sh\n\n"
  355. "set -e\n\n"
  356. "if [ \"$1\" = \"remove\" ]; then\n"
  357. "\tldconfig\n"
  358. "fi\n";
  359. }
  360. cmArchiveWrite::Compress tar_compression_type = cmArchiveWrite::CompressGZip;
  361. const char* debian_compression_type =
  362. this->GetOption("GEN_CPACK_DEBIAN_COMPRESSION_TYPE");
  363. if (!debian_compression_type) {
  364. debian_compression_type = "gzip";
  365. }
  366. std::string compression_suffix;
  367. if (!strcmp(debian_compression_type, "lzma")) {
  368. compression_suffix = ".lzma";
  369. tar_compression_type = cmArchiveWrite::CompressLZMA;
  370. } else if (!strcmp(debian_compression_type, "xz")) {
  371. compression_suffix = ".xz";
  372. tar_compression_type = cmArchiveWrite::CompressXZ;
  373. } else if (!strcmp(debian_compression_type, "bzip2")) {
  374. compression_suffix = ".bz2";
  375. tar_compression_type = cmArchiveWrite::CompressBZip2;
  376. } else if (!strcmp(debian_compression_type, "gzip")) {
  377. compression_suffix = ".gz";
  378. tar_compression_type = cmArchiveWrite::CompressGZip;
  379. } else if (!strcmp(debian_compression_type, "none")) {
  380. compression_suffix = "";
  381. tar_compression_type = cmArchiveWrite::CompressNone;
  382. } else {
  383. cmCPackLogger(cmCPackLog::LOG_ERROR,
  384. "Error unrecognized compression type: "
  385. << debian_compression_type << std::endl);
  386. }
  387. std::string filename_data_tar =
  388. strGenWDIR + "/data.tar" + compression_suffix;
  389. // atomic file generation for data.tar
  390. {
  391. cmGeneratedFileStream fileStream_data_tar;
  392. fileStream_data_tar.Open(filename_data_tar.c_str(), false, true);
  393. if (!fileStream_data_tar) {
  394. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error opening the file \""
  395. << filename_data_tar << "\" for writing" << std::endl);
  396. return 0;
  397. }
  398. cmArchiveWrite data_tar(fileStream_data_tar, tar_compression_type, "paxr");
  399. // uid/gid should be the one of the root user, and this root user has
  400. // always uid/gid equal to 0.
  401. data_tar.SetUIDAndGID(0u, 0u);
  402. data_tar.SetUNAMEAndGNAME("root", "root");
  403. // now add all directories which have to be compressed
  404. // collect all top level install dirs for that
  405. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would
  406. // give /usr and /opt
  407. size_t topLevelLength = strGenWDIR.length();
  408. cmCPackLogger(cmCPackLog::LOG_DEBUG, "WDIR: \""
  409. << strGenWDIR << "\", length = " << topLevelLength
  410. << std::endl);
  411. std::set<std::string> orderedFiles;
  412. // we have to reconstruct the parent folders as well
  413. for (std::vector<std::string>::const_iterator fileIt =
  414. packageFiles.begin();
  415. fileIt != packageFiles.end(); ++fileIt) {
  416. std::string currentPath = *fileIt;
  417. while (currentPath != strGenWDIR) {
  418. // the last one IS strGenWDIR, but we do not want this one:
  419. // XXX/application/usr/bin/myprogram with GEN_WDIR=XXX/application
  420. // should not add XXX/application
  421. orderedFiles.insert(currentPath);
  422. currentPath = cmSystemTools::CollapseCombinedPath(currentPath, "..");
  423. }
  424. }
  425. for (std::set<std::string>::const_iterator fileIt = orderedFiles.begin();
  426. fileIt != orderedFiles.end(); ++fileIt) {
  427. cmCPackLogger(cmCPackLog::LOG_DEBUG, "FILEIT: \"" << *fileIt << "\""
  428. << std::endl);
  429. std::string::size_type slashPos = fileIt->find('/', topLevelLength + 1);
  430. std::string relativeDir =
  431. fileIt->substr(topLevelLength, slashPos - topLevelLength);
  432. cmCPackLogger(cmCPackLog::LOG_DEBUG, "RELATIVEDIR: \""
  433. << relativeDir << "\"" << std::endl);
  434. // do not recurse because the loop will do it
  435. if (!data_tar.Add(*fileIt, topLevelLength, ".", false)) {
  436. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem adding file to tar:"
  437. << std::endl
  438. << "#top level directory: " << strGenWDIR << std::endl
  439. << "#file: " << *fileIt << std::endl
  440. << "#error:" << data_tar.GetError() << std::endl);
  441. return 0;
  442. }
  443. }
  444. } // scope for file generation
  445. std::string md5filename = strGenWDIR + "/md5sums";
  446. {
  447. // the scope is needed for cmGeneratedFileStream
  448. cmGeneratedFileStream out(md5filename.c_str());
  449. std::string topLevelWithTrailingSlash =
  450. this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  451. topLevelWithTrailingSlash += '/';
  452. for (std::vector<std::string>::const_iterator fileIt =
  453. packageFiles.begin();
  454. fileIt != packageFiles.end(); ++fileIt) {
  455. // hash only regular files
  456. if (cmSystemTools::FileIsDirectory(*fileIt) ||
  457. cmSystemTools::FileIsSymlink(*fileIt)) {
  458. continue;
  459. }
  460. char md5sum[33];
  461. if (!cmSystemTools::ComputeFileMD5(*fileIt, md5sum)) {
  462. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem computing the md5 of "
  463. << *fileIt << std::endl);
  464. }
  465. md5sum[32] = 0;
  466. std::string output(md5sum);
  467. output += " " + *fileIt + "\n";
  468. // debian md5sums entries are like this:
  469. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  470. // thus strip the full path (with the trailing slash)
  471. cmSystemTools::ReplaceString(output, topLevelWithTrailingSlash.c_str(),
  472. "");
  473. out << output;
  474. }
  475. // each line contains a eol.
  476. // Do not end the md5sum file with yet another (invalid)
  477. }
  478. std::string filename_control_tar = strGenWDIR + "/control.tar.gz";
  479. // atomic file generation for control.tar
  480. {
  481. cmGeneratedFileStream fileStream_control_tar;
  482. fileStream_control_tar.Open(filename_control_tar.c_str(), false, true);
  483. if (!fileStream_control_tar) {
  484. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error opening the file \""
  485. << filename_control_tar << "\" for writing"
  486. << std::endl);
  487. return 0;
  488. }
  489. cmArchiveWrite control_tar(fileStream_control_tar,
  490. cmArchiveWrite::CompressGZip, "paxr");
  491. // sets permissions and uid/gid for the files
  492. control_tar.SetUIDAndGID(0u, 0u);
  493. control_tar.SetUNAMEAndGNAME("root", "root");
  494. /* permissions are set according to
  495. https://www.debian.org/doc/debian-policy/ch-files.html#s-permissions-owners
  496. and
  497. https://lintian.debian.org/tags/control-file-has-bad-permissions.html
  498. */
  499. const mode_t permission644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  500. const mode_t permissionExecute = S_IXUSR | S_IXGRP | S_IXOTH;
  501. const mode_t permission755 = permission644 | permissionExecute;
  502. // for md5sum and control (that we have generated here), we use 644
  503. // (RW-R--R--)
  504. // so that deb lintian doesn't warn about it
  505. control_tar.SetPermissions(permission644);
  506. // adds control and md5sums
  507. if (!control_tar.Add(md5filename, strGenWDIR.length(), ".") ||
  508. !control_tar.Add(strGenWDIR + "/control", strGenWDIR.length(), ".")) {
  509. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding file to tar:"
  510. << std::endl
  511. << "#top level directory: " << strGenWDIR << std::endl
  512. << "#file: \"control\" or \"md5sums\"" << std::endl
  513. << "#error:" << control_tar.GetError() << std::endl);
  514. return 0;
  515. }
  516. // adds generated shlibs file
  517. if (gen_shibs) {
  518. if (!control_tar.Add(shlibsfilename, strGenWDIR.length(), ".")) {
  519. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding file to tar:"
  520. << std::endl
  521. << "#top level directory: " << strGenWDIR << std::endl
  522. << "#file: \"shlibs\"" << std::endl
  523. << "#error:" << control_tar.GetError() << std::endl);
  524. return 0;
  525. }
  526. }
  527. // adds LDCONFIG related files
  528. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST")) {
  529. control_tar.SetPermissions(permission755);
  530. if (!control_tar.Add(postinst, strGenWDIR.length(), ".")) {
  531. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding file to tar:"
  532. << std::endl
  533. << "#top level directory: " << strGenWDIR << std::endl
  534. << "#file: \"postinst\"" << std::endl
  535. << "#error:" << control_tar.GetError() << std::endl);
  536. return 0;
  537. }
  538. control_tar.SetPermissions(permission644);
  539. }
  540. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM")) {
  541. control_tar.SetPermissions(permission755);
  542. if (!control_tar.Add(postrm, strGenWDIR.length(), ".")) {
  543. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding file to tar:"
  544. << std::endl
  545. << "#top level directory: " << strGenWDIR << std::endl
  546. << "#file: \"postinst\"" << std::endl
  547. << "#error:" << control_tar.GetError() << std::endl);
  548. return 0;
  549. }
  550. control_tar.SetPermissions(permission644);
  551. }
  552. // for the other files, we use
  553. // -either the original permission on the files
  554. // -either a permission strictly defined by the Debian policies
  555. const char* controlExtra =
  556. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA");
  557. if (controlExtra) {
  558. // permissions are now controlled by the original file permissions
  559. const bool permissionStrictPolicy =
  560. this->IsSet("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION");
  561. static const char* strictFiles[] = { "config", "postinst", "postrm",
  562. "preinst", "prerm" };
  563. std::set<std::string> setStrictFiles(
  564. strictFiles,
  565. strictFiles + sizeof(strictFiles) / sizeof(strictFiles[0]));
  566. // default
  567. control_tar.ClearPermissions();
  568. std::vector<std::string> controlExtraList;
  569. cmSystemTools::ExpandListArgument(controlExtra, controlExtraList);
  570. for (std::vector<std::string>::iterator i = controlExtraList.begin();
  571. i != controlExtraList.end(); ++i) {
  572. std::string filenamename = cmsys::SystemTools::GetFilenameName(*i);
  573. std::string localcopy = strGenWDIR + "/" + filenamename;
  574. if (permissionStrictPolicy) {
  575. control_tar.SetPermissions(setStrictFiles.count(filenamename)
  576. ? permission755
  577. : permission644);
  578. }
  579. // if we can copy the file, it means it does exist, let's add it:
  580. if (cmsys::SystemTools::CopyFileIfDifferent(*i, localcopy)) {
  581. control_tar.Add(localcopy, strGenWDIR.length(), ".");
  582. }
  583. }
  584. }
  585. }
  586. // ar -r your-package-name.deb debian-binary control.tar.* data.tar.*
  587. // since debian packages require BSD ar (most Linux distros and even
  588. // FreeBSD and NetBSD ship GNU ar) we use a copy of OpenBSD ar here.
  589. std::vector<std::string> arFiles;
  590. std::string topLevelString = strGenWDIR + "/";
  591. arFiles.push_back(topLevelString + "debian-binary");
  592. arFiles.push_back(topLevelString + "control.tar.gz");
  593. arFiles.push_back(topLevelString + "data.tar" + compression_suffix);
  594. std::string outputFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  595. outputFileName += "/";
  596. outputFileName += this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  597. int res = ar_append(outputFileName.c_str(), arFiles);
  598. if (res != 0) {
  599. std::string tmpFile =
  600. this->GetOption("GEN_CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  601. tmpFile += "/Deb.log";
  602. cmGeneratedFileStream ofs(tmpFile.c_str());
  603. ofs << "# Problem creating archive using: " << res << std::endl;
  604. return 0;
  605. }
  606. return 1;
  607. }
  608. bool cmCPackDebGenerator::SupportsComponentInstallation() const
  609. {
  610. return IsOn("CPACK_DEB_COMPONENT_INSTALL");
  611. }
  612. std::string cmCPackDebGenerator::GetComponentInstallDirNameSuffix(
  613. const std::string& componentName)
  614. {
  615. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  616. return componentName;
  617. }
  618. if (componentPackageMethod == ONE_PACKAGE) {
  619. return std::string("ALL_COMPONENTS_IN_ONE");
  620. }
  621. // We have to find the name of the COMPONENT GROUP
  622. // the current COMPONENT belongs to.
  623. std::string groupVar =
  624. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  625. if (CM_NULLPTR != GetOption(groupVar)) {
  626. return std::string(GetOption(groupVar));
  627. }
  628. return componentName;
  629. }
  630. // The following code is taken from OpenBSD ar:
  631. // http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ar/
  632. // It has been slightly modified:
  633. // -return error codes instead exit() in functions
  634. // -use the stdio file I/O functions instead the file descriptor based ones
  635. // -merged into one cxx file
  636. // -no additional options supported
  637. // The coding style hasn't been modified.
  638. /*-
  639. * Copyright (c) 1990, 1993, 1994
  640. * The Regents of the University of California. All rights reserved.
  641. *
  642. * This code is derived from software contributed to Berkeley by
  643. * Hugh Smith at The University of Guelph.
  644. *
  645. * Redistribution and use in source and binary forms, with or without
  646. * modification, are permitted provided that the following conditions
  647. * are met:
  648. * 1. Redistributions of source code must retain the above copyright
  649. * notice, this list of conditions and the following disclaimer.
  650. * 2. Redistributions in binary form must reproduce the above copyright
  651. * notice, this list of conditions and the following disclaimer in the
  652. * documentation and/or other materials provided with the distribution.
  653. * 3. Neither the name of the University nor the names of its contributors
  654. * may be used to endorse or promote products derived from this software
  655. * without specific prior written permission.
  656. *
  657. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  658. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  659. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  660. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  661. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  662. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  663. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  664. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  665. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  666. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  667. * SUCH DAMAGE.
  668. */
  669. #include <sys/types.h>
  670. // include sys/stat.h after sys/types.h
  671. #include <sys/stat.h>
  672. #include <stdio.h>
  673. #include <stdlib.h>
  674. #include <string.h>
  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. }