cmCPackDebGenerator.cxx 28 KB

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