dpkg_deb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 (30 kb)"
  9. //config: default y
  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. //applet:IF_DPKG_DEB(APPLET_ODDNAME(dpkg-deb, dpkg_deb, BB_DIR_USR_BIN, BB_SUID_DROP, dpkg_deb))
  19. //kbuild:lib-$(CONFIG_DPKG_DEB) += dpkg_deb.o
  20. //usage:#define dpkg_deb_trivial_usage
  21. //usage: "[-cefxX] FILE [DIR]"
  22. //usage:#define dpkg_deb_full_usage "\n\n"
  23. //usage: "Perform actions on Debian packages (.deb)\n"
  24. //usage: "\n -c List files"
  25. //usage: "\n -f Print control fields"
  26. //usage: "\n -e Extract control files to DIR (default: ./DEBIAN)"
  27. //usage: "\n -x Extract files to DIR (no default)"
  28. //usage: "\n -X Verbose -x"
  29. //usage:
  30. //usage:#define dpkg_deb_example_usage
  31. //usage: "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
  32. #include "libbb.h"
  33. #include "bb_archive.h"
  34. #define DPKG_DEB_OPT_CONTENTS 1
  35. #define DPKG_DEB_OPT_CONTROL 2
  36. #define DPKG_DEB_OPT_FIELD 4
  37. #define DPKG_DEB_OPT_EXTRACT_VERBOSE 8
  38. #define DPKG_DEB_OPT_EXTRACT 16
  39. int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  40. int dpkg_deb_main(int argc UNUSED_PARAM, char **argv)
  41. {
  42. archive_handle_t *ar_archive;
  43. archive_handle_t *tar_archive;
  44. llist_t *control_tar_llist = NULL;
  45. unsigned opt;
  46. const char *extract_dir;
  47. /* Setup the tar archive handle */
  48. tar_archive = init_handle();
  49. /* Setup an ar archive handle that refers to the gzip sub archive */
  50. ar_archive = init_handle();
  51. ar_archive->dpkg__sub_archive = tar_archive;
  52. ar_archive->filter = filter_accept_list_reassign;
  53. llist_add_to(&ar_archive->accept, (char*)"data.tar");
  54. llist_add_to(&control_tar_llist, (char*)"control.tar");
  55. #if ENABLE_FEATURE_SEAMLESS_GZ
  56. llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
  57. llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
  58. #endif
  59. #if ENABLE_FEATURE_SEAMLESS_BZ2
  60. llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
  61. llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
  62. #endif
  63. #if ENABLE_FEATURE_SEAMLESS_LZMA
  64. llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
  65. llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
  66. #endif
  67. #if ENABLE_FEATURE_SEAMLESS_XZ
  68. llist_add_to(&ar_archive->accept, (char*)"data.tar.xz");
  69. llist_add_to(&control_tar_llist, (char*)"control.tar.xz");
  70. #endif
  71. /* Must have 1 or 2 args */
  72. opt = getopt32(argv, "^" "cefXx"
  73. "\0" "-1:?2:c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX"
  74. );
  75. argv += optind;
  76. //argc -= optind;
  77. extract_dir = argv[1];
  78. if (opt & DPKG_DEB_OPT_CONTENTS) { // -c
  79. tar_archive->action_header = header_verbose_list;
  80. if (extract_dir)
  81. bb_show_usage();
  82. }
  83. if (opt & DPKG_DEB_OPT_FIELD) { // -f
  84. /* Print the entire control file */
  85. //TODO: standard tool accepts an optional list of fields to print
  86. ar_archive->accept = control_tar_llist;
  87. llist_add_to(&(tar_archive->accept), (char*)"./control");
  88. tar_archive->filter = filter_accept_list;
  89. tar_archive->action_data = data_extract_to_stdout;
  90. if (extract_dir)
  91. bb_show_usage();
  92. }
  93. if (opt & DPKG_DEB_OPT_CONTROL) { // -e
  94. ar_archive->accept = control_tar_llist;
  95. tar_archive->action_data = data_extract_all;
  96. if (!extract_dir)
  97. extract_dir = "./DEBIAN";
  98. }
  99. if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) { // -Xx
  100. if (opt & DPKG_DEB_OPT_EXTRACT_VERBOSE)
  101. tar_archive->action_header = header_list;
  102. tar_archive->action_data = data_extract_all;
  103. if (!extract_dir)
  104. bb_show_usage();
  105. }
  106. /* Standard tool supports "-" */
  107. tar_archive->src_fd = ar_archive->src_fd = xopen_stdin(argv[0]);
  108. if (extract_dir) {
  109. mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
  110. xchdir(extract_dir);
  111. }
  112. /* Do it */
  113. unpack_ar_archive(ar_archive);
  114. /* Cleanup */
  115. if (ENABLE_FEATURE_CLEAN_UP)
  116. close(ar_archive->src_fd);
  117. return EXIT_SUCCESS;
  118. }