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 | 8f923be | 2011-12-03 04:28:49 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011 ProFUSION embedded systems |
| 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 | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 214 | static unsigned buf_freadchars(struct buffer *buf, FILE *in) |
| 215 | { |
| 216 | unsigned i = 0; |
| 217 | int ch; |
| 218 | |
| 219 | while ((ch = read_char(in))) { |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 220 | if (!buf_pushchar(buf, ch)) |
| 221 | break; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 222 | i++; |
| 223 | } |
| 224 | |
| 225 | return i; |
| 226 | } |
| 227 | |
| 228 | static void buf_popchar(struct buffer *buf) |
| 229 | { |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 230 | assert(buf->used > 0); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 231 | buf->used--; |
| 232 | } |
| 233 | |
| 234 | static void buf_popchars(struct buffer *buf, unsigned n) |
| 235 | { |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 236 | assert(buf->used >= n); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 237 | buf->used -= n; |
| 238 | } |
| 239 | |
| 240 | static void buf_clear(struct buffer *buf) |
| 241 | { |
| 242 | buf->used = 0; |
| 243 | } |
| 244 | |
| 245 | /* |
Lucas De Marchi | eb8bb32 | 2011-12-02 10:23:02 -0200 | [diff] [blame] | 246 | * Index file searching |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 247 | */ |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 248 | struct index_node_f { |
| 249 | FILE *file; |
| 250 | char *prefix; /* path compression */ |
| 251 | struct index_value *values; |
| 252 | unsigned char first; /* range of child nodes */ |
| 253 | unsigned char last; |
| 254 | uint32_t children[0]; |
| 255 | }; |
| 256 | |
| 257 | static struct index_node_f *index_read(FILE *in, uint32_t offset) |
| 258 | { |
| 259 | struct index_node_f *node; |
| 260 | char *prefix; |
| 261 | int i, child_count = 0; |
| 262 | |
| 263 | if ((offset & INDEX_NODE_MASK) == 0) |
| 264 | return NULL; |
| 265 | |
| 266 | fseek(in, offset & INDEX_NODE_MASK, SEEK_SET); |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 267 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 268 | if (offset & INDEX_NODE_PREFIX) { |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 269 | struct buffer buf; |
| 270 | buf_init(&buf); |
| 271 | buf_freadchars(&buf, in); |
| 272 | prefix = buf_steal(&buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 273 | } else |
| 274 | prefix = NOFAIL(strdup("")); |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 275 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 276 | if (offset & INDEX_NODE_CHILDS) { |
| 277 | char first = read_char(in); |
| 278 | char last = read_char(in); |
| 279 | child_count = last - first + 1; |
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 | node = NOFAIL(malloc(sizeof(struct index_node_f) + |
| 282 | sizeof(uint32_t) * child_count)); |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 283 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 284 | node->first = first; |
| 285 | node->last = last; |
| 286 | |
| 287 | for (i = 0; i < child_count; i++) |
| 288 | node->children[i] = read_long(in); |
| 289 | } else { |
| 290 | node = NOFAIL(malloc(sizeof(struct index_node_f))); |
| 291 | node->first = INDEX_CHILDMAX; |
| 292 | node->last = 0; |
| 293 | } |
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->values = NULL; |
| 296 | if (offset & INDEX_NODE_VALUES) { |
| 297 | int value_count; |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 298 | struct buffer buf; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 299 | const char *value; |
| 300 | unsigned int priority; |
| 301 | |
| 302 | value_count = read_long(in); |
| 303 | |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 304 | buf_init(&buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 305 | while (value_count--) { |
| 306 | priority = read_long(in); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 307 | buf_freadchars(&buf, in); |
| 308 | value = buf_str(&buf); |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 309 | add_value(&node->values, value, buf.used, priority); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 310 | buf_clear(&buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 311 | } |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 312 | buf_release(&buf); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | node->prefix = prefix; |
| 316 | node->file = in; |
| 317 | return node; |
| 318 | } |
| 319 | |
| 320 | static void index_close(struct index_node_f *node) |
| 321 | { |
| 322 | free(node->prefix); |
| 323 | index_values_free(node->values); |
| 324 | free(node); |
| 325 | } |
| 326 | |
| 327 | struct index_file { |
| 328 | FILE *file; |
| 329 | uint32_t root_offset; |
| 330 | }; |
| 331 | |
| 332 | /* Failures are silent; modprobe will fall back to text files */ |
| 333 | struct index_file *index_file_open(const char *filename) |
| 334 | { |
| 335 | FILE *file; |
| 336 | uint32_t magic, version; |
| 337 | struct index_file *new; |
| 338 | |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 339 | file = fopen(filename, "re"); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 340 | if (!file) |
| 341 | return NULL; |
| 342 | errno = EINVAL; |
| 343 | |
| 344 | magic = read_long(file); |
| 345 | if (magic != INDEX_MAGIC) |
| 346 | return NULL; |
| 347 | |
| 348 | version = read_long(file); |
| 349 | if (version >> 16 != INDEX_VERSION_MAJOR) |
| 350 | return NULL; |
| 351 | |
| 352 | new = NOFAIL(malloc(sizeof(struct index_file))); |
| 353 | new->file = file; |
| 354 | new->root_offset = read_long(new->file); |
| 355 | |
| 356 | errno = 0; |
| 357 | return new; |
| 358 | } |
| 359 | |
Lucas De Marchi | 0fbdfef | 2011-12-02 09:56:22 -0200 | [diff] [blame] | 360 | void index_file_close(struct index_file *idx) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 361 | { |
Lucas De Marchi | 0fbdfef | 2011-12-02 09:56:22 -0200 | [diff] [blame] | 362 | fclose(idx->file); |
| 363 | free(idx); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 364 | } |
| 365 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 366 | static struct index_node_f *index_readroot(struct index_file *in) |
| 367 | { |
| 368 | return index_read(in->file, in->root_offset); |
| 369 | } |
| 370 | |
| 371 | static struct index_node_f *index_readchild(const struct index_node_f *parent, |
| 372 | int ch) |
| 373 | { |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 374 | if (parent->first <= ch && ch <= parent->last) { |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 375 | return index_read(parent->file, |
| 376 | parent->children[ch - parent->first]); |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | return NULL; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 380 | } |
| 381 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 382 | static char *index_search__node(struct index_node_f *node, const char *key, int i) |
| 383 | { |
| 384 | char *value; |
| 385 | struct index_node_f *child; |
| 386 | int ch; |
| 387 | int j; |
| 388 | |
| 389 | while(node) { |
| 390 | for (j = 0; node->prefix[j]; j++) { |
| 391 | ch = node->prefix[j]; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 392 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 393 | if (ch != key[i+j]) { |
| 394 | index_close(node); |
| 395 | return NULL; |
| 396 | } |
| 397 | } |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 398 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 399 | i += j; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 400 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 401 | if (key[i] == '\0') { |
| 402 | if (node->values) { |
| 403 | value = strdup(node->values[0].value); |
| 404 | index_close(node); |
| 405 | return value; |
| 406 | } else { |
| 407 | return NULL; |
| 408 | } |
| 409 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 410 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 411 | child = index_readchild(node, key[i]); |
| 412 | index_close(node); |
| 413 | node = child; |
| 414 | i++; |
| 415 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 416 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | /* |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 421 | * Search the index for a key |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 422 | * |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 423 | * Returns the value of the first match |
| 424 | * |
| 425 | * The recursive functions free their node argument (using index_close). |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 426 | */ |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 427 | char *index_search(struct index_file *in, const char *key) |
| 428 | { |
| 429 | struct index_node_f *root; |
| 430 | char *value; |
| 431 | |
| 432 | root = index_readroot(in); |
| 433 | value = index_search__node(root, key, 0); |
| 434 | |
| 435 | return value; |
| 436 | } |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 437 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 438 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 439 | |
| 440 | /* Level 4: add all the values from a matching node */ |
| 441 | static void index_searchwild__allvalues(struct index_node_f *node, |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 442 | struct index_value **out) |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 443 | { |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 444 | struct index_value *v; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 445 | |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 446 | for (v = node->values; v != NULL; v = v->next) |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 447 | add_value(out, v->value, v->len, v->priority); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 448 | |
| 449 | index_close(node); |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 450 | } |
| 451 | |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 452 | /* |
| 453 | * Level 3: traverse a sub-keyspace which starts with a wildcard, |
| 454 | * looking for matches. |
| 455 | */ |
| 456 | static void index_searchwild__all(struct index_node_f *node, int j, |
| 457 | struct buffer *buf, |
| 458 | const char *subkey, |
| 459 | struct index_value **out) |
| 460 | { |
| 461 | int pushed = 0; |
| 462 | int ch; |
| 463 | |
| 464 | while (node->prefix[j]) { |
| 465 | ch = node->prefix[j]; |
| 466 | |
| 467 | buf_pushchar(buf, ch); |
| 468 | pushed++; |
| 469 | j++; |
| 470 | } |
| 471 | |
| 472 | for (ch = node->first; ch <= node->last; ch++) { |
| 473 | struct index_node_f *child = index_readchild(node, ch); |
| 474 | |
| 475 | if (!child) |
| 476 | continue; |
| 477 | |
| 478 | buf_pushchar(buf, ch); |
| 479 | index_searchwild__all(child, 0, buf, subkey, out); |
| 480 | buf_popchar(buf); |
| 481 | } |
| 482 | |
| 483 | if (node->values) { |
| 484 | if (fnmatch(buf_str(buf), subkey, 0) == 0) |
| 485 | index_searchwild__allvalues(node, out); |
Gustavo Sverzut Barbieri | 27fdf63 | 2011-12-10 13:28:18 -0200 | [diff] [blame] | 486 | else |
| 487 | index_close(node); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 488 | } else { |
| 489 | index_close(node); |
| 490 | } |
| 491 | |
| 492 | buf_popchars(buf, pushed); |
| 493 | } |
| 494 | |
| 495 | /* Level 2: descend the tree (until we hit a wildcard) */ |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 496 | static void index_searchwild__node(struct index_node_f *node, |
| 497 | struct buffer *buf, |
| 498 | const char *key, int i, |
| 499 | struct index_value **out) |
| 500 | { |
| 501 | struct index_node_f *child; |
| 502 | int j; |
| 503 | int ch; |
| 504 | |
| 505 | while(node) { |
| 506 | for (j = 0; node->prefix[j]; j++) { |
| 507 | ch = node->prefix[j]; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 508 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 509 | if (ch == '*' || ch == '?' || ch == '[') { |
| 510 | index_searchwild__all(node, j, buf, |
| 511 | &key[i+j], out); |
| 512 | return; |
| 513 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 514 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 515 | if (ch != key[i+j]) { |
| 516 | index_close(node); |
| 517 | return; |
| 518 | } |
| 519 | } |
Lucas De Marchi | e71970a | 2011-12-02 10:27:15 -0200 | [diff] [blame] | 520 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 521 | i += j; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 522 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 523 | child = index_readchild(node, '*'); |
| 524 | if (child) { |
| 525 | buf_pushchar(buf, '*'); |
| 526 | index_searchwild__all(child, 0, buf, &key[i], out); |
| 527 | buf_popchar(buf); |
| 528 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 529 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 530 | child = index_readchild(node, '?'); |
| 531 | if (child) { |
| 532 | buf_pushchar(buf, '?'); |
| 533 | index_searchwild__all(child, 0, buf, &key[i], out); |
| 534 | buf_popchar(buf); |
| 535 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 536 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 537 | child = index_readchild(node, '['); |
| 538 | if (child) { |
| 539 | buf_pushchar(buf, '['); |
| 540 | index_searchwild__all(child, 0, buf, &key[i], out); |
| 541 | buf_popchar(buf); |
| 542 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 543 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 544 | if (key[i] == '\0') { |
| 545 | index_searchwild__allvalues(node, out); |
| 546 | |
| 547 | return; |
| 548 | } |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 549 | |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 550 | child = index_readchild(node, key[i]); |
| 551 | index_close(node); |
| 552 | node = child; |
| 553 | i++; |
| 554 | } |
| 555 | } |
| 556 | |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 557 | /* |
| 558 | * Search the index for a key. The index may contain wildcards. |
| 559 | * |
| 560 | * Returns a list of all the values of matching keys. |
| 561 | */ |
| 562 | 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] | 563 | { |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 564 | struct index_node_f *root = index_readroot(in); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 565 | struct buffer buf; |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 566 | struct index_value *out = NULL; |
Lucas De Marchi | a7be73b | 2011-11-30 15:59:36 -0200 | [diff] [blame] | 567 | |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 568 | buf_init(&buf); |
| 569 | index_searchwild__node(root, &buf, key, 0, &out); |
| 570 | buf_release(&buf); |
Lucas De Marchi | 1d152ac | 2011-12-02 10:15:00 -0200 | [diff] [blame] | 571 | return out; |
Lucas De Marchi | e8847fd | 2011-11-30 15:23:28 -0200 | [diff] [blame] | 572 | } |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 573 | |
| 574 | #include <sys/mman.h> |
| 575 | #include <sys/stat.h> |
| 576 | #include <unistd.h> |
| 577 | |
Gustavo Sverzut Barbieri | 15c1c14 | 2011-12-10 11:44:31 -0200 | [diff] [blame] | 578 | static const char _idx_empty_str[] = ""; |
| 579 | |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 580 | /**************************************************************************/ |
| 581 | /* |
| 582 | * Alternative implementation, using mmap to map all the file to memory when |
| 583 | * starting |
| 584 | */ |
| 585 | struct index_mm { |
| 586 | struct kmod_ctx *ctx; |
| 587 | void *mm; |
| 588 | uint32_t root_offset; |
| 589 | size_t size; |
| 590 | }; |
| 591 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 592 | struct index_mm_value { |
| 593 | unsigned int priority; |
| 594 | unsigned int len; |
| 595 | const char *value; |
| 596 | }; |
| 597 | |
| 598 | struct index_mm_value_array { |
| 599 | struct index_mm_value *values; |
| 600 | unsigned int len; |
| 601 | }; |
| 602 | |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 603 | struct index_mm_node { |
| 604 | struct index_mm *idx; |
Gustavo Sverzut Barbieri | 15c1c14 | 2011-12-10 11:44:31 -0200 | [diff] [blame] | 605 | const char *prefix; /* mmape'd value */ |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 606 | struct index_mm_value_array values; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 607 | unsigned char first; |
| 608 | unsigned char last; |
| 609 | uint32_t children[]; |
| 610 | }; |
| 611 | |
| 612 | static inline uint32_t read_long_mm(void **p) |
| 613 | { |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 614 | uint8_t *addr = *(uint8_t **)p; |
| 615 | uint32_t v; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 616 | |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 617 | /* addr may be unalined to uint32_t */ |
| 618 | memcpy(&v, addr, sizeof(uint32_t)); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 619 | |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 620 | *p = addr + sizeof(uint32_t); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 621 | return ntohl(v); |
| 622 | } |
| 623 | |
| 624 | static inline uint8_t read_char_mm(void **p) |
| 625 | { |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 626 | uint8_t *addr = *(uint8_t **)p; |
| 627 | uint8_t v = *addr; |
| 628 | *p = addr + sizeof(uint8_t); |
| 629 | return v; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 630 | } |
| 631 | |
Gustavo Sverzut Barbieri | 1433ba9 | 2011-12-08 14:50:29 -0200 | [diff] [blame] | 632 | static inline char *read_chars_mm(void **p, unsigned *rlen) |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 633 | { |
Gustavo Sverzut Barbieri | fc2d835 | 2011-12-10 11:36:35 -0200 | [diff] [blame] | 634 | char *addr = *(char **)p; |
| 635 | size_t len = *rlen = strlen(addr); |
| 636 | *p = addr + len + 1; |
| 637 | return addr; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static struct index_mm_node *index_mm_read_node(struct index_mm *idx, |
| 641 | uint32_t offset) { |
| 642 | void *p = idx->mm; |
| 643 | struct index_mm_node *node; |
Gustavo Sverzut Barbieri | 15c1c14 | 2011-12-10 11:44:31 -0200 | [diff] [blame] | 644 | const char *prefix; |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 645 | int i, child_count, value_count, children_padding; |
| 646 | uint32_t children[INDEX_CHILDMAX]; |
| 647 | char first, last; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 648 | |
| 649 | if ((offset & INDEX_NODE_MASK) == 0) |
| 650 | return NULL; |
| 651 | |
| 652 | p = (char *)p + (offset & INDEX_NODE_MASK); |
| 653 | |
Gustavo Sverzut Barbieri | 15c1c14 | 2011-12-10 11:44:31 -0200 | [diff] [blame] | 654 | if (offset & INDEX_NODE_PREFIX) { |
| 655 | unsigned len; |
| 656 | prefix = read_chars_mm(&p, &len); |
| 657 | } else |
| 658 | prefix = _idx_empty_str; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 659 | |
| 660 | if (offset & INDEX_NODE_CHILDS) { |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 661 | first = read_char_mm(&p); |
| 662 | last = read_char_mm(&p); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 663 | child_count = last - first + 1; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 664 | for (i = 0; i < child_count; i++) |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 665 | children[i] = read_long_mm(&p); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 666 | } else { |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 667 | first = INDEX_CHILDMAX; |
| 668 | last = 0; |
| 669 | child_count = 0; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 670 | } |
| 671 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 672 | children_padding = (offsetof(struct index_mm_node, children) + |
| 673 | (sizeof(uint32_t) * child_count)) % sizeof(void *); |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 674 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 675 | if (offset & INDEX_NODE_VALUES) |
| 676 | value_count = read_long_mm(&p); |
| 677 | else |
| 678 | value_count = 0; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 679 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 680 | node = malloc(sizeof(struct index_mm_node) |
| 681 | + sizeof(uint32_t) * child_count + children_padding |
| 682 | + sizeof(struct index_mm_value) * value_count); |
| 683 | if (node == NULL) |
| 684 | return NULL; |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 685 | |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 686 | node->idx = idx; |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 687 | node->prefix = prefix; |
| 688 | if (value_count == 0) |
| 689 | node->values.values = NULL; |
| 690 | else { |
| 691 | node->values.values = (struct index_mm_value *) |
| 692 | ((char *)node + sizeof(struct index_mm_node) + |
| 693 | sizeof(uint32_t) * child_count + children_padding); |
| 694 | } |
| 695 | node->values.len = value_count; |
| 696 | node->first = first; |
| 697 | node->last = last; |
| 698 | memcpy(node->children, children, sizeof(uint32_t) * child_count); |
| 699 | |
| 700 | for (i = 0; i < value_count; i++) { |
| 701 | struct index_mm_value *v = node->values.values + i; |
| 702 | v->priority = read_long_mm(&p); |
| 703 | v->value = read_chars_mm(&p, &v->len); |
| 704 | } |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 705 | |
| 706 | return node; |
| 707 | } |
| 708 | |
| 709 | static void index_mm_free_node(struct index_mm_node *node) |
| 710 | { |
Lucas De Marchi | 836be9a | 2011-12-02 17:27:52 -0200 | [diff] [blame] | 711 | free(node); |
| 712 | } |
| 713 | |
Lucas De Marchi | 5109f2b | 2011-12-08 15:10:55 -0200 | [diff] [blame] | 714 | struct index_mm *index_mm_open(struct kmod_ctx *ctx, const char *filename, |
| 715 | bool populate) |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 716 | { |
| 717 | int fd; |
Lucas De Marchi | 5109f2b | 2011-12-08 15:10:55 -0200 | [diff] [blame] | 718 | int flags; |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 719 | struct stat st; |
| 720 | struct index_mm *idx; |
| 721 | struct { |
| 722 | uint32_t magic; |
| 723 | uint32_t version; |
| 724 | uint32_t root_offset; |
| 725 | } hdr; |
| 726 | void *p; |
| 727 | |
| 728 | DBG(ctx, "file=%s\n", filename); |
| 729 | |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 730 | idx = malloc(sizeof(*idx)); |
| 731 | if (idx == NULL) { |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 732 | ERR(ctx, "%m\n"); |
| 733 | return NULL; |
| 734 | } |
| 735 | |
Cristian RodrÃguez | 79e5ea9 | 2011-12-16 02:49:54 -0300 | [diff] [blame] | 736 | if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) < 0) { |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 737 | ERR(ctx, "%m\n"); |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 738 | goto fail_open; |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 739 | } |
| 740 | |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 741 | fstat(fd, &st); |
Lucas De Marchi | 5109f2b | 2011-12-08 15:10:55 -0200 | [diff] [blame] | 742 | flags = MAP_PRIVATE; |
| 743 | if (populate) |
| 744 | flags |= MAP_POPULATE; |
| 745 | |
| 746 | if ((idx->mm = mmap(0, st.st_size, PROT_READ, flags, fd, 0)) |
| 747 | == MAP_FAILED) { |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 748 | ERR(ctx, "%m\n"); |
| 749 | goto fail; |
| 750 | } |
| 751 | |
| 752 | p = idx->mm; |
| 753 | hdr.magic = read_long_mm(&p); |
| 754 | hdr.version = read_long_mm(&p); |
| 755 | hdr.root_offset = read_long_mm(&p); |
| 756 | |
| 757 | if (hdr.magic != INDEX_MAGIC) { |
| 758 | ERR(ctx, "magic check fail: %x instead of %x\n", hdr.magic, |
| 759 | INDEX_MAGIC); |
| 760 | goto fail; |
| 761 | } |
| 762 | |
| 763 | if (hdr.version >> 16 != INDEX_VERSION_MAJOR) { |
| 764 | ERR(ctx, "major version check fail: %u instead of %u\n", |
| 765 | hdr.version, INDEX_MAGIC); |
| 766 | goto fail; |
| 767 | } |
| 768 | |
| 769 | idx->root_offset = hdr.root_offset; |
| 770 | idx->size = st.st_size; |
| 771 | idx->ctx = ctx; |
| 772 | close(fd); |
| 773 | |
| 774 | return idx; |
| 775 | |
| 776 | fail: |
| 777 | close(fd); |
Lucas De Marchi | 7d51d8b | 2011-12-12 18:41:57 -0200 | [diff] [blame] | 778 | fail_open: |
Lucas De Marchi | b471a6b | 2011-12-01 23:14:20 -0200 | [diff] [blame] | 779 | if (idx->mm) |
| 780 | munmap(idx->mm, st.st_size); |
| 781 | free(idx); |
| 782 | return NULL; |
| 783 | } |
| 784 | |
| 785 | void index_mm_close(struct index_mm *idx) |
| 786 | { |
| 787 | munmap(idx->mm, idx->size); |
| 788 | free(idx); |
| 789 | } |
Lucas De Marchi | 77bf936 | 2011-12-02 17:41:30 -0200 | [diff] [blame] | 790 | |
| 791 | static struct index_mm_node *index_mm_readroot(struct index_mm *idx) |
| 792 | { |
| 793 | return index_mm_read_node(idx, idx->root_offset); |
| 794 | } |
Lucas De Marchi | 91298dc | 2011-12-02 17:41:46 -0200 | [diff] [blame] | 795 | |
| 796 | static struct index_mm_node *index_mm_readchild(const struct index_mm_node *parent, |
| 797 | int ch) |
| 798 | { |
| 799 | if (parent->first <= ch && ch <= parent->last) { |
| 800 | return index_mm_read_node(parent->idx, |
| 801 | parent->children[ch - parent->first]); |
| 802 | } |
| 803 | |
| 804 | return NULL; |
| 805 | } |
Lucas De Marchi | e33bb87 | 2011-12-02 17:45:01 -0200 | [diff] [blame] | 806 | |
| 807 | static char *index_mm_search_node(struct index_mm_node *node, const char *key, |
| 808 | int i) |
| 809 | { |
| 810 | char *value; |
| 811 | struct index_mm_node *child; |
| 812 | int ch; |
| 813 | int j; |
| 814 | |
| 815 | while(node) { |
| 816 | for (j = 0; node->prefix[j]; j++) { |
| 817 | ch = node->prefix[j]; |
| 818 | |
| 819 | if (ch != key[i+j]) { |
| 820 | index_mm_free_node(node); |
| 821 | return NULL; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | i += j; |
| 826 | |
| 827 | if (key[i] == '\0') { |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 828 | if (node->values.len > 0) { |
| 829 | value = strdup(node->values.values[0].value); |
Lucas De Marchi | e33bb87 | 2011-12-02 17:45:01 -0200 | [diff] [blame] | 830 | index_mm_free_node(node); |
| 831 | return value; |
| 832 | } else { |
| 833 | return NULL; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | child = index_mm_readchild(node, key[i]); |
| 838 | index_mm_free_node(node); |
| 839 | node = child; |
| 840 | i++; |
| 841 | } |
| 842 | |
| 843 | return NULL; |
| 844 | } |
Lucas De Marchi | b797b79 | 2011-12-02 17:49:03 -0200 | [diff] [blame] | 845 | |
| 846 | /* |
| 847 | * Search the index for a key |
| 848 | * |
| 849 | * Returns the value of the first match |
| 850 | * |
| 851 | * The recursive functions free their node argument (using index_close). |
| 852 | */ |
| 853 | char *index_mm_search(struct index_mm *idx, const char *key) |
| 854 | { |
| 855 | struct index_mm_node *root; |
| 856 | char *value; |
| 857 | |
| 858 | root = index_mm_readroot(idx); |
| 859 | value = index_mm_search_node(root, key, 0); |
| 860 | |
| 861 | return value; |
| 862 | } |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 863 | |
| 864 | /* Level 4: add all the values from a matching node */ |
| 865 | static void index_mm_searchwild_allvalues(struct index_mm_node *node, |
| 866 | struct index_value **out) |
| 867 | { |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 868 | struct index_mm_value *itr, *itr_end; |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 869 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 870 | itr = node->values.values; |
| 871 | itr_end = itr + node->values.len; |
| 872 | for (; itr < itr_end; itr++) |
| 873 | add_value(out, itr->value, itr->len, itr->priority); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 874 | |
| 875 | index_mm_free_node(node); |
| 876 | } |
| 877 | |
| 878 | /* |
| 879 | * Level 3: traverse a sub-keyspace which starts with a wildcard, |
| 880 | * looking for matches. |
| 881 | */ |
| 882 | static void index_mm_searchwild_all(struct index_mm_node *node, int j, |
| 883 | struct buffer *buf, |
| 884 | const char *subkey, |
| 885 | struct index_value **out) |
| 886 | { |
| 887 | int pushed = 0; |
| 888 | int ch; |
| 889 | |
| 890 | while (node->prefix[j]) { |
| 891 | ch = node->prefix[j]; |
| 892 | |
| 893 | buf_pushchar(buf, ch); |
| 894 | pushed++; |
| 895 | j++; |
| 896 | } |
| 897 | |
| 898 | for (ch = node->first; ch <= node->last; ch++) { |
| 899 | struct index_mm_node *child = index_mm_readchild(node, ch); |
| 900 | |
| 901 | if (!child) |
| 902 | continue; |
| 903 | |
| 904 | buf_pushchar(buf, ch); |
| 905 | index_mm_searchwild_all(child, 0, buf, subkey, out); |
| 906 | buf_popchar(buf); |
| 907 | } |
| 908 | |
Gustavo Sverzut Barbieri | d091546 | 2011-12-10 13:04:43 -0200 | [diff] [blame] | 909 | if (node->values.len > 0) { |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 910 | if (fnmatch(buf_str(buf), subkey, 0) == 0) |
| 911 | index_mm_searchwild_allvalues(node, out); |
Gustavo Sverzut Barbieri | 27fdf63 | 2011-12-10 13:28:18 -0200 | [diff] [blame] | 912 | else |
| 913 | index_mm_free_node(node); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 914 | } else { |
| 915 | index_mm_free_node(node); |
| 916 | } |
| 917 | |
| 918 | buf_popchars(buf, pushed); |
| 919 | } |
| 920 | |
| 921 | /* Level 2: descend the tree (until we hit a wildcard) */ |
| 922 | static void index_mm_searchwild_node(struct index_mm_node *node, |
| 923 | struct buffer *buf, |
| 924 | const char *key, int i, |
| 925 | struct index_value **out) |
| 926 | { |
| 927 | struct index_mm_node *child; |
| 928 | int j; |
| 929 | int ch; |
| 930 | |
| 931 | while(node) { |
| 932 | for (j = 0; node->prefix[j]; j++) { |
| 933 | ch = node->prefix[j]; |
| 934 | |
| 935 | if (ch == '*' || ch == '?' || ch == '[') { |
| 936 | index_mm_searchwild_all(node, j, buf, |
| 937 | &key[i+j], out); |
| 938 | return; |
| 939 | } |
| 940 | |
| 941 | if (ch != key[i+j]) { |
| 942 | index_mm_free_node(node); |
| 943 | return; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | i += j; |
| 948 | |
| 949 | child = index_mm_readchild(node, '*'); |
| 950 | if (child) { |
| 951 | buf_pushchar(buf, '*'); |
| 952 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
| 953 | buf_popchar(buf); |
| 954 | } |
| 955 | |
| 956 | child = index_mm_readchild(node, '?'); |
| 957 | if (child) { |
| 958 | buf_pushchar(buf, '?'); |
| 959 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
| 960 | buf_popchar(buf); |
| 961 | } |
| 962 | |
| 963 | child = index_mm_readchild(node, '['); |
| 964 | if (child) { |
| 965 | buf_pushchar(buf, '['); |
| 966 | index_mm_searchwild_all(child, 0, buf, &key[i], out); |
| 967 | buf_popchar(buf); |
| 968 | } |
| 969 | |
| 970 | if (key[i] == '\0') { |
| 971 | index_mm_searchwild_allvalues(node, out); |
| 972 | |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | child = index_mm_readchild(node, key[i]); |
| 977 | index_mm_free_node(node); |
| 978 | node = child; |
| 979 | i++; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * Search the index for a key. The index may contain wildcards. |
| 985 | * |
| 986 | * Returns a list of all the values of matching keys. |
| 987 | */ |
| 988 | struct index_value *index_mm_searchwild(struct index_mm *idx, const char *key) |
| 989 | { |
| 990 | struct index_mm_node *root = index_mm_readroot(idx); |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 991 | struct buffer buf; |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 992 | struct index_value *out = NULL; |
| 993 | |
Gustavo Sverzut Barbieri | 405f614 | 2011-12-08 14:36:30 -0200 | [diff] [blame] | 994 | buf_init(&buf); |
| 995 | index_mm_searchwild_node(root, &buf, key, 0, &out); |
| 996 | buf_release(&buf); |
Lucas De Marchi | bf89f70 | 2011-12-02 18:23:36 -0200 | [diff] [blame] | 997 | return out; |
| 998 | } |