dpkg_deb.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. opt_complementary = "c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
  38. opt = getopt32(argv, "cefXx");
  39. argv += optind;
  40. argc -= optind;
  41. if (opt & DPKG_DEB_OPT_CONTENTS) {
  42. tar_archive->action_header = header_verbose_list;
  43. }
  44. extract_dir = NULL;
  45. need_args = 1;
  46. if (opt & DPKG_DEB_OPT_CONTROL) {
  47. ar_archive->accept = control_tar_llist;
  48. tar_archive->action_data = data_extract_all;
  49. if (1 == argc) {
  50. extract_dir = "./DEBIAN";
  51. } else {
  52. need_args++;
  53. }
  54. }
  55. if (opt & DPKG_DEB_OPT_FIELD) {
  56. /* Print the entire control file
  57. * it should accept a second argument which specifies a
  58. * specific field to print */
  59. ar_archive->accept = control_tar_llist;
  60. llist_add_to(&(tar_archive->accept), (char*)"./control");
  61. tar_archive->filter = filter_accept_list;
  62. tar_archive->action_data = data_extract_to_stdout;
  63. }
  64. if (opt & DPKG_DEB_OPT_EXTRACT) {
  65. tar_archive->action_header = header_list;
  66. }
  67. if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
  68. tar_archive->action_data = data_extract_all;
  69. need_args = 2;
  70. }
  71. if (need_args != argc) {
  72. bb_show_usage();
  73. }
  74. tar_archive->src_fd = ar_archive->src_fd = xopen(argv[0], O_RDONLY);
  75. /* Work out where to extract the files */
  76. /* 2nd argument is a dir name */
  77. if (argv[1]) {
  78. extract_dir = argv[1];
  79. }
  80. if (extract_dir) {
  81. mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
  82. xchdir(extract_dir);
  83. }
  84. /* Do it */
  85. unpack_ar_archive(ar_archive);
  86. /* Cleanup */
  87. if (ENABLE_FEATURE_CLEAN_UP)
  88. close(ar_archive->src_fd);
  89. return EXIT_SUCCESS;
  90. }