catalog.c revision 7c670f5b44a6e8758918eaacd59f472ee7942514
/*-*- 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 "util.h"
#include "log.h"
#include "sparse-endian.h"
#include "sd-id128.h"
#include "hashmap.h"
#include "strv.h"
#include "strbuf.h"
#include "conf-files.h"
#include "mkdir.h"
#include "catalog.h"
static const char * const conf_file_dirs[] = {
};
typedef struct CatalogHeader {
typedef struct CatalogItem {
char language[32];
} CatalogItem;
static unsigned catalog_hash_func(const void *p) {
const CatalogItem *i = p;
string_hash_func(i->language);
}
static 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();
r = hashmap_put(h, i, i);
if (r == EEXIST) {
log_warning("Duplicate entry for " SD_ID128_FORMAT_STR ".%s, ignoring.", SD_ID128_FORMAT_VAL(id), language ? language : "C");
free(i);
return 0;
}
return 0;
}
unsigned n = 0;
char language[32];
bool got_id = false, empty_line = true;
int r;
assert(h);
if (!f) {
return -errno;
}
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 > sizeof(language)) {
return -EINVAL;
}
} else
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;
}
#define CATALOG_PATH "/var/lib/systemd/catalog"
int catalog_update(void) {
_cleanup_free_ char *p = NULL;
char **f;
Hashmap *h;
CatalogItem *i;
size_t k;
Iterator j;
unsigned n;
int r;
if (!h)
return -ENOMEM;
sb = strbuf_new();
if (!sb) {
r = log_oom();
goto finish;
}
if (r < 0) {
goto finish;
}
STRV_FOREACH(f, files) {
log_debug("reading file '%s'", *f);
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) {
log_debug("Found " SD_ID128_FORMAT_STR ", language %s", SD_ID128_FORMAT_VAL(i->id), isempty(i->language) ? "C" : i->language);
items[n++] = *i;
}
assert(n == hashmap_size(h));
if (r < 0) {
goto finish;
}
r = fopen_temporary(CATALOG_DATABASE, &w, &p);
if (r < 0) {
log_error("Failed to open database for writing: %s: %s",
CATALOG_DATABASE, strerror(-r));
goto finish;
}
if (k != sizeof(header)) {
log_error("%s: failed to write header.", p);
goto finish;
}
if (k != n * sizeof(CatalogItem)) {
log_error("%s: failed to write database.", p);
goto finish;
}
log_error("%s: failed to write strings.", p);
goto finish;
}
fflush(w);
if (ferror(w)) {
log_error("%s: failed to write database.", p);
goto finish;
}
if (rename(p, CATALOG_DATABASE) < 0) {
r = -errno;
goto finish;
}
log_debug("%s: wrote %u items, with %zu bytes of strings, %zu total size.",
free(p);
p = NULL;
r = 0;
if (sb)
if (p)
unlink(p);
return r;
}
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;
}
}
int catalog_list(FILE *f) {
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;
fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n", SD_ID128_FORMAT_VAL(items[n].id), strna(defined_by), strna(subject));
last_id_set = true;
}
return 0;
}