On Mon, Jan 13, 2025 at 08:49:29PM +0100, Helmut Grohne wrote:
> I'm sorry to tell you that I still got the duplicated diversions wrong.

This never ends. :-(

> I'm attaching a patch that fixes both and also attaching my test cases
> (that are all passing now). I added more comments such that the mess
> becomes easier to understand by my future self and others.

Evidently, I missed a relevant test case (reinstalling and removing the
package) and of course it would fail. Here is a replacement patch and a
new test case.

This time, I am completely avoiding --rename and handling all the cases
explicitly doing the renames where needed. Hope this one passes the test
of time.

Helmut
diff --minimal -Nru bfh-metapackages-20211009/debian/bfh-container.preinst 
bfh-metapackages-20211009/debian/bfh-container.preinst
--- bfh-metapackages-20211009/debian/bfh-container.preinst      2024-12-12 
18:01:16.000000000 +0100
+++ bfh-metapackages-20211009/debian/bfh-container.preinst      2025-01-13 
12:35:28.000000000 +0100
@@ -8,46 +8,59 @@
 
                for FILE in halt poweroff reboot shutdown coldreboot
                do
-                       TRUENAME_USR="$(dpkg-divert --truename 
"/usr/sbin/${FILE}")"
-                       TRUENAME_ALIAS="$(dpkg-divert --truename 
"/sbin/${FILE}")"
-                       RENAME_FLAG="--no-rename"
-
-                       if [ "${TRUENAME_USR}" = "/usr/sbin/${FILE}" ]
+                       # DEP17 M18 duplicated diversion. Once trixie is
+                       # released, remove the aliased diversions in postinst
+                       # and only have the canonical one with --rename here.
+                       TRUENAME="$(dpkg-divert --truename "/sbin/${FILE}")"
+                       if [ "$(dpkg-divert --truename "/usr/sbin/${FILE}")" != 
"/usr/sbin/${FILE}" ]
                        then
-                               if [ "${TRUENAME_ALIAS}" = "/sbin/${FILE}" ]
-                               then
-                                       RENAME_FLAG="--rename"
-                               fi
-
-                               dpkg-divert --package bfh-container --quiet 
--add "${RENAME_FLAG}" --divert "/usr/lib/container/divert/${FILE}.orig" 
"/usr/sbin/${FILE}"
-                       fi
-
-                       # DEP17 M18 duplicated diversion, can be removed after 
trixie.
-                       if [ "${TRUENAME_ALIAS}" = "/sbin/${FILE}" ]
+                               : # Canonically diverted already. Nothing to 
rename.
+                       elif [ "${TRUENAME}" = 
"/lib/container/divert/${FILE}.orig.usr-is-merged" ]
                        then
-                               dpkg-divert --package bfh-container --quiet 
--add "${RENAME_FLAG}" --divert 
"/lib/container/divert/${FILE}.orig.usr-is-merged" "/sbin/${FILE}"
-                       elif [ "${TRUENAME_ALIAS}" != 
"/lib/container/divert/${FILE}.orig.usr-is-merged" ]
+                               : # Aliased diversion as expected. Nothing to 
rename.
+                       elif [ "${TRUENAME}" != "/sbin/${FILE}" ]
                        then
-                               dpkg-divert --package bfh-container --quiet 
--remove --no-rename "/sbin/${FILE}"
-                               dpkg-divert --package bfh-container --quiet 
--add --no-rename --divert "/lib/container/divert/${FILE}.orig.usr-is-merged" 
"/sbin/${FILE}"
-
-                               if [ -e "${TRUENAME_ALIAS}" ] || [ -h 
"${TRUENAME_ALIAS}" ]
+                               # Aliased diversion with earlier target. Fix 
target.
+                               dpkg-divert --remove --no-rename "/sbin/${FILE}"
+                               if [ -e "${DPKG_ROOT}${TRUENAME}" ] || [ -h 
"${DPKG_ROOT}${TRUENAME}" ]
                                then
-                                       mv "${TRUENAME_ALIAS}" 
"/lib/container/divert/${FILE}.orig.usr-is-merged"
+                                       mv "${DPKG_ROOT}${TRUENAME}" 
"${DPKG_ROOT}/lib/container/divert/${FILE}.orig.usr-is-merged"
                                fi
