From 1852a8c8460a100c3fd09021bf4b26410866def6 Mon Sep 17 00:00:00 2001
From: Nicolas Fella <nicolas.fella@gmx.de>
Date: Sun, 1 Sep 2024 23:19:55 +0200
Subject: [PATCH 1/2] [udisks] Don't add/remove devices in slotMediaChanged

We get notified when devices are added via InterfacesAdded/InterfacesRemoved.

When ejecting from a CD drive we first get slotMediaChanged, where we
remove the device from m_deviceCache. Then we get InterfacesRemoved
for org.freedesktop.UDisks2.Filesystem. Because the
org.freedesktop.UDisks2.Block interface is still there we remove and
immediately readd the device. Then in DeviceManagerPrivate::_k_deviceAdded
we call createBackendObject, which fails to create a backend because
the uid is not in m_deviceCache any more. Then we assert because the
backend is empty.

CCBUG: 464149

(cherry picked from commit 99510948944ecda04f9cec6b3bd94b140d191a1c)
---

From 1c76a103ebae87c99fa7461bc2760544ca0945f8 Mon Sep 17 00:00:00 2001
From: Nicolas Fella <nicolas.fella@gmx.de>
Date: Fri, 1 Nov 2024 17:44:39 +0100
Subject: [PATCH 2/2] Restore MediaChanged handling for Audio CDs

99510948944ecda04f9cec6b3bd94b140d191a1c removed this because
it's unneeded for non-audio CDs and is causing problems.

However without it adding or removing audio CDs isn't handled,
so restore it, but only if there is no FileSystem interface
(i.e. no data CD)

(cherry picked from commit df5843ed76065f0e56d1189d010e10497c17f936)
---
 src/solid/devices/backends/udisks2/udisksmanager.cpp | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/solid/devices/backends/udisks2/udisksmanager.cpp b/src/solid/devices/backends/udisks2/udisksmanager.cpp
index 1f1a7e73..799face7 100644
--- a/src/solid/devices/backends/udisks2/udisksmanager.cpp
+++ b/src/solid/devices/backends/udisks2/udisksmanager.cpp
@@ -265,15 +265,18 @@ void Manager::slotMediaChanged(const QDBusMessage &msg)
     qulonglong size = properties.value(QStringLiteral("Size")).toULongLong();
     qCDebug(UDISKS2) << "MEDIA CHANGED in" << udi << "; size is:" << size;
 
-    if (!m_deviceCache.contains(udi) && size > 0) { // we don't know the optdisc, got inserted
-        m_deviceCache.append(udi);
-        Q_EMIT deviceAdded(udi);
-    }
+    Device device(udi);
+    if (!device.interfaces().contains(u"org.freedesktop.UDisks2.Filesystem")) {
+        if (!m_deviceCache.contains(udi) && size > 0) { // we don't know the optdisc, got inserted
+            m_deviceCache.append(udi);
+            Q_EMIT deviceAdded(udi);
+        }
 
-    if (m_deviceCache.contains(udi) && size == 0) { // we know the optdisc, got removed
-        Q_EMIT deviceRemoved(udi);
-        m_deviceCache.removeAll(udi);
-        DeviceBackend::destroyBackend(udi);
+        if (m_deviceCache.contains(udi) && size == 0) { // we know the optdisc, got removed
+            Q_EMIT deviceRemoved(udi);
+            m_deviceCache.removeAll(udi);
+            DeviceBackend::destroyBackend(udi);
+        }
     }
 }
 
