cmCPackDebGenerator.cxx 18 KB

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