catalog.c revision 3ffd4af22052963e7a29431721ee204e634bea75
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2012 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringconst char * const catalog_file_dirs[] = {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#define CATALOG_SIGNATURE (uint8_t[]) { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic void catalog_hash_func(const void *p, struct siphash *state) {
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering siphash24_compress(&i->id, sizeof(i->id), state);
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering siphash24_compress(i->language, strlen(i->language), state);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic int catalog_compare_func(const void *a, const void *b) {
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering const CatalogItem *i = a, *j = b;
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering for (k = 0; k < ELEMENTSOF(j->id.bytes); k++) {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poetteringconst struct hash_ops catalog_hash_ops = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering offset = strbuf_add_string(sb, payload, strlen(payload));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(strlen(language) > 1 && strlen(language) < 32);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_warning("Duplicate entry for " SD_ID128_FORMAT_STR ".%s, ignoring.",
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering SD_ID128_FORMAT_VAL(id), language ? language : "C");
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering } else if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint catalog_file_lang(const char* filename, char **lang) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while (beg > filename && *beg != '.' && *beg != '/' && end - beg < 32)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringstatic int catalog_entry_lang(const char* filename, int line,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering const char* t, const char* deflang, char **lang) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek log_error("[%s:%u] Language too short.", filename, line);
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering log_error("[%s:%u] language too long.", filename, line);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_warning("[%s:%u] language specified unnecessarily",
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_warning("[%s:%u] language differs from default for file",
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poetteringint catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *payload = NULL;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *deflang = NULL, *lang = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek bool got_id = false, empty_line = true;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return log_error_errno(errno, "Failed to open file %s: %m", path);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = catalog_file_lang(path, &deflang);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_error_errno(errno, "Failed to determine language for file %s: %m", path);
9ead3519c54b6d1b79b35541873b5cf7c8b3a7d3Lennart Poettering log_debug("File %s has language %s.", path, deflang);
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering log_error_errno(errno, "Failed to read file %s: %m", path);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (line[0] == 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (line[2+1+32] == ' ' || line[2+1+32] == '\0')) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* New entry */
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering r = finish_item(h, sb, id, lang ?: deflang, payload);
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering r = catalog_entry_lang(path, n, t, deflang, &lang);
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack log_error("[%s:%u] Got payload before ID.", path, n);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering c = a + (empty_line ? 1 : 0) + b + 1 + 1;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt t[a+b] = '\n';
if (got_id) {
size_t k;
return log_oom();
database);
r = -EIO;
if (k != sizeof(header)) {
goto error;
if (k != n * sizeof(CatalogItem)) {
goto error;
goto error;
r = fflush_and_check(w);
goto error;
goto error;
return ftell(w);
(void) unlink(p);
CatalogItem *i;
Iterator j;
if (!h || !sb) {
r = log_oom();
goto finish;
goto finish;
goto finish;
if (hashmap_size(h) <= 0) {
goto finish;
if (!items) {
r = log_oom();
goto finish;
HASHMAP_FOREACH(i, h, j) {
items[n++] = *i;
if (sb)
const CatalogHeader *h;
int fd;
if (fd < 0)
return -errno;
return -errno;
return -EINVAL;
if (p == MAP_FAILED) {
return -errno;
h->incompatible_flags != 0 ||
st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
return -EBADMSG;
*_p = p;
const CatalogHeader *h = p;
const char *loc;
f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
return NULL;
void *p = NULL;
r = -ENOENT;
goto finish;
if (!text) {
r = -ENOMEM;
goto finish;
return NULL;
return NULL;
if (oneline) {
void *p = NULL;
const CatalogHeader *h;
bool last_id_set = false;
last_id_set = true;
char **item;