dpkg-maintscript-helper.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #!/bin/sh
  2. #
  3. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  4. # Copyright © 2008 Joey Hess <joeyh@debian.org>
  5. # Copyright © 2007 Guillem Jover (modifications on wiki.debian.org)
  6. # Copyright © 2005 Scott James Remnant (original implementation on www.dpkg.org)
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. # The conffile related functions are inspired by
  21. # http://wiki.debian.org/DpkgConffileHandling
  22. # This script is documented in dpkg-maintscript-helper(1)
  23. ##
  24. ## Functions to remove an obsolete conffile during upgrade
  25. ##
  26. rm_conffile() {
  27. local CONFFILE="$1"
  28. local LASTVERSION="$2"
  29. local PACKAGE="$3"
  30. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  31. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE"
  32. fi
  33. # Skip remaining parameters up to --
  34. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  35. [ $# -gt 0 ] || badusage
  36. shift
  37. [ -n "$PACKAGE" ] || error "couldn't identify the package"
  38. [ -n "$1" ] || error "maintainer script parameters are missing"
  39. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  40. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  41. debug "Executing $0 rm_conffile in $DPKG_MAINTSCRIPT_NAME "\
  42. "of $DPKG_MAINTSCRIPT_PACKAGE"
  43. debug "CONFFILE=$CONFFILE PACKAGE=$PACKAGE "\
  44. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  45. case "$DPKG_MAINTSCRIPT_NAME" in
  46. preinst)
  47. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  48. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  49. prepare_rm_conffile "$CONFFILE" "$PACKAGE"
  50. fi
  51. ;;
  52. postinst)
  53. if [ "$1" = "configure" ] &&
  54. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  55. finish_rm_conffile $CONFFILE
  56. fi
  57. ;;
  58. postrm)
  59. if [ "$1" = "purge" ]; then
  60. rm -f "$CONFFILE.dpkg-bak" "$CONFFILE.dpkg-remove"
  61. fi
  62. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  63. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  64. abort_rm_conffile "$CONFFILE"
  65. fi
  66. ;;
  67. *)
  68. debug "$0 rm_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  69. ;;
  70. esac
  71. }
  72. prepare_rm_conffile() {
  73. local CONFFILE="$1"
  74. local PACKAGE="$2"
  75. [ -e "$CONFFILE" ] || return 0
  76. local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  77. local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  78. sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  79. if [ "$md5sum" != "$old_md5sum" ]; then
  80. echo "Obsolete conffile $CONFFILE has been modified by you."
  81. echo "Saving as $CONFFILE.dpkg-bak ..."
  82. mv -f "$CONFFILE" "$CONFFILE.dpkg-bak"
  83. else
  84. echo "Moving obsolete conffile $CONFFILE out of the way..."
  85. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  86. fi
  87. }
  88. finish_rm_conffile() {
  89. local CONFFILE="$1"
  90. if [ -e "$CONFFILE.dpkg-remove" ]; then
  91. echo "Removing obsolete conffile $CONFFILE ..."
  92. rm -f "$CONFFILE.dpkg-remove"
  93. fi
  94. }
  95. abort_rm_conffile() {
  96. local CONFFILE="$1"
  97. if [ -e "$CONFFILE.dpkg-remove" ]; then
  98. echo "Reinstalling $CONFFILE that was moved away"
  99. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  100. fi
  101. if [ -e "$CONFFILE.dpkg-bak" ]; then
  102. echo "Reinstalling $CONFFILE that was backupped"
  103. mv "$CONFFILE.dpkg-bak" "$CONFFILE"
  104. fi
  105. }
  106. ##
  107. ## Functions to rename a conffile during upgrade
  108. ##
  109. mv_conffile() {
  110. local OLDCONFFILE="$1"
  111. local NEWCONFFILE="$2"
  112. local LASTVERSION="$3"
  113. local PACKAGE="$4"
  114. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  115. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE"
  116. fi
  117. # Skip remaining parameters up to --
  118. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  119. [ $# -gt 0 ] || badusage
  120. shift
  121. [ -n "$PACKAGE" ] || error "couldn't identify the package"
  122. [ -n "$1" ] || error "maintainer script parameters are missing"
  123. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  124. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  125. debug "Executing $0 mv_conffile in $DPKG_MAINTSCRIPT_NAME "\
  126. "of $DPKG_MAINTSCRIPT_PACKAGE"
  127. debug "CONFFILE=$OLDCONFFILE -> $NEWCONFFILE PACKAGE=$PACKAGE "\
  128. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  129. case "$DPKG_MAINTSCRIPT_NAME" in
  130. preinst)
  131. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  132. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  133. prepare_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  134. fi
  135. ;;
  136. postinst)
  137. if [ "$1" = "configure" ] &&
  138. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  139. finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE"
  140. fi
  141. ;;
  142. postrm)
  143. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  144. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  145. abort_rm_conffile "$OLDCONFFILE"
  146. fi
  147. ;;
  148. *)
  149. debug "$0 mv_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  150. ;;
  151. esac
  152. }
  153. prepare_mv_conffile() {
  154. local CONFFILE="$1"
  155. local PACKAGE="$2"
  156. [ -e "$CONFFILE" ] || return 0
  157. local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  158. local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  159. sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  160. if [ "$md5sum" = "$old_md5sum" ]; then
  161. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  162. fi
  163. }
  164. finish_mv_conffile() {
  165. local OLDCONFFILE="$1"
  166. local NEWCONFFILE="$2"
  167. rm -f $OLDCONFFILE.dpkg-remove
  168. [ -e "$OLDCONFFILE" ] || return 0
  169. echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
  170. mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
  171. mv -f "$OLDCONFFILE" "$NEWCONFFILE"
  172. }
  173. abort_mv_conffile() {
  174. local CONFFILE="$1"
  175. if [ -e "$CONFFILE.dpkg-remove" ]; then
  176. echo "Reinstalling $CONFFILE that was moved away"
  177. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  178. fi
  179. }
  180. # Common functions
  181. debug() {
  182. if [ -n "$DPKG_DEBUG" ]; then
  183. echo "DEBUG: $PROGNAME: $1" >&2
  184. fi
  185. }
  186. error() {
  187. echo "$PROGNAME: error: $1" >&2
  188. exit 1
  189. }
  190. warning() {
  191. echo "$PROGNAME: warning: $1" >&2
  192. }
  193. usage() {
  194. cat <<END
  195. Syntax: $0 <command> [<parameters>] -- <maintainer script parameters>
  196. Commands and parameters:
  197. supports <command>
  198. Returns 0 (success) if the given command is supported, 1
  199. otherwise.
  200. rm_conffile <conffile> <last-version> [<package>]
  201. Remove obsolete conffile.
  202. Must be called in preinst, postinst and postrm.
  203. mv_conffile <old-conf> <new-conf> <last-version> [<package>]
  204. Rename a conffile.
  205. Must be called in preinst, postinst and postrm.
  206. help
  207. Display this usage information.
  208. END
  209. }
  210. badusage() {
  211. usage
  212. exit 1
  213. }
  214. # Main code
  215. set -e
  216. PROGNAME=$(basename $0)
  217. version="unknown"
  218. command="$1"
  219. [ $# -gt 0 ] || badusage
  220. shift
  221. case "$command" in
  222. supports)
  223. case "$1" in
  224. rm_conffile|mv_conffile)
  225. code=0
  226. ;;
  227. *)
  228. code=1
  229. ;;
  230. esac
  231. if [ -z "$DPKG_MAINTSCRIPT_NAME" ]; then
  232. warning "environment variable DPKG_MAINTSCRIPT_NAME missing"
  233. code=1
  234. fi
  235. if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  236. warning "environment variable DPKG_MAINTSCRIPT_PACKAGE missing"
  237. code=1
  238. fi
  239. exit $code
  240. ;;
  241. rm_conffile)
  242. rm_conffile "$@"
  243. ;;
  244. mv_conffile)
  245. mv_conffile "$@"
  246. ;;
  247. --help|help|-?|-h)
  248. usage
  249. ;;
  250. --version)
  251. cat <<-END
  252. Debian $PROGNAME version $version.
  253. Copyright (C) 2010 Raphaël Hertzog <hertzog@debian.org>
  254. Copyright (C) 2008 Joey Hess <joeyh@debian.org>
  255. Copyright (C) 2007 Guillem Jover <guillem@debian.org>
  256. Copyright (C) 2005 Scott James Remnant
  257. This is free software; see the GNU General Public License version 2 or
  258. later for copying conditions. There is NO warranty.
  259. END
  260. ;;
  261. *)
  262. cat >&2 <<-END
  263. $PROGNAME: error: command $command is unknown
  264. Hint: upgrading dpkg to a newer version might help.
  265. END
  266. usage
  267. exit 1
  268. esac
  269. exit 0