cmCPackDebGenerator.cxx 17 KB

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