exploderpm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/sh
  2. # exploderpm
  3. # VERSION 0.3
  4. # Copyright 2009-2013 Gilbert Ashley <amigo@ibiblio.org>
  5. # The code for explode_rpm was originally written by Jeff Johnson
  6. # and modified by Lasse Collin <lasse.collin@tukaani.org>
  7. # Copyright (C) 2005, 2006 Lasse Collin <lasse.collin@tukaani.org>
  8. #110705 bk: added -i install option for rpm. #131124 adjusted
  9. # code for handling debian archives taken from disrpm
  10. # released under the Gnu General Public License (GPL)
  11. # (c) bjdouma@xs4all.nl VER="v1.5, october 2004"
  12. # VERSION 0.3 Gilbert Ashley <amigo@ibiblio.org>
  13. # Added support for newer fedora rpm archives which use 7z instead of xz
  14. # Thanks to Jakub Szefer for finding and reporting this problem
  15. OPT="$1"
  16. FILE="$2"
  17. [[ $OPT = '-lv' ]] && VERBOSE='v' OPT='-l'
  18. [[ $OPT = '-xv' ]] && VERBOSE='v' OPT='-x'
  19. explode() {
  20. case "$1" in
  21. *.rpm) explode_rpm "$FILE" ;;
  22. *.deb) explode_deb "$FILE" ;;
  23. esac
  24. return $?
  25. }
  26. gzip_sieve()
  27. {
  28. # gzip-magic: 0x1F,0x8B
  29. sed -ne '/1[fF]/{;N;/8[bB]$/{;s/1[fF]//g;s/^0*//g;P;};}'
  30. }
  31. bzip2_sieve()
  32. {
  33. # bzip2-magic: 0x42,0x5A,0x68
  34. sed -ne '/42/{;N;/5[aA]$/{;N;/68$/{s/42//g;s/5[aA]//g;s/^0*//g;P;};};}'
  35. }
  36. probe()
  37. {
  38. dd if=$FILE ibs=$O skip=1 2>/dev/null \
  39. | $2 -dc - 2>/dev/null \
  40. | cpio "$1" 2>/dev/null
  41. }
  42. explode_deb() {
  43. PASS1="probe -t$VERBOSE"
  44. case $OPT in
  45. '-l'|'-v') PASS2=":" ;;
  46. '-x') PASS2="probe -idm" ;;
  47. esac
  48. for AR in gzip bzip2 ; do
  49. e=1
  50. HEADER_SIZE=256000
  51. AR_OFFSETS=$(od -A d -N $HEADER_SIZE -v -t x1 -w1 $FILE | ${AR}_sieve)
  52. for O in $AR_OFFSETS ; do
  53. $PASS1 $AR 2>/dev/null && $PASS2 $AR && e=0
  54. done
  55. [ $e -eq 0 ] && return $e
  56. done
  57. }
  58. explode_rpm() {
  59. local pkg o sigsize gz
  60. pkg=$1
  61. o=104
  62. set -- $(od -j $o -N 8 -t u1 -- "$pkg")
  63. sigsize=$((8 + 16 *
  64. (256 * (256 * (256 * $2 + $3) + $4) + $5) +
  65. (256 * (256 * (256 * $6 + $7) + $8) + $9)))
  66. o=$((o + sigsize + (8 - (sigsize % 8)) % 8 + 8))
  67. set -- $(od -j $o -N 8 -t u1 -- "$pkg")
  68. o=$((o + 8 + 16 *
  69. (256 * (256 * (256 * $2 + $3) + $4) + $5) +
  70. (256 * (256 * (256 * $6 + $7) + $8) + $9)))
  71. comp=$(dd if="$pkg" ibs=$o skip=1 count=1 2>/dev/null \
  72. | dd bs=3 count=1 2> /dev/null)
  73. gz="$(echo -en '\037\0213')"
  74. #xz="$(echo -en '\0fd\037\07a\058\05a\000')"
  75. case $OPT in
  76. '-x')
  77. case "$comp" in
  78. BZh) dd if="$pkg" ibs=$o skip=1 2>/dev/null | bunzip2 | cpio --quiet -ivdm ;;
  79. "$gz"*) dd if="$pkg" ibs=$o skip=1 2>/dev/null | gunzip | cpio --quiet -ivdm ;;
  80. "]"*|?"7z"*) dd if="$pkg" ibs=$o skip=1 2>/dev/null | unxz | cpio --quiet -ivdm ;;
  81. *) echo "Unrecognized rpm file: $pkg"; return 1 ;;
  82. esac
  83. ;;
  84. '-l')
  85. case "$comp" in
  86. BZh) dd if="$pkg" ibs=$o skip=1 2>/dev/null | bunzip2 | cpio --quiet -t$VERBOSE ;;
  87. "$gz"*) dd if="$pkg" ibs=$o skip=1 2>/dev/null | gunzip | cpio --quiet -t$VERBOSE ;;
  88. "]"*|?"7z"*) dd if="$pkg" ibs=$o skip=1 2>/dev/null | unxz | cpio --quiet -t$VERBOSE ;;
  89. *) echo "Unrecognized rpm file: $pkg"; return 1 ;;
  90. esac
  91. ;;
  92. '-i') #110705 install
  93. CURRDIR="`pwd`"
  94. if [ ! -f ${CURRDIR}/${pkg} ];then
  95. echo "Error: ${CURRDIR}/${pkg} does not exist"
  96. return 1
  97. fi
  98. cd /
  99. case "$comp" in
  100. BZh) dd if="${CURRDIR}/$pkg" ibs=$o skip=1 2>/dev/null | bunzip2 | cpio --quiet --unconditional -idm ;;
  101. "$gz"*) dd if="${CURRDIR}/$pkg" ibs=$o skip=1 2>/dev/null | gunzip | cpio --quiet --unconditional -idm ;;
  102. "]"*|?"7z"*) dd if="${CURRDIR}/$pkg" ibs=$o skip=1 2>/dev/null | unxz | cpio --quiet --unconditional -idm ;;
  103. *) echo "Unrecognized rpm file: $pkg"; return 1 ;;
  104. esac
  105. RETSTAT=$?
  106. cd "$CURRDIR"
  107. [ $RETSTAT != 0 ] && return 1
  108. return 0
  109. ;;
  110. esac
  111. [ $? != 0 ] && return 1
  112. # The directories that are not listed in the RPM file are always created
  113. # "chmod 0700" by cpio. We will reset those directories to "chmod 0755".
  114. # Unfortunately we cannot detect without extra help from cpio if the
  115. # package had some directories that shouldn't be world readable.
  116. find . -type d -perm 700 -exec chmod 755 {} \;
  117. return 0
  118. }
  119. # End of functions.explode.sh
  120. explode "$FILE"