3
0

dpkg_deb.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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);
  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 = NULL;
  22. short argcount = 1;
  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->sub_archive = tar_archive;
  28. ar_archive->filter = filter_accept_list_reassign;
  29. #if ENABLE_FEATURE_DEB_TAR_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_DEB_TAR_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. if (opt & DPKG_DEB_OPT_CONTENTS) {
  40. tar_archive->action_header = header_verbose_list;
  41. }
  42. if (opt & DPKG_DEB_OPT_CONTROL) {
  43. ar_archive->accept = control_tar_llist;
  44. tar_archive->action_data = data_extract_all;
  45. if (optind + 1 == argc) {
  46. extract_dir = "./DEBIAN";
  47. } else {
  48. argcount++;
  49. }
  50. }
  51. if (opt & DPKG_DEB_OPT_FIELD) {
  52. /* Print the entire control file
  53. * it should accept a second argument which specifies a
  54. * specific field to print */
  55. ar_archive->accept = control_tar_llist;
  56. llist_add_to(&(tar_archive->accept), (char*)"./control");
  57. tar_archive->filter = filter_accept_list;
  58. tar_archive->action_data = data_extract_to_stdout;
  59. }
  60. if (opt & DPKG_DEB_OPT_EXTRACT) {
  61. tar_archive->action_header = header_list;
  62. }
  63. if (opt & (DPKG_DEB_OPT_EXTRACT_VERBOSE | DPKG_DEB_OPT_EXTRACT)) {
  64. tar_archive->action_data = data_extract_all;
  65. argcount = 2;
  66. }
  67. if ((optind + argcount) != argc) {
  68. bb_show_usage();
  69. }
  70. tar_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY);
  71. /* Workout where to extract the files */
  72. /* 2nd argument is a dir name */
  73. if (argv[optind]) {
  74. extract_dir = argv[optind];
  75. }
  76. if (extract_dir) {
  77. mkdir(extract_dir, 0777); /* bb_make_directory(extract_dir, 0777, 0) */
  78. xchdir(extract_dir);
  79. }
  80. unpack_ar_archive(ar_archive);
  81. /* Cleanup */
  82. close(ar_archive->src_fd);
  83. return EXIT_SUCCESS;
  84. }