dpkg-maintscript-helper.sh 18 KB

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