Development/Howto/Alternatives
From Mandriva
The program update-alternatives, originally written by Debian, is here to provide default system-wide commands through the use of symbolic links.
Contents |
Usage
The program update-alternatives, originally written by Debian, is here to provide default system-wide commands through the use of symbolic links. An example will help to understand the point:
# Whenever you call 'vim'... lrwxrwxrwx 1 root root 21 Jan 31 10:25 /bin/vim -> /etc/alternatives/vim # It will go to /etc/alternatives... lrwxrwxrwx 1 root root 26 Mar 20 00:51 /etc/alternatives/vim -> ../../usr/bin/vim-enhanced # Then, to the real program. -rwxr-xr-x 1 root root 1724604 Jun 18 13:25 /usr/bin/vim-enhanced
When a new program wants to provide the same functionality (say vim-minimal ) it would register to the update-alternatives system, and potentially become the real program called under vim. The choice is either made automatically (under that scenario, the program with highest 'priority' will be used, see below) or manually.
Here's how emacs-nox and emacs-X11 register and unregister themselves to the update-alternatives system:
%post nox update-alternatives --install %{_bindir}/emacs emacs %{_bindir}/emacs-nox 10 %post X11 update-alternatives --install %{_bindir}/emacs emacs %{_bindir}/emacs-%{version} 20
The syntax with the --install command is pretty self-explanatory: you need to provide the link, the name, the full path of the real binary, and the priority. Default behaviour of the resulting system is to follow the priority (e.g. the 'automatic' mode).
For uninstalling we have to cope with a peculiarity of RPM: When updating a package, the new package is first installed and then the old one is removed. So if we just call update-alternatives --remove, the alternative is removed when upgrading. A generic way to solve this is to use the first parameter which indicated how many packages are left. So when the user performs an upgrade, this will be 1, if the user removes the package this is 0:
%postun nox if [ "$1" = "0" ]; then update-alternatives --remove emacs %{_bindir}/emacs-nox fi %postun X11 if [ "$1" = "0" ]; then update-alternatives --remove emacs %{_bindir}/emacs-%{version} fi
List of alternatives
This list is far from being complete for now. It is used for virtual Provides, see VirtualProvidesList.
Format is : name ( alternative link ) : package ( priority ) , package ( priority ) , ....
If no list of packages is given, they use the same as the upper package in the list, for readability
- mta ( %{_sbindir}/sendmail ) : ssmtp (5), postfix(20), sendmail (10)
- mta-aliasesman ( %{_mandir}/man5/aliases )
- mta-etc_aliases ( %{_sysconfdir}/aliases )
- mta-in_libdir ( %{_libdir}/sendmail )
- mta-mailq ( %{_bindir}/mailq )
- mta-mailqman ( %{_mandir}/man1/mailq )
- mta-newaliases ( %{_bindir}/newaliases )
- mta-newaliasesman -> %{_mandir}/man1/newaliases )
- mta-rmail ( %{_bindir}/rmail )
Behind the scenes
Alternatives are stored in /var/lib/rpm/alternatives/. This is used to manage the priority, since it cannot be done simply by using only a link. People should not touch these files directly, but use update-alternatives instead.