dpkg-maintscript-helper.sh 8.1 KB

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