2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "alloc-util.h"
32 #include "conf-files.h"
38 #include "path-util.h"
39 #include "siphash24.h"
40 #include "sparse-endian.h"
42 #include "string-util.h"
46 const char * const catalog_file_dirs
[] = {
47 "/usr/local/lib/systemd/catalog/",
48 "/usr/lib/systemd/catalog/",
52 #define CATALOG_SIGNATURE (uint8_t[]) { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
54 typedef struct CatalogHeader
{
55 uint8_t signature
[8]; /* "RHHHKSLP" */
56 le32_t compatible_flags
;
57 le32_t incompatible_flags
;
60 le64_t catalog_item_size
;
63 typedef struct CatalogItem
{
69 static void catalog_hash_func(const void *p
, struct siphash
*state
) {
70 const CatalogItem
*i
= p
;
72 siphash24_compress(&i
->id
, sizeof(i
->id
), state
);
73 siphash24_compress(i
->language
, strlen(i
->language
), state
);
76 static int catalog_compare_func(const void *a
, const void *b
) {
77 const CatalogItem
*i
= a
, *j
= b
;
80 for (k
= 0; k
< ELEMENTSOF(j
->id
.bytes
); k
++) {
81 if (i
->id
.bytes
[k
] < j
->id
.bytes
[k
])
83 if (i
->id
.bytes
[k
] > j
->id
.bytes
[k
])
87 return strcmp(i
->language
, j
->language
);
90 const struct hash_ops catalog_hash_ops
= {
91 .hash
= catalog_hash_func
,
92 .compare
= catalog_compare_func
95 static bool next_header(const char **s
) {
112 static const char *skip_header(const char *s
) {
113 while (next_header(&s
))
118 static char *combine_entries(const char *one
, const char *two
) {
123 /* Find split point of headers to body */
124 b1
= skip_header(one
);
125 b2
= skip_header(two
);
129 dest
= new(char, l1
+ l2
+ 1);
137 /* Headers from @one */
139 p
= mempcpy(p
, one
, n
);
141 /* Headers from @two, these will only be found if not present above */
143 p
= mempcpy(p
, two
, n
);
158 assert(p
- dest
<= (ptrdiff_t)(l1
+ l2
));
163 static int finish_item(
166 const char *language
,
169 _cleanup_free_ CatalogItem
*i
= NULL
;
170 _cleanup_free_
char *combined
= NULL
, *prev
= NULL
;
176 i
= new0(CatalogItem
, 1);
182 assert(strlen(language
) > 1 && strlen(language
) < 32);
183 strcpy(i
->language
, language
);
186 prev
= hashmap_get(h
, i
);
188 /* Already have such an item, combine them */
190 combined
= combine_entries(payload
, prev
);
193 r
= hashmap_update(h
, i
, combined
);
200 r
= hashmap_put(h
, i
, payload
);
209 int catalog_file_lang(const char* filename
, char **lang
) {
210 char *beg
, *end
, *_lang
;
212 end
= endswith(filename
, ".catalog");
217 while (beg
> filename
&& *beg
!= '.' && *beg
!= '/' && end
- beg
< 32)
220 if (*beg
!= '.' || end
<= beg
+ 1)
223 _lang
= strndup(beg
+ 1, end
- beg
- 1);
231 static int catalog_entry_lang(const char* filename
, int line
,
232 const char* t
, const char* deflang
, char **lang
) {
237 log_error("[%s:%u] Language too short.", filename
, line
);
241 log_error("[%s:%u] language too long.", filename
, line
);
246 if (streq(t
, deflang
)) {
247 log_warning("[%s:%u] language specified unnecessarily",
251 log_warning("[%s:%u] language differs from default for file",
262 int catalog_import_file(Hashmap
*h
, const char *path
) {
263 _cleanup_fclose_
FILE *f
= NULL
;
264 _cleanup_free_
char *payload
= NULL
;
267 _cleanup_free_
char *deflang
= NULL
, *lang
= NULL
;
268 bool got_id
= false, empty_line
= true;
274 f
= fopen(path
, "re");
276 return log_error_errno(errno
, "Failed to open file %s: %m", path
);
278 r
= catalog_file_lang(path
, &deflang
);
280 log_error_errno(r
, "Failed to determine language for file %s: %m", path
);
282 log_debug("File %s has language %s.", path
, deflang
);
289 if (!fgets(line
, sizeof(line
), f
)) {
293 return log_error_errno(errno
, "Failed to read file %s: %m", path
);
305 if (strchr(COMMENTS
"\n", line
[0]))
309 strlen(line
) >= 2+1+32 &&
313 (line
[2+1+32] == ' ' || line
[2+1+32] == '\0')) {
320 with_language
= line
[2+1+32] != '\0';
323 if (sd_id128_from_string(line
+ 2 + 1, &jd
) >= 0) {
326 r
= finish_item(h
, id
, lang
?: deflang
, payload
);
335 t
= strstrip(line
+ 2 + 1 + 32 + 1);
337 r
= catalog_entry_lang(path
, n
, t
, deflang
, &lang
);
355 log_error("[%s:%u] Got payload before ID.", path
, n
);
359 a
= payload
? strlen(payload
) : 0;
362 c
= a
+ (empty_line
? 1 : 0) + b
+ 1 + 1;
363 t
= realloc(payload
, c
);
369 memcpy(t
+ a
+ 1, line
, b
);
373 memcpy(t
+ a
, line
, b
);
383 r
= finish_item(h
, id
, lang
?: deflang
, payload
);
392 static int64_t write_catalog(const char *database
, struct strbuf
*sb
,
393 CatalogItem
*items
, size_t n
) {
394 CatalogHeader header
;
395 _cleanup_fclose_
FILE *w
= NULL
;
397 _cleanup_free_
char *d
, *p
= NULL
;
400 d
= dirname_malloc(database
);
404 r
= mkdir_p(d
, 0775);
406 return log_error_errno(r
, "Recursive mkdir %s: %m", d
);
408 r
= fopen_temporary(database
, &w
, &p
);
410 return log_error_errno(r
, "Failed to open database for writing: %s: %m",
414 memcpy(header
.signature
, CATALOG_SIGNATURE
, sizeof(header
.signature
));
415 header
.header_size
= htole64(ALIGN_TO(sizeof(CatalogHeader
), 8));
416 header
.catalog_item_size
= htole64(sizeof(CatalogItem
));
417 header
.n_items
= htole64(n
);
421 k
= fwrite(&header
, 1, sizeof(header
), w
);
422 if (k
!= sizeof(header
)) {
423 log_error("%s: failed to write header.", p
);
427 k
= fwrite(items
, 1, n
* sizeof(CatalogItem
), w
);
428 if (k
!= n
* sizeof(CatalogItem
)) {
429 log_error("%s: failed to write database.", p
);
433 k
= fwrite(sb
->buf
, 1, sb
->len
, w
);
435 log_error("%s: failed to write strings.", p
);
439 r
= fflush_and_check(w
);
441 log_error_errno(r
, "%s: failed to write database: %m", p
);
445 fchmod(fileno(w
), 0644);
447 if (rename(p
, database
) < 0) {
448 r
= log_error_errno(errno
, "rename (%s -> %s) failed: %m", p
, database
);
459 int catalog_update(const char* database
, const char* root
, const char* const* dirs
) {
460 _cleanup_strv_free_
char **files
= NULL
;
462 struct strbuf
*sb
= NULL
;
463 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
464 _cleanup_free_ CatalogItem
*items
= NULL
;
473 h
= hashmap_new(&catalog_hash_ops
);
481 r
= conf_files_list_strv(&files
, ".catalog", root
, dirs
);
483 log_error_errno(r
, "Failed to get catalog files: %m");
487 STRV_FOREACH(f
, files
) {
488 log_debug("Reading file '%s'", *f
);
489 r
= catalog_import_file(h
, *f
);
491 log_error_errno(r
, "Failed to import file '%s': %m", *f
);
496 if (hashmap_size(h
) <= 0) {
497 log_info("No items in catalog.");
500 log_debug("Found %u items in catalog.", hashmap_size(h
));
502 items
= new(CatalogItem
, hashmap_size(h
));
509 HASHMAP_FOREACH_KEY(payload
, i
, h
, j
) {
510 log_debug("Found " SD_ID128_FORMAT_STR
", language %s",
511 SD_ID128_FORMAT_VAL(i
->id
),
512 isempty(i
->language
) ? "C" : i
->language
);
514 offset
= strbuf_add_string(sb
, payload
, strlen(payload
));
519 i
->offset
= htole64((uint64_t) offset
);
523 assert(n
== hashmap_size(h
));
524 qsort_safe(items
, n
, sizeof(CatalogItem
), catalog_compare_func
);
528 sz
= write_catalog(database
, sb
, items
, n
);
530 r
= log_error_errno(sz
, "Failed to write %s: %m", database
);
533 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64
" total size.",
534 database
, n
, sb
->len
, sz
);
543 static int open_mmap(const char *database
, int *_fd
, struct stat
*_st
, void **_p
) {
544 const CatalogHeader
*h
;
553 fd
= open(database
, O_RDONLY
|O_CLOEXEC
);
557 if (fstat(fd
, &st
) < 0) {
562 if (st
.st_size
< (off_t
) sizeof(CatalogHeader
)) {
567 p
= mmap(NULL
, PAGE_ALIGN(st
.st_size
), PROT_READ
, MAP_SHARED
, fd
, 0);
568 if (p
== MAP_FAILED
) {
574 if (memcmp(h
->signature
, CATALOG_SIGNATURE
, sizeof(h
->signature
)) != 0 ||
575 le64toh(h
->header_size
) < sizeof(CatalogHeader
) ||
576 le64toh(h
->catalog_item_size
) < sizeof(CatalogItem
) ||
577 h
->incompatible_flags
!= 0 ||
578 le64toh(h
->n_items
) <= 0 ||
579 st
.st_size
< (off_t
) (le64toh(h
->header_size
) + le64toh(h
->catalog_item_size
) * le64toh(h
->n_items
))) {
581 munmap(p
, st
.st_size
);
592 static const char *find_id(void *p
, sd_id128_t id
) {
593 CatalogItem key
, *f
= NULL
;
594 const CatalogHeader
*h
= p
;
600 loc
= setlocale(LC_MESSAGES
, NULL
);
601 if (loc
&& loc
[0] && !streq(loc
, "C") && !streq(loc
, "POSIX")) {
602 strncpy(key
.language
, loc
, sizeof(key
.language
));
603 key
.language
[strcspn(key
.language
, ".@")] = 0;
605 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), catalog_compare_func
);
609 e
= strchr(key
.language
, '_');
612 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), catalog_compare_func
);
619 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), catalog_compare_func
);
625 return (const char*) p
+
626 le64toh(h
->header_size
) +
627 le64toh(h
->n_items
) * le64toh(h
->catalog_item_size
) +
631 int catalog_get(const char* database
, sd_id128_t id
, char **_text
) {
632 _cleanup_close_
int fd
= -1;
641 r
= open_mmap(database
, &fd
, &st
, &p
);
662 munmap(p
, st
.st_size
);
667 static char *find_header(const char *s
, const char *header
) {
672 v
= startswith(s
, header
);
674 v
+= strspn(v
, WHITESPACE
);
675 return strndup(v
, strcspn(v
, NEWLINE
));
678 if (!next_header(&s
))
683 static void dump_catalog_entry(FILE *f
, sd_id128_t id
, const char *s
, bool oneline
) {
685 _cleanup_free_
char *subject
= NULL
, *defined_by
= NULL
;
687 subject
= find_header(s
, "Subject:");
688 defined_by
= find_header(s
, "Defined-By:");
690 fprintf(f
, SD_ID128_FORMAT_STR
" %s: %s\n",
691 SD_ID128_FORMAT_VAL(id
),
692 strna(defined_by
), strna(subject
));
694 fprintf(f
, "-- " SD_ID128_FORMAT_STR
"\n%s\n",
695 SD_ID128_FORMAT_VAL(id
), s
);
699 int catalog_list(FILE *f
, const char *database
, bool oneline
) {
700 _cleanup_close_
int fd
= -1;
703 const CatalogHeader
*h
;
704 const CatalogItem
*items
;
708 bool last_id_set
= false;
710 r
= open_mmap(database
, &fd
, &st
, &p
);
715 items
= (const CatalogItem
*) ((const uint8_t*) p
+ le64toh(h
->header_size
));
717 for (n
= 0; n
< le64toh(h
->n_items
); n
++) {
720 if (last_id_set
&& sd_id128_equal(last_id
, items
[n
].id
))
723 assert_se(s
= find_id(p
, items
[n
].id
));
725 dump_catalog_entry(f
, items
[n
].id
, s
, oneline
);
728 last_id
= items
[n
].id
;
731 munmap(p
, st
.st_size
);
736 int catalog_list_items(FILE *f
, const char *database
, bool oneline
, char **items
) {
740 STRV_FOREACH(item
, items
) {
743 _cleanup_free_
char *msg
= NULL
;
745 k
= sd_id128_from_string(*item
, &id
);
747 log_error_errno(k
, "Failed to parse id128 '%s': %m", *item
);
753 k
= catalog_get(database
, id
, &msg
);
755 log_full_errno(k
== -ENOENT
? LOG_NOTICE
: LOG_ERR
, k
,
756 "Failed to retrieve catalog entry for '%s': %m", *item
);
762 dump_catalog_entry(f
, id
, msg
, oneline
);