Lucas De Marchi | 8f923be | 2011-12-03 04:28:49 -0200 | [diff] [blame] | 1 | /* |
| 2 | * libkmod - interface to kernel module operations |
| 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
Lucas De Marchi | 8f923be | 2011-12-03 04:28:49 -0200 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
Lucas De Marchi | 8f923be | 2011-12-03 04:28:49 -0200 | [diff] [blame] | 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 20 | |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 21 | #include <arpa/inet.h> |
| 22 | #include <assert.h> |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <fnmatch.h> |
Lucas De Marchi | bfcd31d | 2012-03-02 21:28:11 -0300 | [diff] [blame] | 25 | #include <inttypes.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 29 | |
Lucas De Marchi | 576dd43 | 2014-10-02 22:03:19 -0300 | [diff] [blame] | 30 | #include <shared/macro.h> |
Lucas De Marchi | b4d1f44 | 2014-10-11 13:03:21 -0300 | [diff] [blame] | 31 | #include <shared/strbuf.h> |
Lucas De Marchi | 96573a0 | 2014-10-03 00:01:35 -0300 | [diff] [blame] | 32 | #include <shared/util.h> |
Lucas De Marchi | 576dd43 | 2014-10-02 22:03:19 -0300 | [diff] [blame] | 33 | |
Lucas De Marchi | 83b855a | 2013-07-04 16:13:11 -0300 | [diff] [blame] | 34 | #include "libkmod-internal.h" |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 35 | #include "libkmod-index.h" |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 36 | |
Lucas De Marchi | 8f923be | 2011-12-03 04:28:49 -0200 | [diff] [blame] | 37 | /* index.c: module index file shared functions for modprobe and depmod */ |
| 38 | |
Gustavo Sverzut Barbieri | 148226e | 2011-12-10 11:53:51 -0200 | [diff] [blame] | 39 | #define INDEX_CHILDMAX 128 |
| 40 | |
| 41 | /* Disk format: |
| 42 | |
| 43 | uint32_t magic = INDEX_MAGIC; |
| 44 | uint32_t version = INDEX_VERSION; |
| 45 | uint32_t root_offset; |
| 46 | |
| 47 | (node_offset & INDEX_NODE_MASK) specifies the file offset of nodes: |
| 48 | |
| 49 | char[] prefix; // nul terminated |
| 50 | |
| 51 | char first; |
| 52 | char last; |
| 53 | uint32_t children[last - first + 1]; |
| 54 | |
| 55 | uint32_t value_count; |
| 56 | struct { |
| 57 | uint32_t priority; |
| 58 | char[] value; // nul terminated |
| 59 | } values[value_count]; |
| 60 | |
| 61 | (node_offset & INDEX_NODE_FLAGS) indicates which fields are present. |
| 62 | Empty prefixes are omitted, leaf nodes omit the three child-related fields. |
| 63 | |
| 64 | This could be optimised further by adding a sparse child format |
| 65 | (indicated using a new flag). |
| 66 | */ |
| 67 | |
| 68 | /* Format of node offsets within index file */ |
| 69 | enum node_offset { |
| 70 | INDEX_NODE_FLAGS = 0xF0000000, /* Flags in high nibble */ |
| 71 | INDEX_NODE_PREFIX = 0x80000000, |
| 72 | INDEX_NODE_VALUES = 0x40000000, |
| 73 | INDEX_NODE_CHILDS = 0x20000000, |
| 74 | |
| 75 | INDEX_NODE_MASK = 0x0FFFFFFF, /* Offset value */ |
| 76 | }; |
| 77 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 78 | void index_values_free(struct index_value *values) |
| 79 | { |
| 80 | while (values) { |
| 81 | struct index_value *value = values; |
| 82 | |
| 83 | values = value->next; |
| 84 | free(value); |
| 85 | } |
| 86 | } |
| 87 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 88 | static int add_value(struct index_value **values, |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 89 | const char *value, unsigned len, unsigned int priority) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 90 | { |
| 91 | struct index_value *v; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 92 | |
| 93 | /* find position to insert value */ |
| 94 | while (*values && (*values)->priority < priority) |
| 95 | values = &(*values)->next; |
| 96 | |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 97 | v = malloc(sizeof(struct index_value) + len + 1); |
| 98 | if (!v) |
| 99 | return -1; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 100 | v->next = *values; |
| 101 | v->priority = priority; |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 102 | v->len = len; |
| 103 | memcpy(v->value, value, len); |
| 104 | v->value[len] = '\0'; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 105 | *values = v; |
| 106 | |
Gustavo Sverzut Barbieri | 558b020 | 2011-12-08 16:36:48 -0200 | [diff] [blame] | 107 | return 0; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 108 | } |
| 109 | |
Lucas De Marchi | 9368888 | 2011-12-02 10:05:31 -0200 | [diff] [blame] | 110 | static void read_error(void) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 111 | { |
| 112 | fatal("Module index: unexpected error: %s\n" |
| 113 | "Try re-running depmod\n", errno ? strerror(errno) : "EOF"); |
| 114 | } |
| 115 | |
| 116 | static int read_char(FILE *in) |
| 117 | { |
| 118 | int ch; |
| 119 | |
| 120 | errno = 0; |
| 121 | ch = getc_unlocked(in); |
| 122 | if (ch == EOF) |
| 123 | read_error(); |
| 124 | return ch; |
| 125 | } |
| 126 | |
| 127 | static uint32_t read_long(FILE *in) |
| 128 | { |
| 129 | uint32_t l; |
| 130 | |
| 131 | errno = 0; |
Leandro Pereira | b6d985c | 2014-04-28 20:47:49 -0300 | [diff] [blame] | 132 | if (fread(&l, sizeof(uint32_t), 1, in) != sizeof(uint32_t)) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 133 | read_error(); |
| 134 | return ntohl(l); |
| 135 | } |
| 136 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 137 | static unsigned buf_freadchars(struct strbuf *buf, FILE *in) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 138 | { |
| 139 | unsigned i = 0; |
| 140 | int ch; |
| 141 | |
| 142 | while ((ch = read_char(in))) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 143 | if (!strbuf_pushchar(buf, ch)) |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 144 | break; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 145 | i++; |
| 146 | } |
| 147 | |
| 148 | return i; |
| 149 | } |
| 150 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 151 | /* |
Lucas De Marchi | eb8bb32 | 2011-12-02 10:23:02 -0200 | [diff] [blame] | 152 | * Index file searching |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 153 | */ |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 154 | struct index_node_f { |
| 155 | FILE *file; |
| 156 | char *prefix; /* path compression */ |
| 157 | struct index_value *values; |
| 158 | unsigned char first; /* range of child nodes */ |
| 159 | unsigned char last; |
| 160 | uint32_t children[0]; |
| 161 | }; |
| 162 | |
| 163 | static struct index_node_f *index_read(FILE *in, uint32_t offset) |
| 164 | { |
| 165 | struct index_node_f *node; |
| 166 | char *prefix; |
| 167 | int i, child_count = 0; |
| 168 | |
| 169 | if ((offset & INDEX_NODE_MASK) == 0) |
| 170 | return NULL; |
| 171 | |
| 172 | fseek(in, offset & INDEX_NODE_MASK, SEEK_SET); |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 173 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 174 | if (offset & INDEX_NODE_PREFIX) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 175 | struct strbuf buf; |
| 176 | strbuf_init(&buf); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 177 | buf_freadchars(&buf, in); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 178 | prefix = strbuf_steal(&buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 179 | } else |
| 180 | prefix = NOFAIL(strdup("")); |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 181 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 182 | if (offset & INDEX_NODE_CHILDS) { |
| 183 | char first = read_char(in); |
| 184 | char last = read_char(in); |
| 185 | child_count = last - first + 1; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 186 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 187 | node = NOFAIL(malloc(sizeof(struct index_node_f) + |
| 188 | sizeof(uint32_t) * child_count)); |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 189 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 190 | node->first = first; |
| 191 | node->last = last; |
| 192 | |
| 193 | for (i = 0; i < child_count; i++) |
| 194 | node->children[i] = read_long(in); |
| 195 | } else { |
| 196 | node = NOFAIL(malloc(sizeof(struct index_node_f))); |
| 197 | node->first = INDEX_CHILDMAX; |
| 198 | node->last = 0; |
| 199 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 200 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 201 | node->values = NULL; |
| 202 | if (offset & INDEX_NODE_VALUES) { |
| 203 | int value_count; |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 204 | struct strbuf buf; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 205 | const char *value; |
| 206 | unsigned int priority; |
| 207 | |
| 208 | value_count = read_long(in); |
| 209 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 210 | strbuf_init(&buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 211 | while (value_count--) { |
| 212 | priority = read_long(in); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 213 | buf_freadchars(&buf, in); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 214 | value = strbuf_str(&buf); |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 215 | add_value(&node->values, value, buf.used, priority); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 216 | strbuf_clear(&buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 217 | } |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 218 | strbuf_release(&buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | node->prefix = prefix; |
| 222 | node->file = in; |
| 223 | return node; |
| 224 | } |
| 225 | |
| 226 | static void index_close(struct index_node_f *node) |
| 227 | { |
| 228 | free(node->prefix); |
| 229 | index_values_free(node->values); |
| 230 | free(node); |
| 231 | } |
| 232 | |
| 233 | struct index_file { |
| 234 | FILE *file; |
| 235 | uint32_t root_offset; |
| 236 | }; |
| 237 | |
| 238 | /* Failures are silent; modprobe will fall back to text files */ |
| 239 | struct index_file *index_file_open(const char *filename) |
| 240 | { |
| 241 | FILE *file; |
| 242 | uint32_t magic, version; |
| 243 | struct index_file *new; |
| 244 | |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 245 | file = fopen(filename, "re"); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 246 | if (!file) |
| 247 | return NULL; |
| 248 | errno = EINVAL; |
| 249 | |
| 250 | magic = read_long(file); |
Cristian RodrÃguez | 67d94ad | 2011-12-23 03:06:56 -0200 | [diff] [blame] | 251 | if (magic != INDEX_MAGIC) { |
| 252 | fclose(file); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 253 | return NULL; |
Cristian RodrÃguez | 67d94ad | 2011-12-23 03:06:56 -0200 | [diff] [blame] | 254 | } |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 255 | |
| 256 | version = read_long(file); |
Cristian RodrÃguez | 4088b27 | 2011-12-26 01:38:04 -0300 | [diff] [blame] | 257 | if (version >> 16 != INDEX_VERSION_MAJOR) { |
| 258 | fclose(file); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 259 | return NULL; |
Cristian RodrÃguez | 4088b27 | 2011-12-26 01:38:04 -0300 | [diff] [blame] | 260 | } |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 261 | |
| 262 | new = NOFAIL(malloc(sizeof(struct index_file))); |
| 263 | new->file = file; |
| 264 | new->root_offset = read_long(new->file); |
| 265 | |
| 266 | errno = 0; |
| 267 | return new; |
| 268 | } |
| 269 | |
Lucas De Marchi | 0fbdfef | 2011-12-02 09:56:22 -0200 | [diff] [blame] | 270 | void index_file_close(struct index_file *idx) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 271 | { |
Lucas De Marchi | 0fbdfef | 2011-12-02 09:56:22 -0200 | [diff] [blame] | 272 | fclose(idx->file); |
| 273 | free(idx); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 274 | } |
| 275 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 276 | static struct index_node_f *index_readroot(struct index_file *in) |
| 277 | { |
| 278 | return index_read(in->file, in->root_offset); |
| 279 | } |
| 280 | |
| 281 | static struct index_node_f *index_readchild(const struct index_node_f *parent, |
| 282 | int ch) |
| 283 | { |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 284 | if (parent->first <= ch && ch <= parent->last) { |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 285 | return index_read(parent->file, |
| 286 | parent->children[ch - parent->first]); |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return NULL; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 290 | } |
| 291 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 292 | static void index_dump_node(struct index_node_f *node, struct strbuf *buf, |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 293 | int fd) |
| 294 | { |
| 295 | struct index_value *v; |
| 296 | int ch, pushed; |
| 297 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 298 | pushed = strbuf_pushchars(buf, node->prefix); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 299 | |
| 300 | for (v = node->values; v != NULL; v = v->next) { |
| 301 | write_str_safe(fd, buf->bytes, buf->used); |
| 302 | write_str_safe(fd, " ", 1); |
| 303 | write_str_safe(fd, v->value, strlen(v->value)); |
| 304 | write_str_safe(fd, "\n", 1); |
| 305 | } |
| 306 | |
| 307 | for (ch = node->first; ch <= node->last; ch++) { |
| 308 | struct index_node_f *child = index_readchild(node, ch); |
| 309 | |
| 310 | if (!child) |
| 311 | continue; |
| 312 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 313 | strbuf_pushchar(buf, ch); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 314 | index_dump_node(child, buf, fd); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 315 | strbuf_popchar(buf); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 316 | } |
| 317 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 318 | strbuf_popchars(buf, pushed); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 319 | index_close(node); |
| 320 | } |
| 321 | |
| 322 | void index_dump(struct index_file *in, int fd, const char *prefix) |
| 323 | { |
| 324 | struct index_node_f *root; |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 325 | struct strbuf buf; |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 326 | |
Lucas De Marchi | 681bf89 | 2013-04-23 21:21:00 -0300 | [diff] [blame] | 327 | root = index_readroot(in); |
| 328 | if (root == NULL) |
| 329 | return; |
| 330 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 331 | strbuf_init(&buf); |
| 332 | strbuf_pushchars(&buf, prefix); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 333 | index_dump_node(root, &buf, fd); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 334 | strbuf_release(&buf); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 335 | } |
| 336 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 337 | static char *index_search__node(struct index_node_f *node, const char *key, int i) |
| 338 | { |
| 339 | char *value; |
| 340 | struct index_node_f *child; |
| 341 | int ch; |
| 342 | int j; |
| 343 | |
| 344 | while(node) { |
| 345 | for (j = 0; node->prefix[j]; j++) { |
| 346 | ch = node->prefix[j]; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 347 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 348 | if (ch != key[i+j]) { |
| 349 | index_close(node); |
| 350 | return NULL; |
| 351 | } |
| 352 | } |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 353 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 354 | i += j; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 355 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 356 | if (key[i] == '\0') { |
Lucas De Marchi | 817f4e3 | 2012-02-27 19:54:33 -0300 | [diff] [blame] | 357 | value = node->values != NULL |
| 358 | ? strdup(node->values[0].value) |
| 359 | : NULL; |
| 360 | |
| 361 | index_close(node); |
| 362 | return value; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 363 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 364 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 365 | child = index_readchild(node, key[i]); |
| 366 | index_close(node); |
| 367 | node = child; |
| 368 | i++; |
| 369 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 370 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | /* |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 375 | * Search the index for a key |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 376 | * |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 377 | * Returns the value of the first match |
| 378 | * |
| 379 | * The recursive functions free their node argument (using index_close). |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 380 | */ |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 381 | char *index_search(struct index_file *in, const char *key) |
| 382 | { |
Lucas De Marchi | ee1d188 | 2012-02-27 18:48:02 -0300 | [diff] [blame] | 383 | // FIXME: return value by reference instead of strdup |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 384 | struct index_node_f *root; |
| 385 | char *value; |
| 386 | |
| 387 | root = index_readroot(in); |
| 388 | value = index_search__node(root, key, 0); |
| 389 | |
| 390 | return value; |
| 391 | } |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 392 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 393 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 394 | |
| 395 | /* Level 4: add all the values from a matching node */ |
| 396 | static void index_searchwild__allvalues(struct index_node_f *node, |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 397 | struct index_value **out) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 398 | { |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 399 | struct index_value *v; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 400 | |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 401 | for (v = node->values; v != NULL; v = v->next) |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 402 | add_value(out, v->value, v->len, v->priority); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 403 | |
| 404 | index_close(node); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 405 | } |
| 406 | |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 407 | /* |
| 408 | * Level 3: traverse a sub-keyspace which starts with a wildcard, |
| 409 | * looking for matches. |
| 410 | */ |
| 411 | static void index_searchwild__all(struct index_node_f *node, int j, |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 412 | struct strbuf *buf, |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 413 | const char *subkey, |
| 414 | struct index_value **out) |
| 415 | { |
| 416 | int pushed = 0; |
| 417 | int ch; |
| 418 | |
| 419 | while (node->prefix[j]) { |
| 420 | ch = node->prefix[j]; |
| 421 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 422 | strbuf_pushchar(buf, ch); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 423 | pushed++; |
| 424 | j++; |
| 425 | } |
| 426 | |
| 427 | for (ch = node->first; ch <= node->last; ch++) { |
| 428 | struct index_node_f *child = index_readchild(node, ch); |
| 429 | |
| 430 | if (!child) |
| 431 | continue; |
| 432 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 433 | strbuf_pushchar(buf, ch); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 434 | index_searchwild__all(child, 0, buf, subkey, out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 435 | strbuf_popchar(buf); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if (node->values) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 439 | if (fnmatch(strbuf_str(buf), subkey, 0) == 0) |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 440 | index_searchwild__allvalues(node, out); |
Gustavo Sverzut Barbieri | 27fdf63 | 2011-12-10 13:28:18 -0200 | [diff] [blame] | 441 | else |
| 442 | index_close(node); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 443 | } else { |
| 444 | index_close(node); |
| 445 | } |
| 446 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 447 | strbuf_popchars(buf, pushed); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* Level 2: descend the tree (until we hit a wildcard) */ |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 451 | static void index_searchwild__node(struct index_node_f *node, |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 452 | struct strbuf *buf, |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 453 | const char *key, int i, |
| 454 | struct index_value **out) |
| 455 | { |
| 456 | struct index_node_f *child; |
| 457 | int j; |
| 458 | int ch; |
| 459 | |
| 460 | while(node) { |
| 461 | for (j = 0; node->prefix[j]; j++) { |
| 462 | ch = node->prefix[j]; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 463 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 464 | if (ch == '*' || ch == '?' || ch == '[') { |
| 465 | index_searchwild__all(node, j, buf, |
| 466 | &key[i+j], out); |
| 467 | return; |
| 468 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 469 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 470 | if (ch != key[i+j]) { |
| 471 | index_close(node); |
| 472 | return; |
| 473 | } |
| 474 | } |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 475 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 476 | i += j; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 477 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 478 | child = index_readchild(node, '*'); |
| 479 | if (child) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 480 | strbuf_pushchar(buf, '*'); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 481 | index_searchwild__all(child, 0, buf, &key[i], out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 482 | strbuf_popchar(buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 483 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 484 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 485 | child = index_readchild(node, '?'); |
| 486 | if (child) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 487 | strbuf_pushchar(buf, '?'); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 488 | index_searchwild__all(child, 0, buf, &key[i], out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 489 | strbuf_popchar(buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 490 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 491 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 492 | child = index_readchild(node, '['); |
| 493 | if (child) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 494 | strbuf_pushchar(buf, '['); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 495 | index_searchwild__all(child, 0, buf, &key[i], out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 496 | strbuf_popchar(buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 497 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 498 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 499 | if (key[i] == '\0') { |
| 500 | index_searchwild__allvalues(node, out); |
| 501 | |
| 502 | return; |
| 503 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 504 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 505 | child = index_readchild(node, key[i]); |
| 506 | index_close(node); |
| 507 | node = child; |
| 508 | i++; |
| 509 | } |
| 510 | } |
| 511 | |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 512 | /* |
| 513 | * Search the index for a key. The index may contain wildcards. |
| 514 | * |
| 515 | * Returns a list of all the values of matching keys. |
| 516 | */ |
| 517 | struct index_value *index_searchwild(struct index_file *in, const char *key) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 518 | { |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 519 | struct index_node_f *root = index_readroot(in); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 520 | struct strbuf buf; |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 521 | struct index_value *out = NULL; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 522 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 523 | strbuf_init(&buf); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 524 | index_searchwild__node(root, &buf, key, 0, &out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 525 | strbuf_release(&buf); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 526 | return out; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 527 | } |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 528 | |
| 529 | #include <sys/mman.h> |
| 530 | #include <sys/stat.h> |
| 531 | #include <unistd.h> |
| 532 | |
Gustavo Sverzut Barbieri | 15c1c14 | 2011-12-10 11:44:31 -0200 | [diff] [blame] | 533 | static const char _idx_empty_str[] = ""; |
| 534 | |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 535 | /**************************************************************************/ |
| 536 | /* |
| 537 | * Alternative implementation, using mmap to map all the file to memory when |
| 538 | * starting |
| 539 | */ |
| 540 | struct index_mm { |
| 541 | struct kmod_ctx *ctx; |
| 542 | void *mm; |
| 543 | uint32_t root_offset; |
| 544 | size_t size; |
| 545 | }; |
| 546 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 547 | struct index_mm_value { |
| 548 | unsigned int priority; |
| 549 | unsigned int len; |
| 550 | const char *value; |
| 551 | }; |
| 552 | |
| 553 | struct index_mm_value_array { |
| 554 | struct index_mm_value *values; |
| 555 | unsigned int len; |
| 556 | }; |
| 557 | |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 558 | struct index_mm_node { |
| 559 | struct index_mm *idx; |
Gustavo Sverzut Barbieri | 15c1c14 | 2011-12-10 11:44:31 -0200 | [diff] [blame] | 560 | const char *prefix; /* mmape'd value */ |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 561 | struct index_mm_value_array values; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 562 | unsigned char first; |
| 563 | unsigned char last; |
| 564 | uint32_t children[]; |
| 565 | }; |
| 566 | |
| 567 | static inline uint32_t read_long_mm(void **p) |
| 568 | { |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 569 | uint8_t *addr = *(uint8_t **)p; |
| 570 | uint32_t v; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 571 | |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 572 | /* addr may be unalined to uint32_t */ |
Lucas De Marchi | 5bbec8c | 2012-05-23 19:32:58 -0300 | [diff] [blame] | 573 | v = get_unaligned((uint32_t *) addr); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 574 | |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 575 | *p = addr + sizeof(uint32_t); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 576 | return ntohl(v); |
| 577 | } |
| 578 | |
| 579 | static inline uint8_t read_char_mm(void **p) |
| 580 | { |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 581 | uint8_t *addr = *(uint8_t **)p; |
| 582 | uint8_t v = *addr; |
| 583 | *p = addr + sizeof(uint8_t); |
| 584 | return v; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 585 | } |
| 586 | |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 587 | static inline char *read_chars_mm(void **p, unsigned *rlen) |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 588 | { |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 589 | char *addr = *(char **)p; |
| 590 | size_t len = *rlen = strlen(addr); |
| 591 | *p = addr + len + 1; |
| 592 | return addr; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static struct index_mm_node *index_mm_read_node(struct index_mm *idx, |
| 596 | uint32_t offset) { |
| 597 | void *p = idx->mm; |
| 598 | struct index_mm_node *node; |
Gustavo Sverzut Barbieri | 15c1c14 | 2011-12-10 11:44:31 -0200 | [diff] [blame] | 599 | const char *prefix; |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 600 | int i, child_count, value_count, children_padding; |
| 601 | uint32_t children[INDEX_CHILDMAX]; |
| 602 | char first, last; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 603 | |
| 604 | if ((offset & INDEX_NODE_MASK) == 0) |
| 605 | return NULL; |
| 606 | |
| 607 | p = (char *)p + (offset & INDEX_NODE_MASK); |
| 608 | |
Gustavo Sverzut Barbieri | 15c1c14 | 2011-12-10 11:44:31 -0200 | [diff] [blame] | 609 | if (offset & INDEX_NODE_PREFIX) { |
| 610 | unsigned len; |
| 611 | prefix = read_chars_mm(&p, &len); |
| 612 | } else |
| 613 | prefix = _idx_empty_str; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 614 | |
| 615 | if (offset & INDEX_NODE_CHILDS) { |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 616 | first = read_char_mm(&p); |
| 617 | last = read_char_mm(&p); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 618 | child_count = last - first + 1; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 619 | for (i = 0; i < child_count; i++) |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 620 | children[i] = read_long_mm(&p); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 621 | } else { |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 622 | first = INDEX_CHILDMAX; |
| 623 | last = 0; |
| 624 | child_count = 0; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 625 | } |
| 626 | |
Gustavo Sverzut Barbieri | c6824b6 | 2011-12-21 18:23:58 -0200 | [diff] [blame] | 627 | children_padding = (sizeof(struct index_mm_node) + |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 628 | (sizeof(uint32_t) * child_count)) % sizeof(void *); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 629 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 630 | if (offset & INDEX_NODE_VALUES) |
| 631 | value_count = read_long_mm(&p); |
| 632 | else |
| 633 | value_count = 0; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 634 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 635 | node = malloc(sizeof(struct index_mm_node) |
| 636 | + sizeof(uint32_t) * child_count + children_padding |
| 637 | + sizeof(struct index_mm_value) * value_count); |
| 638 | if (node == NULL) |
| 639 | return NULL; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 640 | |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 641 | node->idx = idx; |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 642 | node->prefix = prefix; |
| 643 | if (value_count == 0) |
| 644 | node->values.values = NULL; |
| 645 | else { |
| 646 | node->values.values = (struct index_mm_value *) |
| 647 | ((char *)node + sizeof(struct index_mm_node) + |
| 648 | sizeof(uint32_t) * child_count + children_padding); |
| 649 | } |
| 650 | node->values.len = value_count; |
| 651 | node->first = first; |
| 652 | node->last = last; |
| 653 | memcpy(node->children, children, sizeof(uint32_t) * child_count); |
| 654 | |
| 655 | for (i = 0; i < value_count; i++) { |
| 656 | struct index_mm_value *v = node->values.values + i; |
| 657 | v->priority = read_long_mm(&p); |
| 658 | v->value = read_chars_mm(&p, &v->len); |
| 659 | } |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 660 | |
| 661 | return node; |
| 662 | } |
| 663 | |
| 664 | static void index_mm_free_node(struct index_mm_node *node) |
| 665 | { |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 666 | free(node); |
| 667 | } |
| 668 | |
Lucas De Marchi | 5109f2b | 2011-12-08 15:10:55 -0200 | [diff] [blame] | 669 | struct index_mm *index_mm_open(struct kmod_ctx *ctx, const char *filename, |
Lucas De Marchi | 2e2e252 | 2012-03-02 20:33:26 -0300 | [diff] [blame] | 670 | unsigned long long *stamp) |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 671 | { |
| 672 | int fd; |
| 673 | struct stat st; |
| 674 | struct index_mm *idx; |
| 675 | struct { |
| 676 | uint32_t magic; |
| 677 | uint32_t version; |
| 678 | uint32_t root_offset; |
| 679 | } hdr; |
| 680 | void *p; |
| 681 | |
| 682 | DBG(ctx, "file=%s\n", filename); |
| 683 | |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 684 | idx = malloc(sizeof(*idx)); |
| 685 | if (idx == NULL) { |
Gustavo Sverzut Barbieri | dfa96f1 | 2012-01-07 19:37:37 -0200 | [diff] [blame] | 686 | ERR(ctx, "malloc: %m\n"); |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 687 | return NULL; |
| 688 | } |
| 689 | |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 690 | if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) < 0) { |
Lucas De Marchi | 7329817 | 2012-02-13 21:58:36 -0200 | [diff] [blame] | 691 | DBG(ctx, "open(%s, O_RDONLY|O_CLOEXEC): %m\n", filename); |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 692 | goto fail_open; |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 693 | } |
| 694 | |
Leandro Pereira | d36c886 | 2014-04-28 20:44:14 -0300 | [diff] [blame] | 695 | if (fstat(fd, &st) < 0) |
| 696 | goto fail_nommap; |
Lucas De Marchi | 5b05c32 | 2012-06-06 09:36:29 -0300 | [diff] [blame] | 697 | if ((size_t) st.st_size < sizeof(hdr)) |
| 698 | goto fail_nommap; |
Lucas De Marchi | 5109f2b | 2011-12-08 15:10:55 -0200 | [diff] [blame] | 699 | |
Kees Cook | c3e8d26 | 2013-02-18 12:02:34 -0800 | [diff] [blame] | 700 | if ((idx->mm = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) |
Lucas De Marchi | 5109f2b | 2011-12-08 15:10:55 -0200 | [diff] [blame] | 701 | == MAP_FAILED) { |
Kees Cook | c3e8d26 | 2013-02-18 12:02:34 -0800 | [diff] [blame] | 702 | ERR(ctx, "mmap(NULL, %"PRIu64", PROT_READ, %d, MAP_PRIVATE, 0): %m\n", |
Lucas De Marchi | 2e2e252 | 2012-03-02 20:33:26 -0300 | [diff] [blame] | 703 | st.st_size, fd); |
Lucas De Marchi | 5b05c32 | 2012-06-06 09:36:29 -0300 | [diff] [blame] | 704 | goto fail_nommap; |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | p = idx->mm; |
| 708 | hdr.magic = read_long_mm(&p); |
| 709 | hdr.version = read_long_mm(&p); |
| 710 | hdr.root_offset = read_long_mm(&p); |
| 711 | |
| 712 | if (hdr.magic != INDEX_MAGIC) { |
| 713 | ERR(ctx, "magic check fail: %x instead of %x\n", hdr.magic, |
| 714 | INDEX_MAGIC); |
| 715 | goto fail; |
| 716 | } |
| 717 | |
| 718 | if (hdr.version >> 16 != INDEX_VERSION_MAJOR) { |
| 719 | ERR(ctx, "major version check fail: %u instead of %u\n", |
Holger Obermaier | 1a4aa7e | 2014-09-04 14:38:16 +0200 | [diff] [blame] | 720 | hdr.version >> 16, INDEX_VERSION_MAJOR); |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 721 | goto fail; |
| 722 | } |
| 723 | |
| 724 | idx->root_offset = hdr.root_offset; |
| 725 | idx->size = st.st_size; |
| 726 | idx->ctx = ctx; |
| 727 | close(fd); |
| 728 | |
Lucas De Marchi | 6068aaa | 2012-01-17 12:10:42 -0200 | [diff] [blame] | 729 | *stamp = stat_mstamp(&st); |
Lucas De Marchi | 9fd58f3 | 2011-12-31 18:53:24 -0200 | [diff] [blame] | 730 | |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 731 | return idx; |
| 732 | |
| 733 | fail: |
Lucas De Marchi | 5b05c32 | 2012-06-06 09:36:29 -0300 | [diff] [blame] | 734 | munmap(idx->mm, st.st_size); |
| 735 | fail_nommap: |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 736 | close(fd); |
Gustavo Sverzut Barbieri | a5a92a6 | 2011-12-16 16:17:50 -0200 | [diff] [blame] | 737 | fail_open: |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 738 | free(idx); |
| 739 | return NULL; |
| 740 | } |
| 741 | |
| 742 | void index_mm_close(struct index_mm *idx) |
| 743 | { |
| 744 | munmap(idx->mm, idx->size); |
| 745 | free(idx); |
| 746 | } |
Lucas De Marchi | 77bf936 | 2011-12-02 17:41:30 -0200 | [diff] [blame] | 747 | |
| 748 | static struct index_mm_node *index_mm_readroot(struct index_mm *idx) |
| 749 | { |
| 750 | return index_mm_read_node(idx, idx->root_offset); |
| 751 | } |
Lucas De Marchi | 91298dc | 2011-12-02 17:41:46 -0200 | [diff] [blame] | 752 | |
| 753 | static struct index_mm_node *index_mm_readchild(const struct index_mm_node *parent, |
| 754 | int ch) |
| 755 | { |
| 756 | if (parent->first <= ch && ch <= parent->last) { |
| 757 | return index_mm_read_node(parent->idx, |
| 758 | parent->children[ch - parent->first]); |
| 759 | } |
| 760 | |
| 761 | return NULL; |
| 762 | } |
Lucas De Marchi | e33bb87 | 2011-12-02 17:45:01 -0200 | [diff] [blame] | 763 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 764 | static void index_mm_dump_node(struct index_mm_node *node, struct strbuf *buf, |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 765 | int fd) |
| 766 | { |
| 767 | struct index_mm_value *itr, *itr_end; |
| 768 | int ch, pushed; |
| 769 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 770 | pushed = strbuf_pushchars(buf, node->prefix); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 771 | |
| 772 | itr = node->values.values; |
| 773 | itr_end = itr + node->values.len; |
| 774 | for (; itr < itr_end; itr++) { |
| 775 | write_str_safe(fd, buf->bytes, buf->used); |
| 776 | write_str_safe(fd, " ", 1); |
| 777 | write_str_safe(fd, itr->value, itr->len); |
| 778 | write_str_safe(fd, "\n", 1); |
| 779 | } |
| 780 | |
| 781 | for (ch = node->first; ch <= node->last; ch++) { |
| 782 | struct index_mm_node *child = index_mm_readchild(node, ch); |
| 783 | |
| 784 | if (child == NULL) |
| 785 | continue; |
| 786 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 787 | strbuf_pushchar(buf, ch); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 788 | index_mm_dump_node(child, buf, fd); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 789 | strbuf_popchar(buf); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 790 | } |
| 791 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 792 | strbuf_popchars(buf, pushed); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 793 | index_mm_free_node(node); |
| 794 | } |
| 795 | |
| 796 | void index_mm_dump(struct index_mm *idx, int fd, const char *prefix) |
| 797 | { |
| 798 | struct index_mm_node *root; |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 799 | struct strbuf buf; |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 800 | |
Lucas De Marchi | 681bf89 | 2013-04-23 21:21:00 -0300 | [diff] [blame] | 801 | root = index_mm_readroot(idx); |
| 802 | if (root == NULL) |
| 803 | return; |
| 804 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 805 | strbuf_init(&buf); |
| 806 | strbuf_pushchars(&buf, prefix); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 807 | index_mm_dump_node(root, &buf, fd); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 808 | strbuf_release(&buf); |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 809 | } |
| 810 | |
Lucas De Marchi | e33bb87 | 2011-12-02 17:45:01 -0200 | [diff] [blame] | 811 | static char *index_mm_search_node(struct index_mm_node *node, const char *key, |
| 812 | int i) |
| 813 | { |
| 814 | char *value; |
| 815 | struct index_mm_node *child; |
| 816 | int ch; |
| 817 | int j; |
| 818 | |
| 819 | while(node) { |
| 820 | for (j = 0; node->prefix[j]; j++) { |
| 821 | ch = node->prefix[j]; |
| 822 | |
| 823 | if (ch != key[i+j]) { |
| 824 | index_mm_free_node(node); |
| 825 | return NULL; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | i += j; |
| 830 | |
| 831 | if (key[i] == '\0') { |
Lucas De Marchi | 817f4e3 | 2012-02-27 19:54:33 -0300 | [diff] [blame] | 832 | value = node->values.len > 0 |
| 833 | ? strdup(node->values.values[0].value) |
| 834 | : NULL; |
| 835 | |
| 836 | index_mm_free_node(node); |
| 837 | return value; |
Lucas De Marchi | e33bb87 | 2011-12-02 17:45:01 -0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | child = index_mm_readchild(node, key[i]); |
| 841 | index_mm_free_node(node); |
| 842 | node = child; |
| 843 | i++; |
| 844 | } |
| 845 | |
| 846 | return NULL; |
| 847 | } |
Lucas De Marchi | b797b79 | 2011-12-02 17:49:03 -0200 | [diff] [blame] | 848 | |
| 849 | /* |
| 850 | * Search the index for a key |
| 851 | * |
| 852 | * Returns the value of the first match |
| 853 | * |
| 854 | * The recursive functions free their node argument (using index_close). |
| 855 | */ |
| 856 | char *index_mm_search(struct index_mm *idx, const char *key) |
| 857 | { |
Lucas De Marchi | ee1d188 | 2012-02-27 18:48:02 -0300 | [diff] [blame] | 858 | // FIXME: return value by reference instead of strdup |
Lucas De Marchi | b797b79 | 2011-12-02 17:49:03 -0200 | [diff] [blame] | 859 | struct index_mm_node *root; |
| 860 | char *value; |
| 861 | |
| 862 | root = index_mm_readroot(idx); |
| 863 | value = index_mm_search_node(root, key, 0); |
| 864 | |
| 865 | return value; |
| 866 | } |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 867 | |
| 868 | /* Level 4: add all the values from a matching node */ |
| 869 | static void index_mm_searchwild_allvalues(struct index_mm_node *node, |
| 870 | struct index_value **out) |
| 871 | { |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 872 | struct index_mm_value *itr, *itr_end; |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 873 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 874 | itr = node->values.values; |
| 875 | itr_end = itr + node->values.len; |
| 876 | for (; itr < itr_end; itr++) |
| 877 | add_value(out, itr->value, itr->len, itr->priority); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 878 | |
| 879 | index_mm_free_node(node); |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Level 3: traverse a sub-keyspace which starts with a wildcard, |
| 884 | * looking for matches. |
| 885 | */ |
| 886 | static void index_mm_searchwild_all(struct index_mm_node *node, int j, |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 887 | struct strbuf *buf, |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 888 | const char *subkey, |
| 889 | struct index_value **out) |
| 890 | { |
| 891 | int pushed = 0; |
| 892 | int ch; |
| 893 | |
| 894 | while (node->prefix[j]) { |
| 895 | ch = node->prefix[j]; |
| 896 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 897 | strbuf_pushchar(buf, ch); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 898 | pushed++; |
| 899 | j++; |
| 900 | } |
| 901 | |
| 902 | for (ch = node->first; ch <= node->last; ch++) { |
| 903 | struct index_mm_node *child = index_mm_readchild(node, ch); |
| 904 | |
| 905 | if (!child) |
| 906 | continue; |
| 907 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 908 | strbuf_pushchar(buf, ch); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 909 | index_mm_searchwild_all(child, 0, buf, subkey, out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 910 | strbuf_popchar(buf); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 911 | } |
| 912 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 913 | if (node->values.len > 0) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 914 | if (fnmatch(strbuf_str(buf), subkey, 0) == 0) |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 915 | index_mm_searchwild_allvalues(node, out); |
Gustavo Sverzut Barbieri | 27fdf63 | 2011-12-10 13:28:18 -0200 | [diff] [blame] | 916 | else |
| 917 | index_mm_free_node(node); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 918 | } else { |
| 919 | index_mm_free_node(node); |
| 920 | } |
| 921 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 922 | strbuf_popchars(buf, pushed); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* Level 2: descend the tree (until we hit a wildcard) */ |
| 926 | static void index_mm_searchwild_node(struct index_mm_node *node, |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 927 | struct strbuf *buf, |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 928 | const char *key, int i, |
| 929 | struct index_value **out) |
| 930 | { |
| 931 | struct index_mm_node *child; |
| 932 | int j; |
| 933 | int ch; |
| 934 | |
| 935 | while(node) { |
| 936 | for (j = 0; node->prefix[j]; j++) { |
| 937 | ch = node->prefix[j]; |
| 938 | |
| 939 | if (ch == '*' || ch == '?' || ch == '[') { |
| 940 | index_mm_searchwild_all(node, j, buf, |
| 941 | &key[i+j], out); |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | if (ch != key[i+j]) { |
| 946 | index_mm_free_node(node); |
| 947 | return; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | i += j; |
| 952 | |
| 953 | child = index_mm_readchild(node, '*'); |
| 954 | if (child) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 955 | strbuf_pushchar(buf, '*'); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 956 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 957 | strbuf_popchar(buf); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | child = index_mm_readchild(node, '?'); |
| 961 | if (child) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 962 | strbuf_pushchar(buf, '?'); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 963 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 964 | strbuf_popchar(buf); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | child = index_mm_readchild(node, '['); |
| 968 | if (child) { |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 969 | strbuf_pushchar(buf, '['); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 970 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 971 | strbuf_popchar(buf); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | if (key[i] == '\0') { |
| 975 | index_mm_searchwild_allvalues(node, out); |
| 976 | |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | child = index_mm_readchild(node, key[i]); |
| 981 | index_mm_free_node(node); |
| 982 | node = child; |
| 983 | i++; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | /* |
| 988 | * Search the index for a key. The index may contain wildcards. |
| 989 | * |
| 990 | * Returns a list of all the values of matching keys. |
| 991 | */ |
| 992 | struct index_value *index_mm_searchwild(struct index_mm *idx, const char *key) |
| 993 | { |
| 994 | struct index_mm_node *root = index_mm_readroot(idx); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 995 | struct strbuf buf; |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 996 | struct index_value *out = NULL; |
| 997 | |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 998 | strbuf_init(&buf); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 999 | index_mm_searchwild_node(root, &buf, key, 0, &out); |
Lucas De Marchi | 15a7ae3 | 2014-10-11 13:25:51 -0300 | [diff] [blame^] | 1000 | strbuf_release(&buf); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 1001 | return out; |
| 1002 | } |