dpkg-maintscript-helper.sh 17 KB

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