dpkg-maintscript-helper.sh 18 KB

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