1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fnmatch.h>
4
5 #include "device-util.h"
6 #include "path-util.h"
7
device_match_sysattr_value(sd_device * device,const char * sysattr,const char * match_value)8 static bool device_match_sysattr_value(sd_device *device, const char *sysattr, const char *match_value) {
9 const char *value;
10
11 assert(device);
12 assert(sysattr);
13
14 if (sd_device_get_sysattr_value(device, sysattr, &value) < 0)
15 return false;
16
17 if (!match_value)
18 return true;
19
20 if (fnmatch(match_value, value, 0) == 0)
21 return true;
22
23 return false;
24 }
25
device_match_sysattr(sd_device * device,Hashmap * match_sysattr,Hashmap * nomatch_sysattr)26 bool device_match_sysattr(sd_device *device, Hashmap *match_sysattr, Hashmap *nomatch_sysattr) {
27 const char *sysattr;
28 const char *value;
29
30 assert(device);
31
32 HASHMAP_FOREACH_KEY(value, sysattr, match_sysattr)
33 if (!device_match_sysattr_value(device, sysattr, value))
34 return false;
35
36 HASHMAP_FOREACH_KEY(value, sysattr, nomatch_sysattr)
37 if (device_match_sysattr_value(device, sysattr, value))
38 return false;
39
40 return true;
41 }
42
device_match_parent(sd_device * device,Set * match_parent,Set * nomatch_parent)43 bool device_match_parent(sd_device *device, Set *match_parent, Set *nomatch_parent) {
44 const char *syspath_parent, *syspath;
45
46 assert(device);
47
48 if (sd_device_get_syspath(device, &syspath) < 0)
49 return false;
50
51 SET_FOREACH(syspath_parent, nomatch_parent)
52 if (path_startswith(syspath, syspath_parent))
53 return false;
54
55 if (set_isempty(match_parent))
56 return true;
57
58 SET_FOREACH(syspath_parent, match_parent)
59 if (path_startswith(syspath, syspath_parent))
60 return true;
61
62 return false;
63 }
64