main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * main.c - main program
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <assert.h>
  26. #include <limits.h>
  27. #if HAVE_LOCALE_H
  28. #include <locale.h>
  29. #endif
  30. #include <errno.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <dirent.h>
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/macros.h>
  38. #include <dpkg/i18n.h>
  39. #include <dpkg/dpkg.h>
  40. #include <dpkg/dpkg-db.h>
  41. #include <dpkg/compress.h>
  42. #include <dpkg/options.h>
  43. #include "dpkg-deb.h"
  44. const char *showformat = "${Package}\t${Version}\n";
  45. static void DPKG_ATTR_NORET
  46. printversion(const struct cmdinfo *cip, const char *value)
  47. {
  48. printf(_("Debian `%s' package archive backend version %s.\n"),
  49. BACKEND, DPKG_VERSION_ARCH);
  50. printf(_(
  51. "This is free software; see the GNU General Public License version 2 or\n"
  52. "later for copying conditions. There is NO warranty.\n"));
  53. m_output(stdout, _("<standard output>"));
  54. exit(0);
  55. }
  56. static void DPKG_ATTR_NORET
  57. usage(const struct cmdinfo *cip, const char *value)
  58. {
  59. printf(_(
  60. "Usage: %s [<option> ...] <command>\n"
  61. "\n"), BACKEND);
  62. printf(_(
  63. "Commands:\n"
  64. " -b|--build <directory> [<deb>] Build an archive.\n"
  65. " -c|--contents <deb> List contents.\n"
  66. " -I|--info <deb> [<cfile> ...] Show info to stdout.\n"
  67. " -W|--show <deb> Show information on package(s)\n"
  68. " -f|--field <deb> [<cfield> ...] Show field(s) to stdout.\n"
  69. " -e|--control <deb> [<directory>] Extract control info.\n"
  70. " -x|--extract <deb> <directory> Extract files.\n"
  71. " -X|--vextract <deb> <directory> Extract & list files.\n"
  72. " -R|--raw-extract <deb> <directory>\n"
  73. " Extract control info and files.\n"
  74. " --ctrl-tarfile <deb> Output control tarfile.\n"
  75. " --fsys-tarfile <deb> Output filesystem tarfile.\n"
  76. "\n"));
  77. printf(_(
  78. " -?, --help Show this help message.\n"
  79. " --version Show the version.\n"
  80. "\n"));
  81. printf(_(
  82. "<deb> is the filename of a Debian format archive.\n"
  83. "<cfile> is the name of an administrative file component.\n"
  84. "<cfield> is the name of a field in the main `control' file.\n"
  85. "\n"));
  86. printf(_(
  87. "Options:\n"
  88. " -v, --verbose Enable verbose output.\n"
  89. " -D, --debug Enable debugging output.\n"
  90. " --showformat=<format> Use alternative format for --show.\n"
  91. " --deb-format=<format> Select archive format.\n"
  92. " Allowed values: 0.939000, 2.0 (default).\n"
  93. " --old Legacy alias for '--deb-format=0.939000'.\n"
  94. " --new Legacy alias for '--deb-format=2.0'.\n"
  95. " --nocheck Suppress control file check (build bad\n"
  96. " packages).\n"
  97. " --uniform-compression Use the compression params on all members.\n"
  98. " -z# Set the compression level when building.\n"
  99. " -Z<type> Set the compression type used when building.\n"
  100. " Allowed types: gzip, xz, bzip2, none.\n"
  101. " -S<strategy> Set the compression strategy when building.\n"
  102. " Allowed values: none; extreme (xz);\n"
  103. " filtered, huffman, rle, fixed (gzip).\n"
  104. "\n"));
  105. printf(_(
  106. "Format syntax:\n"
  107. " A format is a string that will be output for each package. The format\n"
  108. " can include the standard escape sequences \\n (newline), \\r (carriage\n"
  109. " return) or \\\\ (plain backslash). Package information can be included\n"
  110. " by inserting variable references to package fields using the ${var[;width]}\n"
  111. " syntax. Fields will be right-aligned unless the width is negative in which\n"
  112. " case left alignment will be used.\n"));
  113. printf(_(
  114. "\n"
  115. "Use 'dpkg' to install and remove packages from your system, or\n"
  116. "'apt' or 'aptitude' for user-friendly package management. Packages\n"
  117. "unpacked using 'dpkg-deb --extract' will be incorrectly installed !\n"));
  118. m_output(stdout, _("<standard output>"));
  119. exit(0);
  120. }
  121. static const char printforhelp[] =
  122. N_("Type dpkg-deb --help for help about manipulating *.deb files;\n"
  123. "Type dpkg --help for help about installing and deinstalling packages.");
  124. int debugflag = 0;
  125. int nocheckflag = 0;
  126. int opt_verbose = 0;
  127. int opt_uniform_compression = 0;
  128. struct deb_version deb_format = DEB_VERSION(2, 0);
  129. static void
  130. set_deb_format(const struct cmdinfo *cip, const char *value)
  131. {
  132. const char *err;
  133. err = deb_version_parse(&deb_format, value);
  134. if (err)
  135. badusage(_("invalid deb format version: %s"), err);
  136. if ((deb_format.major == 2 && deb_format.minor == 0) ||
  137. (deb_format.major == 0 && deb_format.minor == 939000))
  138. return;
  139. else
  140. badusage(_("unknown deb format version: %s"), value);
  141. }
  142. static void
  143. set_deb_old(const struct cmdinfo *cip, const char *value)
  144. {
  145. deb_format = DEB_VERSION_OBJECT(0, 939000);
  146. warning(_("obsolete option '--%s'; please use '--%s' instead"),
  147. cip->olong, "deb-format=0.939000");
  148. }
  149. static void
  150. set_deb_new(const struct cmdinfo *cip, const char *value)
  151. {
  152. deb_format = DEB_VERSION_OBJECT(2, 0);
  153. warning(_("obsolete option '--%s'; please use '--%s' instead"),
  154. cip->olong, "deb-format=2.0");
  155. }
  156. struct compress_params compress_params = {
  157. .type = DPKG_DEB_DEFAULT_COMPRESSOR,
  158. .strategy = COMPRESSOR_STRATEGY_NONE,
  159. .level = -1,
  160. };
  161. static void
  162. set_compress_level(const struct cmdinfo *cip, const char *value)
  163. {
  164. long level;
  165. level = dpkg_options_parse_arg_int(cip, value);
  166. if (level < 0 || level > 9)
  167. badusage(_("invalid compression level for -%c: %ld"), cip->oshort, level);
  168. compress_params.level = level;
  169. }
  170. static void
  171. set_compress_strategy(const struct cmdinfo *cip, const char *value)
  172. {
  173. compress_params.strategy = compressor_get_strategy(value);
  174. if (compress_params.strategy == COMPRESSOR_STRATEGY_UNKNOWN)
  175. ohshit(_("unknown compression strategy '%s'!"), value);
  176. }
  177. static void
  178. set_compress_type(const struct cmdinfo *cip, const char *value)
  179. {
  180. compress_params.type = compressor_find_by_name(value);
  181. if (compress_params.type == COMPRESSOR_TYPE_UNKNOWN)
  182. ohshit(_("unknown compression type `%s'!"), value);
  183. if (compress_params.type == COMPRESSOR_TYPE_LZMA)
  184. warning(_("deprecated compression type '%s'; use xz instead"), value);
  185. if (compress_params.type == COMPRESSOR_TYPE_BZIP2)
  186. warning(_("deprecated compression type '%s'; use xz or gzip instead"), value);
  187. }
  188. static const struct cmdinfo cmdinfos[]= {
  189. ACTION("build", 'b', 0, do_build),
  190. ACTION("contents", 'c', 0, do_contents),
  191. ACTION("control", 'e', 0, do_control),
  192. ACTION("info", 'I', 0, do_info),
  193. ACTION("field", 'f', 0, do_field),
  194. ACTION("extract", 'x', 0, do_extract),
  195. ACTION("vextract", 'X', 0, do_vextract),
  196. ACTION("raw-extract", 'R', 0, do_raw_extract),
  197. ACTION("ctrl-tarfile", 0, 0, do_ctrltarfile),
  198. ACTION("fsys-tarfile", 0, 0, do_fsystarfile),
  199. ACTION("show", 'W', 0, do_showinfo),
  200. { "deb-format", 0, 1, NULL, NULL, set_deb_format },
  201. { "new", 0, 0, NULL, NULL, set_deb_new },
  202. { "old", 0, 0, NULL, NULL, set_deb_old },
  203. { "debug", 'D', 0, &debugflag, NULL, NULL, 1 },
  204. { "verbose", 'v', 0, &opt_verbose, NULL, NULL, 1 },
  205. { "nocheck", 0, 0, &nocheckflag, NULL, NULL, 1 },
  206. { "uniform-compression", 0, 0, &opt_uniform_compression, NULL, NULL, 1 },
  207. { NULL, 'z', 1, NULL, NULL, set_compress_level },
  208. { NULL, 'Z', 1, NULL, NULL, set_compress_type },
  209. { NULL, 'S', 1, NULL, NULL, set_compress_strategy },
  210. { "showformat", 0, 1, NULL, &showformat, NULL },
  211. { "help", '?', 0, NULL, NULL, usage },
  212. { "version", 0, 0, NULL, NULL, printversion },
  213. { NULL, 0, 0, NULL, NULL, NULL }
  214. };
  215. int main(int argc, const char *const *argv) {
  216. struct dpkg_error err;
  217. int ret;
  218. dpkg_locales_init(PACKAGE);
  219. dpkg_program_init(BACKEND);
  220. dpkg_options_parse(&argv, cmdinfos, printforhelp);
  221. if (!cipaction) badusage(_("need an action option"));
  222. if (!compressor_check_params(&compress_params, &err))
  223. badusage(_("invalid compressor parameters: %s"), err.str);
  224. if (opt_uniform_compression &&
  225. (compress_params.type != COMPRESSOR_TYPE_NONE &&
  226. compress_params.type != COMPRESSOR_TYPE_GZIP &&
  227. compress_params.type != COMPRESSOR_TYPE_XZ))
  228. badusage(_("unsupported compression type '%s' with uniform compression"),
  229. compressor_get_name(compress_params.type));
  230. ret = cipaction->action(argv);
  231. dpkg_program_done();
  232. return ret;
  233. }