dpkg_deb.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. //usage:#define dpkg_deb_trivial_usage
  8. //usage: "[-cefxX] FILE [argument"
  9. //usage:#define dpkg_deb_full_usage "\n\n"
  10. //usage: "Perform actions on Debian packages (.debs)\n"
  11. //usage: "\n -c List contents of filesystem tree"
  12. //usage: "\n -e Extract control files to [argument] directory"
  13. //usage: "\n -f Display control field name starting with [argument]"
  14. //usage: "\n -x Extract packages filesystem tree to directory"
  15. //usage: "\n -X Verbose extract"
  16. //usage:
  17. //usage:#define dpkg_deb_example_usage
  18. //usage: "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
  19. #include "libbb.h"
  20. #include "archive.h"
  21. #define DPKG_DEB_OPT_CONTENTS 1
  22. #define DPKG_DEB_OPT_CONTROL 2
  23. #define DPKG_DEB_OPT_FIELD 4
  24. #define DPKG_DEB_OPT_EXTRACT 8
  25. #define DPKG_DEB_OPT_EXTRACT_VERBOSE 16
  26. int dpkg_deb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int dpkg_deb_main(int argc, char **argv)
  28. {
  29. archive_handle_t *ar_archive;
  30. archive_handle_t *tar_archive;
  31. llist_t *control_tar_llist = NULL;
  32. unsigned opt;
  33. const char *extract_dir;
  34. int need_args;
  35. /* Setup the tar archive handle */
  36. tar_archive = init_handle();
  37. /* Setup an ar archive handle that refers to the gzip sub archive */
  38. ar_archive = init_handle();
  39. ar_archive->dpkg__sub_archive = tar_archive;
  40. ar_archive->filter = filter_accept_list_reassign;
  41. #if ENABLE_FEATURE_SEAMLESS_GZ
  42. llist_add_to(&ar_archive->accept, (char*)"data.tar.gz");
  43. llist_add_to(&control_tar_llist, (char*)"control.tar.gz");
  44. #endif
  45. #if ENABLE_FEATURE_SEAMLESS_BZ2
  46. llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
  47. llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
  48. #endif
  49. #if ENABLE_FEATURE_SEAMLESS_LZMA
  50. llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
  51. llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
  52. #endif
  53. opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
  54. opt = getopt32(argv, "cefXx");
  55. argv += optind;
  56. argc -= optind;
  57. if (opt & DPKG_DEB_OPT_CONTENTS) {
  58. tar_archive->action_header = header_verbose_list;
  59. }
  60. extract_dir = NULL;
  61. need_args = 1;
  62. if (opt & DPKG_DEB_OPT_CONTROL) {
  63. ar_archive->accept = control_tar_llist;
  64. tar_archive->action_data = data_extract_all;
  65. if (1 == argc) {
  66. extract_dir = "./DEBIAN";
  67. } else {
  68. need_args++;
  69. }
  70. }
  71. if (opt & DPKG_DEB_OPT_FIELD) {
  72. /* Print the entire control file
  73. * it should accept a second argument which specifies a
  74. * specific field to print */
  75. ar_archive->accept = control_tar_llist;
  76. llist_add_to(&(tar_archive->accept), (char*)"./control");
  77. tar_archive->filter = filter_accept_list;
  78. tar_archive->action_data = data_extract_to_stdout;
  79. }
  80. if (opt & DPKG_DEB_OPT_EXTRACT) {
  81. tar_archive->action_header = header_list;
  82. }
  83. if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
  84. tar_archive->action_data = data_extract_all;
  85. need_args = 2;
  86. }
  87. if (need_args != argc) {
  88. bb_show_usage();
  89. }
  90. tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
  91. /* Work out where to extract the files */
  92. /* 2nd argument is a dir name */
  93. if (argv[1]) {
  94. extract_dir = argv[1];
  95. }
  96. if (extract_dir) {
  97. mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
  98. xchdir(extract_dir);
  99. }
  100. /* Do it */
  101. unpack_ar_archive(ar_archive);
  102. /* Cleanup */
  103. if (ENABLE_FEATURE_CLEAN_UP)
  104. close(ar_archive->src_fd);
  105. return EXIT_SUCCESS;
  106. }