cmCPackDebGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. return this->Superclass::InitializeInternal();
  38. }
  39. //----------------------------------------------------------------------
  40. int cmCPackDebGenerator::PackageFiles()
  41. {
  42. this->ReadListFile("CPackDeb.cmake");
  43. const char* cmakeExecutable = this->GetOption("CMAKE_COMMAND");
  44. // debian-binary file
  45. std::string dbfilename;
  46. dbfilename = toplevel;
  47. dbfilename += "/debian-binary";
  48. { // the scope is needed for cmGeneratedFileStream
  49. cmGeneratedFileStream out(dbfilename.c_str());
  50. out << "2.0";
  51. out << std::endl; // required for valid debian package
  52. }
  53. // control file
  54. std::string ctlfilename;
  55. ctlfilename = toplevel;
  56. ctlfilename += "/control";
  57. // debian policy enforce lower case for package name
  58. // mandatory entries:
  59. std::string debian_pkg_name = cmsys::SystemTools::LowerCase(
  60. this->GetOption("CPACK_DEBIAN_PACKAGE_NAME") );
  61. const char* debian_pkg_version =
  62. this->GetOption("CPACK_DEBIAN_PACKAGE_VERSION");
  63. const char* debian_pkg_section =
  64. this->GetOption("CPACK_DEBIAN_PACKAGE_SECTION");
  65. const char* debian_pkg_priority =
  66. this->GetOption("CPACK_DEBIAN_PACKAGE_PRIORITY");
  67. const char* debian_pkg_arch =
  68. this->GetOption("CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  69. const char* maintainer = this->GetOption("CPACK_DEBIAN_PACKAGE_MAINTAINER");
  70. const char* desc = this->GetOption("CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  71. // optional entries
  72. const char* debian_pkg_dep = this->GetOption("CPACK_DEBIAN_PACKAGE_DEPENDS");
  73. const char* debian_pkg_rec =
  74. this->GetOption("CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  75. const char* debian_pkg_sug =
  76. this->GetOption("CPACK_DEBIAN_PACKAGE_SUGGESTS");
  77. const char* debian_pkg_url =
  78. this->GetOption("CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  79. { // the scope is needed for cmGeneratedFileStream
  80. cmGeneratedFileStream out(ctlfilename.c_str());
  81. out << "Package: " << debian_pkg_name << "\n";
  82. out << "Version: " << debian_pkg_version << "\n";
  83. out << "Section: " << debian_pkg_section << "\n";
  84. out << "Priority: " << debian_pkg_priority << "\n";
  85. out << "Architecture: " << debian_pkg_arch << "\n";
  86. if(debian_pkg_dep)
  87. {
  88. out << "Depends: " << debian_pkg_dep << "\n";
  89. }
  90. if(debian_pkg_rec)
  91. {
  92. out << "Recommends: " << debian_pkg_rec << "\n";
  93. }
  94. if(debian_pkg_sug)
  95. {
  96. out << "Suggests: " << debian_pkg_sug << "\n";
  97. }
  98. if(debian_pkg_url)
  99. {
  100. out << "Homepage: " << debian_pkg_url << "\n";
  101. }
  102. unsigned long totalSize = 0;
  103. {
  104. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  105. dirName += '/';
  106. for (std::vector<std::string>::const_iterator fileIt = files.begin();
  107. fileIt != files.end(); ++ fileIt )
  108. {
  109. totalSize += cmSystemTools::FileLength(fileIt->c_str());
  110. }
  111. }
  112. out << "Installed-Size: " << (totalSize + 1023) / 1024 << "\n";
  113. out << "Maintainer: " << maintainer << "\n";
  114. out << "Description: " << desc << "\n";
  115. out << std::endl;
  116. }
  117. std::string cmd;
  118. cmd = "\"";
  119. cmd += cmakeExecutable;
  120. cmd += "\" -E tar cfz data.tar.gz ";
  121. // now add all directories which have to be compressed
  122. // collect all top level install dirs for that
  123. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would give /usr and /opt
  124. size_t topLevelLength = toplevel.length();
  125. std::set<std::string> installDirs;
  126. for (std::vector<std::string>::const_iterator fileIt = files.begin();
  127. fileIt != files.end(); ++ fileIt )
  128. {
  129. std::string::size_type slashPos = fileIt->find('/', topLevelLength+1);
  130. std::string relativeDir = fileIt->substr(topLevelLength,
  131. slashPos - topLevelLength);
  132. if (installDirs.find(relativeDir) == installDirs.end())
  133. {
  134. installDirs.insert(relativeDir);
  135. cmd += " .";
  136. cmd += relativeDir;
  137. }
  138. }
  139. std::string output;
  140. int retVal = -1;
  141. int res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  142. &retVal, toplevel.c_str(), this->GeneratorVerbose, 0);
  143. if ( !res || retVal )
  144. {
  145. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  146. tmpFile += "/Deb.log";
  147. cmGeneratedFileStream ofs(tmpFile.c_str());
  148. ofs << "# Run command: " << cmd.c_str() << std::endl
  149. << "# Working directory: " << toplevel << std::endl
  150. << "# Output:" << std::endl
  151. << output.c_str() << std::endl;
  152. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running tar command: "
  153. << cmd.c_str() << std::endl
  154. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  155. return 0;
  156. }
  157. std::string md5filename;
  158. md5filename = toplevel;
  159. md5filename += "/md5sums";
  160. { // the scope is needed for cmGeneratedFileStream
  161. cmGeneratedFileStream out(md5filename.c_str());
  162. std::vector<std::string>::const_iterator fileIt;
  163. std::string topLevelWithTrailingSlash = toplevel;
  164. topLevelWithTrailingSlash += '/';
  165. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  166. {
  167. cmd = "\"";
  168. cmd += cmakeExecutable;
  169. cmd += "\" -E md5sum \"";
  170. cmd += *fileIt;
  171. cmd += "\"";
  172. //std::string output;
  173. //int retVal = -1;
  174. res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  175. &retVal, toplevel.c_str(), this->GeneratorVerbose, 0);
  176. // debian md5sums entries are like this:
  177. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  178. // thus strip the full path (with the trailing slash)
  179. cmSystemTools::ReplaceString(output,
  180. topLevelWithTrailingSlash.c_str(), "");
  181. out << output;
  182. }
  183. // each line contains a eol.
  184. // Do not end the md5sum file with yet another (invalid)
  185. }
  186. cmd = "\"";
  187. cmd += cmakeExecutable;
  188. cmd += "\" -E tar cfz control.tar.gz ./control ./md5sums";
  189. const char* controlExtra =
  190. this->GetOption("CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA");
  191. if( controlExtra )
  192. {
  193. std::vector<std::string> controlExtraList;
  194. cmSystemTools::ExpandListArgument(controlExtra, controlExtraList);
  195. for(std::vector<std::string>::iterator i =
  196. controlExtraList.begin(); i != controlExtraList.end(); ++i)
  197. {
  198. std::string filenamename =
  199. cmsys::SystemTools::GetFilenameName(i->c_str());
  200. std::string localcopy = toplevel;
  201. localcopy += "/";
  202. localcopy += filenamename;
  203. // if we can copy the file, it means it does exist, let's add it:
  204. if( cmsys::SystemTools::CopyFileIfDifferent(
  205. i->c_str(), localcopy.c_str()) )
  206. {
  207. // debian is picky and need relative to ./ path in the tar.gz
  208. cmd += " ./";
  209. cmd += filenamename;
  210. }
  211. }
  212. }
  213. res = cmSystemTools::RunSingleCommand(cmd.c_str(), &output,
  214. &retVal, toplevel.c_str(), this->GeneratorVerbose, 0);
  215. if ( !res || retVal )
  216. {
  217. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  218. tmpFile += "/Deb.log";
  219. cmGeneratedFileStream ofs(tmpFile.c_str());
  220. ofs << "# Run command: " << cmd.c_str() << std::endl
  221. << "# Working directory: " << toplevel << std::endl
  222. << "# Output:" << std::endl
  223. << output.c_str() << std::endl;
  224. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running tar command: "
  225. << cmd.c_str() << std::endl
  226. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  227. return 0;
  228. }
  229. // ar -r your-package-name.deb debian-binary control.tar.gz data.tar.gz
  230. // since debian packages require BSD ar (most Linux distros and even
  231. // FreeBSD and NetBSD ship GNU ar) we use a copy of OpenBSD ar here.
  232. std::vector<std::string> arFiles;
  233. std::string topLevelString = toplevel;
  234. topLevelString += "/";
  235. arFiles.push_back(topLevelString + "debian-binary");
  236. arFiles.push_back(topLevelString + "control.tar.gz");
  237. arFiles.push_back(topLevelString + "data.tar.gz");
  238. res = ar_append(packageFileNames[0].c_str(), arFiles);
  239. if ( res!=0 )
  240. {
  241. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  242. tmpFile += "/Deb.log";
  243. cmGeneratedFileStream ofs(tmpFile.c_str());
  244. ofs << "# Problem creating archive using: " << res << std::endl;
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. // The following code is taken from OpenBSD ar:
  250. // http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ar/
  251. // It has been slightly modified:
  252. // -return error codes instead exit() in functions
  253. // -use the stdio file I/O functions instead the file descriptor based ones
  254. // -merged into one cxx file
  255. // -no additional options supported
  256. // The coding style hasn't been modified.
  257. /*-
  258. * Copyright (c) 1990, 1993, 1994
  259. * The Regents of the University of California. All rights reserved.
  260. *
  261. * This code is derived from software contributed to Berkeley by
  262. * Hugh Smith at The University of Guelph.
  263. *
  264. * Redistribution and use in source and binary forms, with or without
  265. * modification, are permitted provided that the following conditions
  266. * are met:
  267. * 1. Redistributions of source code must retain the above copyright
  268. * notice, this list of conditions and the following disclaimer.
  269. * 2. Redistributions in binary form must reproduce the above copyright
  270. * notice, this list of conditions and the following disclaimer in the
  271. * documentation and/or other materials provided with the distribution.
  272. * 3. Neither the name of the University nor the names of its contributors
  273. * may be used to endorse or promote products derived from this software
  274. * without specific prior written permission.
  275. *
  276. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  277. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  278. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  279. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  280. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  281. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  282. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  283. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  284. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  285. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  286. * SUCH DAMAGE.
  287. */
  288. #include <sys/types.h>
  289. #include <sys/stat.h>
  290. #include <stdio.h>
  291. #include <string.h>
  292. #include <stdlib.h>
  293. #define ARMAG "!<arch>\n" /* ar "magic number" */
  294. #define SARMAG 8 /* strlen(ARMAG); */
  295. #define AR_EFMT1 "#1/" /* extended format #1 */
  296. #define ARFMAG "`\n"
  297. /* Header format strings. */
  298. #define HDR1 "%s%-13d%-12ld%-6u%-6u%-8o%-10lld%2s"
  299. #define HDR2 "%-16.16s%-12ld%-6u%-6u%-8o%-10lld%2s"
  300. struct ar_hdr {
  301. char ar_name[16]; /* name */
  302. char ar_date[12]; /* modification time */
  303. char ar_uid[6]; /* user id */
  304. char ar_gid[6]; /* group id */
  305. char ar_mode[8]; /* octal file permissions */
  306. char ar_size[10]; /* size in bytes */
  307. char ar_fmag[2]; /* consistency check */
  308. };
  309. /* Set up file copy. */
  310. #define SETCF(from, fromname, to, toname, pad) { \
  311. cf.rFile = from; \
  312. cf.rname = fromname; \
  313. cf.wFile = to; \
  314. cf.wname = toname; \
  315. cf.flags = pad; \
  316. }
  317. /* File copy structure. */
  318. typedef struct {
  319. FILE* rFile; /* read file descriptor */
  320. const char *rname; /* read name */
  321. FILE* wFile; /* write file descriptor */
  322. const char *wname; /* write name */
  323. #define NOPAD 0x00 /* don't pad */
  324. #define WPAD 0x02 /* pad on writes */
  325. unsigned int flags; /* pad flags */
  326. } CF;
  327. /* misc.c */
  328. static const char * ar_rname(const char *path)
  329. {
  330. const char *ind = strrchr(path, '/');
  331. return (ind ) ? ind + 1 : path;
  332. }
  333. /* archive.c */
  334. typedef struct ar_hdr HDR;
  335. static char ar_hb[sizeof(HDR) + 1]; /* real header */
  336. static size_t ar_already_written;
  337. /* copy_ar --
  338. * Copy size bytes from one file to another - taking care to handle the
  339. * extra byte (for odd size files) when reading archives and writing an
  340. * extra byte if necessary when adding files to archive. The length of
  341. * the object is the long name plus the object itself; the variable
  342. * already_written gets set if a long name was written.
  343. *
  344. * The padding is really unnecessary, and is almost certainly a remnant
  345. * of early archive formats where the header included binary data which
  346. * a PDP-11 required to start on an even byte boundary. (Or, perhaps,
  347. * because 16-bit word addressed copies were faster?) Anyhow, it should
  348. * have been ripped out long ago.
  349. */
  350. static int copy_ar(CF *cfp, off_t size)
  351. {
  352. static char pad = '\n';
  353. off_t sz = size;
  354. size_t nr, nw;
  355. char buf[8*1024];
  356. if (sz == 0)
  357. return 0;
  358. FILE* from = cfp->rFile;
  359. FILE* to = cfp->wFile;
  360. while (sz &&
  361. (nr = fread(buf, 1, sz < static_cast<off_t>(sizeof(buf))
  362. ? static_cast<size_t>(sz) : sizeof(buf), from ))
  363. > 0) {
  364. sz -= nr;
  365. for (size_t off = 0; off < nr; nr -= off, off += nw)
  366. if ((nw = fwrite(buf + off, 1, nr, to)) < nr)
  367. return -1;
  368. }
  369. if (sz)
  370. return -2;
  371. if (cfp->flags & WPAD && (size + ar_already_written) & 1
  372. && fwrite(&pad, 1, 1, to) != 1)
  373. return -4;
  374. return 0;
  375. }
  376. /* put_arobj -- Write an archive member to a file. */
  377. static int put_arobj(CF *cfp, struct stat *sb)
  378. {
  379. int result = 0;
  380. struct ar_hdr *hdr;
  381. /* If passed an sb structure, reading a file from disk. Get stat(2)
  382. * information, build a name and construct a header. (Files are named
  383. * by their last component in the archive.) */
  384. const char* name = ar_rname(cfp->rname);
  385. (void)stat(cfp->rname, sb);
  386. /* If not truncating names and the name is too long or contains
  387. * a space, use extended format 1. */
  388. size_t lname = strlen(name);
  389. uid_t uid = sb->st_uid;
  390. gid_t gid = sb->st_gid;
  391. if (uid > USHRT_MAX) {
  392. uid = USHRT_MAX;
  393. }
  394. if (gid > USHRT_MAX) {
  395. gid = USHRT_MAX;
  396. }
  397. if (lname > sizeof(hdr->ar_name) || strchr(name, ' '))
  398. (void)sprintf(ar_hb, HDR1, AR_EFMT1, (int)lname,
  399. (long int)sb->st_mtime, uid, gid, sb->st_mode,
  400. (long long)sb->st_size + lname, ARFMAG);
  401. else {
  402. lname = 0;
  403. (void)sprintf(ar_hb, HDR2, name,
  404. (long int)sb->st_mtime, uid, gid, sb->st_mode,
  405. (long long)sb->st_size, ARFMAG);
  406. }
  407. off_t size = sb->st_size;
  408. if (fwrite(ar_hb, 1, sizeof(HDR), cfp->wFile) != sizeof(HDR))
  409. return -1;
  410. if (lname) {
  411. if (fwrite(name, 1, lname, cfp->wFile) != lname)
  412. return -2;
  413. ar_already_written = lname;
  414. }
  415. result = copy_ar(cfp, size);
  416. ar_already_written = 0;
  417. return result;
  418. }
  419. /* append.c */
  420. /* append --
  421. * Append files to the archive - modifies original archive or creates
  422. * a new archive if named archive does not exist.
  423. */
  424. static int ar_append(const char* archive,const std::vector<std::string>& files)
  425. {
  426. int eval = 0;
  427. FILE* aFile = fopen(archive, "wb+");
  428. if (aFile!=NULL) {
  429. fwrite(ARMAG, SARMAG, 1, aFile);
  430. if (fseek(aFile, 0, SEEK_END) != -1) {
  431. CF cf;
  432. struct stat sb;
  433. /* Read from disk, write to an archive; pad on write. */
  434. SETCF(NULL, 0, aFile, archive, WPAD);
  435. for(std::vector<std::string>::const_iterator fileIt = files.begin();
  436. fileIt!=files.end(); ++fileIt) {
  437. const char* filename = fileIt->c_str();
  438. FILE* file = fopen(filename, "rb");
  439. if (file == NULL) {
  440. eval = -1;
  441. continue;
  442. }
  443. cf.rFile = file;
  444. cf.rname = filename;
  445. int result = put_arobj(&cf, &sb);
  446. (void)fclose(file);
  447. if (result!=0) {
  448. eval = -2;
  449. break;
  450. }
  451. }
  452. }
  453. else {
  454. eval = -3;
  455. }
  456. fclose(aFile);
  457. }
  458. else {
  459. eval = -4;
  460. }
  461. return eval;
  462. }