3
0

rpm2cpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini rpm2cpio implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Laurence Anderson
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config RPM2CPIO
  10. //config: bool "rpm2cpio"
  11. //config: default y
  12. //config: help
  13. //config: Converts a RPM file into a CPIO archive.
  14. //applet:IF_RPM2CPIO(APPLET(rpm2cpio, BB_DIR_USR_BIN, BB_SUID_DROP))
  15. //kbuild:lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o
  16. //usage:#define rpm2cpio_trivial_usage
  17. //usage: "package.rpm"
  18. //usage:#define rpm2cpio_full_usage "\n\n"
  19. //usage: "Output a cpio archive of the rpm file"
  20. #include "libbb.h"
  21. #include "bb_archive.h"
  22. #include "rpm.h"
  23. enum { rpm_fd = STDIN_FILENO };
  24. static unsigned skip_header(void)
  25. {
  26. struct rpm_header header;
  27. unsigned len;
  28. xread(rpm_fd, &header, sizeof(header));
  29. // if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
  30. // bb_error_msg_and_die("invalid RPM header magic");
  31. // }
  32. // if (header.version != 1) {
  33. // bb_error_msg_and_die("unsupported RPM header version");
  34. // }
  35. if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
  36. bb_error_msg_and_die("invalid RPM header magic or unsupported version");
  37. // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
  38. }
  39. /* Seek past index entries, and past store */
  40. len = 16 * ntohl(header.entries) + ntohl(header.size);
  41. seek_by_jump(rpm_fd, len);
  42. return sizeof(header) + len;
  43. }
  44. /* No getopt required */
  45. int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  46. int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
  47. {
  48. struct rpm_lead lead;
  49. unsigned pos;
  50. if (argv[1]) {
  51. xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
  52. }
  53. xread(rpm_fd, &lead, sizeof(lead));
  54. /* Just check the magic, the rest is irrelevant */
  55. if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
  56. bb_error_msg_and_die("invalid RPM magic");
  57. }
  58. /* Skip the signature header, align to 8 bytes */
  59. pos = skip_header();
  60. seek_by_jump(rpm_fd, (-(int)pos) & 7);
  61. /* Skip the main header */
  62. skip_header();
  63. //if (SEAMLESS_COMPRESSION)
  64. // /* We need to know whether child (gzip/bzip/etc) exits abnormally */
  65. // signal(SIGCHLD, check_errors_in_children);
  66. /* This works, but doesn't report uncompress errors (they happen in child) */
  67. setup_unzip_on_fd(rpm_fd, /*fail_if_not_compressed:*/ 1);
  68. if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
  69. bb_error_msg_and_die("error unpacking");
  70. if (ENABLE_FEATURE_CLEAN_UP) {
  71. close(rpm_fd);
  72. }
  73. if (SEAMLESS_COMPRESSION) {
  74. check_errors_in_children(0);
  75. return bb_got_signal;
  76. }
  77. return EXIT_SUCCESS;
  78. }