]>
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); | |
c90c39ff | 220 | if (c < 2) |
baaa35ad ZJS |
221 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
222 | "[%s:%u] Language too short.", | |
223 | filename, line); | |
224 | if (c > 31) | |
225 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
226 | "[%s:%u] language too long.", filename, | |
227 | line); | |
baf167ee ZJS |
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) { | |
baaa35ad ZJS |
307 | if (payload_size == 0) |
308 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
309 | "[%s:%u] No payload text.", | |
310 | path, | |
311 | n); | |
06466a7f ZJS |
312 | |
313 | r = finish_item(h, id, lang ?: deflang, payload, payload_size); | |
d4205751 LP |
314 | if (r < 0) |
315 | return r; | |
c7332b08 | 316 | |
97b11eed | 317 | lang = mfree(lang); |
06466a7f | 318 | payload_size = 0; |
d4205751 LP |
319 | } |
320 | ||
321 | if (with_language) { | |
06466a7f | 322 | char *t; |
d4205751 | 323 | |
06466a7f | 324 | t = strstrip(line + 2 + 1 + 32 + 1); |
baf167ee ZJS |
325 | r = catalog_entry_lang(path, n, t, deflang, &lang); |
326 | if (r < 0) | |
327 | return r; | |
c7332b08 | 328 | } |
d4205751 LP |
329 | |
330 | got_id = true; | |
331 | empty_line = false; | |
332 | id = jd; | |
333 | ||
d4205751 LP |
334 | continue; |
335 | } | |
336 | } | |
337 | ||
338 | /* Payload */ | |
baaa35ad ZJS |
339 | if (!got_id) |
340 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
341 | "[%s:%u] Got payload before ID.", | |
342 | path, n); | |
d4205751 | 343 | |
06466a7f ZJS |
344 | line_len = strlen(line); |
345 | if (!GREEDY_REALLOC(payload, payload_allocated, | |
346 | payload_size + (empty_line ? 1 : 0) + line_len + 1 + 1)) | |
d4205751 LP |
347 | return log_oom(); |
348 | ||
06466a7f ZJS |
349 | if (empty_line) |
350 | payload[payload_size++] = '\n'; | |
351 | memcpy(payload + payload_size, line, line_len); | |
352 | payload_size += line_len; | |
353 | payload[payload_size++] = '\n'; | |
354 | payload[payload_size] = '\0'; | |
d4205751 | 355 | |
d4205751 LP |
356 | empty_line = false; |
357 | } | |
358 | ||
359 | if (got_id) { | |
baaa35ad ZJS |
360 | if (payload_size == 0) |
361 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
362 | "[%s:%u] No payload text.", | |
363 | path, n); | |
06466a7f ZJS |
364 | |
365 | r = finish_item(h, id, lang ?: deflang, payload, payload_size); | |
d4205751 LP |
366 | if (r < 0) |
367 | return r; | |
368 | } | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
77ba8233 MS |
373 | static int64_t write_catalog(const char *database, struct strbuf *sb, |
374 | CatalogItem *items, size_t n) { | |
844ec79b ZJS |
375 | CatalogHeader header; |
376 | _cleanup_fclose_ FILE *w = NULL; | |
377 | int r; | |
7fd1b19b | 378 | _cleanup_free_ char *d, *p = NULL; |
844ec79b ZJS |
379 | size_t k; |
380 | ||
381 | d = dirname_malloc(database); | |
382 | if (!d) | |
383 | return log_oom(); | |
384 | ||
385 | r = mkdir_p(d, 0775); | |
eb56eb9b MS |
386 | if (r < 0) |
387 | return log_error_errno(r, "Recursive mkdir %s: %m", d); | |
844ec79b ZJS |
388 | |
389 | r = fopen_temporary(database, &w, &p); | |
eb56eb9b MS |
390 | if (r < 0) |
391 | return log_error_errno(r, "Failed to open database for writing: %s: %m", | |
392 | database); | |
844ec79b ZJS |
393 | |
394 | zero(header); | |
395 | memcpy(header.signature, CATALOG_SIGNATURE, sizeof(header.signature)); | |
396 | header.header_size = htole64(ALIGN_TO(sizeof(CatalogHeader), 8)); | |
397 | header.catalog_item_size = htole64(sizeof(CatalogItem)); | |
8d2ecdb2 | 398 | header.n_items = htole64(n); |
80343dc1 | 399 | |
844ec79b ZJS |
400 | r = -EIO; |
401 | ||
402 | k = fwrite(&header, 1, sizeof(header), w); | |
403 | if (k != sizeof(header)) { | |
404 | log_error("%s: failed to write header.", p); | |
405 | goto error; | |
406 | } | |
407 | ||
408 | k = fwrite(items, 1, n * sizeof(CatalogItem), w); | |
409 | if (k != n * sizeof(CatalogItem)) { | |
410 | log_error("%s: failed to write database.", p); | |
411 | goto error; | |
412 | } | |
413 | ||
414 | k = fwrite(sb->buf, 1, sb->len, w); | |
415 | if (k != sb->len) { | |
416 | log_error("%s: failed to write strings.", p); | |
417 | goto error; | |
418 | } | |
419 | ||
dacd6cee LP |
420 | r = fflush_and_check(w); |
421 | if (r < 0) { | |
422 | log_error_errno(r, "%s: failed to write database: %m", p); | |
844ec79b ZJS |
423 | goto error; |
424 | } | |
425 | ||
426 | fchmod(fileno(w), 0644); | |
427 | ||
428 | if (rename(p, database) < 0) { | |
dacd6cee | 429 | r = log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database); |
844ec79b ZJS |
430 | goto error; |
431 | } | |
432 | ||
77ba8233 | 433 | return ftello(w); |
844ec79b ZJS |
434 | |
435 | error: | |
dacd6cee | 436 | (void) unlink(p); |
844ec79b ZJS |
437 | return r; |
438 | } | |
439 | ||
440 | int catalog_update(const char* database, const char* root, const char* const* dirs) { | |
d4205751 | 441 | _cleanup_strv_free_ char **files = NULL; |
d4205751 | 442 | char **f; |
f201daec | 443 | _cleanup_(strbuf_cleanupp) struct strbuf *sb = NULL; |
dbae138d | 444 | _cleanup_hashmap_free_free_free_ Hashmap *h = NULL; |
d4205751 | 445 | _cleanup_free_ CatalogItem *items = NULL; |
dbae138d SW |
446 | ssize_t offset; |
447 | char *payload; | |
d4205751 | 448 | CatalogItem *i; |
d4205751 LP |
449 | Iterator j; |
450 | unsigned n; | |
77ba8233 MS |
451 | int r; |
452 | int64_t sz; | |
d4205751 | 453 | |
d5099efc | 454 | h = hashmap_new(&catalog_hash_ops); |
d4205751 | 455 | sb = strbuf_new(); |
f201daec ZJS |
456 | if (!h || !sb) |
457 | return log_oom(); | |
d4205751 | 458 | |
b5084605 | 459 | r = conf_files_list_strv(&files, ".catalog", root, 0, dirs); |
f201daec ZJS |
460 | if (r < 0) |
461 | return log_error_errno(r, "Failed to get catalog files: %m"); | |
d4205751 LP |
462 | |
463 | STRV_FOREACH(f, files) { | |
e3b9d9c8 | 464 | log_debug("Reading file '%s'", *f); |
dbae138d | 465 | r = catalog_import_file(h, *f); |
f201daec ZJS |
466 | if (r < 0) |
467 | return log_error_errno(r, "Failed to import file '%s': %m", *f); | |
d4205751 LP |
468 | } |
469 | ||
470 | if (hashmap_size(h) <= 0) { | |
471 | log_info("No items in catalog."); | |
f201daec | 472 | return 0; |
80343dc1 ZJS |
473 | } else |
474 | log_debug("Found %u items in catalog.", hashmap_size(h)); | |
d4205751 | 475 | |
d4205751 | 476 | items = new(CatalogItem, hashmap_size(h)); |
f201daec ZJS |
477 | if (!items) |
478 | return log_oom(); | |
d4205751 LP |
479 | |
480 | n = 0; | |
dbae138d | 481 | HASHMAP_FOREACH_KEY(payload, i, h, j) { |
18cd5fe9 ZJS |
482 | log_debug("Found " SD_ID128_FORMAT_STR ", language %s", |
483 | SD_ID128_FORMAT_VAL(i->id), | |
484 | isempty(i->language) ? "C" : i->language); | |
dbae138d SW |
485 | |
486 | offset = strbuf_add_string(sb, payload, strlen(payload)); | |
f201daec ZJS |
487 | if (offset < 0) |
488 | return log_oom(); | |
489 | ||
dbae138d | 490 | i->offset = htole64((uint64_t) offset); |
d4205751 LP |
491 | items[n++] = *i; |
492 | } | |
493 | ||
494 | assert(n == hashmap_size(h)); | |
93bab288 | 495 | typesafe_qsort(items, n, catalog_compare_func); |
d4205751 | 496 | |
dbae138d SW |
497 | strbuf_complete(sb); |
498 | ||
77ba8233 MS |
499 | sz = write_catalog(database, sb, items, n); |
500 | if (sz < 0) | |
f201daec | 501 | return log_error_errno(sz, "Failed to write %s: %m", database); |
d4205751 | 502 | |
f201daec ZJS |
503 | log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64" total size.", |
504 | database, n, sb->len, sz); | |
505 | return 0; | |
d4205751 LP |
506 | } |
507 | ||
844ec79b | 508 | static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p) { |
d4205751 LP |
509 | const CatalogHeader *h; |
510 | int fd; | |
511 | void *p; | |
512 | struct stat st; | |
513 | ||
514 | assert(_fd); | |
515 | assert(_st); | |
516 | assert(_p); | |
517 | ||
844ec79b | 518 | fd = open(database, O_RDONLY|O_CLOEXEC); |
d4205751 LP |
519 | if (fd < 0) |
520 | return -errno; | |
521 | ||
522 | if (fstat(fd, &st) < 0) { | |
03e334a1 | 523 | safe_close(fd); |
d4205751 LP |
524 | return -errno; |
525 | } | |
526 | ||
527 | if (st.st_size < (off_t) sizeof(CatalogHeader)) { | |
03e334a1 | 528 | safe_close(fd); |
d4205751 LP |
529 | return -EINVAL; |
530 | } | |
531 | ||
532 | p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0); | |
533 | if (p == MAP_FAILED) { | |
03e334a1 | 534 | safe_close(fd); |
d4205751 LP |
535 | return -errno; |
536 | } | |
537 | ||
538 | h = p; | |
539 | if (memcmp(h->signature, CATALOG_SIGNATURE, sizeof(h->signature)) != 0 || | |
83f6936a LP |
540 | le64toh(h->header_size) < sizeof(CatalogHeader) || |
541 | le64toh(h->catalog_item_size) < sizeof(CatalogItem) || | |
d4205751 | 542 | h->incompatible_flags != 0 || |
83f6936a LP |
543 | le64toh(h->n_items) <= 0 || |
544 | st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) { | |
03e334a1 | 545 | safe_close(fd); |
d4205751 LP |
546 | munmap(p, st.st_size); |
547 | return -EBADMSG; | |
548 | } | |
549 | ||
550 | *_fd = fd; | |
551 | *_st = st; | |
552 | *_p = p; | |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
557 | static const char *find_id(void *p, sd_id128_t id) { | |
2e38df53 | 558 | CatalogItem *f = NULL, key = { .id = id }; |
d4205751 LP |
559 | const CatalogHeader *h = p; |
560 | const char *loc; | |
561 | ||
d4205751 LP |
562 | loc = setlocale(LC_MESSAGES, NULL); |
563 | if (loc && loc[0] && !streq(loc, "C") && !streq(loc, "POSIX")) { | |
564 | strncpy(key.language, loc, sizeof(key.language)); | |
565 | key.language[strcspn(key.language, ".@")] = 0; | |
566 | ||
93bab288 | 567 | 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 |
568 | if (!f) { |
569 | char *e; | |
570 | ||
571 | e = strchr(key.language, '_'); | |
572 | if (e) { | |
573 | *e = 0; | |
93bab288 | 574 | 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 |
575 | } |
576 | } | |
577 | } | |
578 | ||
579 | if (!f) { | |
580 | zero(key.language); | |
93bab288 | 581 | 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 |
582 | } |
583 | ||
584 | if (!f) | |
585 | return NULL; | |
586 | ||
587 | return (const char*) p + | |
83f6936a LP |
588 | le64toh(h->header_size) + |
589 | le64toh(h->n_items) * le64toh(h->catalog_item_size) + | |
590 | le64toh(f->offset); | |
d4205751 LP |
591 | } |
592 | ||
844ec79b | 593 | int catalog_get(const char* database, sd_id128_t id, char **_text) { |
d4205751 LP |
594 | _cleanup_close_ int fd = -1; |
595 | void *p = NULL; | |
a7f7d1bd | 596 | struct stat st = {}; |
d4205751 LP |
597 | char *text = NULL; |
598 | int r; | |
599 | const char *s; | |
600 | ||
601 | assert(_text); | |
602 | ||
844ec79b | 603 | r = open_mmap(database, &fd, &st, &p); |
d4205751 LP |
604 | if (r < 0) |
605 | return r; | |
606 | ||
607 | s = find_id(p, id); | |
608 | if (!s) { | |
609 | r = -ENOENT; | |
610 | goto finish; | |
611 | } | |
612 | ||
613 | text = strdup(s); | |
614 | if (!text) { | |
615 | r = -ENOMEM; | |
616 | goto finish; | |
617 | } | |
618 | ||
619 | *_text = text; | |
620 | r = 0; | |
621 | ||
622 | finish: | |
623 | if (p) | |
624 | munmap(p, st.st_size); | |
625 | ||
626 | return r; | |
627 | } | |
628 | ||
629 | static char *find_header(const char *s, const char *header) { | |
630 | ||
631 | for (;;) { | |
dbae138d | 632 | const char *v; |
d4205751 LP |
633 | |
634 | v = startswith(s, header); | |
635 | if (v) { | |
636 | v += strspn(v, WHITESPACE); | |
637 | return strndup(v, strcspn(v, NEWLINE)); | |
638 | } | |
639 | ||
dbae138d | 640 | if (!next_header(&s)) |
d4205751 | 641 | return NULL; |
d4205751 LP |
642 | } |
643 | } | |
644 | ||
54b7254c ZJS |
645 | static void dump_catalog_entry(FILE *f, sd_id128_t id, const char *s, bool oneline) { |
646 | if (oneline) { | |
647 | _cleanup_free_ char *subject = NULL, *defined_by = NULL; | |
648 | ||
649 | subject = find_header(s, "Subject:"); | |
650 | defined_by = find_header(s, "Defined-By:"); | |
651 | ||
652 | fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n", | |
653 | SD_ID128_FORMAT_VAL(id), | |
654 | strna(defined_by), strna(subject)); | |
655 | } else | |
656 | fprintf(f, "-- " SD_ID128_FORMAT_STR "\n%s\n", | |
657 | SD_ID128_FORMAT_VAL(id), s); | |
658 | } | |
659 | ||
844ec79b | 660 | int catalog_list(FILE *f, const char *database, bool oneline) { |
d4205751 LP |
661 | _cleanup_close_ int fd = -1; |
662 | void *p = NULL; | |
663 | struct stat st; | |
664 | const CatalogHeader *h; | |
665 | const CatalogItem *items; | |
666 | int r; | |
667 | unsigned n; | |
668 | sd_id128_t last_id; | |
669 | bool last_id_set = false; | |
670 | ||
844ec79b | 671 | r = open_mmap(database, &fd, &st, &p); |
d4205751 LP |
672 | if (r < 0) |
673 | return r; | |
674 | ||
675 | h = p; | |
83f6936a | 676 | items = (const CatalogItem*) ((const uint8_t*) p + le64toh(h->header_size)); |
d4205751 | 677 | |
83f6936a | 678 | for (n = 0; n < le64toh(h->n_items); n++) { |
d4205751 | 679 | const char *s; |
d4205751 LP |
680 | |
681 | if (last_id_set && sd_id128_equal(last_id, items[n].id)) | |
682 | continue; | |
683 | ||
684 | assert_se(s = find_id(p, items[n].id)); | |
685 | ||
54b7254c | 686 | dump_catalog_entry(f, items[n].id, s, oneline); |
d4205751 LP |
687 | |
688 | last_id_set = true; | |
689 | last_id = items[n].id; | |
690 | } | |
691 | ||
692 | munmap(p, st.st_size); | |
693 | ||
694 | return 0; | |
695 | } | |
54b7254c | 696 | |
844ec79b | 697 | int catalog_list_items(FILE *f, const char *database, bool oneline, char **items) { |
54b7254c ZJS |
698 | char **item; |
699 | int r = 0; | |
700 | ||
701 | STRV_FOREACH(item, items) { | |
702 | sd_id128_t id; | |
703 | int k; | |
7fd1b19b | 704 | _cleanup_free_ char *msg = NULL; |
54b7254c ZJS |
705 | |
706 | k = sd_id128_from_string(*item, &id); | |
707 | if (k < 0) { | |
e53fc357 | 708 | log_error_errno(k, "Failed to parse id128 '%s': %m", *item); |
464264ac | 709 | if (r == 0) |
54b7254c ZJS |
710 | r = k; |
711 | continue; | |
712 | } | |
713 | ||
844ec79b | 714 | k = catalog_get(database, id, &msg); |
54b7254c | 715 | if (k < 0) { |
e53fc357 LP |
716 | log_full_errno(k == -ENOENT ? LOG_NOTICE : LOG_ERR, k, |
717 | "Failed to retrieve catalog entry for '%s': %m", *item); | |
464264ac | 718 | if (r == 0) |
54b7254c ZJS |
719 | r = k; |
720 | continue; | |
721 | } | |
722 | ||
723 | dump_catalog_entry(f, id, msg, oneline); | |
724 | } | |
725 | ||
726 | return r; | |
727 | } |