sd-device: rename device-util.c -> device-filter.c
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 27 Aug 2022 18:22:02 +0000 (03:22 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 28 Aug 2022 00:08:21 +0000 (09:08 +0900)
The functions provided by the file are only used in sd-device.

src/libsystemd/meson.build
src/libsystemd/sd-device/device-enumerator.c
src/libsystemd/sd-device/device-filter.c [new file with mode: 0644]
src/libsystemd/sd-device/device-filter.h [new file with mode: 0644]
src/libsystemd/sd-device/device-monitor.c
src/libsystemd/sd-device/device-util.c [deleted file]
src/libsystemd/sd-device/device-util.h

index 2b288ccb2884a8e0b9c8883825f7f27d7694db01..7f10e87d6c1d6a7fd6a50f5ff1a1beefc9baddcd 100644 (file)
@@ -121,12 +121,13 @@ libsystemd_sources = files(
         'sd-bus/sd-bus.c',
         'sd-device/device-enumerator-private.h',
         'sd-device/device-enumerator.c',
+        'sd-device/device-filter.c',
+        'sd-device/device-filter.h',
         'sd-device/device-internal.h',
         'sd-device/device-monitor-private.h',
         'sd-device/device-monitor.c',
         'sd-device/device-private.c',
         'sd-device/device-private.h',
-        'sd-device/device-util.c',
         'sd-device/device-util.h',
         'sd-device/sd-device.c',
         'sd-hwdb/hwdb-internal.h',
index 30f2520b7245eed5febba2f0d6bc809ae279553f..ffed6fef9b4e2dc2e8560922095389953b0a5e65 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "alloc-util.h"
 #include "device-enumerator-private.h"
+#include "device-filter.h"
 #include "device-util.h"
 #include "dirent-util.h"
 #include "fd-util.h"
diff --git a/src/libsystemd/sd-device/device-filter.c b/src/libsystemd/sd-device/device-filter.c
new file mode 100644 (file)
index 0000000..4101e7d
--- /dev/null
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <fnmatch.h>
+
+#include "device-filter.h"
+#include "path-util.h"
+
+int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null) {
+        char **strv;
+        int r;
+
+        assert(match_strv);
+        assert(key);
+
+        strv = hashmap_get(*match_strv, key);
+        if (strv) {
+                if (!value) {
+                        char **v;
+
+                        if (strv_isempty(strv) || !clear_on_null)
+                                return 0;
+
+                        /* Accept all value. Clear previous assignment. */
+
+                        v = new0(char*, 1);
+                        if (!v)
+                                return -ENOMEM;
+
+                        strv_free_and_replace(strv, v);
+                } else {
+                        if (strv_contains(strv, value))
+                                return 0;
+
+                        r = strv_extend(&strv, value);
+                        if (r < 0)
+                                return r;
+                }
+
+                r = hashmap_update(*match_strv, key, strv);
+                if (r < 0)
+                        return r;
+
+        } else {
+                _cleanup_strv_free_ char **strv_alloc = NULL;
+                _cleanup_free_ char *key_alloc = NULL;
+
+                key_alloc = strdup(key);
+                if (!key_alloc)
+                        return -ENOMEM;
+
+                strv_alloc = strv_new(value);
+                if (!strv_alloc)
+                        return -ENOMEM;
+
+                r = hashmap_ensure_put(match_strv, &string_hash_ops_free_strv_free, key_alloc, strv_alloc);
+                if (r < 0)
+                        return r;
+
+                TAKE_PTR(key_alloc);
+                TAKE_PTR(strv_alloc);
+        }
+
+        return 1;
+}
+
+static bool device_match_sysattr_value(sd_device *device, const char *sysattr, char * const *patterns) {
+        const char *value;
+
+        assert(device);
+        assert(sysattr);
+
+        if (sd_device_get_sysattr_value(device, sysattr, &value) < 0)
+                return false;
+
+        return strv_fnmatch_or_empty(patterns, value, 0);
+}
+
+bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr) {
+        char * const *patterns;
+        const char *sysattr;
+
+        assert(device);
+
+        HASHMAP_FOREACH_KEY(patterns, sysattr, match_sysattr)
+                if (!device_match_sysattr_value(device, sysattr, patterns))
+                        return false;
+
+        HASHMAP_FOREACH_KEY(patterns, sysattr, nomatch_sysattr)
+                if (device_match_sysattr_value(device, sysattr, patterns))
+                        return false;
+
+        return true;
+}
+
+bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent) {
+        const char *syspath_parent, *syspath;
+
+        assert(device);
+
+        if (sd_device_get_syspath(device, &syspath) < 0)
+                return false;
+
+        SET_FOREACH(syspath_parent, nomatch_parent)
+                if (path_startswith(syspath, syspath_parent))
+                        return false;
+
+        if (set_isempty(match_parent))
+                return true;
+
+        SET_FOREACH(syspath_parent, match_parent)
+                if (path_startswith(syspath, syspath_parent))
+                        return true;
+
+        return false;
+}
diff --git a/src/libsystemd/sd-device/device-filter.h b/src/libsystemd/sd-device/device-filter.h
new file mode 100644 (file)
index 0000000..0c5f34e
--- /dev/null
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <stdbool.h>
+
+#include "sd-device.h"
+
+#include "hashmap.h"
+#include "set.h"
+
+int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null);
+bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr);
+bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent);
index 566280d66f3f03da729a64fa2dfaf7d2c6c4918b..ba997b7f2c3bbf069f87f7df56e3152f71c53733 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "MurmurHash2.h"
 #include "alloc-util.h"
