udevadm-hwdb.c revision 796b06c21b62d13c9021e2fbd9c58a5c6edb2764
/***
This file is part of systemd.
Copyright 2012 Kay Sievers <kay.sievers@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 "udev-hwdb.h"
/*
*/
static const char * const conf_file_dirs[] = {
SYSCONFDIR "/udev/hwdb.d",
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++)
}
}
size_t k, v;
struct trie_value_entry *val;
struct trie_value_entry search;
/* replace existing earlier key with new value */
val = xbsearch_r(&search, node->values, node->values_count, sizeof(struct trie_value_entry), trie_values_cmp, trie);
if (val) {
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 = {
};
struct trie_child_entry_f *children;
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("child pointers: %8zu bytes (%8zi)\n", t.children_count * sizeof(struct trie_child_entry_f), t.children_count);
log_debug("value pointers: %8zu bytes (%8zi)\n", t.values_count * sizeof(struct trie_value_entry_f), t.values_count);
out:
return err;
}
FILE *f;
if (f == NULL)
return -errno;
match[0] = '\0';
if (line[0] == '#')
continue;
/* new line, new record */
if (line[0] == '\n') {
match[0] = '\0';
continue;
}
/* remove newline */
if (len < 2)
continue;
/* start of new record */
if (match[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 [--create] [--help]\n"
" --update update the hardware database\n"
" --help\n\n");
}
{}
};
bool update = false;
char **files, **f;
int err;
int rc = EXIT_SUCCESS;
for (;;) {
int option;
if (option == -1)
break;
switch (option) {
case 'u':
update = true;
break;
case 'h':
help();
return EXIT_SUCCESS;
}
}
if (!update) {
help();
return EXIT_SUCCESS;
}
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", trie->nodes_count * sizeof(struct trie_node), trie->nodes_count);
log_debug("children arrays: %8zu bytes (%8zu)\n", trie->children_count * sizeof(struct trie_child_entry), trie->children_count);
log_debug("values arrays: %8zu bytes (%8zu)\n", trie->values_count * sizeof(struct trie_value_entry), trie->values_count);
log_debug("strings dedup'ed: %8zu bytes (%8zu)\n", trie->strings->dedup_len, trie->strings->dedup_count);
if (err < 0) {
log_error("Failure writing hardware database '%s': %s", SYSCONFDIR "/udev/hwdb.bin", strerror(-err));
rc = EXIT_FAILURE;
}
out:
return rc;
}
const struct udevadm_cmd udevadm_hwdb = {
.name = "hwdb",
.help = "maintain the hardware database index",
};