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