chiark / gitweb /
lib/dpkg/tarfn.c: Kludge `tar_header_decode' to handle spurious `errno'.
[dpkg] / scripts / dpkg-maintscript-helper.sh
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
21 # The conffile related functions are inspired by
22 # https://wiki.debian.org/DpkgConffileHandling
23
24 # This script is documented in dpkg-maintscript-helper(1)
25
26 ##
27 ## Functions to remove an obsolete conffile during upgrade
28 ##
29 rm_conffile() {
30         local CONFFILE="$1"
31         local LASTVERSION="$2"
32         local PACKAGE="$3"
33         if [ "$LASTVERSION" = "--" ]; then
34                 LASTVERSION=""
35                 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
36         fi
37         if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
38                 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
39         fi
40         # Skip remaining parameters up to --
41         while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
42         [ $# -gt 0 ] || badusage "missing arguments after --"
43         shift
44
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"
52
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
58         preinst)
59                 if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
60                    dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
61                         prepare_rm_conffile "$CONFFILE" "$PACKAGE"
62                 fi
63                 ;;
64         postinst)
65                 if [ "$1" = "configure" ] && [ -n "$2" ] &&
66                    dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
67                         finish_rm_conffile "$CONFFILE"
68                 fi
69                 ;;
70         postrm)
71                 if [ "$1" = "purge" ]; then
72                         rm -f "$CONFFILE.dpkg-bak" "$CONFFILE.dpkg-remove" \
73                               "$CONFFILE.dpkg-backup"
74                 fi
75                 if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
76                    [ -n "$2" ] &&
77                    dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
78                         abort_rm_conffile "$CONFFILE" "$PACKAGE"
79                 fi
80                 ;;
81         *)
82                 debug "$0 rm_conffile not required in $DPKG_MAINTSCRIPT_NAME"
83                 ;;
84         esac
85 }
86
87 prepare_rm_conffile() {
88         local CONFFILE="$1"
89         local PACKAGE="$2"
90
91         [ -e "$CONFFILE" ] || return 0
92         ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
93
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"
100         else
101                 mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
102         fi
103 }
104
105 finish_rm_conffile() {
106         local CONFFILE="$1"
107
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"
112         fi
113         if [ -e "$CONFFILE.dpkg-remove" ]; then
114                 echo "Removing obsolete conffile $CONFFILE ..."
115                 rm -f "$CONFFILE.dpkg-remove"
116         fi
117 }
118
119 abort_rm_conffile() {
120         local CONFFILE="$1"
121         local PACKAGE="$2"
122
123         ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
124
125         if [ -e "$CONFFILE.dpkg-remove" ]; then
126                 echo "Reinstalling $CONFFILE that was moved away"
127                 mv "$CONFFILE.dpkg-remove" "$CONFFILE"
128         fi
129         if [ -e "$CONFFILE.dpkg-backup" ]; then
130                 echo "Reinstalling $CONFFILE that was backed-up"
131                 mv "$CONFFILE.dpkg-backup" "$CONFFILE"
132         fi
133 }
134
135 ##
136 ## Functions to rename a conffile during upgrade
137 ##
138 mv_conffile() {
139         local OLDCONFFILE="$1"
140         local NEWCONFFILE="$2"
141         local LASTVERSION="$3"
142         local PACKAGE="$4"
143         if [ "$LASTVERSION" = "--" ]; then
144                 LASTVERSION=""
145                 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
146         fi
147         if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
148                 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
149         fi
150         # Skip remaining parameters up to --
151         while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
152         [ $# -gt 0 ] || badusage "missing arguments after --"
153         shift
154
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"
164
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
170         preinst)
171                 if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
172                    dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
173                         prepare_mv_conffile "$OLDCONFFILE" "$PACKAGE"
174                 fi
175                 ;;
176         postinst)
177                 if [ "$1" = "configure" ] && [ -n "$2" ] &&
178                    dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
179                         finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE" "$PACKAGE"
180                 fi
181                 ;;
182         postrm)
183                 if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
184                    [ -n "$2" ] &&
185                    dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
186                         abort_mv_conffile "$OLDCONFFILE" "$PACKAGE"
187                 fi
188                 ;;
189         *)
190                 debug "$0 mv_conffile not required in $DPKG_MAINTSCRIPT_NAME"
191                 ;;
192         esac
193 }
194
195 prepare_mv_conffile() {
196         local CONFFILE="$1"
197         local PACKAGE="$2"
198
199         [ -e "$CONFFILE" ] || return 0
200
201         ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
202
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"
209         fi
210 }
211
212 finish_mv_conffile() {
213         local OLDCONFFILE="$1"
214         local NEWCONFFILE="$2"
215         local PACKAGE="$3"
216
217         rm -f "$OLDCONFFILE.dpkg-remove"
218
219         [ -e "$OLDCONFFILE" ] || return 0
220         ensure_package_owns_file "$PACKAGE" "$OLDCONFFILE" || return 0
221
222         echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
223         if [ -e "$NEWCONFFILE" ]; then
224                 mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
225         fi
226         mv -f "$OLDCONFFILE" "$NEWCONFFILE"
227 }
228
229 abort_mv_conffile() {
230         local CONFFILE="$1"
231         local PACKAGE="$2"
232
233         ensure_package_owns_file "$PACKAGE" "$CONFFILE" || return 0
234
235         if [ -e "$CONFFILE.dpkg-remove" ]; then
236                 echo "Reinstalling $CONFFILE that was moved away"
237                 mv "$CONFFILE.dpkg-remove" "$CONFFILE"
238         fi
239 }
240
241 ##
242 ## Functions to replace a symlink with a directory
243 ##
244 symlink_to_dir() {
245         local SYMLINK="$1"
246         local SYMLINK_TARGET="$2"
247         local LASTVERSION="$3"
248         local PACKAGE="$4"
249
250         if [ "$LASTVERSION" = "--" ]; then
251                 LASTVERSION=""
252                 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
253         fi
254         if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
255                 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
256         fi
257
258         # Skip remaining parameters up to --
259         while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
260         [ $# -gt 0 ] || badusage "missing arguments after --"
261         shift
262
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"
274
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"
279
280         case "$DPKG_MAINTSCRIPT_NAME" in
281         preinst)
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"
287                 fi
288                 ;;
289         postinst)
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"
296                 then
297                         rm -f "${SYMLINK}.dpkg-backup"
298                 fi
299                 ;;
300         postrm)
301                 if [ "$1" = "purge" ] && [ -h "${SYMLINK}.dpkg-backup" ]; then
302                     rm -f "${SYMLINK}.dpkg-backup"
303                 fi
304                 if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
305                    [ -n "$2" ] &&
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"
311                 fi
312                 ;;
313         *)
314                 debug "$0 symlink_to_dir not required in $DPKG_MAINTSCRIPT_NAME"
315                 ;;
316         esac
317 }
318
319 ##
320 ## Functions to replace a directory with a symlink
321 ##
322 dir_to_symlink() {
323         local PATHNAME="${1%/}"
324         local SYMLINK_TARGET="$2"
325         local LASTVERSION="$3"
326         local PACKAGE="$4"
327
328         if [ "$LASTVERSION" = "--" ]; then
329                 LASTVERSION=""
330                 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
331         fi
332         if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
333                 PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE${DPKG_MAINTSCRIPT_ARCH:+:$DPKG_MAINTSCRIPT_ARCH}"
334         fi
335
336         # Skip remaining parameters up to --
337         while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
338         [ $# -gt 0 ] || badusage "missing arguments after --"
339         shift
340
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"
350
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"
355
356         case "$DPKG_MAINTSCRIPT_NAME" in
357         preinst)
358                 if [ "$1" = "install" -o "$1" = "upgrade" ] &&
359                    [ -n "$2" ] &&
360                    [ ! -h "$PATHNAME" ] && [ -d "$PATHNAME" ] &&
361                    dpkg --compare-versions -- "$2" le-nl "$LASTVERSION"; then
362                         prepare_dir_to_symlink "$PACKAGE" "$PATHNAME"
363                 fi
364                 ;;
365         postinst)
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"
375                 fi
376                 ;;
377         postrm)
378                 if [ "$1" = "purge" ] && [ -d "${PATHNAME}.dpkg-backup" ]; then
379                     rm -rf "${PATHNAME}.dpkg-backup"
380                 fi
381                 if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
382                    [ -n "$2" ] &&
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"
391                 fi
392                 ;;
393         *)
394                 debug "$0 dir_to_symlink not required in $DPKG_MAINTSCRIPT_NAME"
395                 ;;
396         esac
397 }
398
399 prepare_dir_to_symlink()
400 {
401         local PACKAGE="$1"
402         local PATHNAME="$2"
403
404         local LINE
405         # If there are conffiles we should not perform the switch.
406         dpkg-query -W -f='${Conffiles}\n' "$PACKAGE" | while read -r LINE; do
407                 case "$LINE" in
408                 "$PATHNAME"/*)
409                         error "directory '$PATHNAME' contains conffiles," \
410                               "cannot switch to symlink"
411                         ;;
412                 esac
413         done
414
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
423
424         # At this point, we know that the directory either contains no files,
425         # or only non-conffiles owned by the package.
426         #
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.
431         #
432         # We cannot replace the directory with a staging symlink either,
433         # because dpkg will update symlinks to their new target.
434         #
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"
439         mkdir "$PATHNAME"
440
441         # Mark it as a staging directory, so that we can track things.
442         touch "$PATHNAME/.dpkg-staging-dir"
443 }
444
445 finish_dir_to_symlink()
446 {
447         local PATHNAME="$1"
448         local SYMLINK_TARGET="$2"
449
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"
456         else
457                 ABS_SYMLINK_TARGET="$SYMLINK_TARGET"
458         fi
459         rm "$PATHNAME/.dpkg-staging-dir"
460         find "$PATHNAME" -mindepth 1 -print0 | \
461                 xargs -0 -i% mv -f "%" "$ABS_SYMLINK_TARGET/"
462
463         # Remove the staging directory.
464         rmdir "$PATHNAME"
465
466         # Do the actual switch.
467         ln -s "$SYMLINK_TARGET" "$PATHNAME"
468
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"
472 }
473
474 abort_dir_to_symlink()
475 {
476         local PATHNAME="$1"
477
478         echo "Restoring backup of $PATHNAME ..."
479         if [ -h "$PATHNAME" ]; then
480                 rm -f "$PATHNAME"
481         else
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"
485                 rmdir "$PATHNAME"
486         fi
487
488         mv "${PATHNAME}.dpkg-backup" "$PATHNAME"
489 }
490
491 # Common functions
492 validate_optional_version() {
493         local VERSION="$1"
494
495         if [ -z "$VERSION" ]; then
496                 return
497         fi
498
499         if ! VERSIONCHECK=$(dpkg --validate-version -- "$VERSION" 2>&1); then
500                 error "$VERSIONCHECK"
501         fi
502 }
503
504 ensure_package_owns_file() {
505         local PACKAGE="$1"
506         local FILE="$2"
507
508         if ! dpkg-query -L "$PACKAGE" | grep -F -q -x "$FILE"; then
509                 debug "File '$FILE' not owned by package " \
510                       "'$PACKAGE', skipping $command"
511                 return 1
512         fi
513         return 0
514 }
515
516 internal_pkg_must_own_file()
517 {
518         local PACKAGE="$1"
519         local FILE="$2"
520
521         if [ "$DPKG_MAINTSCRIPT_HELPER_INTERNAL_API" != "$version" ]; then
522                 error "internal API used by external command"
523         fi
524
525         if ! ensure_package_owns_file "$PACKAGE" "$FILE"; then
526                 error "file '$FILE' not owned by package '$PACKAGE'"
527         fi
528         return 0
529 }
530
531 symlink_match()
532 {
533         local SYMLINK="$1"
534         local SYMLINK_TARGET="$2"
535
536         [ "$(readlink "$SYMLINK")" = "$SYMLINK_TARGET" ] || \
537         [ "$(readlink -f "$SYMLINK")" = "$SYMLINK_TARGET" ]
538 }
539
540 debug() {
541         if [ -n "$DPKG_DEBUG" ]; then
542                 echo "DEBUG: $PROGNAME: $*" >&2
543         fi
544 }
545
546 error() {
547         echo "$PROGNAME: error: $*" >&2
548         exit 1
549 }
550
551 warning() {
552         echo "$PROGNAME: warning: $*" >&2
553 }
554
555 usage() {
556         cat <<END
557 Usage: $PROGNAME <command> <parameter>... -- <maintainer-script-parameter>...
558
559 Commands:
560   supports <command>
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
564         postrm.
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,
569         postinst and postrm.
570   dir_to_symlink <pathname> <new-symlink-target> [<last-version> [<package>]]
571         Replace a directory with a symlink. Must be called in preinst,
572         postinst and postrm.
573   help
574         Display this usage information.
575 END
576 }
577
578 badusage() {
579         echo "$PROGNAME: error: $1" >&2
580         echo >&2
581         echo "Use '$PROGNAME help' for program usage information." >&2
582         exit 1
583 }
584
585 # Main code
586 set -e
587
588 PROGNAME=$(basename "$0")
589 version="unknown"
590 command="$1"
591 [ $# -gt 0 ] || badusage "missing command"
592 shift
593
594 case "$command" in
595 supports)
596         case "$1" in
597         rm_conffile|mv_conffile|symlink_to_dir|dir_to_symlink)
598                 code=0
599                 ;;
600         *)
601                 code=1
602                 ;;
603         esac
604         if [ -z "$DPKG_MAINTSCRIPT_NAME" ]; then
605                 warning "environment variable DPKG_MAINTSCRIPT_NAME missing"
606                 code=1
607         fi
608         if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
609                 warning "environment variable DPKG_MAINTSCRIPT_PACKAGE missing"
610                 code=1
611         fi
612         exit $code
613         ;;
614 rm_conffile)
615         rm_conffile "$@"
616         ;;
617 mv_conffile)
618         mv_conffile "$@"
619         ;;
620 symlink_to_dir)
621         symlink_to_dir "$@"
622         ;;
623 dir_to_symlink)
624         dir_to_symlink "$@"
625         ;;
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 "$@"
629         ;;
630 --help|help|-?)
631         usage
632         ;;
633 --version)
634         cat <<-END
635         Debian $PROGNAME version $version.
636
637         This is free software; see the GNU General Public License version 2 or
638         later for copying conditions. There is NO warranty.
639         END
640         ;;
641 *)
642         badusage "command $command is unknown
643 Hint: upgrading dpkg to a newer version might help."
644 esac
645
646 exit 0