dpkg_deb.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * dpkg-deb packs, unpacks and provides information about Debian archives.
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. */
  7. //config:config DPKG_DEB
  8. //config: bool "dpkg_deb"
  9. //config: default n
  10. //config: select FEATURE_SEAMLESS_GZ
  11. //config: help
  12. //config: dpkg-deb unpacks and provides information about Debian archives.
  13. //config:
  14. //config: This implementation of dpkg-deb cannot pack archives.
  15. //config:
  16. //config: Unless you have a specific application which requires dpkg-deb,
  17. //config: say N here.
  18. //config:
  19. //config:config FEATURE_DPKG_DEB_EXTRACT_ONLY
  20. //config: bool "Extract only (-x)"
  21. //config: default n
  22. //config: depends on DPKG_DEB
  23. //config: help
  24. //config: This reduces dpkg-deb to the equivalent of
  25. //config: "ar -p <deb> data.tar.gz | tar -zx". However it saves space as none
  26. //config: of the extra dpkg-deb, ar or tar options are needed, they are linked
  27. //config: to internally.
  28. //applet:IF_DPKG_DEB(APPLET_ODDNAME(dpkg-deb, dpkg_deb, BB_DIR_USR_BIN, BB_SUID_DROP, dpkg_deb))
  29. //kbuild:lib-$(CONFIG_DPKG_DEB) += dpkg_deb.o
  30. //usage:#define dpkg_deb_trivial_usage
  31. //usage: "[-cefxX] FILE [argument]"
  32. //usage:#define dpkg_deb_full_usage "\n\n"
  33. //usage: "Perform actions on Debian packages (.debs)\n"
  34. //usage: "\n -c List contents of filesystem tree"
  35. //usage: "\n -e Extract control files to [argument] directory"
  36. //usage: "\n -f Display control field name starting with [argument]"
  37. //usage: "\n -x Extract packages filesystem tree to directory"
  38. //usage: "\n -X Verbose extract"
  39. //usage:
  40. //usage:#define dpkg_deb_example_usage
  41. //usage: "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
  42. #include "libbb.h"
  43. #include "bb_archive.h"
  44. #define DPKG_DEB_OPT_CONTENTS 1
  45. #define DPKG_DEB_OPT_CONTROL 2
  46. #define DPKG_DEB_OPT_FIELD 4
  47. #define DPKG_DEB_OPT_EXTRACT 8
  48. #define DPKG_DEB_OPT_EXTRACT_VERBOSE 16
  49. int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  50. int dpkg_deb_main(int argc, char **argv)
  51. {
  52. archive_handle_t *ar_archive;
  53. archive_handle_t *tar_archive;
  54. llist_t *control_tar_llist = NULL;
  55. unsigned opt;
  56. const char *extract_dir;
  57. int need_args;
  58. /* Setup the tar archive handle */
  59. tar_archive = init_handle();
  60. /* Setup an ar archive handle that refers to the gzip sub archive */
  61. ar_archive = init_handle();
  62. ar_archive->dpkg__sub_archive = tar_archive;
  63. ar_archive->filter = filter_accept_list_reassign;
  64. #if ENABLE_FEATURE_SEAMLESS_GZ
  65. llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
  66. llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
  67. #endif
  68. #if ENABLE_FEATURE_SEAMLESS_BZ2
  69. llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
  70. llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
  71. #endif
  72. #if ENABLE_FEATURE_SEAMLESS_LZMA
  73. llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
  74. llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
  75. #endif
  76. opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
  77. opt = getopt32(argv, "cefXx");
  78. argv += optind;
  79. argc -= optind;
  80. if (opt & DPKG_DEB_OPT_CONTENTS) {
  81. tar_archive->action_header = header_verbose_list;
  82. }
  83. extract_dir = NULL;
  84. need_args = 1;
  85. if (opt & DPKG_DEB_OPT_CONTROL) {
  86. ar_archive->accept = control_tar_llist;
  87. tar_archive->action_data = data_extract_all;
  88. if (1 == argc) {
  89. extract_dir = "./DEBIAN";
  90. } else {
  91. need_args++;
  92. }
  93. }
  94. if (opt & DPKG_DEB_OPT_FIELD) {
  95. /* Print the entire control file
  96. * it should accept a second argument which specifies a
  97. * specific field to print */
  98. ar_archive->accept = control_tar_llist;
  99. llist_add_to(&(tar_archive->accept), (char*)"./control");
  100. tar_archive->filter = filter_accept_list;
  101. tar_archive->action_data = data_extract_to_stdout;
  102. }
  103. if (opt & DPKG_DEB_OPT_EXTRACT) {
  104. tar_archive->action_header = header_list;
  105. }
  106. if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
  107. tar_archive->action_data = data_extract_all;
  108. need_args = 2;
  109. }
  110. if (need_args != argc) {
  111. bb_show_usage();
  112. }
  113. tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
  114. /* Work out where to extract the files */
  115. /* 2nd argument is a dir name */
  116. if (argv[1]) {
  117. extract_dir = argv[1];
  118. }
  119. if (extract_dir) {
  120. mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
  121. xchdir(extract_dir);
  122. }
  123. /* Do it */
  124. unpack_ar_archive(ar_archive);
  125. /* Cleanup */
  126. if (ENABLE_FEATURE_CLEAN_UP)
  127. close(ar_archive->src_fd);
  128. return EXIT_SUCCESS;
  129. }