maintscript-helper 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 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 "$1" != "" ]; do shift; done
  35. shift
  36. [ -n "$PACKAGE" ] || error "couldn't identify the package"
  37. [ -n "$1" ] || error "maintainer script parameters are missing"
  38. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  39. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  40. debug "Executing $0 rm_conffile in $DPKG_MAINTSCRIPT_NAME "\
  41. "of $DPKG_MAINTSCRIPT_PACKAGE"
  42. debug "CONFFILE=$CONFFILE PACKAGE=$PACKAGE "\
  43. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  44. case "$DPKG_MAINTSCRIPT_NAME" in
  45. preinst)
  46. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  47. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  48. prepare_rm_conffile "$CONFFILE" "$PACKAGE"
  49. fi
  50. ;;
  51. postinst)
  52. if [ "$1" = "configure" ] &&
  53. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  54. finish_rm_conffile $CONFFILE
  55. fi
  56. ;;
  57. postrm)
  58. if [ "$1" = "purge" ]; then
  59. rm -f "$CONFFILE.dpkg-bak" "$CONFFILE.dpkg-remove"
  60. fi
  61. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  62. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  63. abort_rm_conffile "$CONFFILE"
  64. fi
  65. ;;
  66. *)
  67. debug "$0 rm_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  68. ;;
  69. esac
  70. }
  71. prepare_rm_conffile() {
  72. local CONFFILE="$1"
  73. local PACKAGE="$2"
  74. [ -e "$CONFFILE" ] || return 0
  75. local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  76. local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  77. sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  78. if [ "$md5sum" != "$old_md5sum" ]; then
  79. echo "Obsolete conffile $CONFFILE has been modified by you."
  80. echo "Saving as $CONFFILE.dpkg-bak ..."
  81. mv -f "$CONFFILE" "$CONFFILE.dpkg-bak"
  82. else
  83. echo "Moving obsolete conffile $CONFFILE out of the way..."
  84. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  85. fi
  86. }
  87. finish_rm_conffile() {
  88. local CONFFILE="$1"
  89. if [ -e "$CONFFILE.dpkg-remove" ]; then
  90. echo "Removing obsolete conffile $CONFFILE ..."
  91. rm -f "$CONFFILE.dpkg-remove"
  92. fi
  93. }
  94. abort_rm_conffile() {
  95. local CONFFILE="$1"
  96. if [ -e "$CONFFILE.dpkg-remove" ]; then
  97. echo "Reinstalling $CONFFILE that was moved away"
  98. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  99. fi
  100. if [ -e "$CONFFILE.dpkg-bak" ]; then
  101. echo "Reinstalling $CONFFILE that was backupped"
  102. mv "$CONFFILE.dpkg-bak" "$CONFFILE"
  103. fi
  104. }
  105. ##
  106. ## Functions to rename a conffile during upgrade
  107. ##
  108. mv_conffile() {
  109. local OLDCONFFILE="$1"
  110. local NEWCONFFILE="$2"
  111. local LASTVERSION="$3"
  112. local PACKAGE="$4"
  113. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  114. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE"
  115. fi
  116. # Skip remaining parameters up to --
  117. while [ "$1" != "--" -a "$1" != "" ]; do shift; done
  118. shift
  119. [ -n "$PACKAGE" ] || error "couldn't identify the package"
  120. [ -n "$1" ] || error "maintainer script parameters are missing"
  121. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  122. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  123. debug "Executing $0 mv_conffile in $DPKG_MAINTSCRIPT_NAME "\
  124. "of $DPKG_MAINTSCRIPT_PACKAGE"
  125. debug "CONFFILE=$OLDCONFFILE -> $NEWCONFFILE PACKAGE=$PACKAGE "\
  126. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  127. case "$DPKG_MAINTSCRIPT_NAME" in
  128. preinst)
  129. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  130. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  131. prepare_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  132. fi
  133. ;;
  134. postinst)
  135. if [ "$1" = "configure" ] &&
  136. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  137. finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE"
  138. fi
  139. ;;
  140. postrm)
  141. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  142. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  143. abort_rm_conffile "$OLDCONFFILE"
  144. fi
  145. ;;
  146. *)
  147. debug "$0 mv_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  148. ;;
  149. esac
  150. }
  151. prepare_mv_conffile() {
  152. local CONFFILE="$1"
  153. local PACKAGE="$2"
  154. [ -e "$CONFFILE" ] || return 0
  155. local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  156. local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  157. sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  158. if [ "$md5sum" = "$old_md5sum" ]; then
  159. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  160. fi
  161. }
  162. finish_mv_conffile() {
  163. local OLDCONFFILE="$1"
  164. local NEWCONFFILE="$2"
  165. rm -f $OLDCONFFILE.dpkg-remove
  166. [ -e "$OLDCONFFILE" ] || return 0
  167. echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
  168. mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
  169. mv -f "$OLDCONFFILE" "$NEWCONFFILE"
  170. }
  171. abort_mv_conffile() {
  172. local CONFFILE="$1"
  173. if [ -e "$CONFFILE.dpkg-remove" ]; then
  174. echo "Reinstalling $CONFFILE that was moved away"
  175. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  176. fi
  177. }
  178. # Common functions
  179. debug() {
  180. if [ -n "$DPKG_DEBUG" ]; then
  181. echo "DEBUG: $PROGNAME: $1" >&2
  182. fi
  183. }
  184. error() {
  185. echo "ERROR: $PROGNAME: $1" >&2
  186. exit 1
  187. }
  188. usage() {
  189. cat <<END
  190. Syntax: $0 <command> [<parameters>] -- <maintainer script parameters>
  191. Commands and parameters:
  192. rm_conffile <conffile> <last-version> [<package>]
  193. Remove obsolete conffile.
  194. Must be called in preinst, postinst and postrm.
  195. mv_conffile <old-conf> <new-conf> <last-version> [<package>]
  196. Rename a conffile.
  197. Must be called in preinst, postinst and postrm.
  198. help
  199. Display this usage information.
  200. END
  201. }
  202. # Main code
  203. set -e
  204. PROGNAME=$(basename $0)
  205. command="$1"
  206. shift
  207. case "$command" in
  208. rm_conffile)
  209. rm_conffile "$@"
  210. ;;
  211. mv_conffile)
  212. mv_conffile "$@"
  213. ;;
  214. --help|help|-?|-h)
  215. usage
  216. ;;
  217. *)
  218. usage
  219. exit 1
  220. esac
  221. exit 0