udevadm-hwdb.c revision beb23689e357ed549f5c9d4a3440da2132a419f1
/***
This file is part of systemd.
Copyright 2012 Kay Sievers <kay@vrfy.org>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include "util.h"
#include "strbuf.h"
#include "conf-files.h"
#include "udev.h"
#include "libudev-hwdb-def.h"
/*
*/
static const char * const conf_file_dirs[] = {
UDEVLIBEXECDIR "/hwdb.d",
};
/* in-memory trie objects */
struct trie {
};
struct trie_node {
/* prefix, common part for all children of this node */
/* sorted array of pointers to children nodes */
struct trie_child_entry *children;
struct trie_value_entry *values;
};
/* children array item with char (0-255) index */
struct trie_child_entry {
uint8_t c;
};
struct trie_value_entry {
};
}
static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t c) {
struct trie_child_entry *child;
int err = 0;
/* extend array, add new entry, sort for bisection */
if (!child) {
goto out;
}
trie->children_count++;
node->children_count++;
trie->nodes_count++;
out:
return err;
}
struct trie_child_entry *child;
struct trie_child_entry search;
search.c = c;
child = bsearch(&search, node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp);
if (child)
return NULL;
}
size_t i;
for (i = 0; i < node->children_count; i++)
}
}
ssize_t k, v;
struct trie_value_entry *val;
if (k < 0)
return k;
if (v < 0)
return v;
if (node->values_count) {
struct trie_value_entry search = {
.key_off = k,
.value_off = v,
};
val = xbsearch_r(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie);
if (val) {
/* replace existing earlier key with new value */
return 0;
}
}
/* extend array, add new entry, sort for bisection */
if (!val)
return -ENOMEM;
trie->values_count++;
node->values_count++;
return 0;
}
size_t i = 0;
int err = 0;
for (;;) {
size_t p;
uint8_t c;
char *s;
if (c == search[i + p])
continue;
/* split node */
if (!child) {
goto out;
}
/* move values from parent to child */
/* update parent; use strdup() because the source gets realloc()d */
if (!s) {
goto out;
}
free(s);
if (off < 0) {
goto out;
}
node->children_count = 0;
node->values_count = 0;
if (err)
goto out;
break;
}
i += p;
c = search[i];
if (c == '\0')
if (!child) {
/* new child */
if (!child) {
goto out;
}
if (off < 0) {
goto out;
}
if (err)
goto out;
}
i++;
}
out:
return err;
}
struct trie_f {
FILE *f;
};
/* calculate the storage space for the nodes, children arrays, value arrays */
uint64_t i;
for (i = 0; i < node->children_count; i++)
for (i = 0; i < node->children_count; i++)
for (i = 0; i < node->values_count; i++)
}
uint64_t i;
struct trie_node_f n = {
};
if (node->children_count) {
if (!children)
return -ENOMEM;
}
/* post-order recursion */
for (i = 0; i < node->children_count; i++) {
if (child_off < 0)
return child_off;
}
/* write node */
trie->nodes_count++;
/* append children array */
if (node->children_count) {
}
/* append values array */
for (i = 0; i < node->values_count; i++) {
struct trie_value_entry_f v = {
};
trie->values_count++;
}
return node_off;
}
struct trie_f t = {
};
char *filename_tmp;
struct trie_header_f h = {
};
int err;
/* calculate size of header, nodes, children entries, value entries */
t.strings_off = sizeof(struct trie_header_f);
if (err < 0)
return err;
/* write nodes */
/* write string buffer */
/* write header */
if (err)
fclose(t.f);
goto out;
}
log_debug("=== trie on-disk ===\n");
log_debug("nodes: %8llu bytes (%8llu)\n",
log_debug("child pointers: %8llu bytes (%8llu)\n",
(unsigned long long)t.children_count * sizeof(struct trie_child_entry_f), (unsigned long long)t.children_count);
log_debug("value pointers: %8llu bytes (%8llu)\n",
(unsigned long long)t.values_count * sizeof(struct trie_value_entry_f), (unsigned long long)t.values_count);
out:
return err;
}
FILE *f;
if (f == NULL)
return -errno;
match[0] = '\0';
cond[0] = '\0';
if (line[0] == '#')
continue;
/* new line, new record */
if (line[0] == '\n') {
match[0] = '\0';
cond[0] = '\0';
continue;
}
/* remove newline */
if (len < 2)
continue;
/* start of new record */
if (match[0] == '\0') {
cond[0] = '\0';
continue;
}
if (line[0] == '+') {
continue;
}
/* TODO: support +; skip the entire record until we support it */
if (cond[0] != '\0')
continue;
/* value lines */
if (line[0] == ' ') {
char *value;
if (!value)
continue;
value[0] = '\0';
value++;
}
}
fclose(f);
return 0;
}
static void help(void) {
printf("Usage: udevadm hwdb OPTIONS\n"
" --update update the hardware database\n"
" --test <modalias> query database and print result\n"
" --help\n\n");
}
{}
};
bool update = false;
int err;
int rc = EXIT_SUCCESS;
for (;;) {
int option;
if (option == -1)
break;
switch (option) {
case 'u':
update = true;
break;
case 't':
break;
case 'h':
help();
return EXIT_SUCCESS;
}
}
help();
return EXIT_SUCCESS;
}
if (update) {
char **files, **f;
if (!trie) {
rc = EXIT_FAILURE;
goto out;
}
/* string store */
rc = EXIT_FAILURE;
goto out;
}
/* index */
rc = EXIT_FAILURE;
goto out;
}
trie->nodes_count++;
if (err < 0) {
rc = EXIT_FAILURE;
goto out;
}
STRV_FOREACH(f, files) {
log_debug("reading file '%s'", *f);
import_file(trie, *f);
}
log_debug("=== trie in-memory ===\n");
log_debug("nodes: %8zu bytes (%8zu)\n",
log_debug("children arrays: %8zu bytes (%8zu)\n",
log_debug("values arrays: %8zu bytes (%8zu)\n",
log_debug("strings: %8zu bytes\n",
log_debug("strings incoming: %8zu bytes (%8zu)\n",
log_debug("strings dedup'ed: %8zu bytes (%8zu)\n",
if (err < 0) {
rc = EXIT_FAILURE;
}
}
if (test) {
if (hwdb) {
struct udev_list_entry *entry;
}
}
out:
if (trie) {
}
return rc;
}
const struct udevadm_cmd udevadm_hwdb = {
.name = "hwdb",
.help = "maintain the hardware database index",
};