cmCPackDebGenerator.cxx 16 KB

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