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