cmCPackDebGenerator.cxx 32 KB

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