dpkg-maintscript-helper.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #!/bin/sh
  2. #
  3. # Copyright © 2007, 2011-2015 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 <https://www.gnu.org/licenses/>.
  20. # The conffile related functions are inspired by
  21. # https://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_file "$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. mv -f "$CONFFILE" "$CONFFILE.dpkg-backup"
  88. else
  89. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  90. fi
  91. }
  92. finish_rm_conffile() {
  93. local CONFFILE="$1"
  94. if [ -e "$CONFFILE.dpkg-backup" ]; then
  95. echo "Obsolete conffile $CONFFILE has been modified by you."
  96. echo "Saving as $CONFFILE.dpkg-bak ..."
  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. local PACKAGE="$2"
  107. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  108. if [ -e "$CONFFILE.dpkg-remove" ]; then
  109. echo "Reinstalling $CONFFILE that was moved away"
  110. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  111. fi
  112. if [ -e "$CONFFILE.dpkg-backup" ]; then
  113. echo "Reinstalling $CONFFILE that was backupped"
  114. mv "$CONFFILE.dpkg-backup" "$CONFFILE"
  115. fi
  116. }
  117. ##
  118. ## Functions to rename a conffile during upgrade
  119. ##
  120. mv_conffile() {
  121. local OLDCONFFILE="$1"
  122. local NEWCONFFILE="$2"
  123. local LASTVERSION="$3"
  124. local PACKAGE="$4"
  125. if [ "$LASTVERSION" = "--" ]; then
  126. LASTVERSION=""
  127. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  128. fi
  129. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  130. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  131. fi
  132. # Skip remaining parameters up to --
  133. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  134. [ $# -gt 0 ] || badusage "missing arguments after --"
  135. shift
  136. [ -n "$PACKAGE" ] || error "couldn't identify the package"
  137. [ -n "$1" ] || error "maintainer script parameters are missing"
  138. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  139. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  140. debug "Executing $0 mv_conffile in $DPKG_MAINTSCRIPT_NAME" \
  141. "of $DPKG_MAINTSCRIPT_PACKAGE"
  142. debug "CONFFILE=$OLDCONFFILE -> $NEWCONFFILE PACKAGE=$PACKAGE" \
  143. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  144. case "$DPKG_MAINTSCRIPT_NAME" in
  145. preinst)
  146. if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
  147. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  148. prepare_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  149. fi
  150. ;;
  151. postinst)
  152. if [ "$1" = "configure" ] && [ -n "$2" ] &&
  153. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  154. finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE" "$PACKAGE"
  155. fi
  156. ;;
  157. postrm)
  158. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  159. [ -n "$2" ] &&
  160. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  161. abort_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  162. fi
  163. ;;
  164. *)
  165. debug "$0 mv_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  166. ;;
  167. esac
  168. }
  169. prepare_mv_conffile() {
  170. local CONFFILE="$1"
  171. local PACKAGE="$2"
  172. [ -e "$CONFFILE" ] || return 0
  173. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  174. local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  175. local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  176. sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  177. if [ "$md5sum" = "$old_md5sum" ]; then
  178. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  179. fi
  180. }
  181. finish_mv_conffile() {
  182. local OLDCONFFILE="$1"
  183. local NEWCONFFILE="$2"
  184. local PACKAGE="$3"
  185. rm -f $OLDCONFFILE.dpkg-remove
  186. [ -e "$OLDCONFFILE" ] || return 0
  187. ensure_package_owns_file "$PACKAGE" "$OLDCONFFILE" || return 0
  188. echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
  189. if [ -e "$NEWCONFFILE" ]; then
  190. mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
  191. fi
  192. mv -f "$OLDCONFFILE" "$NEWCONFFILE"
  193. }
  194. abort_mv_conffile() {
  195. local CONFFILE="$1"
  196. local PACKAGE="$2"
  197. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  198. if [ -e "$CONFFILE.dpkg-remove" ]; then
  199. echo "Reinstalling $CONFFILE that was moved away"
  200. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  201. fi
  202. }
  203. ##
  204. ## Functions to replace a symlink with a directory
  205. ##
  206. symlink_to_dir() {
  207. local SYMLINK="$1"
  208. local SYMLINK_TARGET="$2"
  209. local LASTVERSION="$3"
  210. local PACKAGE="$4"
  211. if [ "$LASTVERSION" = "--" ]; then
  212. LASTVERSION=""
  213. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  214. fi
  215. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  216. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  217. fi
  218. # Skip remaining parameters up to --
  219. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  220. [ $# -gt 0 ] || badusage "missing arguments after --"
  221. shift
  222. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  223. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  224. [ -n "$PACKAGE" ] || error "cannot identify the package"
  225. [ -n "$SYMLINK" ] || error "symlink parameter is missing"
  226. [ "${SYMLINK#/}" = "$SYMLINK" ] && \
  227. error "symlink pathname is not an absolute path"
  228. [ "${SYMLINK%/}" = "$SYMLINK" ] || \
  229. error "symlink pathname ends with a slash"
  230. [ -n "$SYMLINK_TARGET" ] || error "original symlink target is missing"
  231. [ -n "$1" ] || error "maintainer script parameters are missing"
  232. debug "Executing $0 symlink_to_dir in $DPKG_MAINTSCRIPT_NAME" \
  233. "of $DPKG_MAINTSCRIPT_PACKAGE"
  234. debug "SYMLINK=$SYMLINK -> $SYMLINK_TARGET PACKAGE=$PACKAGE" \
  235. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  236. case "$DPKG_MAINTSCRIPT_NAME" in
  237. preinst)
  238. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  239. [ -n "$2" ] && [ -h "$SYMLINK" ] &&
  240. symlink_match "$SYMLINK" "$SYMLINK_TARGET" &&
  241. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  242. mv -f "$SYMLINK" "${SYMLINK}.dpkg-backup"
  243. fi
  244. ;;
  245. postinst)
  246. # We cannot bail depending on the version, as here we only
  247. # know what was the last configured version, and we might
  248. # have been unpacked, then upgraded with an unpack and thus
  249. # never been configured before.
  250. if [ "$1" = "configure" ] && [ -h "${SYMLINK}.dpkg-backup" ] &&
  251. symlink_match "${SYMLINK}.dpkg-backup" "$SYMLINK_TARGET"
  252. then
  253. rm -f "${SYMLINK}.dpkg-backup"
  254. fi
  255. ;;
  256. postrm)
  257. if [ "$1" = "purge" ] && [ -h "${SYMLINK}.dpkg-backup" ]; then
  258. rm -f "${SYMLINK}.dpkg-backup"
  259. fi
  260. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  261. [ -n "$2" ] &&
  262. [ ! -e "$SYMLINK" ] && [ -h "${SYMLINK}.dpkg-backup" ] &&
  263. symlink_match "${SYMLINK}.dpkg-backup" "$SYMLINK_TARGET" &&
  264. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  265. echo "Restoring backup of $SYMLINK ..."
  266. mv "${SYMLINK}.dpkg-backup" "$SYMLINK"
  267. fi
  268. ;;
  269. *)
  270. debug "$0 symlink_to_dir not required in $DPKG_MAINTSCRIPT_NAME"
  271. ;;
  272. esac
  273. }
  274. ##
  275. ## Functions to replace a directory with a symlink
  276. ##
  277. dir_to_symlink() {
  278. local PATHNAME="${1%/}"
  279. local SYMLINK_TARGET="$2"
  280. local LASTVERSION="$3"
  281. local PACKAGE="$4"
  282. if [ "$LASTVERSION" = "--" ]; then
  283. LASTVERSION=""
  284. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  285. fi
  286. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  287. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  288. fi
  289. # Skip remaining parameters up to --
  290. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  291. [ $# -gt 0 ] || badusage "missing arguments after --"
  292. shift
  293. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  294. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  295. [ -n "$PACKAGE" ] || error "cannot identify the package"
  296. [ -n "$PATHNAME" ] || error "directory parameter is missing"
  297. [ "${PATHNAME#/}" = "$PATHNAME" ] && \
  298. error "directory parameter is not an absolute path"
  299. [ -n "$SYMLINK_TARGET" ] || error "new symlink target is missing"
  300. [ -n "$1" ] || error "maintainer script parameters are missing"
  301. debug "Executing $0 dir_to_symlink in $DPKG_MAINTSCRIPT_NAME" \
  302. "of $DPKG_MAINTSCRIPT_PACKAGE"
  303. debug "PATHNAME=$PATHNAME SYMLINK_TARGET=$SYMLINK_TARGET" \
  304. "PACKAGE=$PACKAGE LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  305. case "$DPKG_MAINTSCRIPT_NAME" in
  306. preinst)
  307. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  308. [ -n "$2" ] &&
  309. [ ! -h "$PATHNAME" ] && [ -d "$PATHNAME" ] &&
  310. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  311. prepare_dir_to_symlink "$PACKAGE" "$PATHNAME"
  312. fi
  313. ;;
  314. postinst)
  315. # We cannot bail depending on the version, as here we only
  316. # know what was the last configured version, and we might
  317. # have been unpacked, then upgraded with an unpack and thus
  318. # never been configured before.
  319. if [ "$1" = "configure" ] &&
  320. [ -d "${PATHNAME}.dpkg-backup" ] &&
  321. [ ! -h "$PATHNAME" ] && [ -d "$PATHNAME" ] &&
  322. [ -f "$PATHNAME/.dpkg-staging-dir" ]; then
  323. finish_dir_to_symlink "$PATHNAME" "$SYMLINK_TARGET"
  324. fi
  325. ;;
  326. postrm)
  327. if [ "$1" = "purge" ] && [ -d "${PATHNAME}.dpkg-backup" ]; then
  328. rm -rf "${PATHNAME}.dpkg-backup"
  329. fi
  330. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  331. [ -n "$2" ] &&
  332. [ -d "${PATHNAME}.dpkg-backup" ] &&
  333. [ \( ! -h "$PATHNAME" -a -d "$PATHNAME" -a \
  334. -f "$PATHNAME/.dpkg-staging-dir" \) -o \
  335. \( -h "$PATHNAME" -a \
  336. \( "$(readlink $PATHNAME)" = "$SYMLINK_TARGET" -o \
  337. "$(readlink -f $PATHNAME)" = "$SYMLINK_TARGET" \) \) ] &&
  338. dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  339. abort_dir_to_symlink "$PATHNAME"
  340. fi
  341. ;;
  342. *)
  343. debug "$0 dir_to_symlink not required in $DPKG_MAINTSCRIPT_NAME"
  344. ;;
  345. esac
  346. }
  347. prepare_dir_to_symlink()
  348. {
  349. local PACKAGE="$1"
  350. local PATHNAME="$2"
  351. # If there are conffiles we should not perform the switch.
  352. if dpkg-query -W -f='${Conffiles}' "$PACKAGE" | \
  353. grep -q "$PATHNAME/."; then
  354. error "directory '$PATHNAME' contains conffiles," \
  355. "cannot switch to symlink"
  356. fi
  357. # If there are locally created files or files owned by another package
  358. # we should not perform the switch.
  359. find "$PATHNAME" -print0 | xargs -0 -n1 sh -c '
  360. package="$1"
  361. file="$2"
  362. if ! dpkg-query -L "$package" | grep -q -x "$file"; then
  363. exit 1
  364. fi
  365. exit 0
  366. ' check-files-ownership "$PACKAGE" || \
  367. error "directory '$PATHNAME' contains files not owned by" \
  368. "package $PACKAGE, cannot switch to symlink"
  369. # At this point, we know that the directory either contains no files,
  370. # or only non-conffiles owned by the package.
  371. #
  372. # To do the switch we cannot simply replace it with the final symlink
  373. # just yet, because dpkg needs to remove any file present in the old
  374. # package that have disappeared in the new one, and we do not want to
  375. # lose files resolving to the same pathname in the symlink target.
  376. #
  377. # We cannot replace the directory with a staging symlink either,
  378. # because dpkg will update symlinks to their new target.
  379. #
  380. # So we need to create a staging directory, to avoid removing files
  381. # from other packages, and to trap any new files in the directory
  382. # to move them to their correct place later on.
  383. mv -f "$PATHNAME" "${PATHNAME}.dpkg-backup"
  384. mkdir "$PATHNAME"
  385. # Mark it as a staging directory, so that we can track things.
  386. touch "$PATHNAME/.dpkg-staging-dir"
  387. }
  388. finish_dir_to_symlink()
  389. {
  390. local PATHNAME="$1"
  391. local SYMLINK_TARGET="$2"
  392. # Move the contents of the staging directory to the symlink target,
  393. # as those are all new files installed between this package being
  394. # unpacked and configured.
  395. local ABS_SYMLINK_TARGET
  396. if [ "${SYMLINK_TARGET#/}" = "$SYMLINK_TARGET" ]; then
  397. ABS_SYMLINK_TARGET="$(dirname "$PATHNAME")/$SYMLINK_TARGET"
  398. else
  399. ABS_SYMLINK_TARGET="$SYMLINK_TARGET"
  400. fi
  401. rm "$PATHNAME/.dpkg-staging-dir"
  402. find "$PATHNAME" -mindepth 1 -print0 | \
  403. xargs -0 -i% mv -f "%" "$ABS_SYMLINK_TARGET/"
  404. # Remove the staging directory.
  405. rmdir "$PATHNAME"
  406. # Do the actual switch.
  407. ln -s "$SYMLINK_TARGET" "$PATHNAME"
  408. # We are left behind the old files owned by this package in the backup
  409. # directory, just remove it.
  410. rm -rf "${PATHNAME}.dpkg-backup"
  411. }
  412. abort_dir_to_symlink()
  413. {
  414. local PATHNAME="$1"
  415. echo "Restoring backup of $PATHNAME ..."
  416. if [ -h "$PATHNAME" ]; then
  417. rm -f "$PATHNAME"
  418. else
  419. # The staging directory must be empty, as no other package
  420. # should have been unpacked inbetween.
  421. rm -f "$PATHNAME/.dpkg-staging-dir"
  422. rmdir "$PATHNAME"
  423. fi
  424. mv "${PATHNAME}.dpkg-backup" "$PATHNAME"
  425. }
  426. # Common functions
  427. ensure_package_owns_file() {
  428. local PACKAGE="$1"
  429. local FILE="$2"
  430. if ! dpkg-query -L "$PACKAGE" | grep -q -x "$FILE"; then
  431. debug "File '$FILE' not owned by package " \
  432. "'$PACKAGE', skipping $command"
  433. return 1
  434. fi
  435. return 0
  436. }
  437. symlink_match()
  438. {
  439. local SYMLINK="$1"
  440. local SYMLINK_TARGET="$2"
  441. [ "$(readlink $SYMLINK)" = "$SYMLINK_TARGET" ] || \
  442. [ "$(readlink -f $SYMLINK)" = "$SYMLINK_TARGET" ]
  443. }
  444. debug() {
  445. if [ -n "$DPKG_DEBUG" ]; then
  446. echo "DEBUG: $PROGNAME: $*" >&2
  447. fi
  448. }
  449. error() {
  450. echo "$PROGNAME: error: $*" >&2
  451. exit 1
  452. }
  453. warning() {
  454. echo "$PROGNAME: warning: $*" >&2
  455. }
  456. usage() {
  457. cat <<END
  458. Usage: $PROGNAME <command> <parameter>... -- <maintainer-script-parameter>...
  459. Commands:
  460. supports <command>
  461. Returns 0 (success) if the given command is supported, 1 otherwise.
  462. rm_conffile <conffile> [<last-version> [<package>]]
  463. Remove obsolete conffile. Must be called in preinst, postinst and
  464. postrm.
  465. mv_conffile <old-conf> <new-conf> [<last-version> [<package>]]
  466. Rename a conffile. Must be called in preinst, postinst and postrm.
  467. symlink_to_dir <pathname> <old-symlink-target> [<last-version> [<package>]]
  468. Replace a symlink with a directory. Must be called in preinst,
  469. postinst and postrm.
  470. dir_to_symlink <pathname> <new-symlink-target> [<last-version> [<package>]]
  471. Replace a directory with a symlink. Must be called in preinst,
  472. postinst and postrm.
  473. help
  474. Display this usage information.
  475. END
  476. }
  477. badusage() {
  478. echo "$PROGNAME: error: $1" >&2
  479. echo >&2
  480. echo "Use '$PROGNAME help' for program usage information." >&2
  481. exit 1
  482. }
  483. # Main code
  484. set -e
  485. PROGNAME=$(basename $0)
  486. version="unknown"
  487. command="$1"
  488. [ $# -gt 0 ] || badusage "missing command"
  489. shift
  490. case "$command" in
  491. supports)
  492. case "$1" in
  493. rm_conffile|mv_conffile|symlink_to_dir|dir_to_symlink)
  494. code=0
  495. ;;
  496. *)
  497. code=1
  498. ;;
  499. esac
  500. if [ -z "$DPKG_MAINTSCRIPT_NAME" ]; then
  501. warning "environment variable DPKG_MAINTSCRIPT_NAME missing"
  502. code=1
  503. fi
  504. if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  505. warning "environment variable DPKG_MAINTSCRIPT_PACKAGE missing"
  506. code=1
  507. fi
  508. exit $code
  509. ;;
  510. rm_conffile)
  511. rm_conffile "$@"
  512. ;;
  513. mv_conffile)
  514. mv_conffile "$@"
  515. ;;
  516. symlink_to_dir)
  517. symlink_to_dir "$@"
  518. ;;
  519. dir_to_symlink)
  520. dir_to_symlink "$@"
  521. ;;
  522. --help|help|-?)
  523. usage
  524. ;;
  525. --version)
  526. cat <<-END
  527. Debian $PROGNAME version $version.
  528. This is free software; see the GNU General Public License version 2 or
  529. later for copying conditions. There is NO warranty.
  530. END
  531. ;;
  532. *)
  533. badusage "command $command is unknown
  534. Hint: upgrading dpkg to a newer version might help."
  535. esac
  536. exit 0