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