~vcs-imports/debian-policy/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Diversions - overriding a package's version of a file (from old Packaging Manual)
=================================================================================

It is possible to have ``dpkg`` not overwrite a file when it reinstalls
the package it belongs to, and to have it put the file from the package
somewhere else instead.

This can be used locally to override a package's version of a file, or
by one package to override another's version (or provide a wrapper for
it).

Before deciding to use a diversion, read
:doc:`ap-pkg-alternatives` to see if you really want a
diversion rather than several alternative versions of a program.

There is a diversion list, which is read by ``dpkg``, and updated by a
special program ``dpkg-divert``. Please see :manpage:`dpkg-divert(8)` for
full details of its operation.

When a package wishes to divert a file from another, it should call
``dpkg-divert`` in its preinst to add the diversion and rename the
existing file. For example, supposing that a ``smailwrapper`` package
wishes to install a wrapper around ``/usr/sbin/smail``:

::

    dpkg-divert --package smailwrapper --add --rename \
        --divert /usr/sbin/smail.real /usr/sbin/smail

The ``--package smailwrapper`` ensures that ``smailwrapper``'s copy of
``/usr/sbin/smail`` can bypass the diversion and get installed as the
true version. It's safe to add the diversion unconditionally on upgrades
since it will be left unchanged if it already exists, but
``dpkg-divert`` will display a message. To suppress that message, make
the command conditional on the version from which the package is being
upgraded:

::

    if [ upgrade != "$1" ] || dpkg --compare-versions "$2" lt 1.0-2; then
        dpkg-divert --package smailwrapper --add --rename \
            --divert /usr/sbin/smail.real /usr/sbin/smail
    fi

where ``1.0-2`` is the version at which the diversion was first added to
the package. Running the command during abort-upgrade is pointless but
harmless.

The postrm has to do the reverse:

::

    if [ remove = "$1" -o abort-install = "$1" -o disappear = "$1" ]; then
        dpkg-divert --package smailwrapper --remove --rename \
            --divert /usr/sbin/smail.real /usr/sbin/smail
    fi

If the diversion was added at a particular version, the postrm should
also handle the failure case of upgrading from an older version (unless
the older version is so old that direct upgrades are no longer
supported):

::

    if [ abort-upgrade = "$1" ] && dpkg --compare-versions "$2" lt 1.0-2; then
        dpkg-divert --package smailwrapper --remove --rename \
            --divert /usr/sbin/smail.real /usr/sbin/smail
    fi

where ``1.0-2`` is the version at which the diversion was first added to
the package. The postrm should not remove the diversion on upgrades both
because there's no reason to remove the diversion only to immediately
re-add it and since the postrm of the old package is run after unpacking
so the removal of the diversion will fail.

Do not attempt to divert a file which is vitally important for the
system's operation - when using ``dpkg-divert`` there is a time, after
it has been diverted but before ``dpkg`` has installed the new version,
when the file does not exist.

Do not attempt to divert a conffile, as ``dpkg`` does not handle it
well.