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 | 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 | |
| 30 | #include "libkmod-private.h" |
| 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; |
| 129 | if (fread(&l, sizeof(uint32_t), 1, in) <= 0) |
| 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 | |
| 435 | buf_init(&buf); |
| 436 | buf_pushchars(&buf, prefix); |
| 437 | root = index_readroot(in); |
| 438 | index_dump_node(root, &buf, fd); |
| 439 | buf_release(&buf); |
| 440 | } |
| 441 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 442 | static char *index_search__node(struct index_node_f *node, const char *key, int i) |
| 443 | { |
| 444 | char *value; |
| 445 | struct index_node_f *child; |
| 446 | int ch; |
| 447 | int j; |
| 448 | |
| 449 | while(node) { |
| 450 | for (j = 0; node->prefix[j]; j++) { |
| 451 | ch = node->prefix[j]; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 452 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 453 | if (ch != key[i+j]) { |
| 454 | index_close(node); |
| 455 | return NULL; |
| 456 | } |
| 457 | } |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 458 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 459 | i += j; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 460 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 461 | if (key[i] == '\0') { |
Lucas De Marchi | 817f4e3 | 2012-02-27 19:54:33 -0300 | [diff] [blame] | 462 | value = node->values != NULL |
| 463 | ? strdup(node->values[0].value) |
| 464 | : NULL; |
| 465 | |
| 466 | index_close(node); |
| 467 | return value; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 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 */ |
Lucas De Marchi | 5bbec8c | 2012-05-23 19:32:58 -0300 | [diff] [blame^] | 678 | v = get_unaligned((uint32_t *) addr); |
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 | 2e2e252 | 2012-03-02 20:33:26 -0300 | [diff] [blame] | 775 | unsigned long long *stamp) |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 776 | { |
| 777 | int fd; |
| 778 | struct stat st; |
| 779 | struct index_mm *idx; |
| 780 | struct { |
| 781 | uint32_t magic; |
| 782 | uint32_t version; |
| 783 | uint32_t root_offset; |
| 784 | } hdr; |
| 785 | void *p; |
| 786 | |
| 787 | DBG(ctx, "file=%s\n", filename); |
| 788 | |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 789 | idx = malloc(sizeof(*idx)); |
| 790 | if (idx == NULL) { |
Gustavo Sverzut Barbieri | dfa96f1 | 2012-01-07 19:37:37 -0200 | [diff] [blame] | 791 | ERR(ctx, "malloc: %m\n"); |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 792 | return NULL; |
| 793 | } |
| 794 | |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 795 | if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) < 0) { |
Lucas De Marchi | 7329817 | 2012-02-13 21:58:36 -0200 | [diff] [blame] | 796 | DBG(ctx, "open(%s, O_RDONLY|O_CLOEXEC): %m\n", filename); |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 797 | goto fail_open; |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 798 | } |
| 799 | |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 800 | fstat(fd, &st); |
Lucas De Marchi | 5109f2b | 2011-12-08 15:10:55 -0200 | [diff] [blame] | 801 | |
Lucas De Marchi | 2e2e252 | 2012-03-02 20:33:26 -0300 | [diff] [blame] | 802 | if ((idx->mm = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) |
Lucas De Marchi | 5109f2b | 2011-12-08 15:10:55 -0200 | [diff] [blame] | 803 | == MAP_FAILED) { |
Lucas De Marchi | bfcd31d | 2012-03-02 21:28:11 -0300 | [diff] [blame] | 804 | ERR(ctx, "mmap(0, %"PRIu64", PROT_READ, %d, MAP_PRIVATE, 0): %m\n", |
Lucas De Marchi | 2e2e252 | 2012-03-02 20:33:26 -0300 | [diff] [blame] | 805 | st.st_size, fd); |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 806 | goto fail; |
| 807 | } |
| 808 | |
| 809 | p = idx->mm; |
| 810 | hdr.magic = read_long_mm(&p); |
| 811 | hdr.version = read_long_mm(&p); |
| 812 | hdr.root_offset = read_long_mm(&p); |
| 813 | |
| 814 | if (hdr.magic != INDEX_MAGIC) { |
| 815 | ERR(ctx, "magic check fail: %x instead of %x\n", hdr.magic, |
| 816 | INDEX_MAGIC); |
| 817 | goto fail; |
| 818 | } |
| 819 | |
| 820 | if (hdr.version >> 16 != INDEX_VERSION_MAJOR) { |
| 821 | ERR(ctx, "major version check fail: %u instead of %u\n", |
| 822 | hdr.version, INDEX_MAGIC); |
| 823 | goto fail; |
| 824 | } |
| 825 | |
| 826 | idx->root_offset = hdr.root_offset; |
| 827 | idx->size = st.st_size; |
| 828 | idx->ctx = ctx; |
| 829 | close(fd); |
| 830 | |
Lucas De Marchi | 6068aaa | 2012-01-17 12:10:42 -0200 | [diff] [blame] | 831 | *stamp = stat_mstamp(&st); |
Lucas De Marchi | 9fd58f3 | 2011-12-31 18:53:24 -0200 | [diff] [blame] | 832 | |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 833 | return idx; |
| 834 | |
| 835 | fail: |
| 836 | close(fd); |
Gustavo Sverzut Barbieri | a5a92a6 | 2011-12-16 16:17:50 -0200 | [diff] [blame] | 837 | if (idx->mm != MAP_FAILED) |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 838 | munmap(idx->mm, st.st_size); |
Gustavo Sverzut Barbieri | a5a92a6 | 2011-12-16 16:17:50 -0200 | [diff] [blame] | 839 | fail_open: |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 840 | free(idx); |
| 841 | return NULL; |
| 842 | } |
| 843 | |
| 844 | void index_mm_close(struct index_mm *idx) |
| 845 | { |
| 846 | munmap(idx->mm, idx->size); |
| 847 | free(idx); |
| 848 | } |
Lucas De Marchi | 77bf936 | 2011-12-02 17:41:30 -0200 | [diff] [blame] | 849 | |
| 850 | static struct index_mm_node *index_mm_readroot(struct index_mm *idx) |
| 851 | { |
| 852 | return index_mm_read_node(idx, idx->root_offset); |
| 853 | } |
Lucas De Marchi | 91298dc | 2011-12-02 17:41:46 -0200 | [diff] [blame] | 854 | |
| 855 | static struct index_mm_node *index_mm_readchild(const struct index_mm_node *parent, |
| 856 | int ch) |
| 857 | { |
| 858 | if (parent->first <= ch && ch <= parent->last) { |
| 859 | return index_mm_read_node(parent->idx, |
| 860 | parent->children[ch - parent->first]); |
| 861 | } |
| 862 | |
| 863 | return NULL; |
| 864 | } |
Lucas De Marchi | e33bb87 | 2011-12-02 17:45:01 -0200 | [diff] [blame] | 865 | |
Lucas De Marchi | 758428a | 2012-01-16 15:56:17 -0200 | [diff] [blame] | 866 | static void index_mm_dump_node(struct index_mm_node *node, struct buffer *buf, |
| 867 | int fd) |
| 868 | { |
| 869 | struct index_mm_value *itr, *itr_end; |
| 870 | int ch, pushed; |
| 871 | |
| 872 | pushed = buf_pushchars(buf, node->prefix); |
| 873 | |
| 874 | itr = node->values.values; |
| 875 | itr_end = itr + node->values.len; |
| 876 | for (; itr < itr_end; itr++) { |
| 877 | write_str_safe(fd, buf->bytes, buf->used); |
| 878 | write_str_safe(fd, " ", 1); |
| 879 | write_str_safe(fd, itr->value, itr->len); |
| 880 | write_str_safe(fd, "\n", 1); |
| 881 | } |
| 882 | |
| 883 | for (ch = node->first; ch <= node->last; ch++) { |
| 884 | struct index_mm_node *child = index_mm_readchild(node, ch); |
| 885 | |
| 886 | if (child == NULL) |
| 887 | continue; |
| 888 | |
| 889 | buf_pushchar(buf, ch); |
| 890 | index_mm_dump_node(child, buf, fd); |
| 891 | buf_popchar(buf); |
| 892 | } |
| 893 | |
| 894 | buf_popchars(buf, pushed); |
| 895 | index_mm_free_node(node); |
| 896 | } |
| 897 | |
| 898 | void index_mm_dump(struct index_mm *idx, int fd, const char *prefix) |
| 899 | { |
| 900 | struct index_mm_node *root; |
| 901 | struct buffer buf; |
| 902 | |
| 903 | buf_init(&buf); |
| 904 | buf_pushchars(&buf, prefix); |
| 905 | root = index_mm_readroot(idx); |
| 906 | index_mm_dump_node(root, &buf, fd); |
| 907 | buf_release(&buf); |
| 908 | } |
| 909 | |
Lucas De Marchi | e33bb87 | 2011-12-02 17:45:01 -0200 | [diff] [blame] | 910 | static char *index_mm_search_node(struct index_mm_node *node, const char *key, |
| 911 | int i) |
| 912 | { |
| 913 | char *value; |
| 914 | struct index_mm_node *child; |
| 915 | int ch; |
| 916 | int j; |
| 917 | |
| 918 | while(node) { |
| 919 | for (j = 0; node->prefix[j]; j++) { |
| 920 | ch = node->prefix[j]; |
| 921 | |
| 922 | if (ch != key[i+j]) { |
| 923 | index_mm_free_node(node); |
| 924 | return NULL; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | i += j; |
| 929 | |
| 930 | if (key[i] == '\0') { |
Lucas De Marchi | 817f4e3 | 2012-02-27 19:54:33 -0300 | [diff] [blame] | 931 | value = node->values.len > 0 |
| 932 | ? strdup(node->values.values[0].value) |
| 933 | : NULL; |
| 934 | |
| 935 | index_mm_free_node(node); |
| 936 | return value; |
Lucas De Marchi | e33bb87 | 2011-12-02 17:45:01 -0200 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | child = index_mm_readchild(node, key[i]); |
| 940 | index_mm_free_node(node); |
| 941 | node = child; |
| 942 | i++; |
| 943 | } |
| 944 | |
| 945 | return NULL; |
| 946 | } |
Lucas De Marchi | b797b79 | 2011-12-02 17:49:03 -0200 | [diff] [blame] | 947 | |
| 948 | /* |
| 949 | * Search the index for a key |
| 950 | * |
| 951 | * Returns the value of the first match |
| 952 | * |
| 953 | * The recursive functions free their node argument (using index_close). |
| 954 | */ |
| 955 | char *index_mm_search(struct index_mm *idx, const char *key) |
| 956 | { |
Lucas De Marchi | ee1d188 | 2012-02-27 18:48:02 -0300 | [diff] [blame] | 957 | // FIXME: return value by reference instead of strdup |
Lucas De Marchi | b797b79 | 2011-12-02 17:49:03 -0200 | [diff] [blame] | 958 | struct index_mm_node *root; |
| 959 | char *value; |
| 960 | |
| 961 | root = index_mm_readroot(idx); |
| 962 | value = index_mm_search_node(root, key, 0); |
| 963 | |
| 964 | return value; |
| 965 | } |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 966 | |
| 967 | /* Level 4: add all the values from a matching node */ |
| 968 | static void index_mm_searchwild_allvalues(struct index_mm_node *node, |
| 969 | struct index_value **out) |
| 970 | { |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 971 | struct index_mm_value *itr, *itr_end; |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 972 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 973 | itr = node->values.values; |
| 974 | itr_end = itr + node->values.len; |
| 975 | for (; itr < itr_end; itr++) |
| 976 | add_value(out, itr->value, itr->len, itr->priority); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 977 | |
| 978 | index_mm_free_node(node); |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Level 3: traverse a sub-keyspace which starts with a wildcard, |
| 983 | * looking for matches. |
| 984 | */ |
| 985 | static void index_mm_searchwild_all(struct index_mm_node *node, int j, |
| 986 | struct buffer *buf, |
| 987 | const char *subkey, |
| 988 | struct index_value **out) |
| 989 | { |
| 990 | int pushed = 0; |
| 991 | int ch; |
| 992 | |
| 993 | while (node->prefix[j]) { |
| 994 | ch = node->prefix[j]; |
| 995 | |
| 996 | buf_pushchar(buf, ch); |
| 997 | pushed++; |
| 998 | j++; |
| 999 | } |
| 1000 | |
| 1001 | for (ch = node->first; ch <= node->last; ch++) { |
| 1002 | struct index_mm_node *child = index_mm_readchild(node, ch); |
| 1003 | |
| 1004 | if (!child) |
| 1005 | continue; |
| 1006 | |
| 1007 | buf_pushchar(buf, ch); |
| 1008 | index_mm_searchwild_all(child, 0, buf, subkey, out); |
| 1009 | buf_popchar(buf); |
| 1010 | } |
| 1011 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 1012 | if (node->values.len > 0) { |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 1013 | if (fnmatch(buf_str(buf), subkey, 0) == 0) |
| 1014 | index_mm_searchwild_allvalues(node, out); |
Gustavo Sverzut Barbieri | 27fdf63 | 2011-12-10 13:28:18 -0200 | [diff] [blame] | 1015 | else |
| 1016 | index_mm_free_node(node); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 1017 | } else { |
| 1018 | index_mm_free_node(node); |
| 1019 | } |
| 1020 | |
| 1021 | buf_popchars(buf, pushed); |
| 1022 | } |
| 1023 | |
| 1024 | /* Level 2: descend the tree (until we hit a wildcard) */ |
| 1025 | static void index_mm_searchwild_node(struct index_mm_node *node, |
| 1026 | struct buffer *buf, |
| 1027 | const char *key, int i, |
| 1028 | struct index_value **out) |
| 1029 | { |
| 1030 | struct index_mm_node *child; |
| 1031 | int j; |
| 1032 | int ch; |
| 1033 | |
| 1034 | while(node) { |
| 1035 | for (j = 0; node->prefix[j]; j++) { |
| 1036 | ch = node->prefix[j]; |
| 1037 | |
| 1038 | if (ch == '*' || ch == '?' || ch == '[') { |
| 1039 | index_mm_searchwild_all(node, j, buf, |
| 1040 | &key[i+j], out); |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | if (ch != key[i+j]) { |
| 1045 | index_mm_free_node(node); |
| 1046 | return; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | i += j; |
| 1051 | |
| 1052 | child = index_mm_readchild(node, '*'); |
| 1053 | if (child) { |
| 1054 | buf_pushchar(buf, '*'); |
| 1055 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
| 1056 | buf_popchar(buf); |
| 1057 | } |
| 1058 | |
| 1059 | child = index_mm_readchild(node, '?'); |
| 1060 | if (child) { |
| 1061 | buf_pushchar(buf, '?'); |
| 1062 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
| 1063 | buf_popchar(buf); |
| 1064 | } |
| 1065 | |
| 1066 | child = index_mm_readchild(node, '['); |
| 1067 | if (child) { |
| 1068 | buf_pushchar(buf, '['); |
| 1069 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
| 1070 | buf_popchar(buf); |
| 1071 | } |
| 1072 | |
| 1073 | if (key[i] == '\0') { |
| 1074 | index_mm_searchwild_allvalues(node, out); |
| 1075 | |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | child = index_mm_readchild(node, key[i]); |
| 1080 | index_mm_free_node(node); |
| 1081 | node = child; |
| 1082 | i++; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * Search the index for a key. The index may contain wildcards. |
| 1088 | * |
| 1089 | * Returns a list of all the values of matching keys. |
| 1090 | */ |
| 1091 | struct index_value *index_mm_searchwild(struct index_mm *idx, const char *key) |
| 1092 | { |
| 1093 | struct index_mm_node *root = index_mm_readroot(idx); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 1094 | struct buffer buf; |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 1095 | struct index_value *out = NULL; |
| 1096 | |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 1097 | buf_init(&buf); |
| 1098 | index_mm_searchwild_node(root, &buf, key, 0, &out); |
| 1099 | buf_release(&buf); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 1100 | return out; |
| 1101 | } |