dpkg-maintscript-helper.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. #!/fs/jb/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. validate_optional_version "$LASTVERSION"
  48. debug "Executing $0 rm_conffile in $DPKG_MAINTSCRIPT_NAME" \
  49. "of $DPKG_MAINTSCRIPT_PACKAGE"
  50. debug "CONFFILE=$CONFFILE PACKAGE=$PACKAGE" \
  51. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  52. case "$DPKG_MAINTSCRIPT_NAME" in
  53. preinst)
  54. if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
  55. dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
  56. prepare_rm_conffile "$CONFFILE" "$PACKAGE"
  57. fi
  58. ;;
  59. postinst)
  60. if [ "$1" = "configure" ] && [ -n "$2" ] &&
  61. dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
  62. finish_rm_conffile "$CONFFILE"
  63. fi
  64. ;;
  65. postrm)
  66. if [ "$1" = "purge" ]; then
  67. rm -f "$CONFFILE.dpkg-bak" "$CONFFILE.dpkg-remove" \
  68. "$CONFFILE.dpkg-backup"
  69. fi
  70. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  71. [ -n "$2" ] &&
  72. dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
  73. abort_rm_conffile "$CONFFILE" "$PACKAGE"
  74. fi
  75. ;;
  76. *)
  77. debug "$0 rm_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  78. ;;
  79. esac
  80. }
  81. prepare_rm_conffile() {
  82. local CONFFILE="$1"
  83. local PACKAGE="$2"
  84. [ -e "$CONFFILE" ] || return 0
  85. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  86. local md5sum old_md5sum
  87. md5sum="$(md5sum "$CONFFILE" | sed -e 's/ .*//')"
  88. old_md5sum="$(dpkg-query -W -f='${Conffiles}' "$PACKAGE" | \
  89. sed -n -e "\'^ $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  90. if [ "$md5sum" != "$old_md5sum" ]; then
  91. mv -f "$CONFFILE" "$CONFFILE.dpkg-backup"
  92. else
  93. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  94. fi
  95. }
  96. finish_rm_conffile() {
  97. local CONFFILE="$1"
  98. if [ -e "$CONFFILE.dpkg-backup" ]; then
  99. echo "Obsolete conffile $CONFFILE has been modified by you."
  100. echo "Saving as $CONFFILE.dpkg-bak ..."
  101. mv -f "$CONFFILE.dpkg-backup" "$CONFFILE.dpkg-bak"
  102. fi
  103. if [ -e "$CONFFILE.dpkg-remove" ]; then
  104. echo "Removing obsolete conffile $CONFFILE ..."
  105. rm -f "$CONFFILE.dpkg-remove"
  106. fi
  107. }
  108. abort_rm_conffile() {
  109. local CONFFILE="$1"
  110. local PACKAGE="$2"
  111. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  112. if [ -e "$CONFFILE.dpkg-remove" ]; then
  113. echo "Reinstalling $CONFFILE that was moved away"
  114. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  115. fi
  116. if [ -e "$CONFFILE.dpkg-backup" ]; then
  117. echo "Reinstalling $CONFFILE that was backupped"
  118. mv "$CONFFILE.dpkg-backup" "$CONFFILE"
  119. fi
  120. }
  121. ##
  122. ## Functions to rename a conffile during upgrade
  123. ##
  124. mv_conffile() {
  125. local OLDCONFFILE="$1"
  126. local NEWCONFFILE="$2"
  127. local LASTVERSION="$3"
  128. local PACKAGE="$4"
  129. if [ "$LASTVERSION" = "--" ]; then
  130. LASTVERSION=""
  131. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  132. fi
  133. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  134. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  135. fi
  136. # Skip remaining parameters up to --
  137. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  138. [ $# -gt 0 ] || badusage "missing arguments after --"
  139. shift
  140. [ -n "$PACKAGE" ] || error "couldn't identify the package"
  141. [ -n "$1" ] || error "maintainer script parameters are missing"
  142. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  143. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  144. [ "${OLDCONFFILE}" != "${OLDCONFFILE#/}" ] || \
  145. error "old-conffile '$OLDCONFFILE' is not an absolute path"
  146. [ "${NEWCONFFILE}" != "${NEWCONFFILE#/}" ] || \
  147. error "new-conffile '$NEWCONFFILE' is not an absolute path"
  148. validate_optional_version "$LASTVERSION"
  149. debug "Executing $0 mv_conffile in $DPKG_MAINTSCRIPT_NAME" \
  150. "of $DPKG_MAINTSCRIPT_PACKAGE"
  151. debug "CONFFILE=$OLDCONFFILE -> $NEWCONFFILE PACKAGE=$PACKAGE" \
  152. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  153. case "$DPKG_MAINTSCRIPT_NAME" in
  154. preinst)
  155. if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
  156. dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
  157. prepare_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  158. fi
  159. ;;
  160. postinst)
  161. if [ "$1" = "configure" ] && [ -n "$2" ] &&
  162. dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
  163. finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE" "$PACKAGE"
  164. fi
  165. ;;
  166. postrm)
  167. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  168. [ -n "$2" ] &&
  169. dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
  170. abort_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  171. fi
  172. ;;
  173. *)
  174. debug "$0 mv_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  175. ;;
  176. esac
  177. }
  178. prepare_mv_conffile() {
  179. local CONFFILE="$1"
  180. local PACKAGE="$2"
  181. [ -e "$CONFFILE" ] || return 0
  182. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  183. local md5sum old_md5sum
  184. md5sum="$(md5sum "$CONFFILE" | sed -e 's/ .*//')"
  185. old_md5sum="$(dpkg-query -W -f='${Conffiles}' "$PACKAGE" | \
  186. sed -n -e "\'^ $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  187. if [ "$md5sum" = "$old_md5sum" ]; then
  188. mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  189. fi
  190. }
  191. finish_mv_conffile() {
  192. local OLDCONFFILE="$1"
  193. local NEWCONFFILE="$2"
  194. local PACKAGE="$3"
  195. rm -f "$OLDCONFFILE.dpkg-remove"
  196. [ -e "$OLDCONFFILE" ] || return 0
  197. ensure_package_owns_file "$PACKAGE" "$OLDCONFFILE" || return 0
  198. echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
  199. if [ -e "$NEWCONFFILE" ]; then
  200. mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
  201. fi
  202. mv -f "$OLDCONFFILE" "$NEWCONFFILE"
  203. }
  204. abort_mv_conffile() {
  205. local CONFFILE="$1"
  206. local PACKAGE="$2"
  207. ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
  208. if [ -e "$CONFFILE.dpkg-remove" ]; then
  209. echo "Reinstalling $CONFFILE that was moved away"
  210. mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  211. fi
  212. }
  213. ##
  214. ## Functions to replace a symlink with a directory
  215. ##
  216. symlink_to_dir() {
  217. local SYMLINK="$1"
  218. local SYMLINK_TARGET="$2"
  219. local LASTVERSION="$3"
  220. local PACKAGE="$4"
  221. if [ "$LASTVERSION" = "--" ]; then
  222. LASTVERSION=""
  223. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  224. fi
  225. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  226. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  227. fi
  228. # Skip remaining parameters up to --
  229. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  230. [ $# -gt 0 ] || badusage "missing arguments after --"
  231. shift
  232. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  233. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  234. [ -n "$PACKAGE" ] || error "cannot identify the package"
  235. [ -n "$SYMLINK" ] || error "symlink parameter is missing"
  236. [ "${SYMLINK#/}" = "$SYMLINK" ] && \
  237. error "symlink pathname is not an absolute path"
  238. [ "${SYMLINK%/}" = "$SYMLINK" ] || \
  239. error "symlink pathname ends with a slash"
  240. [ -n "$SYMLINK_TARGET" ] || error "original symlink target is missing"
  241. [ -n "$1" ] || error "maintainer script parameters are missing"
  242. validate_optional_version "$LASTVERSION"
  243. debug "Executing $0 symlink_to_dir in $DPKG_MAINTSCRIPT_NAME" \
  244. "of $DPKG_MAINTSCRIPT_PACKAGE"
  245. debug "SYMLINK=$SYMLINK -> $SYMLINK_TARGET PACKAGE=$PACKAGE" \
  246. "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  247. case "$DPKG_MAINTSCRIPT_NAME" in
  248. preinst)
  249. if [ "$1" = "install" -o "$1" = "upgrade" ] &&
  250. [ -n "$2" ] && [ -h "$SYMLINK" ] &&
  251. symlink_match "$SYMLINK" "$SYMLINK_TARGET" &&
  252. dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
  253. mv -f "$SYMLINK" "${SYMLINK}.dpkg-backup"
  254. fi
  255. ;;
  256. postinst)
  257. # We cannot bail depending on the version, as here we only
  258. # know what was the last configured version, and we might
  259. # have been unpacked, then upgraded with an unpack and thus
  260. # never been configured before.
  261. if [ "$1" = "configure" ] && [ -h "${SYMLINK}.dpkg-backup" ] &&
  262. symlink_match "${SYMLINK}.dpkg-backup" "$SYMLINK_TARGET"
  263. then
  264. rm -f "${SYMLINK}.dpkg-backup"
  265. fi
  266. ;;
  267. postrm)
  268. if [ "$1" = "purge" ] && [ -h "${SYMLINK}.dpkg-backup" ]; then
  269. rm -f "${SYMLINK}.dpkg-backup"
  270. fi
  271. if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  272. [ -n "$2" ] &&
  273. [ ! -e "$SYMLINK" ] && [ -h "${SYMLINK}.dpkg-backup" ] &&
  274. symlink_match "${SYMLINK}.dpkg-backup" "$SYMLINK_TARGET" &&
  275. dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
  276. echo "Restoring backup of $SYMLINK ..."
  277. mv "${SYMLINK}.dpkg-backup" "$SYMLINK"
  278. fi
  279. ;;
  280. *)
  281. debug "$0 symlink_to_dir not required in $DPKG_MAINTSCRIPT_NAME"
  282. ;;
  283. esac
  284. }
  285. ##
  286. ## Functions to replace a directory with a symlink
  287. ##
  288. dir_to_symlink() {
  289. local PATHNAME="${1%/}"
  290. local SYMLINK_TARGET="$2"
  291. local LASTVERSION="$3"
  292. local PACKAGE="$4"
  293. if [ "$LASTVERSION" = "--" ]; then
  294. LASTVERSION=""
  295. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  296. fi
  297. if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  298. PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
  299. fi
  300. # Skip remaining parameters up to --
  301. while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  302. [ $# -gt 0 ] || badusage "missing arguments after --"
  303. shift
  304. [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  305. error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  306. [ -n "$PACKAGE" ] || error "cannot identify the package"
  307. [ -n "$PATHNAME" ] || error "directory parameter is missing"
  308. [ "${PATHNAME#/}" = "$PATHNAME" ] && \
  309. error "directory parameter is not an absolute path"
  310. [ -n "$SYMLINK_TARGET" ] || error "new symlink target is missing"
  311. [ -n "$1" ] || error "maintainer script parameters are missing"
  312. validate_optional_version "$LASTVERSION"
  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. validate_optional_version() {
  444. local VERSION="$1"
  445. if [ -z "$VERSION" ]; then
  446. return
  447. fi
  448. if ! VERSIONCHECK=$(dpkg --validate-version -- "$VERSION" 2>&1); then
  449. error "$VERSIONCHECK"
  450. fi
  451. }
  452. ensure_package_owns_file() {
  453. local PACKAGE="$1"
  454. local FILE="$2"
  455. if ! dpkg-query -L "$PACKAGE" | grep -F -q -x "$FILE"; then
  456. debug "File '$FILE' not owned by package " \
  457. "'$PACKAGE', skipping $command"
  458. return 1
  459. fi
  460. return 0
  461. }
  462. symlink_match()
  463. {
  464. local SYMLINK="$1"
  465. local SYMLINK_TARGET="$2"
  466. [ "$(readlink "$SYMLINK")" = "$SYMLINK_TARGET" ] || \
  467. [ "$(readlink -f "$SYMLINK")" = "$SYMLINK_TARGET" ]
  468. }
  469. debug() {
  470. if [ -n "$DPKG_DEBUG" ]; then
  471. echo "DEBUG: $PROGNAME: $*" >&2
  472. fi
  473. }
  474. error() {
  475. echo "$PROGNAME: error: $*" >&2
  476. exit 1
  477. }
  478. warning() {
  479. echo "$PROGNAME: warning: $*" >&2
  480. }
  481. usage() {
  482. cat <<END
  483. Usage: $PROGNAME <command> <parameter>... -- <maintainer-script-parameter>...
  484. Commands:
  485. supports <command>
  486. Returns 0 (success) if the given command is supported, 1 otherwise.
  487. rm_conffile <conffile> [<last-version> [<package>]]
  488. Remove obsolete conffile. Must be called in preinst, postinst and
  489. postrm.
  490. mv_conffile <old-conf> <new-conf> [<last-version> [<package>]]
  491. Rename a conffile. Must be called in preinst, postinst and postrm.
  492. symlink_to_dir <pathname> <old-symlink-target> [<last-version> [<package>]]
  493. Replace a symlink with a directory. Must be called in preinst,
  494. postinst and postrm.
  495. dir_to_symlink <pathname> <new-symlink-target> [<last-version> [<package>]]
  496. Replace a directory with a symlink. Must be called in preinst,
  497. postinst and postrm.
  498. help
  499. Display this usage information.
  500. END
  501. }
  502. badusage() {
  503. echo "$PROGNAME: error: $1" >&2
  504. echo >&2
  505. echo "Use '$PROGNAME help' for program usage information." >&2
  506. exit 1
  507. }
  508. # Main code
  509. set -e
  510. PROGNAME=$(basename "$0")
  511. version="unknown"
  512. command="$1"
  513. [ $# -gt 0 ] || badusage "missing command"
  514. shift
  515. case "$command" in
  516. supports)
  517. case "$1" in
  518. rm_conffile|mv_conffile|symlink_to_dir|dir_to_symlink)
  519. code=0
  520. ;;
  521. *)
  522. code=1
  523. ;;
  524. esac
  525. if [ -z "$DPKG_MAINTSCRIPT_NAME" ]; then
  526. warning "environment variable DPKG_MAINTSCRIPT_NAME missing"
  527. code=1
  528. fi
  529. if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  530. warning "environment variable DPKG_MAINTSCRIPT_PACKAGE missing"
  531. code=1
  532. fi
  533. exit $code
  534. ;;
  535. rm_conffile)
  536. rm_conffile "$@"
  537. ;;
  538. mv_conffile)
  539. mv_conffile "$@"
  540. ;;
  541. symlink_to_dir)
  542. symlink_to_dir "$@"
  543. ;;
  544. dir_to_symlink)
  545. dir_to_symlink "$@"
  546. ;;
  547. --help|help|-?)
  548. usage
  549. ;;
  550. --version)
  551. cat <<-END
  552. Debian $PROGNAME version $version.
  553. This is free software; see the GNU General Public License version 2 or
  554. later for copying conditions. There is NO warranty.
  555. END
  556. ;;
  557. *)
  558. badusage "command $command is unknown
  559. Hint: upgrading dpkg to a newer version might help."
  560. esac
  561. exit 0