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