3
0

rpm2cpio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. //usage:#define rpm2cpio_trivial_usage
  10. //usage: "package.rpm"
  11. //usage:#define rpm2cpio_full_usage "\n\n"
  12. //usage: "Output a cpio archive of the rpm file"
  13. #include "libbb.h"
  14. #include "archive.h"
  15. #include "rpm.h"
  16. enum { rpm_fd = STDIN_FILENO };
  17. static unsigned skip_header(void)
  18. {
  19. struct rpm_header header;
  20. unsigned len;
  21. xread(rpm_fd, &header, sizeof(header));
  22. // if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
  23. // bb_error_msg_and_die("invalid RPM header magic");
  24. // }
  25. // if (header.version != 1) {
  26. // bb_error_msg_and_die("unsupported RPM header version");
  27. // }
  28. if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
  29. bb_error_msg_and_die("invalid RPM header magic or unsupported version");
  30. // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
  31. }
  32. /* Seek past index entries, and past store */
  33. len = 16 * ntohl(header.entries) + ntohl(header.size);
  34. seek_by_jump(rpm_fd, len);
  35. return sizeof(header) + len;
  36. }
  37. /* No getopt required */
  38. int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
  40. {
  41. struct rpm_lead lead;
  42. unsigned pos;
  43. if (argv[1]) {
  44. xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
  45. }
  46. xread(rpm_fd, &lead, sizeof(lead));
  47. /* Just check the magic, the rest is irrelevant */
  48. if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
  49. bb_error_msg_and_die("invalid RPM magic");
  50. }
  51. /* Skip the signature header, align to 8 bytes */
  52. pos = skip_header();
  53. seek_by_jump(rpm_fd, (-(int)pos) & 7);
  54. /* Skip the main header */
  55. skip_header();
  56. #if 0
  57. /* This works, but doesn't report uncompress errors (they happen in child) */
  58. setup_unzip_on_fd(rpm_fd /*fail_if_not_detected: 1*/);
  59. if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
  60. bb_error_msg_and_die("error unpacking");
  61. #else
  62. /* BLOAT */
  63. {
  64. union {
  65. uint8_t b[4];
  66. uint16_t b16[2];
  67. uint32_t b32[1];
  68. } magic;
  69. IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
  70. xread(rpm_fd, magic.b16, sizeof(magic.b16[0]));
  71. if (magic.b16[0] == GZIP_MAGIC) {
  72. unpack = unpack_gz_stream;
  73. } else
  74. if (ENABLE_FEATURE_SEAMLESS_BZ2
  75. && magic.b16[0] == BZIP2_MAGIC
  76. ) {
  77. unpack = unpack_bz2_stream;
  78. } else
  79. if (ENABLE_FEATURE_SEAMLESS_XZ
  80. && magic.b16[0] == XZ_MAGIC1
  81. ) {
  82. xread(rpm_fd, magic.b32, sizeof(magic.b32[0]));
  83. if (magic.b32[0] != XZ_MAGIC2)
  84. goto no_magic;
  85. /* unpack_xz_stream wants fd at position 6, no need to seek */
  86. //xlseek(rpm_fd, -6, SEEK_CUR);
  87. unpack = unpack_xz_stream;
  88. } else {
  89. no_magic:
  90. bb_error_msg_and_die("no gzip"
  91. IF_FEATURE_SEAMLESS_BZ2("/bzip2")
  92. IF_FEATURE_SEAMLESS_XZ("/xz")
  93. " magic");
  94. }
  95. if (unpack(rpm_fd, STDOUT_FILENO) < 0)
  96. bb_error_msg_and_die("error unpacking");
  97. }
  98. #endif
  99. if (ENABLE_FEATURE_CLEAN_UP) {
  100. close(rpm_fd);
  101. }
  102. return 0;
  103. }