| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "hashmap.h" |
| 5 |
#include "libudev-list-internal.h" |
| 6 |
#include "list.h" |
| 7 |
#include "string-util.h" |
| 8 |
|
| 9 |
/** |
| 10 |
* SECTION:libudev-list |
| 11 |
* @short_description: list operation |
| 12 |
* |
| 13 |
* Libudev list operations. |
| 14 |
*/ |
| 15 |
|
| 16 |
/** |
| 17 |
* udev_list_entry: |
| 18 |
* |
| 19 |
* Opaque object representing one entry in a list. An entry contains |
| 20 |
* contains a name, and optionally a value. |
| 21 |
*/ |
| 22 |
struct udev_list_entry { |
| 23 |
struct udev_list *list; |
| 24 |
char *name; |
| 25 |
char *value; |
| 26 |
|
| 27 |
LIST_FIELDS(struct udev_list_entry, entries); |
| 28 |
}; |
| 29 |
|
| 30 |
struct udev_list { |
| 31 |
Hashmap *unique_entries; |
| 32 |
LIST_HEAD(struct udev_list_entry, entries); |
| 33 |
bool unique:1; |
| 34 |
bool uptodate:1; |
| 35 |
}; |
| 36 |
|
| 37 |
static struct udev_list_entry* udev_list_entry_free(struct udev_list_entry *entry) { |
| 38 |
if (!entry) |
| 39 |
return NULL; |
| 40 |
|
| 41 |
if (entry->list) { |
| 42 |
if (entry->list->unique && entry->name) |
| 43 |
hashmap_remove(entry->list->unique_entries, entry->name); |
| 44 |
|
| 45 |
if (!entry->list->unique || entry->list->uptodate) |
| 46 |
LIST_REMOVE(entries, entry->list->entries, entry); |
| 47 |
} |
| 48 |
|
| 49 |
free(entry->name); |
| 50 |
free(entry->value); |
| 51 |
|
| 52 |
return mfree(entry); |
| 53 |
} |
| 54 |
|
| 55 |
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_list_entry*, udev_list_entry_free); |
| 56 |
|
| 57 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 58 |
udev_list_entry_hash_ops, |
| 59 |
char, string_hash_func, string_compare_func, |
| 60 |
struct udev_list_entry, udev_list_entry_free); |
| 61 |
|
| 62 |
struct udev_list* udev_list_new(bool unique) { |
| 63 |
struct udev_list *list; |
| 64 |
|
| 65 |
list = new(struct udev_list, 1); |
| 66 |
if (!list) |
| 67 |
return NULL; |
| 68 |
|
| 69 |
*list = (struct udev_list) { |
| 70 |
.unique = unique, |
| 71 |
}; |
| 72 |
|
| 73 |
return list; |
| 74 |
} |
| 75 |
|
| 76 |
struct udev_list_entry* udev_list_entry_add(struct udev_list *list, const char *name, const char *value) { |
| 77 |
_cleanup_(udev_list_entry_freep) struct udev_list_entry *entry = NULL; |
| 78 |
|
| 79 |
assert(list); |
| 80 |
assert(name); |
| 81 |
|
| 82 |
entry = new0(struct udev_list_entry, 1); |
| 83 |
if (!entry) |
| 84 |
return NULL; |
| 85 |
|
| 86 |
if (strdup_to(&entry->name, name) < 0) |
| 87 |
return NULL; |
| 88 |
|
| 89 |
if (strdup_to(&entry->value, value) < 0) |
| 90 |
return NULL; |
| 91 |
|
| 92 |
if (list->unique) { |
| 93 |
udev_list_entry_free(hashmap_get(list->unique_entries, entry->name)); |
| 94 |
|
| 95 |
if (hashmap_ensure_put(&list->unique_entries, &udev_list_entry_hash_ops, entry->name, entry) < 0) |
| 96 |
return NULL; |
| 97 |
|
| 98 |
list->uptodate = false; |
| 99 |
} else |
| 100 |
LIST_APPEND(entries, list->entries, entry); |
| 101 |
|
| 102 |
entry->list = list; |
| 103 |
|
| 104 |
return TAKE_PTR(entry); |
| 105 |
} |
| 106 |
|
| 107 |
void udev_list_cleanup(struct udev_list *list) { |
| 108 |
if (!list) |
| 109 |
return; |
| 110 |
|
| 111 |
if (list->unique) { |
| 112 |
list->uptodate = false; |
| 113 |
hashmap_clear(list->unique_entries); |
| 114 |
} else |
| 115 |
LIST_FOREACH(entries, i, list->entries) |
| 116 |
udev_list_entry_free(i); |
| 117 |
} |
| 118 |
|
| 119 |
struct udev_list* udev_list_free(struct udev_list *list) { |
| 120 |
if (!list) |
| 121 |
return NULL; |
| 122 |
|
| 123 |
udev_list_cleanup(list); |
| 124 |
hashmap_free(list->unique_entries); |
| 125 |
|
| 126 |
return mfree(list); |
| 127 |
} |
| 128 |
|
| 129 |
struct udev_list_entry* udev_list_get_entry(struct udev_list *list) { |
| 130 |
if (!list) |
| 131 |
return NULL; |
| 132 |
|
| 133 |
if (list->unique && !list->uptodate) { |
| 134 |
size_t n; |
| 135 |
|
| 136 |
LIST_HEAD_INIT(list->entries); |
| 137 |
|
| 138 |
n = hashmap_size(list->unique_entries); |
| 139 |
if (n == 0) |
| 140 |
; |
| 141 |
else if (n == 1) |
| 142 |
LIST_PREPEND(entries, list->entries, hashmap_first(list->unique_entries)); |
| 143 |
else { |
| 144 |
_cleanup_free_ struct udev_list_entry **buf = NULL; |
| 145 |
|
| 146 |
if (hashmap_dump_sorted(list->unique_entries, (void***) &buf, /* ret_n= */ NULL) < 0) |
| 147 |
return NULL; |
| 148 |
|
| 149 |
for (size_t j = n; j > 0; j--) |
| 150 |
LIST_PREPEND(entries, list->entries, buf[j-1]); |
| 151 |
} |
| 152 |
|
| 153 |
list->uptodate = true; |
| 154 |
} |
| 155 |
|
| 156 |
return list->entries; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* udev_list_entry_get_next: |
| 161 |
* @list_entry: current entry |
| 162 |
* |
| 163 |
* Get the next entry from the list. |
| 164 |
* |
| 165 |
* Returns: udev_list_entry, #NULL if no more entries are available. |
| 166 |
*/ |
| 167 |
_public_ struct udev_list_entry* udev_list_entry_get_next(struct udev_list_entry *list_entry) { |
| 168 |
if (!list_entry) |
| 169 |
return NULL; |
| 170 |
if (list_entry->list->unique && !list_entry->list->uptodate) |
| 171 |
return NULL; |
| 172 |
return list_entry->entries_next; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* udev_list_entry_get_by_name: |
| 177 |
* @list_entry: current entry |
| 178 |
* @name: name string to match |
| 179 |
* |
| 180 |
* Lookup an entry in the list with a certain name. |
| 181 |
* |
| 182 |
* Returns: udev_list_entry, #NULL if no matching entry is found. |
| 183 |
*/ |
| 184 |
_public_ struct udev_list_entry* udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name) { |
| 185 |
if (!list_entry) |
| 186 |
return NULL; |
| 187 |
if (!list_entry->list->unique || !list_entry->list->uptodate) |
| 188 |
return NULL; |
| 189 |
return hashmap_get(list_entry->list->unique_entries, name); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* udev_list_entry_get_name: |
| 194 |
* @list_entry: current entry |
| 195 |
* |
| 196 |
* Get the name of a list entry. |
| 197 |
* |
| 198 |
* Returns: the name string of this entry. |
| 199 |
*/ |
| 200 |
_public_ const char* udev_list_entry_get_name(struct udev_list_entry *list_entry) { |
| 201 |
if (!list_entry) |
| 202 |
return NULL; |
| 203 |
return list_entry->name; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* udev_list_entry_get_value: |
| 208 |
* @list_entry: current entry |
| 209 |
* |
| 210 |
* Get the value of list entry. |
| 211 |
* |
| 212 |
* Returns: the value string of this entry. |
| 213 |
*/ |
| 214 |
_public_ const char* udev_list_entry_get_value(struct udev_list_entry *list_entry) { |
| 215 |
if (!list_entry) |
| 216 |
return NULL; |
| 217 |
return list_entry->value; |
| 218 |
} |