]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/catalog.c
catalog: don't make catalog_entry_lang() clobber output params on failure
[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
ZJS
224 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
225 "[%s:%u] Language too short.",
226 filename, line);
227 if (c > 31)
228 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
229 "[%s:%u] language too long.", filename,
230 line);
baf167ee
ZJS
231
232 if (deflang) {
233 if (streq(t, deflang)) {
234 log_warning("[%s:%u] language specified unnecessarily",
235 filename, line);
236 return 0;
237 } else
238 log_warning("[%s:%u] language differs from default for file",
239 filename, line);
240 }
241
bbe80432
LP
242 z = strdup(t);
243 if (!z)
244 return -ENOMEM;
baf167ee 245
bbe80432 246 *ret = z;
baf167ee
ZJS
247 return 0;
248}
249
dbae138d 250int catalog_import_file(Hashmap *h, const char *path) {
d4205751
LP
251 _cleanup_fclose_ FILE *f = NULL;
252 _cleanup_free_ char *payload = NULL;
06466a7f 253 size_t payload_size = 0, payload_allocated = 0;
d4205751
LP
254 unsigned n = 0;
255 sd_id128_t id;
c7332b08 256 _cleanup_free_ char *deflang = NULL, *lang = NULL;
d4205751
LP
257 bool got_id = false, empty_line = true;
258 int r;
259
260 assert(h);
d4205751
LP
261 assert(path);
262
263 f = fopen(path, "re");
4a62c710
MS
264 if (!f)
265 return log_error_errno(errno, "Failed to open file %s: %m", path);
d4205751 266
c7332b08
ZJS
267 r = catalog_file_lang(path, &deflang);
268 if (r < 0)
709f6e46 269 log_error_errno(r, "Failed to determine language for file %s: %m", path);
c7332b08
ZJS
270 if (r == 1)
271 log_debug("File %s has language %s.", path, deflang);
272
d4205751 273 for (;;) {
df2b06d1 274 _cleanup_free_ char *line = NULL;
06466a7f 275 size_t line_len;
d4205751 276
df2b06d1
LP
277 r = read_line(f, LONG_LINE_MAX, &line);
278 if (r < 0)
279 return log_error_errno(r, "Failed to read file %s: %m", path);
280 if (r == 0)
281 break;
d4205751
LP
282
283 n++;
284
df2b06d1 285 if (isempty(line)) {
d4205751
LP
286 empty_line = true;
287 continue;
288 }
289
df2b06d1 290 if (strchr(COMMENTS, line[0]))
d4205751
LP
291 continue;
292
293 if (empty_line &&
294 strlen(line) >= 2+1+32 &&
295 line[0] == '-' &&
296 line[1] == '-' &&
297 line[2] == ' ' &&
4c701096 298 IN_SET(line[2+1+32], ' ', '\0')) {
d4205751
LP
299
300 bool with_language;
301 sd_id128_t jd;
302
303 /* New entry */
304
18cd5fe9
ZJS
305 with_language = line[2+1+32] != '\0';
306 line[2+1+32] = '\0';
d4205751
LP
307
308 if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
309
310 if (got_id) {
baaa35ad
ZJS
311 if (payload_size == 0)
312 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
313 "[%s:%u] No payload text.",
314 path,
315 n);
06466a7f
ZJS
316
317 r = finish_item(h, id, lang ?: deflang, payload, payload_size);
d4205751
LP
318 if (r < 0)
319 return r;
c7332b08 320
97b11eed 321 lang = mfree(lang);
06466a7f 322 payload_size = 0;
d4205751
LP
323 }
324
325 if (with_language) {
06466a7f 326 char *t;
d4205751 327
06466a7f 328 t = strstrip(line + 2 + 1 + 32 + 1);
baf167ee
ZJS
329 r = catalog_entry_lang(path, n, t, deflang, &lang);
330 if (r < 0)
331 return r;
c7332b08 332 }
d4205751
LP
333
334 got_id = true;
335 empty_line = false;
336 id = jd;
337
d4205751
LP
338 continue;
339 }
340 }
341
342 /* Payload */
baaa35ad
ZJS
343 if (!got_id)
344 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
345 "[%s:%u] Got payload before ID.",
346 path, n);
d4205751 347
06466a7f
ZJS
348 line_len = strlen(line);
349 if (!GREEDY_REALLOC(payload, payload_allocated,
350 payload_size + (empty_line ? 1 : 0) + line_len + 1 + 1))
d4205751
LP
351 return log_oom();
352
06466a7f
ZJS
353 if (empty_line)
354 payload[payload_size++] = '\n';
355 memcpy(payload + payload_size, line, line_len);
356 payload_size += line_len;
357 payload[payload_size++] = '\n';
358 payload[payload_size] = '\0';
d4205751 359
d4205751
LP
360 empty_line = false;
361 }
362
363 if (got_id) {
baaa35ad
ZJS
364 if (payload_size == 0)
365 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
366 "[%s:%u] No payload text.",
367 path, n);
06466a7f
ZJS
368
369 r = finish_item(h, id, lang ?: deflang, payload, payload_size);
d4205751
LP
370 if (r < 0)
371 return r;
372 }
373
374 return 0;
375}
376
77ba8233
MS
377static int64_t write_catalog(const char *database, struct strbuf *sb,
378 CatalogItem *items, size_t n) {
844ec79b
ZJS
379 CatalogHeader header;
380 _cleanup_fclose_ FILE *w = NULL;
381 int r;
7fd1b19b 382 _cleanup_free_ char *d, *p = NULL;
844ec79b
ZJS
383 size_t k;
384
385 d = dirname_malloc(database);
386 if (!d)
387 return log_oom();
388
389 r = mkdir_p(d, 0775);
eb56eb9b
MS
390 if (r < 0)
391 return log_error_errno(r, "Recursive mkdir %s: %m", d);
844ec79b
ZJS
392
393 r = fopen_temporary(database, &w, &p);
eb56eb9b
MS
394 if (r < 0)
395 return log_error_errno(r, "Failed to open database for writing: %s: %m",
396 database);
844ec79b
ZJS
397
398 zero(header);
399 memcpy(header.signature, CATALOG_SIGNATURE, sizeof(header.signature));
400 header.header_size = htole64(ALIGN_TO(sizeof(CatalogHeader), 8));
401 header.catalog_item_size = htole64(sizeof(CatalogItem));
8d2ecdb2 402 header.n_items = htole64(n);
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
430 fchmod(fileno(w), 0644);
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;
dbae138d 448 _cleanup_hashmap_free_free_free_ Hashmap *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
d5099efc 458 h = 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
474 if (hashmap_size(h) <= 0) {
475 log_info("No items in catalog.");
f201daec 476 return 0;
80343dc1
ZJS
477 } else
478 log_debug("Found %u items in catalog.", hashmap_size(h));
d4205751 479
d4205751 480 items = new(CatalogItem, hashmap_size(h));
f201daec
ZJS
481 if (!items)
482 return log_oom();
d4205751
LP
483
484 n = 0;
dbae138d 485 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
498 assert(n == 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) {
d4205751
LP
513 const CatalogHeader *h;
514 int fd;
515 void *p;
516 struct stat st;
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
526 if (fstat(fd, &st) < 0) {
03e334a1 527 safe_close(fd);
d4205751
LP
528 return -errno;
529 }
530
531 if (st.st_size < (off_t) sizeof(CatalogHeader)) {
03e334a1 532 safe_close(fd);
d4205751
LP
533 return -EINVAL;
534 }
535
536 p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0);
537 if (p == MAP_FAILED) {
03e334a1 538 safe_close(fd);
d4205751
LP
539 return -errno;
540 }
541
542 h = p;
543 if (memcmp(h->signature, CATALOG_SIGNATURE, sizeof(h->signature)) != 0 ||
83f6936a
LP
544 le64toh(h->header_size) < sizeof(CatalogHeader) ||
545 le64toh(h->catalog_item_size) < sizeof(CatalogItem) ||
d4205751 546 h->incompatible_flags != 0 ||
83f6936a
LP
547 le64toh(h->n_items) <= 0 ||
548 st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
03e334a1 549 safe_close(fd);
d4205751
LP
550 munmap(p, st.st_size);
551 return -EBADMSG;
552 }
553
554 *_fd = fd;
555 *_st = st;
556 *_p = p;
557
558 return 0;
559}
560
561static const char *find_id(void *p, sd_id128_t id) {
2e38df53 562 CatalogItem *f = NULL, key = { .id = id };
d4205751
LP
563 const CatalogHeader *h = p;
564 const char *loc;
565
d4205751 566 loc = setlocale(LC_MESSAGES, NULL);
00e1adf8
ZJS
567 if (!isempty(loc) && !STR_IN_SET(loc, "C", "POSIX")) {
568 size_t len;
569
570 len = strcspn(loc, ".@");
571 if (len > sizeof(key.language) - 1)
572 log_debug("LC_MESSAGES value too long, ignoring: \"%.*s\"", (int) len, loc);
573 else {
574 strncpy(key.language, loc, len);
575 key.language[len] = '\0';
576
577 f = bsearch(&key,
578 (const uint8_t*) p + le64toh(h->header_size),
579 le64toh(h->n_items),
580 le64toh(h->catalog_item_size),
581 (comparison_fn_t) catalog_compare_func);
582 if (!f) {
583 char *e;
584
585 e = strchr(key.language, '_');
586 if (e) {
587 *e = 0;
588 f = bsearch(&key,
589 (const uint8_t*) p + le64toh(h->header_size),
590 le64toh(h->n_items),
591 le64toh(h->catalog_item_size),
592 (comparison_fn_t) catalog_compare_func);
593 }
d4205751
LP
594 }
595 }
596 }
597
598 if (!f) {
599 zero(key.language);
00e1adf8
ZJS
600 f = bsearch(&key,
601 (const uint8_t*) p + le64toh(h->header_size),
602 le64toh(h->n_items),
603 le64toh(h->catalog_item_size),
604 (comparison_fn_t) catalog_compare_func);
d4205751
LP
605 }
606
607 if (!f)
608 return NULL;
609
610 return (const char*) p +
83f6936a
LP
611 le64toh(h->header_size) +
612 le64toh(h->n_items) * le64toh(h->catalog_item_size) +
613 le64toh(f->offset);
d4205751
LP
614}
615
844ec79b 616int catalog_get(const char* database, sd_id128_t id, char **_text) {
d4205751
LP
617 _cleanup_close_ int fd = -1;
618 void *p = NULL;
a7f7d1bd 619 struct stat st = {};
d4205751
LP
620 char *text = NULL;
621 int r;
622 const char *s;
623
624 assert(_text);
625
844ec79b 626 r = open_mmap(database, &fd, &st, &p);
d4205751
LP
627 if (r < 0)
628 return r;
629
630 s = find_id(p, id);
631 if (!s) {
632 r = -ENOENT;
633 goto finish;
634 }
635
636 text = strdup(s);
637 if (!text) {
638 r = -ENOMEM;
639 goto finish;
640 }
641
642 *_text = text;
643 r = 0;
644
645finish:
646 if (p)
647 munmap(p, st.st_size);
648
649 return r;
650}
651
652static char *find_header(const char *s, const char *header) {
653
654 for (;;) {
dbae138d 655 const char *v;
d4205751
LP
656
657 v = startswith(s, header);
658 if (v) {
659 v += strspn(v, WHITESPACE);
660 return strndup(v, strcspn(v, NEWLINE));
661 }
662
dbae138d 663 if (!next_header(&s))
d4205751 664 return NULL;
d4205751
LP
665 }
666}
667
54b7254c
ZJS
668static void dump_catalog_entry(FILE *f, sd_id128_t id, const char *s, bool oneline) {
669 if (oneline) {
670 _cleanup_free_ char *subject = NULL, *defined_by = NULL;
671
672 subject = find_header(s, "Subject:");
673 defined_by = find_header(s, "Defined-By:");
674
675 fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n",
676 SD_ID128_FORMAT_VAL(id),
677 strna(defined_by), strna(subject));
678 } else
679 fprintf(f, "-- " SD_ID128_FORMAT_STR "\n%s\n",
680 SD_ID128_FORMAT_VAL(id), s);
681}
682
844ec79b 683int catalog_list(FILE *f, const char *database, bool oneline) {
d4205751
LP
684 _cleanup_close_ int fd = -1;
685 void *p = NULL;
686 struct stat st;
687 const CatalogHeader *h;
688 const CatalogItem *items;
689 int r;
690 unsigned n;
691 sd_id128_t last_id;
692 bool last_id_set = false;
693
844ec79b 694 r = open_mmap(database, &fd, &st, &p);
d4205751
LP
695 if (r < 0)
696 return r;
697
698 h = p;
83f6936a 699 items = (const CatalogItem*) ((const uint8_t*) p + le64toh(h->header_size));
d4205751 700
83f6936a 701 for (n = 0; n < le64toh(h->n_items); n++) {
d4205751 702 const char *s;
d4205751
LP
703
704 if (last_id_set && sd_id128_equal(last_id, items[n].id))
705 continue;
706
707 assert_se(s = find_id(p, items[n].id));
708
54b7254c 709 dump_catalog_entry(f, items[n].id, s, oneline);
d4205751
LP
710
711 last_id_set = true;
712 last_id = items[n].id;
713 }
714
715 munmap(p, st.st_size);
716
717 return 0;
718}
54b7254c 719
844ec79b 720int catalog_list_items(FILE *f, const char *database, bool oneline, char **items) {
54b7254c
ZJS
721 char **item;
722 int r = 0;
723
724 STRV_FOREACH(item, items) {
725 sd_id128_t id;
726 int k;
7fd1b19b 727 _cleanup_free_ char *msg = NULL;
54b7254c
ZJS
728
729 k = sd_id128_from_string(*item, &id);
730 if (k < 0) {
e53fc357 731 log_error_errno(k, "Failed to parse id128 '%s': %m", *item);
464264ac 732 if (r == 0)
54b7254c
ZJS
733 r = k;
734 continue;
735 }
736
844ec79b 737 k = catalog_get(database, id, &msg);
54b7254c 738 if (k < 0) {
e53fc357
LP
739 log_full_errno(k == -ENOENT ? LOG_NOTICE : LOG_ERR, k,
740 "Failed to retrieve catalog entry for '%s': %m", *item);
464264ac 741 if (r == 0)
54b7254c
ZJS
742 r = k;
743 continue;
744 }
745
746 dump_catalog_entry(f, id, msg, oneline);
747 }
748
749 return r;
750}