+#include "device-filter.h"
 #include "device-monitor-private.h"
 #include "device-private.h"
 #include "device-util.h"
diff --git a/src/libsystemd/sd-device/device-util.c b/src/libsystemd/sd-device/device-util.c
deleted file mode 100644 (file)
index 3b8689e..0000000
+++ /dev/null
@@ -1,115 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include <fnmatch.h>
-
-#include "device-util.h"
-#include "path-util.h"
-
-int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null) {
-        char **strv;
-        int r;
-
-        assert(match_strv);
-        assert(key);
-
-        strv = hashmap_get(*match_strv, key);
-        if (strv) {
-                if (!value) {
-                        char **v;
-
-                        if (strv_isempty(strv) || !clear_on_null)
-                                return 0;
-
-                        /* Accept all value. Clear previous assignment. */
-
-                        v = new0(char*, 1);
-                        if (!v)
-                                return -ENOMEM;
-
-                        strv_free_and_replace(strv, v);
-                } else {
-                        if (strv_contains(strv, value))
-                                return 0;
-
-                        r = strv_extend(&strv, value);
-                        if (r < 0)
-                                return r;
-                }
-
-                r = hashmap_update(*match_strv, key, strv);
-                if (r < 0)
-                        return r;
-
-        } else {
-                _cleanup_strv_free_ char **strv_alloc = NULL;
-                _cleanup_free_ char *key_alloc = NULL;
-
-                key_alloc = strdup(key);
-                if (!key_alloc)
-                        return -ENOMEM;
-
-                strv_alloc = strv_new(value);
-                if (!strv_alloc)
-                        return -ENOMEM;
-
-                r = hashmap_ensure_put(match_strv, &string_hash_ops_free_strv_free, key_alloc, strv_alloc);
-                if (r < 0)
-                        return r;
-
-                TAKE_PTR(key_alloc);
-                TAKE_PTR(strv_alloc);
-        }
-
-        return 1;
-}
-
-static bool device_match_sysattr_value(sd_device *device, const char *sysattr, char * const *patterns) {
-        const char *value;
-
-        assert(device);
-        assert(sysattr);
-
-        if (sd_device_get_sysattr_value(device, sysattr, &value) < 0)
-                return false;
-
-        return strv_fnmatch_or_empty(patterns, value, 0);
-}
-
-bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr) {
-        char * const *patterns;
-        const char *sysattr;
-
-        assert(device);
-
-        HASHMAP_FOREACH_KEY(patterns, sysattr, match_sysattr)
-                if (!device_match_sysattr_value(device, sysattr, patterns))
-                        return false;
-
-        HASHMAP_FOREACH_KEY(patterns, sysattr, nomatch_sysattr)
-                if (device_match_sysattr_value(device, sysattr, patterns))
-                        return false;
-
-        return true;
-}
-
-bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent) {
-        const char *syspath_parent, *syspath;
-
-        assert(device);
-
-        if (sd_device_get_syspath(device, &syspath) < 0)
-                return false;
-
-        SET_FOREACH(syspath_parent, nomatch_parent)
-                if (path_startswith(syspath, syspath_parent))
-                        return false;
-
-        if (set_isempty(match_parent))
-                return true;
-
-        SET_FOREACH(syspath_parent, match_parent)
-                if (path_startswith(syspath, syspath_parent))
-                        return true;
-
-        return false;
-}
index d9e9f1e8c4a0a8581b8144ed08a2e97d8522f52f..d92cbd6cc7411b659b9c995240614791383b06bb 100644 (file)
@@ -5,10 +5,8 @@
 
 #include "sd-device.h"
 
-#include "hashmap.h"
 #include "log.h"
 #include "macro.h"
-#include "set.h"
 
 #define device_unref_and_replace(a, b)                                  \
         unref_and_replace_full(a, b, sd_device_ref, sd_device_unref)
@@ -81,7 +79,3 @@
 #define log_device_notice_errno(device, error, ...)  log_device_full_errno(device, LOG_NOTICE, error, __VA_ARGS__)
 #define log_device_warning_errno(device, error, ...) log_device_full_errno(device, LOG_WARNING, error, __VA_ARGS__)
 #define log_device_error_errno(device, error, ...)   log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__)
-
-int update_match_strv(Hashmap **match_strv, const char *key, const char *value, bool clear_on_null);
-bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr);
-bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent);