catalog.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
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 <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "sd-id128.h"
#include "alloc-util.h"
#include "catalog.h"
#include "conf-files.h"
#include "fd-util.h"
#include "fileio.h"
#include "hashmap.h"
#include "log.h"
#include "mkdir.h"
#include "path-util.h"
#include "siphash24.h"
#include "sparse-endian.h"
#include "strbuf.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
const char * const catalog_file_dirs[] = {
};
typedef struct CatalogHeader {
typedef struct CatalogItem {
char language[32];
} CatalogItem;
const CatalogItem *i = p;
}
static int catalog_compare_func(const void *a, const void *b) {
const CatalogItem *i = a, *j = b;
unsigned k;
return -1;
return 1;
}
}
const struct hash_ops catalog_hash_ops = {
};
static bool next_header(const char **s) {
const char *e;
e = strchr(*s, '\n');
/* Unexpected end */
if (!e)
return false;
/* End of headers */
if (e == *s)
return false;
*s = e + 1;
return true;
}
static const char *skip_header(const char *s) {
while (next_header(&s))
;
return s;
}
char *dest, *p;
/* Find split point of headers to body */
if (!dest) {
log_oom();
return NULL;
}
p = dest;
/* Headers from @one */
/* Headers from @two, these will only be found if not present above */
/* Body from @one */
if (n > 0) {
p += n;
/* Body from @two */
} else {
p += n;
}
p[0] = '\0';
return dest;
}
static int finish_item(
Hashmap *h,
const char *language,
char *payload) {
int r;
assert(h);
if (!i)
return log_oom();
if (language) {
}
prev = hashmap_get(h, i);
/* Already have such an item, combine them */
if (prev) {
if (!combined)
return log_oom();
r = hashmap_update(h, i, combined);
if (r < 0)
return r;
/* A new item */
} else {
r = hashmap_put(h, i, payload);
if (r < 0)
return r;
i = NULL;
}
return 0;
}
if (!end)
return 0;
beg --;
return 0;
if (!_lang)
return -ENOMEM;
return 1;
}
size_t c;
c = strlen(t);
if (c == 0) {
return -EINVAL;
}
if (c > 31) {
return -EINVAL;
}
if (deflang) {
log_warning("[%s:%u] language specified unnecessarily",
return 0;
} else
log_warning("[%s:%u] language differs from default for file",
}
if (!*lang)
return -ENOMEM;
return 0;
}
unsigned n = 0;
bool got_id = false, empty_line = true;
int r;
assert(h);
if (!f)
if (r < 0)
if (r == 1)
for (;;) {
size_t a, b, c;
char *t;
if (feof(f))
break;
}
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) {
if (r < 0)
return r;
}
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 log_error_errno(r, "Recursive mkdir %s: %m", d);
r = fopen_temporary(database, &w, &p);
if (r < 0)
return log_error_errno(r, "Failed to open database for writing: %s: %m",
database);
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;
}
r = fflush_and_check(w);
if (r < 0) {
log_error_errno(r, "%s: failed to write database: %m", p);
goto error;
}
goto error;
}
return ftello(w);
(void) unlink(p);
return r;
}
char **f;
char *payload;
CatalogItem *i;
Iterator j;
unsigned n;
int r;
h = hashmap_new(&catalog_hash_ops);
sb = strbuf_new();
if (!h || !sb) {
r = log_oom();
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to get catalog files: %m");
goto finish;
}
STRV_FOREACH(f, files) {
log_debug("Reading file '%s'", *f);
r = catalog_import_file(h, *f);
if (r < 0) {
log_error_errno(r, "Failed to import file '%s': %m", *f);
goto finish;
}
}
if (hashmap_size(h) <= 0) {
log_info("No items in catalog.");
goto finish;
} else
if (!items) {
r = log_oom();
goto finish;
}
n = 0;
HASHMAP_FOREACH_KEY(payload, i, h, j) {
SD_ID128_FORMAT_VAL(i->id),
if (offset < 0) {
r = log_oom();
goto finish;
}
items[n++] = *i;
}
assert(n == hashmap_size(h));
if (sz < 0)
else {
r = 0;
}
return r;
}
const CatalogHeader *h;
int fd;
void *p;
if (fd < 0)
return -errno;
safe_close(fd);
return -errno;
}
safe_close(fd);
return -EINVAL;
}
if (p == MAP_FAILED) {
safe_close(fd);
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))) {
safe_close(fd);
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;
v = startswith(s, header);
if (v) {
v += strspn(v, WHITESPACE);
}
if (!next_header(&s))
return NULL;
}
}
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) {
if (r == 0)
r = k;
continue;
}
if (k < 0) {
"Failed to retrieve catalog entry for '%s': %m", *item);
if (r == 0)
r = k;
continue;
}
}
return r;
}