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