Commit 6436558c authored by Christian Kellner's avatar Christian Kellner

manager: label new devices, detect duplicates

Instead of cleaning up the name and then storing the cleaned-up
name in the database, we store the name, vendor as we get them
from sysfs. We then cleanup the name, detect duplicate name, vendor
combinations and label the device with the cleaned-up name with
an optional suffix in case of duplicates.
Adapt the integration test to reflect those changes.
parent 4a34f210
Pipeline #507 passed with stage
in 6 minutes and 49 seconds
......@@ -596,34 +596,6 @@ bolt_auth_flags_from_info (BoltDevInfo *info,
return val;
}
static const char *
cleanup_name (const char *name,
const char *vendor,
GError **error)
{
g_return_val_if_fail (vendor != NULL, NULL);
g_return_val_if_fail (name != NULL, NULL);
/* some devices have the vendor name as a prefix */
if (!g_str_has_prefix (name, vendor))
return name;
name += strlen (vendor);
while (g_ascii_isspace (*name))
name++;
if (*name == '\0')
{
g_set_error_literal (error,
BOLT_ERROR, BOLT_ERROR_UDEV,
"device has empty name after cleanup");
return NULL;
}
return name;
}
/* device authorization */
static gboolean
......@@ -1048,10 +1020,6 @@ bolt_device_new_for_udev (struct udev_device *udev,
if (vendor == NULL)
return NULL;
name = cleanup_name (name, vendor, error);
if (name == NULL)
return NULL;
ok = bolt_sysfs_info_for_device (udev, TRUE, &info, error);
if (!ok)
return NULL;
......
......@@ -51,7 +51,7 @@ static void bolt_manager_initable_iface_init (GInitableIface *iface);
static gboolean bolt_manager_initialize (GInitable *initable,
GCancellable *cancellable,
GError **error);
/* */
/* device related functions */
static void manager_register_device (BoltManager *mgr,
BoltDevice *device);
......@@ -72,6 +72,10 @@ static BoltDevice * bolt_manager_get_parent (BoltManager *mgr,
static GPtrArray * bolt_manager_get_children (BoltManager *mgr,
BoltDevice *target);
static void bolt_manager_label_device (BoltManager *mgr,
BoltDevice *target);
/* udev events */
static void handle_udev_device_added (BoltManager *mgr,
struct udev_device *udev);
......@@ -805,6 +809,51 @@ bolt_manager_get_children (BoltManager *mgr,
return res;
}
static void
bolt_manager_label_device (BoltManager *mgr,
BoltDevice *target)
{
g_autofree char *label = NULL;
const char *name;
const char *vendor;
guint count = 0;
name = bolt_device_get_name (target);
vendor = bolt_device_get_vendor (target);
for (guint i = 0; i < mgr->devices->len; i++)
{
BoltDevice *dev = g_ptr_array_index (mgr->devices, i);
const char *dev_name = bolt_device_get_name (dev);
const char *dev_vendor = bolt_device_get_vendor (dev);
if (bolt_streq (dev_name, name) &&
bolt_streq (dev_vendor, vendor))
count++;
}
/* cleanup name */
if (g_str_has_prefix (name, vendor))
{
name += strlen (vendor);
while (g_ascii_isspace (*name))
name++;
if (*name == '\0')
name = bolt_device_get_name (target);
}
/* we counted the target too, > 1 means duplicates */
if (count > 1)
label = g_strdup_printf ("%s %s #%u", vendor, name, count);
else
label = g_strdup_printf ("%s %s", vendor, name);
bolt_info (LOG_DEV (target), "labeling device: %s", label);
g_object_set (G_OBJECT (target), "label", label, NULL);
}
/* device authorization */
static void
authorize_device_finish (GObject *source,
......@@ -949,6 +998,8 @@ handle_udev_device_added (BoltManager *mgr,
bolt_msg (LOG_DEV (dev), "device added, status: %s, at %s",
bolt_status_to_string (status), syspath);
bolt_manager_label_device (mgr, dev);
manager_maybe_auto_import_device (mgr, dev);
/* if we have a valid dbus connection */
......
......@@ -1101,28 +1101,31 @@ class BoltTest(dbusmock.DBusTestCase):
# prepare the basic setup
dc, host = self.add_domain_host()
# duplicated vendor in name
d1_vendor = "GNOME.org"
d1_name = "Cable"
d2_name = '⍾ Laptop'
d2_vendor = 'Evil Corp. ☢'
_, d1_uid = self.add_device(host, 1, d1_vendor + d1_name, d1_vendor)
_, d2_uid = self.add_device(host, 2, d2_name, d2_vendor)
devs = [
# name vendor label notes
['GNOME.org Cable', 'GNOME.org', 'GNOME.org Cable' ], # duplicated vendor name
['GNOME.org Cable', 'GNOME.org', 'GNOME.org Cable #2' ], # duplicated device
['GNOME.org Cable', 'GNOME.org', 'GNOME.org Cable #3' ], # duplicated device, again
['⍾ Laptop', 'Evil Corp. ☢', 'Evil Corp. ☢ ⍾ Laptop'], # utf-8 chars
]
self.daemon_start()
devices = self.client.list_devices()
self.assertEqual(len(devices), 3)
devs = [{'name': d[0], 'vendor': d[1], 'label': d[2], 'id': i+1} for i, d in enumerate(devs)]
device = self.find_device_by_uid(devices, d1_uid)
self.assertEqual(device.Name, d1_name)
self.assertEqual(device.Vendor, d1_vendor)
for i, d in enumerate(devs):
did, name, vendor = d['id'], d['name'], d['vendor']
path, uid = self.add_device(host, did, name, vendor);
d['path'] = path
d['uid'] = uid
device = self.find_device_by_uid(devices, d2_uid)
self.assertEqual(device.Name, d2_name)
self.assertEqual(device.Vendor, d2_vendor)
self.daemon_start()
devices = self.client.list_devices()
self.assertEqual(len(devices), len(devs) + 1)
label = device.label
self.assertIsNone(label)
for d in devs:
remote = self.find_device_by_uid(devices, d['uid'])
self.assertEqual(remote.name, d['name'])
self.assertEqual(remote.vendor, d['vendor'])
self.assertEqual(remote.label, d['label'])
self.daemon_stop()
......@@ -1536,7 +1539,7 @@ class BoltTest(dbusmock.DBusTestCase):
local.reload_auth()
label = remote.label
self.assertIsNone(label)
self.assertEqual(remote.label, "%s %s" % (local.vendor_name, local.device_name))
self.assertDeviceEqual(local, remote)
self.assertTrue(remote.stored, True)
......@@ -1557,8 +1560,7 @@ class BoltTest(dbusmock.DBusTestCase):
self.assertEqual(err.domain, GLib.quark_to_string(Gio.DBusError.quark()))
self.assertEqual(err.code, int(Gio.DBusError.INVALID_ARGS))
label = remote.label
self.assertIsNone(label)
self.assertEqual(remote.label, "%s %s" % (local.vendor_name, local.device_name))
val = 'A valid label'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment