summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberts Muktupāvels <alberts.muktupavels@gmail.com>2022-05-11 22:06:06 +0300
committerAlberts Muktupāvels <alberts.muktupavels@gmail.com>2022-11-23 22:40:34 +0200
commit0cfd33f0c1101b4a93c503cbda4274786a6d41e2 (patch)
treeba746ee763c77bd125c4eb27d47dce3406be3d24
parent4216ecd4513bd4c8af73543817a51d6f72f166cc (diff)
downloadlibgudev-0cfd33f0c1101b4a93c503cbda4274786a6d41e2.tar.gz
device: add g_udev_device_get_current_tags
Starting with systemd-udevd 247 tags are "sticky" meaning that once a tag is assigned to a device it will not be removed from the device again until the device itself is removed. New property CURRENT_TAGS has been added that works similar to the existing TAGS property but only lists tags set by the most recent uevent/database update. https://lists.freedesktop.org/archives/systemd-devel/2020-November/045646.html
-rw-r--r--gudev/gudevdevice.c38
-rw-r--r--gudev/gudevdevice.h1
-rw-r--r--libgudev-1.0.sym1
-rw-r--r--meson.build2
-rw-r--r--tests/test-gudevdevice.c6
5 files changed, 47 insertions, 1 deletions
diff --git a/gudev/gudevdevice.c b/gudev/gudevdevice.c
index 248d272..25af9d1 100644
--- a/gudev/gudevdevice.c
+++ b/gudev/gudevdevice.c
@@ -76,6 +76,7 @@ struct _GUdevDevicePrivate
gchar **property_keys;
gchar **sysfs_attr_keys;
gchar **tags;
+ gchar **current_tags;
GHashTable *prop_strvs;
GHashTable *sysfs_attr_strvs;
};
@@ -91,6 +92,7 @@ g_udev_device_finalize (GObject *object)
g_strfreev (device->priv->property_keys);
g_strfreev (device->priv->sysfs_attr_keys);
g_strfreev (device->priv->tags);
+ g_strfreev (device->priv->current_tags);
if (device->priv->udevice != NULL)
udev_device_unref (device->priv->udevice);
@@ -119,6 +121,20 @@ g_udev_device_init (GUdevDevice *device)
device->priv = g_udev_device_get_instance_private (device);
}
+static void
+fetch_current_tags (GUdevDevice *device)
+{
+ struct udev_list_entry *l;
+ GPtrArray *p;
+
+ p = g_ptr_array_new ();
+ for (l = udev_device_get_current_tags_list_entry (device->priv->udevice); l != NULL; l = udev_list_entry_get_next (l))
+ {
+ g_ptr_array_add (p, g_strdup (udev_list_entry_get_name (l)));
+ }
+ g_ptr_array_add (p, NULL);
+ device->priv->current_tags = (gchar **) g_ptr_array_free (p, FALSE);
+}
GUdevDevice *
_g_udev_device_new (struct udev_device *udevice)
@@ -128,6 +144,8 @@ _g_udev_device_new (struct udev_device *udevice)
device = G_UDEV_DEVICE (g_object_new (G_UDEV_TYPE_DEVICE, NULL));
device->priv->udevice = udev_device_ref (udevice);
+ fetch_current_tags (device);
+
return device;
}
@@ -1207,6 +1225,26 @@ g_udev_device_get_tags (GUdevDevice *device)
}
/**
+ * g_udev_device_get_current_tags:
+ * @device: A #GUdevDevice.
+ *
+ * Gets all current tags for @device.
+ *
+ * https://www.freedesktop.org/software/systemd/man/udev_device_has_current_tag.html
+ *
+ * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): A %NULL terminated string array of current tags. This array is owned by @device and should not be freed by the caller.
+ *
+ * Since: 238
+ */
+const gchar* const *
+g_udev_device_get_current_tags (GUdevDevice *device)
+{
+ g_return_val_if_fail (G_UDEV_IS_DEVICE (device), NULL);
+
+ return (const gchar * const *) device->priv->current_tags;
+}
+
+/**
* g_udev_device_get_is_initialized:
* @device: A #GUdevDevice.
*
diff --git a/gudev/gudevdevice.h b/gudev/gudevdevice.h
index 69ecc0a..7d674f9 100644
--- a/gudev/gudevdevice.h
+++ b/gudev/gudevdevice.h
@@ -115,6 +115,7 @@ gboolean g_udev_device_get_sysfs_attr_as_boolean (GUdevDevice *devic
const gchar* const *g_udev_device_get_sysfs_attr_as_strv (GUdevDevice *device,
const gchar *name);
const gchar* const *g_udev_device_get_tags (GUdevDevice *device);
+const gchar* const *g_udev_device_get_current_tags (GUdevDevice *device);
gboolean g_udev_device_has_sysfs_attr_uncached (GUdevDevice *device,
const gchar *key);
diff --git a/libgudev-1.0.sym b/libgudev-1.0.sym
index ab9cccf..170f202 100644
--- a/libgudev-1.0.sym
+++ b/libgudev-1.0.sym
@@ -44,6 +44,7 @@ global:
g_udev_device_get_sysfs_attr_keys;
g_udev_device_get_sysfs_path;
g_udev_device_get_tags;
+ g_udev_device_get_current_tags;
g_udev_device_get_type;
g_udev_device_get_usec_since_initialized;
g_udev_device_has_property;
diff --git a/meson.build b/meson.build
index f547a6d..3a1ec98 100644
--- a/meson.build
+++ b/meson.build
@@ -41,7 +41,7 @@ vapidir = join_paths(datadir, 'vala', 'vapi')
cc = meson.get_compiler('c')
glib_req = '>= 2.38.0'
-libudev_req = '>= 199'
+libudev_req = '>= 251'
glib_dep = dependency('glib-2.0', version: glib_req)
gobject_dep = dependency('gobject-2.0', version: glib_req)
diff --git a/tests/test-gudevdevice.c b/tests/test-gudevdevice.c
index a197c22..9639058 100644
--- a/tests/test-gudevdevice.c
+++ b/tests/test-gudevdevice.c
@@ -19,6 +19,7 @@ static void
test_tags ()
{
const char *expected_tags[] = { "tag1", "tag2", "tag3", NULL };
+ const char *expected_current_tags[] = { "tag2", "tag3", NULL };
struct udev *udev = NULL;
struct udev_device *udev_device = NULL;
g_autoptr(GUdevDevice) dev = NULL;
@@ -30,6 +31,7 @@ test_tags ()
g_setenv ("SEQNUM", "1", TRUE);
g_setenv ("TAGS", "tag1:tag2:tag3", TRUE);
g_setenv ("CURRENT_TAGS", "tag2:tag3", TRUE);
+ g_setenv ("UDEV_DATABASE_VERSION", "1", TRUE);
udev = udev_new ();
udev_device = udev_device_new_from_environment (udev);
@@ -38,8 +40,12 @@ test_tags ()
dev = _g_udev_device_new (udev_device);
+ g_assert_nonnull (g_udev_device_get_tags (dev));
g_assert_cmpstrv (expected_tags, g_udev_device_get_tags (dev));
+ g_assert_nonnull (g_udev_device_get_current_tags (dev));
+ g_assert_cmpstrv (expected_current_tags, g_udev_device_get_current_tags (dev));
+
g_clear_pointer (&udev, udev_unref);
g_clear_pointer (&udev_device, udev_device_unref);
}