xzdiff 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.4.2"
  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 <xz@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. # xz needs -qQ to ignore warnings like unsupported check type.
  53. xz1="$xz -qQ"
  54. xz2="$xz -qQ"
  55. xz_status=0
  56. exec 3>&1
  57. if test $# -eq 1; then
  58. case $1 in
  59. *[-.]xz | *[-.]lzma | *[-.]lz | *.t[lx]z)
  60. ;;
  61. *[-.]bz2 | *.tbz | *.tbz2)
  62. xz1=bzip2;;
  63. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z)
  64. xz1=gzip;;
  65. *[-.]lzo | *.tzo)
  66. xz1=lzop;;
  67. *[-.]zst | *.tzst)
  68. xz1='zstd -q';;
  69. *)
  70. printf '%s\n' "$0: $1: Unknown compressed file name suffix" >&2
  71. exit 2;;
  72. esac
  73. case $1 in
  74. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lz | *[-.]lzo | *[-.]zst)
  75. FILE=`expr "X$1" : 'X\(.*\)[-.][abglmostxzZ2]*$'`;;
  76. *.t[abglx]z)
  77. FILE=`expr "X$1" : 'X\(.*[-.]t\)[abglx]z$'`ar;;
  78. *.tbz2)
  79. FILE=`expr "X$1" : 'X\(.*[-.]t\)bz2$'`ar;;
  80. *.tzo)
  81. FILE=`expr "X$1" : 'X\(.*[-.]t\)zo$'`ar;;
  82. *.tzst)
  83. FILE=`expr "X$1" : 'X\(.*[-.]t\)zst$'`ar;;
  84. esac
  85. xz_status=$(
  86. exec 4>&1
  87. ($xz1 -cd -- "$1" 4>&-; echo $? >&4) 3>&- | eval "$cmp" - '"$FILE"' >&3
  88. )
  89. elif test $# -eq 2; then
  90. case $1 in
  91. *[-.]bz2 | *.tbz | *.tbz2) xz1=bzip2;;
  92. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) xz1=gzip;;
  93. *[-.]lzo | *.tzo) xz1=lzop;;
  94. *[-.]zst | *.tzst) xz1='zstd -q';;
  95. esac
  96. case $2 in
  97. *[-.]bz2 | *.tbz | *.tbz2) xz2=bzip2;;
  98. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) xz2=gzip;;
  99. *[-.]lzo | *.tzo) xz2=lzop;;
  100. *[-.]zst | *.tzst) xz2='zstd -q';;
  101. esac
  102. case $1 in
  103. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lz | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | -)
  104. case "$2" in
  105. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lz | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | -)
  106. if test "$1$2" = --; then
  107. xz_status=$(
  108. exec 4>&1
  109. ($xz1 -cdf - 4>&-; echo $? >&4) 3>&- |
  110. eval "$cmp" - - >&3
  111. )
  112. elif # Reject Solaris 8's buggy /bin/bash 2.03.
  113. echo X | (echo X | eval "$cmp" /dev/fd/5 - >/dev/null 2>&1) 5<&0; then
  114. # NOTE: xz_status will contain two numbers.
  115. xz_status=$(
  116. exec 4>&1
  117. ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
  118. ( ($xz2 -cdf -- "$2" 4>&-; echo $? >&4) 3>&- 5<&- </dev/null |
  119. eval "$cmp" /dev/fd/5 - >&3) 5<&0
  120. )
  121. else
  122. F=`expr "/$2" : '.*/\(.*\)[-.][ablmotxz2]*$'` || F=$prog
  123. tmp=
  124. trap '
  125. test -n "$tmp" && rm -rf "$tmp"
  126. (exit 2); exit 2
  127. ' HUP INT PIPE TERM 0
  128. if type mktemp >/dev/null 2>&1; then
  129. # Note that FreeBSD's mktemp isn't fully compatible with
  130. # the implementations from mktemp.org and GNU coreutils.
  131. # It is important that the -t argument is the last argument
  132. # and that no "--" is used between -t and the template argument.
  133. # This way this command works on all implementations.
  134. tmp=`mktemp -d -t "$prog.XXXXXXXXXX"` || exit 2
  135. else
  136. # Fallback code if mktemp is missing. This isn't as
  137. # robust as using mktemp since this doesn't try with
  138. # different file names in case of a file name conflict.
  139. #
  140. # There's no need to save the original umask since
  141. # we don't create any non-temp files. Note that using
  142. # mkdir -m 0077 isn't secure since some mkdir implementations
  143. # create the dir with the default umask and chmod the
  144. # the dir afterwards.
  145. umask 0077
  146. mkdir -- "${TMPDIR-/tmp}/$prog.$$" || exit 2
  147. tmp="${TMPDIR-/tmp}/$prog.$$"
  148. fi
  149. $xz2 -cdf -- "$2" > "$tmp/$F" || exit 2
  150. xz_status=$(
  151. exec 4>&1
  152. ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
  153. eval "$cmp" - '"$tmp/$F"' >&3
  154. )
  155. cmp_status=$?
  156. rm -rf "$tmp" || xz_status=$?
  157. trap - HUP INT PIPE TERM 0
  158. (exit $cmp_status)
  159. fi;;
  160. *)
  161. xz_status=$(
  162. exec 4>&1
  163. ($xz1 -cdf -- "$1" 4>&-; echo $? >&4) 3>&- |
  164. eval "$cmp" - '"$2"' >&3
  165. );;
  166. esac;;
  167. *)
  168. case "$2" in
  169. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lz | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | *[-.]zst | *.tzst | -)
  170. xz_status=$(
  171. exec 4>&1
  172. ($xz2 -cdf -- "$2" 4>&-; echo $? >&4) 3>&- |
  173. eval "$cmp" '"$1"' - >&3
  174. );;
  175. *)
  176. eval "$cmp" '"$1"' '"$2"';;
  177. esac;;
  178. esac
  179. else
  180. printf '%s\n' "$0: Invalid number of operands; try \`${0##*/} --help' for help" >&2
  181. exit 2
  182. fi
  183. cmp_status=$?
  184. for num in $xz_status ; do
  185. # 0 from decompressor means successful decompression. SIGPIPE from
  186. # decompressor is possible when diff or cmp exits before the whole file
  187. # has been decompressed. In that case we want to retain the exit status
  188. # from diff or cmp. Note that using "trap '' PIPE" is not possible
  189. # because gzip changes its behavior (including exit status) if SIGPIPE
  190. # is ignored.
  191. test "$num" -eq 0 && continue
  192. test "$num" -ge 128 \
  193. && test "$(kill -l "$num" 2> /dev/null)" = "PIPE" \
  194. && continue
  195. exit 2
  196. done
  197. exit $cmp_status