dpkg-maintscript-helper.sh 8.8 KB

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