dpkg_deb.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "busybox.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)
  15. {
  16. archive_handle_t *ar_archive;
  17. archive_handle_t *tar_archive;
  18. llist_t *control_tar_llist = NULL;
  19. unsigned opt;
  20. char *extract_dir = NULL;
  21. short argcount = 1;
  22. /* Setup the tar archive handle */
  23. tar_archive = init_handle();
  24. /* Setup an ar archive handle that refers to the gzip sub archive */
  25. ar_archive = init_handle();
  26. ar_archive->sub_archive = tar_archive;
  27. ar_archive->filter = filter_accept_list_reassign;
  28. #ifdef CONFIG_FEATURE_DEB_TAR_GZ
  29. llist_add_to(&(ar_archive->accept), "data.tar.gz");
  30. llist_add_to(&control_tar_llist, "control.tar.gz");
  31. #endif
  32. #ifdef CONFIG_FEATURE_DEB_TAR_BZ2
  33. llist_add_to(&(ar_archive->accept), "data.tar.bz2");
  34. llist_add_to(&control_tar_llist, "control.tar.bz2");
  35. #endif
  36. opt_complementary = "?c--efXx:e--cfXx:f--ceXx:X--cefx:x--cefX";
  37. opt = getopt32(argc, argv, "cefXx");
  38. if (opt & DPKG_DEB_OPT_CONTENTS) {
  39. tar_archive->action_header = header_verbose_list;
  40. }
  41. if (opt & DPKG_DEB_OPT_CONTROL) {
  42. ar_archive->accept = control_tar_llist;
  43. tar_archive->action_data = data_extract_all;
  44. if (optind + 1 == argc) {
  45. extract_dir = "./DEBIAN";
  46. } else {
  47. argcount++;
  48. }
  49. }
  50. if (opt & DPKG_DEB_OPT_FIELD) {
  51. /* Print the entire control file
  52. * it should accept a second argument which specifies a
  53. * specific field to print */
  54. ar_archive->accept = control_tar_llist;
  55. llist_add_to(&(tar_archive->accept), "./control");
  56. tar_archive->filter = filter_accept_list;
  57. tar_archive->action_data = data_extract_to_stdout;
  58. }
  59. if (opt & DPKG_DEB_OPT_EXTRACT) {
  60. tar_archive->action_header = header_list;
  61. }
  62. if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
  63. tar_archive->action_data = data_extract_all;
  64. argcount = 2;
  65. }
  66. if ((optind + argcount) != argc) {
  67. bb_show_usage();
  68. }
  69. tar_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY);
  70. /* Workout where to extract the files */
  71. /* 2nd argument is a dir name */
  72. if (argv[optind]) {
  73. extract_dir = argv[optind];
  74. }
  75. if (extract_dir) {
  76. mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
  77. xchdir(extract_dir);
  78. }
  79. unpack_ar_archive(ar_archive);
  80. /* Cleanup */
  81. close(ar_archive->src_fd);
  82. return EXIT_SUCCESS;
  83. }