3
0

dpkg_deb.c 2.9 KB

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