From 661752bbc136103a88651e34bf7a0c010c9d1c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Mon, 15 May 2017 21:04:50 +0200 Subject: [PATCH] devices: list persistent devices also when not currently attached When device extension do not return some "persistent" device as currently attached, still return it, as it will be attached at next domain startup. User can distinguish such devices by having frontend_domain=None (or other VM). Also, return a set from DeviceCollection.assignments(). --- qubes/devices.py | 13 +++++++------ qubes/tests/devices.py | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/qubes/devices.py b/qubes/devices.py index 21b8c7b3..2a641ca2 100644 --- a/qubes/devices.py +++ b/qubes/devices.py @@ -71,6 +71,7 @@ class DeviceAssignment(object): # pylint: disable=too-few-public-methods return "[%s]:%s" % (self.backend_domain, self.ident) def __hash__(self): + # it's important to use the same hash as DeviceInfo return hash((self.backend_domain, self.ident)) def __eq__(self, other): @@ -234,21 +235,21 @@ class DeviceCollection(object): devices = self._vm.fire_event('device-list-attached:' + self._class, persistent=persistent) - result = [] + result = set() for dev, options in devices: if dev in self._set and not persistent: continue elif dev in self._set: - result.append(self._set.get(dev)) + result.add(self._set.get(dev)) elif dev not in self._set and persistent: continue else: - result.append( + result.add( DeviceAssignment(backend_domain=dev.backend_domain, ident=dev.ident, options=options, frontend_domain=self._vm)) - if persistent is not False and not result: - result.extend(self._set) + if persistent is not False: + result.update(self._set) return result def available(self): @@ -335,7 +336,7 @@ class DeviceInfo(object): setattr(self, group, dev_match.group(group)) def __hash__(self): - return hash(self.ident) + return hash((self.backend_domain, self.ident)) def __eq__(self, other): return ( diff --git a/qubes/tests/devices.py b/qubes/tests/devices.py index 86a3926a..540e03ef 100644 --- a/qubes/tests/devices.py +++ b/qubes/tests/devices.py @@ -111,7 +111,7 @@ class TC_00_DeviceCollection(qubes.tests.QubesTestCase): def test_011_double_attach(self): self.collection.attach(self.assignment) - with self.assertRaises(LookupError): + with self.assertRaises(qubes.devices.DeviceAlreadyAttached): self.collection.attach(self.assignment) def test_012_double_detach(self):