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