catalog.c revision 4b8268f843b0da1cfe1995d93a0b1f95faccc454
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
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 <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <libgen.h>
#include "util.h"
#include "log.h"
#include "sparse-endian.h"
#include "sd-id128.h"
#include "hashmap.h"
#include "strv.h"
#include "strbuf.h"
#include "strxcpyx.h"
#include "conf-files.h"
#include "mkdir.h"
#include "catalog.h"
const char * const catalog_file_dirs[] = {
};
typedef struct CatalogHeader {
typedef struct CatalogItem {
char language[32];
} CatalogItem;
unsigned catalog_hash_func(const void *p) {
const CatalogItem *i = p;
string_hash_func(i->language);
}
int catalog_compare_func(const void *a, const void *b) {
const CatalogItem *i = a, *j = b;
unsigned k;
return -1;
return 1;
}
}
static int finish_item(
Hashmap *h,
const char *language,
const char *payload) {
CatalogItem *i;
int r;
assert(h);
if (offset < 0)
return log_oom();
if (!i)
return log_oom();
if (language) {
}
r = hashmap_put(h, i, i);
if (r == EEXIST) {
free(i);
return 0;
}
return 0;
}
if (!end)
return 0;
beg --;
return 0;
if (!_lang)
return -ENOMEM;
return 1;
}
unsigned n = 0;
bool got_id = false, empty_line = true;
int r;
assert(h);
if (!f) {
return -errno;
}
if (r < 0)
if (r == 1)
for (;;) {
size_t a, b, c;
char *t;
if (feof(f))
break;
return -errno;
}
n++;
if (line[0] == 0) {
empty_line = true;
continue;
}
continue;
if (empty_line &&
line[0] == '-' &&
bool with_language;
/* New entry */
if (got_id) {
if (r < 0)
return r;
}
if (with_language) {
c = strlen(t);
if (c <= 0) {
return -EINVAL;
}
if (c > 31) {
return -EINVAL;
}
if (deflang) {
"specified unnecessarily" :
"differs from default for file");
if (!lang)
return -ENOMEM;
}
}
got_id = true;
empty_line = false;
if (payload)
payload[0] = '\0';
continue;
}
}
/* Payload */
if (!got_id) {
return -EINVAL;
}
if (!t)
return log_oom();
if (empty_line) {
t[a] = '\n';
t[a+b+1] = '\n';
t[a+b+2] = 0;
} else {
t[a+b] = '\n';
t[a+b+1] = 0;
}
payload = t;
empty_line = false;
}
if (got_id) {
if (r < 0)
return r;
}
return 0;
}
int r;
_cleanup_free_ char *d, *p = NULL;
size_t k;
d = dirname_malloc(database);
if (!d)
return log_oom();
r = mkdir_p(d, 0775);
if (r < 0) {
return r;
}
r = fopen_temporary(database, &w, &p);
if (r < 0) {
log_error("Failed to open database for writing: %s: %s",
return r;
}
r = -EIO;
if (k != sizeof(header)) {
log_error("%s: failed to write header.", p);
goto error;
}
if (k != n * sizeof(CatalogItem)) {
log_error("%s: failed to write database.", p);
goto error;
}
log_error("%s: failed to write strings.", p);
goto error;
}
fflush(w);
if (ferror(w)) {
log_error("%s: failed to write database.", p);
goto error;
}
r = -errno;
goto error;
}
return ftell(w);
unlink(p);
return r;
}
char **f;
Hashmap *h;
CatalogItem *i;
Iterator j;
unsigned n;
long r;
sb = strbuf_new();
if (!h || !sb) {
r = log_oom();
goto finish;
}
if (r < 0) {
goto finish;
}
STRV_FOREACH(f, files) {
log_debug("reading file '%s'", *f);
catalog_import_file(h, sb, *f);
}
if (hashmap_size(h) <= 0) {
log_info("No items in catalog.");
r = 0;
goto finish;
} else
if (!items) {
r = log_oom();
goto finish;
}
n = 0;
HASHMAP_FOREACH(i, h, j) {
SD_ID128_FORMAT_VAL(i->id),
items[n++] = *i;
}
assert(n == hashmap_size(h));
if (r < 0)
else
log_debug("%s: wrote %u items, with %zu bytes of strings, %ld total size.",
r = 0;
if (h)
if (sb)
return r < 0 ? r : 0;
}
const CatalogHeader *h;
int fd;
void *p;
if (fd < 0)
return -errno;
return -errno;
}
return -EINVAL;
}
if (p == MAP_FAILED) {
return -errno;
}
h = p;
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;
return 0;
}
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);
if (!f) {
char *e;
if (e) {
*e = 0;
f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
}
}
}
if (!f) {
f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
}
if (!f)
return NULL;
return (const char*) p +
le64toh(h->header_size) +
}
void *p = NULL;
int r;
const char *s;
if (r < 0)
return r;
if (!s) {
r = -ENOENT;
goto finish;
}
if (!text) {
r = -ENOMEM;
goto finish;
}
r = 0;
if (p)
return r;
}
static char *find_header(const char *s, const char *header) {
for (;;) {
const char *v, *e;
v = startswith(s, header);
if (v) {
v += strspn(v, WHITESPACE);
}
/* End of text */
e = strchr(s, '\n');
if (!e)
return NULL;
/* End of header */
if (e == s)
return NULL;
s = e + 1;
}
}
if (oneline) {
} else
SD_ID128_FORMAT_VAL(id), s);
}
void *p = NULL;
const CatalogHeader *h;
const CatalogItem *items;
int r;
unsigned n;
bool last_id_set = false;
if (r < 0)
return r;
h = p;
const char *s;
continue;
last_id_set = true;
}
return 0;
}
char **item;
int r = 0;
int k;
if (k < 0) {
log_error("Failed to parse id128 '%s': %s",
if (r == 0)
r = k;
continue;
}
if (k < 0) {
"Failed to retrieve catalog entry for '%s': %s",
if (r == 0)
r = k;
continue;
}
}
return r;
}