dpkg_deb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. */
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <getopt.h>
  22. #include "unarchive.h"
  23. #include "busybox.h"
  24. #define DPKG_DEB_OPT_CONTENTS 1
  25. #define DPKG_DEB_OPT_CONTROL 2
  26. #define DPKG_DEB_OPT_FIELD 4
  27. #define DPKG_DEB_OPT_EXTRACT 8
  28. #define DPKG_DEB_OPT_EXTRACT_VERBOSE 16
  29. extern int dpkg_deb_main(int argc, char **argv)
  30. {
  31. archive_handle_t *ar_archive;
  32. archive_handle_t *tar_archive;
  33. llist_t *control_tar_llist = NULL;
  34. unsigned long opt;
  35. char *extract_dir = NULL;
  36. short argcount = 1;
  37. /* Setup the tar archive handle */
  38. tar_archive = init_handle();
  39. /* Setup an ar archive handle that refers to the gzip sub archive */
  40. ar_archive = init_handle();
  41. ar_archive->sub_archive = tar_archive;
  42. ar_archive->filter = filter_accept_list_reassign;
  43. #ifdef CONFIG_FEATURE_DEB_TAR_GZ
  44. ar_archive->accept = llist_add_to(NULL, "data.tar.gz");
  45. control_tar_llist = llist_add_to(NULL, "control.tar.gz");
  46. #endif
  47. #ifdef CONFIG_FEATURE_DEB_TAR_BZ2
  48. ar_archive->accept = llist_add_to(ar_archive->accept, "data.tar.bz2");
  49. control_tar_llist = llist_add_to(control_tar_llist, "control.tar.bz2");
  50. #endif
  51. bb_opt_complementaly = "c~efXx:e~cfXx:f~ceXx:X~cefx:x~cefX";
  52. opt = bb_getopt_ulflags(argc, argv, "cefXx");
  53. if (opt & DPKG_DEB_OPT_CONTENTS) {
  54. tar_archive->action_header = header_verbose_list;
  55. }
  56. if (opt & DPKG_DEB_OPT_CONTROL) {
  57. ar_archive->accept = control_tar_llist;
  58. tar_archive->action_data = data_extract_all;
  59. if (optind + 1 == argc) {
  60. extract_dir = "./DEBIAN";
  61. } else {
  62. argcount++;
  63. }
  64. }
  65. if (opt & DPKG_DEB_OPT_FIELD) {
  66. /* Print the entire control file
  67. * it should accept a second argument which specifies a
  68. * specific field to print */
  69. ar_archive->accept = control_tar_llist;
  70. tar_archive->accept = llist_add_to(NULL, "./control");;
  71. tar_archive->filter = filter_accept_list;
  72. tar_archive->action_data = data_extract_to_stdout;
  73. }
  74. if (opt & DPKG_DEB_OPT_EXTRACT) {
  75. tar_archive->action_header = header_list;
  76. }
  77. if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
  78. tar_archive->action_data = data_extract_all;
  79. argcount = 2;
  80. }
  81. if ((optind + argcount != argc) || (opt & BB_GETOPT_ERROR)) {
  82. bb_show_usage();
  83. }
  84. tar_archive->src_fd = ar_archive->src_fd = bb_xopen(argv[optind++], O_RDONLY);
  85. /* Workout where to extract the files */
  86. /* 2nd argument is a dir name */
  87. if (argv[optind]) {
  88. extract_dir = argv[optind];
  89. }
  90. if (extract_dir) {
  91. mkdir(extract_dir, 0777);
  92. chdir(extract_dir);
  93. }
  94. unpack_ar_archive(ar_archive);
  95. /* Cleanup */
  96. close (ar_archive->src_fd);
  97. return(EXIT_SUCCESS);
  98. }