xzdiff 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/bin/sh
  2. # Copyright (C) 1998, 2002, 2006, 2007 Free Software Foundation
  3. # Copyright (C) 1993 Jean-loup Gailly
  4. # Modified for XZ Utils by Andrew Dudman and Lasse Collin.
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #SET_PATH - This line is a placeholder to ease patching this script.
  14. # Instead of unsetting XZ_OPT, just make sure that xz will use file format
  15. # autodetection. This way memory usage limit and thread limit can be
  16. # specified via XZ_OPT. With gzip, bzip2, and lzop it's OK to just unset the
  17. # environment variables.
  18. xz='xz --format=auto'
  19. unset GZIP BZIP BZIP2 LZOP
  20. case ${0##*/} in
  21. *cmp*) prog=xzcmp; cmp=${CMP:-cmp};;
  22. *) prog=xzdiff; cmp=${DIFF:-diff};;
  23. esac
  24. version="$prog (XZ Utils) 5.2.7"
  25. usage="Usage: ${0##*/} [OPTION]... FILE1 [FILE2]
  26. Compare FILE1 to FILE2, using their uncompressed contents if they are
  27. compressed. If FILE2 is omitted, then the files compared are FILE1 and
  28. FILE1 from which the compression format suffix has been stripped.
  29. Do comparisons like '$cmp' does. OPTIONs are the same as for '$cmp'.
  30. Report bugs to <lasse.collin@tukaani.org>."
  31. # sed script to escape all ' for the shell, and then (to handle trailing
  32. # newlines correctly) turn trailing X on last line into '.
  33. escape='
  34. s/'\''/'\''\\'\'''\''/g
  35. $s/X$/'\''/
  36. '
  37. while :; do
  38. case $1 in
  39. --h*) printf '%s\n' "$usage" || exit 2; exit;;
  40. --v*) printf '%s\n' "$version" || exit 2; exit;;
  41. --) shift; break;;
  42. -*\'*) cmp="$cmp '"`printf '%sX\n' "$1" | sed "$escape"`;;
  43. -?*) cmp="$cmp '$1'";;
  44. *) break;;
  45. esac
  46. shift
  47. done
  48. cmp="$cmp --"
  49. for file; do
  50. test "X$file" = X- || <"$file" || exit 2
  51. done
  52. xz1=$xz
  53. xz2=$xz
  54. xz_status=0
  55. exec 3>&1
  56. if test $# -eq 1; then
  57. case $1 in
  58. *[-.]xz | *[-.]lzma | *.t[lx]z)
  59. ;;
  60. *[-.]bz2 | *.tbz | *.tbz2)
  61. xz1=bzip2;;
  62. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z)
  63. xz1=gzip;;
  64. *[-.]lzo | *.tzo)
  65. xz1=lzop;;
  66. *[-.]zst | *.tzst)
  67. xz1='zstd -q';;
  68. *)
  69. printf '%s\n' "$0: $1: Unknown compressed file name suffix" >&2
  70. exit 2;;
  71. esac
  72. case $1 in
  73. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lzo | *[-.]zst)
  74. FILE=`expr "X$1" : 'X\(.*\)[-.][abglmostxzZ2]*$'`;;
  75. *.t[abglx]z)
  76. FILE=`expr "X$1" : 'X\(.*[-.]t\)[abglx]z$'`ar;;
  77. *.tbz2)
  78. FILE=`expr "X$1" : 'X\(.*[-.]t\)bz2$'`ar;;
  79. *.tzo)
  80. FILE=`expr "X$1" : 'X\(.*[-.]t\)zo$'`ar;;
  81. *.tzst)
  82. FILE=`expr "X$1" : 'X\(.*[-.]t\)zst$'`ar;;
  83. esac
  84. xz_status=$(
  85. exec 4>&1
  86. ($xz1 -cd -- "$1" 4>&-; echo $? >&4) 3>&- | eval "$cmp" - '"$FILE"' >&3
  87. )
  88. elif test $# -eq 2; then
  89. case $1 in
  90. *[-.]bz2 | *.tbz | *.tbz2) xz1=bzip2;;
  91. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) xz1=gzip;;
  92. *[-.]lzo | *.tzo) xz1=lzop;;
  93. *[-.]zst | *.tzst) xz1='zstd -q';;
  94. esac
  95. case $2 in
  96. *[-.]bz2 | *.tbz | *.tbz2) xz2=bzip2;;
  97. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) xz2=gzip;;
  98. *[-.]lzo | *.tzo) xz2=lzop;;
  99. *[-.]zst | *.tzst) xz2='zstd -q';;
  100. esac
  101. case $1 in
  102. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | -)
  103. case "$2" in
  104. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | -)
  105. if test "$1$2" = --; then
  106. xz_status=$(
  107. exec 4>&1
  108. ($xz1 -cdf - 4>&-; echo $? >&4) 3>&- |
  109. eval "$cmp" - - >&3
  110. )
  111. elif # Reject Solaris 8's buggy /bin/bash 2.03.
  112. echo X | (echo X | eval "$cmp" /dev/fd/5 - >/dev/null 2>&1) 5<&0; then
  113. # NOTE: xz_status will contain two numbers.
  114. xz_status=$(
  115. exec 4>&1
  116. ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
  117. ( ($xz2 -cdf -- "$2" 4>&-; echo $? >&4) 3>&- 5<&- </dev/null |
  118. eval "$cmp" /dev/fd/5 - >&3) 5<&0
  119. )
  120. else
  121. F=`expr "/$2" : '.*/\(.*\)[-.][ablmotxz2]*$'` || F=$prog
  122. tmp=
  123. trap '
  124. test -n "$tmp" && rm -rf "$tmp"
  125. (exit 2); exit 2
  126. ' HUP INT PIPE TERM 0
  127. if type mktemp >/dev/null 2>&1; then
  128. # Note that FreeBSD's mktemp isn't fully compatible with
  129. # the implementations from mktemp.org and GNU coreutils.
  130. # It is important that the -t argument is the last argument
  131. # and that no "--" is used between -t and the template argument.
  132. # This way this command works on all implementations.
  133. tmp=`mktemp -d -t "$prog.XXXXXXXXXX"` || exit 2
  134. else
  135. # Fallback code if mktemp is missing. This isn't as
  136. # robust as using mktemp since this doesn't try with
  137. # different file names in case of a file name conflict.
  138. #
  139. # There's no need to save the original umask since
  140. # we don't create any non-temp files. Note that using
  141. # mkdir -m 0077 isn't secure since some mkdir implementations
  142. # create the dir with the default umask and chmod the
  143. # the dir afterwards.
  144. umask 0077
  145. mkdir -- "${TMPDIR-/tmp}/$prog.$$" || exit 2
  146. tmp="${TMPDIR-/tmp}/$prog.$$"
  147. fi
  148. $xz2 -cdf -- "$2" > "$tmp/$F" || exit 2
  149. xz_status=$(
  150. exec 4>&1
  151. ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
  152. eval "$cmp" - '"$tmp/$F"' >&3
  153. )
  154. cmp_status=$?
  155. rm -rf "$tmp" || xz_status=$?
  156. trap - HUP INT PIPE TERM 0
  157. (exit $cmp_status)
  158. fi;;
  159. *)
  160. xz_status=$(
  161. exec 4>&1
  162. ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
  163. eval "$cmp" - '"$2"' >&3
  164. );;
  165. esac;;
  166. *)
  167. case "$2" in
  168. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | -)
  169. xz_status=$(
  170. exec 4>&1
  171. ($xz2 -cdf -- "$2" 4>&-; echo $? >&4) 3>&- |
  172. eval "$cmp" '"$1"' - >&3
  173. );;
  174. *)
  175. eval "$cmp" '"$1"' '"$2"';;
  176. esac;;
  177. esac
  178. else
  179. printf '%s\n' "$0: Invalid number of operands; try \`${0##*/} --help' for help" >&2
  180. exit 2
  181. fi
  182. cmp_status=$?
  183. for num in $xz_status ; do
  184. # 0 from decompressor means successful decompression. SIGPIPE from
  185. # decompressor is possible when diff or cmp exits before the whole file
  186. # has been decompressed. In that case we want to retain the exit status
  187. # from diff or cmp. Note that using "trap '' PIPE" is not possible
  188. # because gzip changes its behavior (including exit status) if SIGPIPE
  189. # is ignored.
  190. test "$num" -eq 0 && continue
  191. test "$num" -ge 128 \
  192. && test "$(kill -l "$num" 2> /dev/null)" = "PIPE" \
  193. && continue
  194. exit 2
  195. done
  196. exit $cmp_status