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