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)
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.
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.
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/>.
21 # The conffile related functions are inspired by
22 # https://wiki.debian.org/DpkgConffileHandling
24 # This script is documented in dpkg-maintscript-helper(1)
27 ## Functions to remove an obsolete conffile during upgrade
31 local LASTVERSION="$2"
33 if [ "$LASTVERSION" = "--" ]; then
35 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
37 if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
38 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
40 # Skip remaining parameters up to --
41 while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
42 [ $# -gt 0 ] || badusage "missing arguments after --"
45 [ -n "$PACKAGE" ] || error "couldn't identify the package"
46 [ -n "$1" ] || error "maintainer script parameters are missing"
47 [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
48 error "environment variable DPKG_MAINTSCRIPT_NAME is required"
49 [ "${CONFFILE}" != "${CONFFILE#/}" ] || \
50 error "conffile '$CONFFILE' is not an absolute path"
51 validate_optional_version "$LASTVERSION"
53 debug "Executing $0 rm_conffile in $DPKG_MAINTSCRIPT_NAME" \
54 "of $DPKG_MAINTSCRIPT_PACKAGE"
55 debug "CONFFILE=$CONFFILE PACKAGE=$PACKAGE" \
56 "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
57 case "$DPKG_MAINTSCRIPT_NAME" in
59 if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
60 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
61 prepare_rm_conffile "$CONFFILE" "$PACKAGE"
65 if [ "$1" = "configure" ] && [ -n "$2" ] &&
66 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
67 finish_rm_conffile "$CONFFILE"
71 if [ "$1" = "purge" ]; then
72 rm -f "$CONFFILE.dpkg-bak" "$CONFFILE.dpkg-remove" \
73 "$CONFFILE.dpkg-backup"
75 if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
77 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
78 abort_rm_conffile "$CONFFILE" "$PACKAGE"
82 debug "$0 rm_conffile not required in $DPKG_MAINTSCRIPT_NAME"
87 prepare_rm_conffile() {
91 [ -e "$CONFFILE" ] || return 0
92 ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
94 local md5sum old_md5sum
95 md5sum="$(md5sum "$CONFFILE" | sed -e 's/ .*//')"
96 old_md5sum="$(dpkg-query -W -f='${Conffiles}' "$PACKAGE" | \
97 sed -n -e "\'^ $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
98 if [ "$md5sum" != "$old_md5sum" ]; then
99 mv -f "$CONFFILE" "$CONFFILE.dpkg-backup"
101 mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
105 finish_rm_conffile() {
108 if [ -e "$CONFFILE.dpkg-backup" ]; then
109 echo "Obsolete conffile $CONFFILE has been modified by you."
110 echo "Saving as $CONFFILE.dpkg-bak ..."
111 mv -f "$CONFFILE.dpkg-backup" "$CONFFILE.dpkg-bak"
113 if [ -e "$CONFFILE.dpkg-remove" ]; then
114 echo "Removing obsolete conffile $CONFFILE ..."
115 rm -f "$CONFFILE.dpkg-remove"
119 abort_rm_conffile() {
123 ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
125 if [ -e "$CONFFILE.dpkg-remove" ]; then
126 echo "Reinstalling $CONFFILE that was moved away"
127 mv "$CONFFILE.dpkg-remove" "$CONFFILE"
129 if [ -e "$CONFFILE.dpkg-backup" ]; then
130 echo "Reinstalling $CONFFILE that was backed-up"
131 mv "$CONFFILE.dpkg-backup" "$CONFFILE"
136 ## Functions to rename a conffile during upgrade
139 local OLDCONFFILE="$1"
140 local NEWCONFFILE="$2"
141 local LASTVERSION="$3"
143 if [ "$LASTVERSION" = "--" ]; then
145 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
147 if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
148 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
150 # Skip remaining parameters up to --
151 while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
152 [ $# -gt 0 ] || badusage "missing arguments after --"
155 [ -n "$PACKAGE" ] || error "couldn't identify the package"
156 [ -n "$1" ] || error "maintainer script parameters are missing"
157 [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
158 error "environment variable DPKG_MAINTSCRIPT_NAME is required"
159 [ "${OLDCONFFILE}" != "${OLDCONFFILE#/}" ] || \
160 error "old-conffile '$OLDCONFFILE' is not an absolute path"
161 [ "${NEWCONFFILE}" != "${NEWCONFFILE#/}" ] || \
162 error "new-conffile '$NEWCONFFILE' is not an absolute path"
163 validate_optional_version "$LASTVERSION"
165 debug "Executing $0 mv_conffile in $DPKG_MAINTSCRIPT_NAME" \
166 "of $DPKG_MAINTSCRIPT_PACKAGE"
167 debug "CONFFILE=$OLDCONFFILE -> $NEWCONFFILE PACKAGE=$PACKAGE" \
168 "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
169 case "$DPKG_MAINTSCRIPT_NAME" in
171 if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
172 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
173 prepare_mv_conffile "$OLDCONFFILE" "$PACKAGE"
177 if [ "$1" = "configure" ] && [ -n "$2" ] &&
178 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
179 finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE" "$PACKAGE"
183 if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
185 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
186 abort_mv_conffile "$OLDCONFFILE" "$PACKAGE"
190 debug "$0 mv_conffile not required in $DPKG_MAINTSCRIPT_NAME"
195 prepare_mv_conffile() {
199 [ -e "$CONFFILE" ] || return 0
201 ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
203 local md5sum old_md5sum
204 md5sum="$(md5sum "$CONFFILE" | sed -e 's/ .*//')"
205 old_md5sum="$(dpkg-query -W -f='${Conffiles}' "$PACKAGE" | \
206 sed -n -e "\'^ $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
207 if [ "$md5sum" = "$old_md5sum" ]; then
208 mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
212 finish_mv_conffile() {
213 local OLDCONFFILE="$1"
214 local NEWCONFFILE="$2"
217 rm -f "$OLDCONFFILE.dpkg-remove"
219 [ -e "$OLDCONFFILE" ] || return 0
220 ensure_package_owns_file "$PACKAGE" "$OLDCONFFILE" || return 0
222 echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
223 if [ -e "$NEWCONFFILE" ]; then
224 mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
226 mv -f "$OLDCONFFILE" "$NEWCONFFILE"
229 abort_mv_conffile() {
233 ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
235 if [ -e "$CONFFILE.dpkg-remove" ]; then
236 echo "Reinstalling $CONFFILE that was moved away"
237 mv "$CONFFILE.dpkg-remove" "$CONFFILE"
242 ## Functions to replace a symlink with a directory
246 local SYMLINK_TARGET="$2"
247 local LASTVERSION="$3"
250 if [ "$LASTVERSION" = "--" ]; then
252 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
254 if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
255 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
258 # Skip remaining parameters up to --
259 while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
260 [ $# -gt 0 ] || badusage "missing arguments after --"
263 [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
264 error "environment variable DPKG_MAINTSCRIPT_NAME is required"
265 [ -n "$PACKAGE" ] || error "cannot identify the package"
266 [ -n "$SYMLINK" ] || error "symlink parameter is missing"
267 [ "${SYMLINK#/}" = "$SYMLINK" ] && \
268 error "symlink pathname is not an absolute path"
269 [ "${SYMLINK%/}" = "$SYMLINK" ] || \
270 error "symlink pathname ends with a slash"
271 [ -n "$SYMLINK_TARGET" ] || error "original symlink target is missing"
272 [ -n "$1" ] || error "maintainer script parameters are missing"
273 validate_optional_version "$LASTVERSION"
275 debug "Executing $0 symlink_to_dir in $DPKG_MAINTSCRIPT_NAME" \
276 "of $DPKG_MAINTSCRIPT_PACKAGE"
277 debug "SYMLINK=$SYMLINK -> $SYMLINK_TARGET PACKAGE=$PACKAGE" \
278 "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
280 case "$DPKG_MAINTSCRIPT_NAME" in
282 if [ "$1" = "install" -o "$1" = "upgrade" ] &&
283 [ -n "$2" ] && [ -h "$SYMLINK" ] &&
284 symlink_match "$SYMLINK" "$SYMLINK_TARGET" &&
285 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
286 mv -f "$SYMLINK" "${SYMLINK}.dpkg-backup"
290 # We cannot bail depending on the version, as here we only
291 # know what was the last configured version, and we might
292 # have been unpacked, then upgraded with an unpack and thus
293 # never been configured before.
294 if [ "$1" = "configure" ] && [ -h "${SYMLINK}.dpkg-backup" ] &&
295 symlink_match "${SYMLINK}.dpkg-backup" "$SYMLINK_TARGET"
297 rm -f "${SYMLINK}.dpkg-backup"
301 if [ "$1" = "purge" ] && [ -h "${SYMLINK}.dpkg-backup" ]; then
302 rm -f "${SYMLINK}.dpkg-backup"
304 if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
306 [ ! -e "$SYMLINK" ] && [ -h "${SYMLINK}.dpkg-backup" ] &&
307 symlink_match "${SYMLINK}.dpkg-backup" "$SYMLINK_TARGET" &&
308 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
309 echo "Restoring backup of $SYMLINK ..."
310 mv "${SYMLINK}.dpkg-backup" "$SYMLINK"
314 debug "$0 symlink_to_dir not required in $DPKG_MAINTSCRIPT_NAME"
320 ## Functions to replace a directory with a symlink
323 local PATHNAME="${1%/}"
324 local SYMLINK_TARGET="$2"
325 local LASTVERSION="$3"
328 if [ "$LASTVERSION" = "--" ]; then
330 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
332 if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
333 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
336 # Skip remaining parameters up to --
337 while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
338 [ $# -gt 0 ] || badusage "missing arguments after --"
341 [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
342 error "environment variable DPKG_MAINTSCRIPT_NAME is required"
343 [ -n "$PACKAGE" ] || error "cannot identify the package"
344 [ -n "$PATHNAME" ] || error "directory parameter is missing"
345 [ "${PATHNAME#/}" = "$PATHNAME" ] && \
346 error "directory parameter is not an absolute path"
347 [ -n "$SYMLINK_TARGET" ] || error "new symlink target is missing"
348 [ -n "$1" ] || error "maintainer script parameters are missing"
349 validate_optional_version "$LASTVERSION"
351 debug "Executing $0 dir_to_symlink in $DPKG_MAINTSCRIPT_NAME" \
352 "of $DPKG_MAINTSCRIPT_PACKAGE"
353 debug "PATHNAME=$PATHNAME SYMLINK_TARGET=$SYMLINK_TARGET" \
354 "PACKAGE=$PACKAGE LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
356 case "$DPKG_MAINTSCRIPT_NAME" in
358 if [ "$1" = "install" -o "$1" = "upgrade" ] &&
360 [ ! -h "$PATHNAME" ] && [ -d "$PATHNAME" ] &&
361 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
362 prepare_dir_to_symlink "$PACKAGE" "$PATHNAME"
366 # We cannot bail depending on the version, as here we only
367 # know what was the last configured version, and we might
368 # have been unpacked, then upgraded with an unpack and thus
369 # never been configured before.
370 if [ "$1" = "configure" ] &&
371 [ -d "${PATHNAME}.dpkg-backup" ] &&
372 [ ! -h "$PATHNAME" ] && [ -d "$PATHNAME" ] &&
373 [ -f "$PATHNAME/.dpkg-staging-dir" ]; then
374 finish_dir_to_symlink "$PATHNAME" "$SYMLINK_TARGET"
378 if [ "$1" = "purge" ] && [ -d "${PATHNAME}.dpkg-backup" ]; then
379 rm -rf "${PATHNAME}.dpkg-backup"
381 if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
383 [ -d "${PATHNAME}.dpkg-backup" ] &&
384 [ \( ! -h "$PATHNAME" -a -d "$PATHNAME" -a \
385 -f "$PATHNAME/.dpkg-staging-dir" \) -o \
386 \( -h "$PATHNAME" -a \
387 \( "$(readlink "$PATHNAME")" = "$SYMLINK_TARGET" -o \
388 "$(readlink -f "$PATHNAME")" = "$SYMLINK_TARGET" \) \) ] &&
389 dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
390 abort_dir_to_symlink "$PATHNAME"
394 debug "$0 dir_to_symlink not required in $DPKG_MAINTSCRIPT_NAME"
399 prepare_dir_to_symlink()
405 # If there are conffiles we should not perform the switch.
406 dpkg-query -W -f='${Conffiles}\n' "$PACKAGE" | while read -r LINE; do
409 error "directory '$PATHNAME' contains conffiles," \
410 "cannot switch to symlink"
415 # If there are locally created files or files owned by another package
416 # we should not perform the switch.
417 export DPKG_MAINTSCRIPT_HELPER_INTERNAL_API="$version"
418 find "$PATHNAME" -print0 | \
419 xargs -0 -n1 $0 _internal_pkg_must_own_file "$PACKAGE" || \
420 error "directory '$PATHNAME' contains files not owned by" \
421 "package $PACKAGE, cannot switch to symlink"
422 unset DPKG_MAINTSCRIPT_HELPER_INTERNAL_API
424 # At this point, we know that the directory either contains no files,
425 # or only non-conffiles owned by the package.
427 # To do the switch we cannot simply replace it with the final symlink
428 # just yet, because dpkg needs to remove any file present in the old
429 # package that have disappeared in the new one, and we do not want to
430 # lose files resolving to the same pathname in the symlink target.
432 # We cannot replace the directory with a staging symlink either,
433 # because dpkg will update symlinks to their new target.
435 # So we need to create a staging directory, to avoid removing files
436 # from other packages, and to trap any new files in the directory
437 # to move them to their correct place later on.
438 mv -f "$PATHNAME" "${PATHNAME}.dpkg-backup"
441 # Mark it as a staging directory, so that we can track things.
442 touch "$PATHNAME/.dpkg-staging-dir"
445 finish_dir_to_symlink()
448 local SYMLINK_TARGET="$2"
450 # Move the contents of the staging directory to the symlink target,
451 # as those are all new files installed between this package being
452 # unpacked and configured.
453 local ABS_SYMLINK_TARGET
454 if [ "${SYMLINK_TARGET#/}" = "$SYMLINK_TARGET" ]; then
455 ABS_SYMLINK_TARGET="$(dirname "$PATHNAME")/$SYMLINK_TARGET"
457 ABS_SYMLINK_TARGET="$SYMLINK_TARGET"
459 rm "$PATHNAME/.dpkg-staging-dir"
460 find "$PATHNAME" -mindepth 1 -print0 | \
461 xargs -0 -i% mv -f "%" "$ABS_SYMLINK_TARGET/"
463 # Remove the staging directory.
466 # Do the actual switch.
467 ln -s "$SYMLINK_TARGET" "$PATHNAME"
469 # We are left behind the old files owned by this package in the backup
470 # directory, just remove it.
471 rm -rf "${PATHNAME}.dpkg-backup"
474 abort_dir_to_symlink()
478 echo "Restoring backup of $PATHNAME ..."
479 if [ -h "$PATHNAME" ]; then
482 # The staging directory must be empty, as no other package
483 # should have been unpacked in between.
484 rm -f "$PATHNAME/.dpkg-staging-dir"
488 mv "${PATHNAME}.dpkg-backup" "$PATHNAME"
492 validate_optional_version() {
495 if [ -z "$VERSION" ]; then
499 if ! VERSIONCHECK=$(dpkg --validate-version -- "$VERSION" 2>&1); then
500 error "$VERSIONCHECK"
504 ensure_package_owns_file() {
508 if ! dpkg-query -L "$PACKAGE" | grep -F -q -x "$FILE"; then
509 debug "File '$FILE' not owned by package " \
510 "'$PACKAGE', skipping $command"
516 internal_pkg_must_own_file()
521 if [ "$DPKG_MAINTSCRIPT_HELPER_INTERNAL_API" != "$version" ]; then
522 error "internal API used by external command"
525 if ! ensure_package_owns_file "$PACKAGE" "$FILE"; then
526 error "file '$FILE' not owned by package '$PACKAGE'"
534 local SYMLINK_TARGET="$2"
536 [ "$(readlink "$SYMLINK")" = "$SYMLINK_TARGET" ] || \
537 [ "$(readlink -f "$SYMLINK")" = "$SYMLINK_TARGET" ]
541 if [ -n "$DPKG_DEBUG" ]; then
542 echo "DEBUG: $PROGNAME: $*" >&2
547 echo "$PROGNAME: error: $*" >&2
552 echo "$PROGNAME: warning: $*" >&2
557 Usage: $PROGNAME <command> <parameter>... -- <maintainer-script-parameter>...
561 Returns 0 (success) if the given command is supported, 1 otherwise.
562 rm_conffile <conffile> [<last-version> [<package>]]
563 Remove obsolete conffile. Must be called in preinst, postinst and
565 mv_conffile <old-conf> <new-conf> [<last-version> [<package>]]
566 Rename a conffile. Must be called in preinst, postinst and postrm.
567 symlink_to_dir <pathname> <old-symlink-target> [<last-version> [<package>]]
568 Replace a symlink with a directory. Must be called in preinst,
570 dir_to_symlink <pathname> <new-symlink-target> [<last-version> [<package>]]
571 Replace a directory with a symlink. Must be called in preinst,
574 Display this usage information.
579 echo "$PROGNAME: error: $1" >&2
581 echo "Use '$PROGNAME help' for program usage information." >&2
588 PROGNAME=$(basename "$0")
591 [ $# -gt 0 ] || badusage "missing command"
597 rm_conffile|mv_conffile|symlink_to_dir|dir_to_symlink)
604 if [ -z "$DPKG_MAINTSCRIPT_NAME" ]; then
605 warning "environment variable DPKG_MAINTSCRIPT_NAME missing"
608 if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
609 warning "environment variable DPKG_MAINTSCRIPT_PACKAGE missing"
626 _internal_pkg_must_own_file)
627 # This is an internal command, must not be used outside this program.
628 internal_pkg_must_own_file "$@"
635 Debian $PROGNAME version $version.
637 This is free software; see the GNU General Public License version 2 or
638 later for copying conditions. There is NO warranty.
642 badusage "command $command is unknown
643 Hint: upgrading dpkg to a newer version might help."