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