dpkg_deb.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. llist_add_to(&ar_archive->accept, (char*)"data.tar");
  65. llist_add_to(&control_tar_llist, (char*)"control.tar");
  66. #if ENABLE_FEATURE_SEAMLESS_GZ
  67. llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
  68. llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
  69. #endif
  70. #if ENABLE_FEATURE_SEAMLESS_BZ2
  71. llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
  72. llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
  73. #endif
  74. #if ENABLE_FEATURE_SEAMLESS_LZMA
  75. llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
  76. llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
  77. #endif
  78. #if ENABLE_FEATURE_SEAMLESS_XZ
  79. llist_add_to(&ar_archive->accept, (char*)"data.tar.xz");
  80. llist_add_to(&control_tar_llist, (char*)"control.tar.xz");
  81. #endif
  82. opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
  83. opt = getopt32(argv, "cefXx");
  84. argv += optind;
  85. argc -= optind;
  86. if (opt & DPKG_DEB_OPT_CONTENTS) {
  87. tar_archive->action_header = header_verbose_list;
  88. }
  89. extract_dir = NULL;
  90. need_args = 1;
  91. if (opt & DPKG_DEB_OPT_CONTROL) {
  92. ar_archive->accept = control_tar_llist;
  93. tar_archive->action_data = data_extract_all;
  94. if (1 == argc) {
  95. extract_dir = "./DEBIAN";
  96. } else {
  97. need_args++;
  98. }
  99. }
  100. if (opt & DPKG_DEB_OPT_FIELD) {
  101. /* Print the entire control file
  102. * it should accept a second argument which specifies a
  103. * specific field to print */
  104. ar_archive->accept = control_tar_llist;
  105. llist_add_to(&(tar_archive->accept), (char*)"./control");
  106. tar_archive->filter = filter_accept_list;
  107. tar_archive->action_data = data_extract_to_stdout;
  108. }
  109. if (opt & DPKG_DEB_OPT_EXTRACT) {
  110. tar_archive->action_header = header_list;
  111. }
  112. if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
  113. tar_archive->action_data = data_extract_all;
  114. need_args = 2;
  115. }
  116. if (need_args != argc) {
  117. bb_show_usage();
  118. }
  119. tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
  120. /* Work out where to extract the files */
  121. /* 2nd argument is a dir name */
  122. if (argv[1]) {
  123. extract_dir = argv[1];
  124. }
  125. if (extract_dir) {
  126. mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
  127. xchdir(extract_dir);
  128. }
  129. /* Do it */
  130. unpack_ar_archive(ar_archive);
  131. /* Cleanup */
  132. if (ENABLE_FEATURE_CLEAN_UP)
  133. close(ar_archive->src_fd);
  134. return EXIT_SUCCESS;
  135. }