cmCPackDebGenerator.cxx 32 KB

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