+                       elif dpkg -S "/sbin/${FILE}" >/dev/null 2>&1
+                       then
+                               # Not diverted yet. Installed as aliased.
+                               mv "${DPKG_ROOT}/sbin/${FILE}" 
"${DPKG_ROOT}/lib/container/divert/${FILE}.orig.usr-is-merged"
+                       elif [ -e "${DPKG_ROOT}/usr/sbin/${FILE}" ] || [ -h 
"${DPKG_ROOT}/usr/sbin/${FILE}" ]
+                       then
+                               # Not diverted yet. Installed as canonical.
+                               mv "${DPKG_ROOT}/usr/sbin/${FILE}" 
"${DPKG_ROOT}/usr/lib/container/divert/${FILE}.orig"
                        fi
+                       # All necessary moving has been done. Hence --no-rename.
+                       dpkg-divert --package bfh-container --quiet --add 
--no-rename --divert "/lib/container/divert/${FILE}.orig.usr-is-merged" 
"/sbin/${FILE}"
+                       dpkg-divert --package bfh-container --quiet --add 
--no-rename --divert "/usr/lib/container/divert/${FILE}.orig" 
"/usr/sbin/${FILE}"
                done
 
                for FILE in pm-hibernate pm-suspend pm-suspend-hybrid
                do
-                       TRUENAME_USR="$(dpkg-divert --truename 
"/usr/sbin/${FILE}")"
+                       TRUENAME="$(dpkg-divert --truename "/usr/sbin/${FILE}")"
 
-                       if [ "${TRUENAME_USR}" = 
"/lib/container/divert/${FILE}.orig" ]
+                       if [ "${TRUENAME}" = "/usr/sbin/${FILE}" ]
+                       then
+                               # Not diverted yet.
+                               dpkg-divert --package bfh-container --quiet 
--add --rename --divert "/usr/lib/container/divert/${FILE}.orig" 
"/usr/sbin/${FILE}"
+                       elif [ "${TRUENAME}" != 
"/usr/lib/container/divert/${FILE}.orig" ]
                        then
+                               # Upgrading from pre-trixie. Update the 
diversion target from aliased to canonical.
                                dpkg-divert --package bfh-container --quiet 
--remove --no-rename "/usr/sbin/${FILE}"
                                dpkg-divert --package bfh-container --quiet 
--add --no-rename --divert "/usr/lib/container/divert/${FILE}.orig" 
"/usr/sbin/${FILE}"
-                       else
-                               dpkg-divert --package bfh-container --quiet 
--add --rename --divert "/usr/lib/container/divert/${FILE}.orig" 
"/usr/sbin/${FILE}"
+                               if [ "${TRUENAME}" != 
"/lib/container/divert/${FILE}.orig" ]
+                               then
+                                       # Diversion target differs in more than 
aliasing.
+                                       if [ -e "${DPKG_ROOT}${TRUENAME}" ] || 
[ -h "${DPKG_ROOT}${TRUENAME}" ]
+                                       then
+                                               mv "${DPKG_ROOT}${TRUENAME}" 
"${DPKG_ROOT}/usr/lib/container/divert/${FILE}.orig"
+                                       fi
+                               fi
                        fi
                done
                ;;
diff --minimal -Nru bfh-metapackages-20211009/debian/changelog 
bfh-metapackages-20211009/debian/changelog
--- bfh-metapackages-20211009/debian/changelog  2024-12-12 18:07:25.000000000 
+0100
+++ bfh-metapackages-20211009/debian/changelog  2025-01-13 12:35:28.000000000 
+0100
@@ -1,3 +1,10 @@
+bfh-metapackages (20211009-31.1) UNRELEASED; urgency=medium
+
+  * Non-maintainer upload.
+  * Fix --rename flags on duplicated diversions (DEP17 M18, Closes: #-1)
+
+ -- Helmut Grohne <hel...@subdivi.de>  Mon, 13 Jan 2025 12:35:28 +0100
+
 bfh-metapackages (20211009-31) sid; urgency=medium
 
   * Uploading to sid.

Attachment: test-bfh.sh
Description: Bourne shell script

Reply via email to