]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/catalog.c
tree-wide: use siphash24_compress_string() where it is applicable
[thirdparty/systemd.git] / src / journal / catalog.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d4205751 2
07630cea 3#include <errno.h>
d4205751 4#include <fcntl.h>
07630cea 5#include <locale.h>
d4205751 6#include <stdio.h>
d4205751 7#include <sys/mman.h>
ca78ad1d
ZJS
8#include <sys/stat.h>
9#include <sys/types.h>
07630cea 10#include <unistd.h>
d4205751 11
d4205751 12#include "sd-id128.h"
07630cea 13
b5efdb8a 14#include "alloc-util.h"
07630cea 15#include "catalog.h"
d4205751 16#include "conf-files.h"
3ffd4af2 17#include "fd-util.h"
0d39fa9c 18#include "fileio.h"
07630cea
LP
19#include "hashmap.h"
20#include "log.h"
0a970718 21#include "memory-util.h"
d4205751 22#include "mkdir.h"
5f311f8c 23#include "path-util.h"
9bf3b535 24#include "siphash24.h"
760877e9 25#include "sort-util.h"
07630cea
LP
26#include "sparse-endian.h"
27#include "strbuf.h"
28#include "string-util.h"
29#include "strv.h"
e4de7287 30#include "tmpfile-util.h"
d4205751 31
844ec79b 32const char * const catalog_file_dirs[] = {
d4205751
LP
33 "/usr/local/lib/systemd/catalog/",
34 "/usr/lib/systemd/catalog/",
35 NULL
36};
37
5432d8a1 38#define CATALOG_SIGNATURE { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
d4205751
LP
39
40typedef struct CatalogHeader {
41 uint8_t signature[8]; /* "RHHHKSLP" */
42 le32_t compatible_flags;
43 le32_t incompatible_flags;
83f6936a
LP
44 le64_t header_size;
45 le64_t n_items;
46 le64_t catalog_item_size;
d4205751
LP
47} CatalogHeader;
48
49typedef struct CatalogItem {
50 sd_id128_t id;
00e1adf8
ZJS
51 char language[32]; /* One byte is used for termination, so the maximum allowed
52 * length of the string is actually 31 bytes. */
83f6936a 53 le64_t offset;
d4205751
LP
54} CatalogItem;
55
7a08d314 56static void catalog_hash_func(const CatalogItem *i, struct siphash *state) {
b826ab58 57 siphash24_compress(&i->id, sizeof(i->id), state);
f281fc1e 58 siphash24_compress_string(i->language, state);
d4205751
LP
59}
60
93bab288 61static int catalog_compare_func(const CatalogItem *a, const CatalogItem *b) {
d4205751 62 unsigned k;
a0edd02e 63 int r;
d4205751 64
93bab288
YW
65 for (k = 0; k < ELEMENTSOF(b->id.bytes); k++) {
66 r = CMP(a->id.bytes[k], b->id.bytes[k]);
a0edd02e
FB
67 if (r != 0)
68 return r;
d4205751
LP
69 }
70
93bab288 71 return strcmp(a->language, b->language);
d4205751
LP
72}
73
7a08d314 74DEFINE_HASH_OPS(catalog_hash_ops, CatalogItem, catalog_hash_func, catalog_compare_func);
d5099efc 75
dbae138d
SW
76static bool next_header(const char **s) {
77 const char *e;
78
79 e = strchr(*s, '\n');
80
81 /* Unexpected end */
82 if (!e)
83 return false;
84
85 /* End of headers */
86 if (e == *s)
87 return false;
88
89 *s = e + 1;
90 return true;
91}
92
93static const char *skip_header(const char *s) {
94 while (next_header(&s))
95 ;
96 return s;
97}
98
99static char *combine_entries(const char *one, const char *two) {
100 const char *b1, *b2;
101 size_t l1, l2, n;
102 char *dest, *p;
103
104 /* Find split point of headers to body */
105 b1 = skip_header(one);
106 b2 = skip_header(two);
107
108 l1 = strlen(one);
109 l2 = strlen(two);
110 dest = new(char, l1 + l2 + 1);
111 if (!dest) {
112 log_oom();
113 return NULL;
114 }
115
116 p = dest;
117
118 /* Headers from @one */
119 n = b1 - one;
120 p = mempcpy(p, one, n);
121
122 /* Headers from @two, these will only be found if not present above */
123 n = b2 - two;
124 p = mempcpy(p, two, n);
125
126 /* Body from @one */
127 n = l1 - (b1 - one);
128 if (n > 0) {
129 memcpy(p, b1, n);
130 p += n;
131
132 /* Body from @two */
133 } else {
134 n = l2 - (b2 - two);
135 memcpy(p, b2, n);
136 p += n;
137 }
138
139 assert(p - dest <= (ptrdiff_t)(l1 + l2));
140 p[0] = '\0';
141 return dest;
142}
143
d4205751 144static int finish_item(
a95686bb 145 OrderedHashmap *h,
d4205751
LP
146 sd_id128_t id,
147 const char *language,
06466a7f 148 char *payload, size_t payload_size) {
d4205751 149
e3b9d9c8 150 _cleanup_free_ CatalogItem *i = NULL;
06466a7f 151 _cleanup_free_ char *prev = NULL, *combined = NULL;
d4205751
LP
152
153 assert(h);
d4205751 154 assert(payload);
06466a7f 155 assert(payload_size > 0);
d4205751 156
d4205751
LP
157 i = new0(CatalogItem, 1);
158 if (!i)
159 return log_oom();
160
161 i->id = id;
c7332b08
ZJS
162 if (language) {
163 assert(strlen(language) > 1 && strlen(language) < 32);
164 strcpy(i->language, language);
165 }
d4205751 166
a95686bb 167 prev = ordered_hashmap_get(h, i);
dbae138d 168 if (prev) {
06466a7f 169 /* Already have such an item, combine them */
dbae138d
SW
170 combined = combine_entries(payload, prev);
171 if (!combined)
172 return log_oom();
dbae138d 173
a95686bb 174 if (ordered_hashmap_update(h, i, combined) < 0)
06466a7f
ZJS
175 return log_oom();
176 combined = NULL;
dbae138d 177 } else {
06466a7f
ZJS
178 /* A new item */
179 combined = memdup(payload, payload_size + 1);
180 if (!combined)
181 return log_oom();
182
a95686bb 183 if (ordered_hashmap_put(h, i, combined) < 0)
06466a7f 184 return log_oom();
dbae138d 185 i = NULL;
06466a7f 186 combined = NULL;
dbae138d 187 }
d4205751
LP
188
189 return 0;
190}
191
c7332b08
ZJS
192int catalog_file_lang(const char* filename, char **lang) {
193 char *beg, *end, *_lang;
194
195 end = endswith(filename, ".catalog");
196 if (!end)
197 return 0;
198
199 beg = end - 1;
4c701096 200 while (beg > filename && !IN_SET(*beg, '.', '/') && end - beg < 32)
313cefa1 201 beg--;
c7332b08 202
4b8268f8 203 if (*beg != '.' || end <= beg + 1)
c7332b08
ZJS
204 return 0;
205
206 _lang = strndup(beg + 1, end - beg - 1);
207 if (!_lang)
208 return -ENOMEM;
209
210 *lang = _lang;
211 return 1;
212}
213
bbe80432
LP
214static int catalog_entry_lang(
215 const char* filename,
216 unsigned line,
217 const char* t,
218 const char* deflang,
219 char **ret) {
220
baf167ee 221 size_t c;
bbe80432 222 char *z;
baf167ee
ZJS
223
224 c = strlen(t);
c90c39ff 225 if (c < 2)
baaa35ad 226 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
0640d48a 227 "[%s:%u] Language too short.", filename, line);
baaa35ad
ZJS
228 if (c > 31)
229 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
0640d48a 230 "[%s:%u] language too long.", filename, line);
baf167ee
ZJS
231
232 if (deflang) {
233 if (streq(t, deflang)) {
0640d48a 234 log_warning("[%s:%u] language specified unnecessarily", filename, line);
baf167ee 235 return 0;
0640d48a
LP
236 }
237
238 log_warning("[%s:%u] language differs from default for file", filename, line);
baf167ee
ZJS
239 }
240
bbe80432
LP
241 z = strdup(t);
242 if (!z)
243 return -ENOMEM;
baf167ee 244
bbe80432 245 *ret = z;
baf167ee
ZJS
246 return 0;
247}
248
a95686bb 249int catalog_import_file(OrderedHashmap *h, const char *path) {
d4205751
LP
250 _cleanup_fclose_ FILE *f = NULL;
251 _cleanup_free_ char *payload = NULL;
06466a7f 252 size_t payload_size = 0, payload_allocated = 0;
d4205751
LP
253 unsigned n = 0;
254 sd_id128_t id;
c7332b08 255 _cleanup_free_ char *deflang = NULL, *lang = NULL;
d4205751
LP
256 bool got_id = false, empty_line = true;
257 int r;
258
259 assert(h);
d4205751
LP
260 assert(path);
261
262 f = fopen(path, "re");
4a62c710
MS
263 if (!f)
264 return log_error_errno(errno, "Failed to open file %s: %m", path);
d4205751 265
c7332b08
ZJS
266 r = catalog_file_lang(path, &deflang);
267 if (r < 0)
709f6e46 268 log_error_errno(r, "Failed to determine language for file %s: %m", path);
c7332b08
ZJS
269 if (r == 1)
270 log_debug("File %s has language %s.", path, deflang);
271
d4205751 272 for (;;) {
df2b06d1 273 _cleanup_free_ char *line = NULL;
06466a7f 274 size_t line_len;
d4205751 275
df2b06d1
LP
276 r = read_line(f, LONG_LINE_MAX, &line);
277 if (r < 0)
278 return log_error_errno(r, "Failed to read file %s: %m", path);
279 if (r == 0)
280 break;
d4205751
LP
281
282 n++;
283
df2b06d1 284 if (isempty(line)) {
d4205751
LP
285 empty_line = true;
286 continue;
287 }
288
df2b06d1 289 if (strchr(COMMENTS, line[0]))
d4205751
LP
290 continue;
291
292 if (empty_line &&
293 strlen(line) >= 2+1+32 &&
294 line[0] == '-' &&
295 line[1] == '-' &&
296 line[2] == ' ' &&
4c701096 297 IN_SET(line[2+1+32], ' ', '\0')) {
d4205751
LP
298
299 bool with_language;
300 sd_id128_t jd;
301
302 /* New entry */
303
18cd5fe9
ZJS
304 with_language = line[2+1+32] != '\0';
305 line[2+1+32] = '\0';
d4205751
LP
306
307 if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
308
309 if (got_id) {
baaa35ad
ZJS
310 if (payload_size == 0)
311 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
312 "[%s:%u] No payload text.",
313 path,
314 n);
06466a7f
ZJS
315
316 r = finish_item(h, id, lang ?: deflang, payload, payload_size);
d4205751
LP
317 if (r < 0)
318 return r;
c7332b08 319
97b11eed 320 lang = mfree(lang);
06466a7f 321 payload_size = 0;
d4205751
LP
322 }
323
324 if (with_language) {
06466a7f 325 char *t;
d4205751 326
06466a7f 327 t = strstrip(line + 2 + 1 + 32 + 1);
baf167ee
ZJS
328 r = catalog_entry_lang(path, n, t, deflang, &lang);
329 if (r < 0)
330 return r;
c7332b08 331 }
d4205751
LP
332
333 got_id = true;
334 empty_line = false;
335 id = jd;
336
d4205751
LP
337 continue;
338 }
339 }
340
341 /* Payload */
baaa35ad
ZJS
342 if (!got_id)
343 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
344 "[%s:%u] Got payload before ID.",
345 path, n);
d4205751 346
06466a7f
ZJS
347 line_len = strlen(line);
348 if (!GREEDY_REALLOC(payload, payload_allocated,
349 payload_size + (empty_line ? 1 : 0) + line_len + 1 + 1))
d4205751
LP
350 return log_oom();
351
06466a7f
ZJS
352 if (empty_line)
353 payload[payload_size++] = '\n';
354 memcpy(payload + payload_size, line, line_len);
355 payload_size += line_len;
356 payload[payload_size++] = '\n';
357 payload[payload_size] = '\0';
d4205751 358
d4205751
LP
359 empty_line = false;
360 }
361
362 if (got_id) {
baaa35ad
ZJS
363 if (payload_size == 0)
364 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
365 "[%s:%u] No payload text.",
366 path, n);
06466a7f
ZJS
367
368 r = finish_item(h, id, lang ?: deflang, payload, payload_size);
d4205751
LP
369 if (r < 0)
370 return r;
371 }
372
373 return 0;
374}
375
3f1e2714
LP
376static int64_t write_catalog(
377 const char *database,
378 struct strbuf *sb,
379 CatalogItem *items,
380 size_t n) {
381
844ec79b 382 _cleanup_fclose_ FILE *w = NULL;
3f1e2714
LP
383 _cleanup_free_ char *p = NULL;
384 CatalogHeader header;
844ec79b 385 size_t k;
3f1e2714 386 int r;
844ec79b 387
3f1e2714 388 r = mkdir_parents(database, 0755);
eb56eb9b 389 if (r < 0)
3f1e2714 390 return log_error_errno(r, "Failed to create parent directories of %s: %m", database);
844ec79b
ZJS
391
392 r = fopen_temporary(database, &w, &p);
eb56eb9b
MS
393 if (r < 0)
394 return log_error_errno(r, "Failed to open database for writing: %s: %m",
395 database);
844ec79b 396
5432d8a1
LP
397 header = (CatalogHeader) {
398 .signature = CATALOG_SIGNATURE,
399 .header_size = htole64(ALIGN_TO(sizeof(CatalogHeader), 8)),
400 .catalog_item_size = htole64(sizeof(CatalogItem)),
401 .n_items = htole64(n),
402 };
80343dc1 403
844ec79b
ZJS
404 r = -EIO;
405
406 k = fwrite(&header, 1, sizeof(header), w);
407 if (k != sizeof(header)) {
408 log_error("%s: failed to write header.", p);
409 goto error;
410 }
411
412 k = fwrite(items, 1, n * sizeof(CatalogItem), w);
413 if (k != n * sizeof(CatalogItem)) {
414 log_error("%s: failed to write database.", p);
415 goto error;
416 }
417
418 k = fwrite(sb->buf, 1, sb->len, w);
419 if (k != sb->len) {
420 log_error("%s: failed to write strings.", p);
421 goto error;
422 }
423
dacd6cee
LP
424 r = fflush_and_check(w);
425 if (r < 0) {
426 log_error_errno(r, "%s: failed to write database: %m", p);
844ec79b
ZJS
427 goto error;
428 }
429
3d477d61 430 (void) fchmod(fileno(w), 0644);
844ec79b
ZJS
431
432 if (rename(p, database) < 0) {
dacd6cee 433 r = log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
844ec79b
ZJS
434 goto error;
435 }
436
77ba8233 437 return ftello(w);
844ec79b
ZJS
438
439error:
dacd6cee 440 (void) unlink(p);
844ec79b
ZJS
441 return r;
442}
443
444int catalog_update(const char* database, const char* root, const char* const* dirs) {
d4205751 445 _cleanup_strv_free_ char **files = NULL;
d4205751 446 char **f;
f201daec 447 _cleanup_(strbuf_cleanupp) struct strbuf *sb = NULL;
a95686bb 448 _cleanup_ordered_hashmap_free_free_free_ OrderedHashmap *h = NULL;
d4205751 449 _cleanup_free_ CatalogItem *items = NULL;
dbae138d
SW
450 ssize_t offset;
451 char *payload;
d4205751 452 CatalogItem *i;
d4205751
LP
453 Iterator j;
454 unsigned n;
77ba8233
MS
455 int r;
456 int64_t sz;
d4205751 457
a95686bb 458 h = ordered_hashmap_new(&catalog_hash_ops);
d4205751 459 sb = strbuf_new();
f201daec
ZJS
460 if (!h || !sb)
461 return log_oom();
d4205751 462
b5084605 463 r = conf_files_list_strv(&files, ".catalog", root, 0, dirs);
f201daec
ZJS
464 if (r < 0)
465 return log_error_errno(r, "Failed to get catalog files: %m");
d4205751
LP
466
467 STRV_FOREACH(f, files) {
e3b9d9c8 468 log_debug("Reading file '%s'", *f);
dbae138d 469 r = catalog_import_file(h, *f);
f201daec
ZJS
470 if (r < 0)
471 return log_error_errno(r, "Failed to import file '%s': %m", *f);
d4205751
LP
472 }
473
a95686bb 474 if (ordered_hashmap_size(h) <= 0) {
d4205751 475 log_info("No items in catalog.");
f201daec 476 return 0;
80343dc1 477 } else
a95686bb 478 log_debug("Found %u items in catalog.", ordered_hashmap_size(h));
d4205751 479
a95686bb 480 items = new(CatalogItem, ordered_hashmap_size(h));
f201daec
ZJS
481 if (!items)
482 return log_oom();
d4205751
LP
483
484 n = 0;
a95686bb 485 ORDERED_HASHMAP_FOREACH_KEY(payload, i, h, j) {
18cd5fe9
ZJS
486 log_debug("Found " SD_ID128_FORMAT_STR ", language %s",
487 SD_ID128_FORMAT_VAL(i->id),
488 isempty(i->language) ? "C" : i->language);
dbae138d
SW
489
490 offset = strbuf_add_string(sb, payload, strlen(payload));
f201daec
ZJS
491 if (offset < 0)
492 return log_oom();
493
dbae138d 494 i->offset = htole64((uint64_t) offset);
d4205751
LP
495 items[n++] = *i;
496 }
497
a95686bb 498 assert(n == ordered_hashmap_size(h));
93bab288 499 typesafe_qsort(items, n, catalog_compare_func);
d4205751 500
dbae138d
SW
501 strbuf_complete(sb);
502
77ba8233
MS
503 sz = write_catalog(database, sb, items, n);
504 if (sz < 0)
f201daec 505 return log_error_errno(sz, "Failed to write %s: %m", database);
d4205751 506
f201daec
ZJS
507 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64" total size.",
508 database, n, sb->len, sz);
509 return 0;
d4205751
LP
510}
511
844ec79b 512static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p) {
0c0e87fc 513 _cleanup_close_ int fd = -1;
d4205751 514 const CatalogHeader *h;
d4205751 515 struct stat st;
0c0e87fc 516 void *p;
d4205751
LP
517
518 assert(_fd);
519 assert(_st);
520 assert(_p);
521
844ec79b 522 fd = open(database, O_RDONLY|O_CLOEXEC);
d4205751
LP
523 if (fd < 0)
524 return -errno;
525
0c0e87fc 526 if (fstat(fd, &st) < 0)
d4205751 527 return -errno;
d4205751 528
0c0e87fc 529 if (st.st_size < (off_t) sizeof(CatalogHeader))
d4205751 530 return -EINVAL;
d4205751
LP
531
532 p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0);
0c0e87fc 533 if (p == MAP_FAILED)
d4205751 534 return -errno;
d4205751
LP
535
536 h = p;
5432d8a1 537 if (memcmp(h->signature, (const uint8_t[]) CATALOG_SIGNATURE, sizeof(h->signature)) != 0 ||
83f6936a
LP
538 le64toh(h->header_size) < sizeof(CatalogHeader) ||
539 le64toh(h->catalog_item_size) < sizeof(CatalogItem) ||
d4205751 540 h->incompatible_flags != 0 ||
83f6936a
LP
541 le64toh(h->n_items) <= 0 ||
542 st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
d4205751
LP
543 munmap(p, st.st_size);
544 return -EBADMSG;
545 }
546
0c0e87fc 547 *_fd = TAKE_FD(fd);
d4205751
LP
548 *_st = st;
549 *_p = p;
550
551 return 0;
552}
553
554static const char *find_id(void *p, sd_id128_t id) {
2e38df53 555 CatalogItem *f = NULL, key = { .id = id };
d4205751
LP
556 const CatalogHeader *h = p;
557 const char *loc;
558
d4205751 559 loc = setlocale(LC_MESSAGES, NULL);
00e1adf8
ZJS
560 if (!isempty(loc) && !STR_IN_SET(loc, "C", "POSIX")) {
561 size_t len;
562
563 len = strcspn(loc, ".@");
564 if (len > sizeof(key.language) - 1)
565 log_debug("LC_MESSAGES value too long, ignoring: \"%.*s\"", (int) len, loc);
566 else {
567 strncpy(key.language, loc, len);
568 key.language[len] = '\0';
569
570 f = bsearch(&key,
571 (const uint8_t*) p + le64toh(h->header_size),
572 le64toh(h->n_items),
573 le64toh(h->catalog_item_size),
574 (comparison_fn_t) catalog_compare_func);
575 if (!f) {
576 char *e;
577
578 e = strchr(key.language, '_');
579 if (e) {
580 *e = 0;
581 f = bsearch(&key,
582 (const uint8_t*) p + le64toh(h->header_size),
583 le64toh(h->n_items),
584 le64toh(h->catalog_item_size),
585 (comparison_fn_t) catalog_compare_func);
586 }
d4205751
LP
587 }
588 }
589 }
590
591 if (!f) {
592 zero(key.language);
00e1adf8
ZJS
593 f = bsearch(&key,
594 (const uint8_t*) p + le64toh(h->header_size),
595 le64toh(h->n_items),
596 le64toh(h->catalog_item_size),
597 (comparison_fn_t) catalog_compare_func);
d4205751
LP
598 }
599
600 if (!f)
601 return NULL;
602
603 return (const char*) p +
83f6936a
LP
604 le64toh(h->header_size) +
605 le64toh(h->n_items) * le64toh(h->catalog_item_size) +
606 le64toh(f->offset);
d4205751
LP
607}
608
844ec79b 609int catalog_get(const char* database, sd_id128_t id, char **_text) {
d4205751
LP
610 _cleanup_close_ int fd = -1;
611 void *p = NULL;
a7f7d1bd 612 struct stat st = {};
d4205751
LP
613 char *text = NULL;
614 int r;
615 const char *s;
616
617 assert(_text);
618
844ec79b 619 r = open_mmap(database, &fd, &st, &p);
d4205751
LP
620 if (r < 0)
621 return r;
622
623 s = find_id(p, id);
624 if (!s) {
625 r = -ENOENT;
626 goto finish;
627 }
628
629 text = strdup(s);
630 if (!text) {
631 r = -ENOMEM;
632 goto finish;
633 }
634
635 *_text = text;
636 r = 0;
637
638finish:
639 if (p)
640 munmap(p, st.st_size);
641
642 return r;
643}
644
645static char *find_header(const char *s, const char *header) {
646
647 for (;;) {
dbae138d 648 const char *v;
d4205751
LP
649
650 v = startswith(s, header);
651 if (v) {
652 v += strspn(v, WHITESPACE);
653 return strndup(v, strcspn(v, NEWLINE));
654 }
655
dbae138d 656 if (!next_header(&s))
d4205751 657 return NULL;
d4205751
LP
658 }
659}
660
54b7254c
ZJS
661static void dump_catalog_entry(FILE *f, sd_id128_t id, const char *s, bool oneline) {
662 if (oneline) {
663 _cleanup_free_ char *subject = NULL, *defined_by = NULL;
664
665 subject = find_header(s, "Subject:");
666 defined_by = find_header(s, "Defined-By:");
667
668 fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n",
669 SD_ID128_FORMAT_VAL(id),
670 strna(defined_by), strna(subject));
671 } else
672 fprintf(f, "-- " SD_ID128_FORMAT_STR "\n%s\n",
673 SD_ID128_FORMAT_VAL(id), s);
674}
675
844ec79b 676int catalog_list(FILE *f, const char *database, bool oneline) {
d4205751
LP
677 _cleanup_close_ int fd = -1;
678 void *p = NULL;
679 struct stat st;
680 const CatalogHeader *h;
681 const CatalogItem *items;
682 int r;
683 unsigned n;
684 sd_id128_t last_id;
685 bool last_id_set = false;
686
844ec79b 687 r = open_mmap(database, &fd, &st, &p);
d4205751
LP
688 if (r < 0)
689 return r;
690
691 h = p;
83f6936a 692 items = (const CatalogItem*) ((const uint8_t*) p + le64toh(h->header_size));
d4205751 693
83f6936a 694 for (n = 0; n < le64toh(h->n_items); n++) {
d4205751 695 const char *s;
d4205751
LP
696
697 if (last_id_set && sd_id128_equal(last_id, items[n].id))
698 continue;
699
700 assert_se(s = find_id(p, items[n].id));
701
54b7254c 702 dump_catalog_entry(f, items[n].id, s, oneline);
d4205751
LP
703
704 last_id_set = true;
705 last_id = items[n].id;
706 }
707
708 munmap(p, st.st_size);
709
710 return 0;
711}
54b7254c 712
844ec79b 713int catalog_list_items(FILE *f, const char *database, bool oneline, char **items) {
54b7254c
ZJS
714 char **item;
715 int r = 0;
716
717 STRV_FOREACH(item, items) {
718 sd_id128_t id;
719 int k;
7fd1b19b 720 _cleanup_free_ char *msg = NULL;
54b7254c
ZJS
721
722 k = sd_id128_from_string(*item, &id);
723 if (k < 0) {
e53fc357 724 log_error_errno(k, "Failed to parse id128 '%s': %m", *item);
464264ac 725 if (r == 0)
54b7254c
ZJS
726 r = k;
727 continue;
728 }
729
844ec79b 730 k = catalog_get(database, id, &msg);
54b7254c 731 if (k < 0) {
e53fc357
LP
732 log_full_errno(k == -ENOENT ? LOG_NOTICE : LOG_ERR, k,
733 "Failed to retrieve catalog entry for '%s': %m", *item);
464264ac 734 if (r == 0)
54b7254c
ZJS
735 r = k;
736 continue;
737 }
738
739 dump_catalog_entry(f, id, msg, oneline);
740 }
741
742 return r;
743}