#1033594 The diversion of "/usr/bin/firefox" by "firefox-esr" package does not respect a previously added local diversion

Package:
firefox-esr
Source:
firefox-esr
Description:
Mozilla Firefox web browser - Extended Support Release (ESR)
Submitter:
Jmkr
Date:
2023-03-27 21:45:03 UTC
Severity:
normal
#1033594#5
Date:
2023-03-27 21:40:35 UTC
From:
To:
Dear Maintainers,

the diversion of "/usr/bin/firefox" to "/usr/bin/firefox.real" by
"firefox-esr" package does not respect a local diversion previously
added by local admin. If such a local diversion exists it prevents the
"firefox-esr" package installation from completing successfully (and
probably also prevents any "firefox-esr" package updates until this bug
is fixed).

This can be tested easily assuming the "firefox-esr" package is already
installed:

- Replace the "firefox-esr" package diversion by a local diversion:
     dpkg-divert --no-rename --remove /usr/bin/firefox
     dpkg-divert --divert /usr/bin/firefox.dpkg-dist --local --rename \
                 --add /usr/bin/firefox

- Reinstall the "firefox-esr" package and an error should occur
   (line breaks added to avoid line wrapping by email software):
     dpkg-divert: error: 'diversion of /usr/bin/firefox to
       /usr/bin/firefox.real by firefox-esr' clashes with 'local
       diversion of /usr/bin/firefox to /usr/bin/firefox.dpkg-dist'
     dpkg: error processing archive
       /var/cache/apt/archives/firefox-esr_102.9.0esr-1~deb10u1_amd64.deb
       (--unpack):
         new firefox-esr package pre-installation script subprocess
         returned error exit status 2

- To cleanup:
   - Remove the local diversion:
     dpkg-divert --rename --remove /usr/bin/firefox
   - Add back the "firefox-esr" package diversion:
     dpkg-divert --package firefox-esr --divert /usr/bin/firefox.real \
                 --rename /usr/bin/firefox

The bug could be easily fixed by adding some check for a previously
added diversion instead of just blindly trying to add the "firefox-esr"
package diversion to the system. For example "dash" package in its
"postinst" script checks for and respects any previously added local
diversions before adding its own diversions. Thus, adapting that check
for "firefox-esr" package "preinst" script could be as easy as replacing
line 4 of the "preinst" script with this condition:

if [ $(dpkg-divert --listpackage /usr/bin/firefox) != LOCAL ]; then
     dpkg-divert --package firefox-esr --divert /usr/bin/firefox.real \
                 --rename /usr/bin/firefox
fi

This should probably also help with (or even fix) Debian bug 861783.

Regards,
Jmkr