catalog.c revision 3ffd4af22052963e7a29431721ee204e634bea75
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2012 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <errno.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <fcntl.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <locale.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stdio.h>
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen#include <string.h>
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include <sys/mman.h>
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering#include <unistd.h>
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "sd-id128.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "catalog.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "conf-files.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "fd-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "hashmap.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include "log.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include "mkdir.h"
28b8191e2f391f043d380d47eb79ed9ff66f14bdLennart Poettering#include "siphash24.h"
28b8191e2f391f043d380d47eb79ed9ff66f14bdLennart Poettering#include "sparse-endian.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include "strbuf.h"
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack#include "string-util.h"
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack#include "strv.h"
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack#include "util.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringconst char * const catalog_file_dirs[] = {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering "/usr/local/lib/systemd/catalog/",
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering "/usr/lib/systemd/catalog/",
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering NULL
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering};
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#define CATALOG_SIGNATURE (uint8_t[]) { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringtypedef struct CatalogHeader {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering uint8_t signature[8]; /* "RHHHKSLP" */
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering le32_t compatible_flags;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering le32_t incompatible_flags;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering le64_t header_size;
e0240c64b76ba8f0c9219feb23a5783f23100216Lennart Poettering le64_t n_items;
e0240c64b76ba8f0c9219feb23a5783f23100216Lennart Poettering le64_t catalog_item_size;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering} CatalogHeader;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringtypedef struct CatalogItem {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering sd_id128_t id;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering char language[32];
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering le64_t offset;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering} CatalogItem;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic void catalog_hash_func(const void *p, struct siphash *state) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering const CatalogItem *i = p;
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering siphash24_compress(&i->id, sizeof(i->id), state);
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering siphash24_compress(i->language, strlen(i->language), state);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering}
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poetteringstatic int catalog_compare_func(const void *a, const void *b) {
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering const CatalogItem *i = a, *j = b;
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering unsigned k;
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering for (k = 0; k < ELEMENTSOF(j->id.bytes); k++) {
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering if (i->id.bytes[k] < j->id.bytes[k])
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering return -1;
d15ad74251454d55b715958d8e6f50f45195904aLennart Poettering if (i->id.bytes[k] > j->id.bytes[k])
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return strcmp(i->language, j->language);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poetteringconst struct hash_ops catalog_hash_ops = {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering .hash = catalog_hash_func,
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering .compare = catalog_compare_func
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering};
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poetteringstatic int finish_item(
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering Hashmap *h,
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering struct strbuf *sb,
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering sd_id128_t id,
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering const char *language,
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering const char *payload) {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering ssize_t offset;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering _cleanup_free_ CatalogItem *i = NULL;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(h);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(sb);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(payload);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering offset = strbuf_add_string(sb, payload, strlen(payload));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (offset < 0)
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering return log_oom();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
ee3d6aff9bd73c1b23e29d1fa1fa6f7a1ef0533bLennart Poettering i = new0(CatalogItem, 1);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if (!i)
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return log_oom();
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering i->id = id;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if (language) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(strlen(language) > 1 && strlen(language) < 32);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering strcpy(i->language, language);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering i->offset = htole64((uint64_t) offset);
d75acfb059ece4512278b8820a9103664996f1e5Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = hashmap_put(h, i, i);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (r == -EEXIST) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_warning("Duplicate entry for " SD_ID128_FORMAT_STR ".%s, ignoring.",
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering SD_ID128_FORMAT_VAL(id), language ? language : "C");
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering } else if (r < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering i = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint catalog_file_lang(const char* filename, char **lang) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *beg, *end, *_lang;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering end = endswith(filename, ".catalog");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!end)
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
c0eb11cfd016381fe02875a4ef29c1ade00c94e7Lennart Poettering beg = end - 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering while (beg > filename && *beg != '.' && *beg != '/' && end - beg < 32)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering beg --;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (*beg != '.' || end <= beg + 1)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _lang = strndup(beg + 1, end - beg - 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!_lang)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return -ENOMEM;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering *lang = _lang;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return 1;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering}
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringstatic int catalog_entry_lang(const char* filename, int line,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering const char* t, const char* deflang, char **lang) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering size_t c;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek c = strlen(t);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (c == 0) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek log_error("[%s:%u] Language too short.", filename, line);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -EINVAL;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (c > 31) {
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering log_error("[%s:%u] language too long.", filename, line);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -EINVAL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (deflang) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (streq(t, deflang)) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_warning("[%s:%u] language specified unnecessarily",
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek filename, line);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek } else
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_warning("[%s:%u] language differs from default for file",
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen filename, line);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering *lang = strdup(t);
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (!*lang)
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen return -ENOMEM;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 0;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poetteringint catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering _cleanup_fclose_ FILE *f = NULL;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *payload = NULL;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek unsigned n = 0;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek sd_id128_t id;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *deflang = NULL, *lang = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek bool got_id = false, empty_line = true;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(h);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(sb);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(path);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek f = fopen(path, "re");
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!f)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return log_error_errno(errno, "Failed to open file %s: %m", path);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = catalog_file_lang(path, &deflang);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r < 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_error_errno(errno, "Failed to determine language for file %s: %m", path);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r == 1)
9ead3519c54b6d1b79b35541873b5cf7c8b3a7d3Lennart Poettering log_debug("File %s has language %s.", path, deflang);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen for (;;) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen char line[LINE_MAX];
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen size_t a, b, c;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen char *t;
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering if (!fgets(line, sizeof(line), f)) {
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering if (feof(f))
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering break;
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering log_error_errno(errno, "Failed to read file %s: %m", path);
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering return -errno;
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering }
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen n++;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen truncate_nl(line);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (line[0] == 0) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen empty_line = true;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen continue;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (strchr(COMMENTS "\n", line[0]))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (empty_line &&
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering strlen(line) >= 2+1+32 &&
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering line[0] == '-' &&
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering line[1] == '-' &&
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering line[2] == ' ' &&
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (line[2+1+32] == ' ' || line[2+1+32] == '\0')) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering bool with_language;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_id128_t jd;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* New entry */
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering with_language = line[2+1+32] != '\0';
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering line[2+1+32] = '\0';
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering if (got_id) {
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering r = finish_item(h, sb, id, lang ?: deflang, payload);
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering if (r < 0)
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering return r;
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering lang = mfree(lang);
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering }
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering if (with_language) {
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering t = strstrip(line + 2 + 1 + 32 + 1);
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering r = catalog_entry_lang(path, n, t, deflang, &lang);
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering if (r < 0)
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering return r;
85aeaccc10b111e8d16d3879b7c30a219ee6e10aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen got_id = true;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering empty_line = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering id = jd;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (payload)
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering payload[0] = '\0';
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering continue;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen }
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Payload */
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!got_id) {
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack log_error("[%s:%u] Got payload before ID.", path, n);
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack return -EINVAL;
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack }
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering a = payload ? strlen(payload) : 0;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering b = strlen(line);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering c = a + (empty_line ? 1 : 0) + b + 1 + 1;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering t = realloc(payload, c);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!t)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return log_oom();
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (empty_line) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering t[a] = '\n';
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering memcpy(t + a + 1, line, b);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering t[a+b+1] = '\n';
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering t[a+b+2] = 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering } else {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering memcpy(t + a, line, b);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt t[a+b] = '\n';
c9c72065419e6595131a6fe1e663e2184a843f7cLennart Poettering t[a+b+1] = 0;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering }
8e54f5d90a6b9dd1ff672fb97ea98de66c49e332Lennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering payload = t;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering empty_line = false;
8e54f5d90a6b9dd1ff672fb97ea98de66c49e332Lennart Poettering }
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
if (got_id) {
r = finish_item(h, sb, id, lang ?: deflang, payload);
if (r < 0)
return r;
}
return 0;
}
static long write_catalog(const char *database, Hashmap *h, struct strbuf *sb,
CatalogItem *items, size_t n) {
CatalogHeader header;
_cleanup_fclose_ FILE *w = NULL;
int r;
_cleanup_free_ char *d, *p = NULL;
size_t k;
d = dirname_malloc(database);
if (!d)
return log_oom();
r = mkdir_p(d, 0775);
if (r < 0)
return log_error_errno(r, "Recursive mkdir %s: %m", d);
r = fopen_temporary(database, &w, &p);
if (r < 0)
return log_error_errno(r, "Failed to open database for writing: %s: %m",
database);
zero(header);
memcpy(header.signature, CATALOG_SIGNATURE, sizeof(header.signature));
header.header_size = htole64(ALIGN_TO(sizeof(CatalogHeader), 8));
header.catalog_item_size = htole64(sizeof(CatalogItem));
header.n_items = htole64(hashmap_size(h));
r = -EIO;
k = fwrite(&header, 1, sizeof(header), w);
if (k != sizeof(header)) {
log_error("%s: failed to write header.", p);
goto error;
}
k = fwrite(items, 1, n * sizeof(CatalogItem), w);
if (k != n * sizeof(CatalogItem)) {
log_error("%s: failed to write database.", p);
goto error;
}
k = fwrite(sb->buf, 1, sb->len, w);
if (k != sb->len) {
log_error("%s: failed to write strings.", p);
goto error;
}
r = fflush_and_check(w);
if (r < 0) {
log_error_errno(r, "%s: failed to write database: %m", p);
goto error;
}
fchmod(fileno(w), 0644);
if (rename(p, database) < 0) {
r = log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
goto error;
}
return ftell(w);
error:
(void) unlink(p);
return r;
}
int catalog_update(const char* database, const char* root, const char* const* dirs) {
_cleanup_strv_free_ char **files = NULL;
char **f;
struct strbuf *sb = NULL;
_cleanup_hashmap_free_free_ Hashmap *h = NULL;
_cleanup_free_ CatalogItem *items = NULL;
CatalogItem *i;
Iterator j;
unsigned n;
long r;
h = hashmap_new(&catalog_hash_ops);
sb = strbuf_new();
if (!h || !sb) {
r = log_oom();
goto finish;
}
r = conf_files_list_strv(&files, ".catalog", root, dirs);
if (r < 0) {
log_error_errno(r, "Failed to get catalog files: %m");
goto finish;
}
STRV_FOREACH(f, files) {
log_debug("Reading file '%s'", *f);
r = catalog_import_file(h, sb, *f);
if (r < 0) {
log_error_errno(r, "Failed to import file '%s': %m", *f);
goto finish;
}
}
if (hashmap_size(h) <= 0) {
log_info("No items in catalog.");
goto finish;
} else
log_debug("Found %u items in catalog.", hashmap_size(h));
strbuf_complete(sb);
items = new(CatalogItem, hashmap_size(h));
if (!items) {
r = log_oom();
goto finish;
}
n = 0;
HASHMAP_FOREACH(i, h, j) {
log_debug("Found " SD_ID128_FORMAT_STR ", language %s",
SD_ID128_FORMAT_VAL(i->id),
isempty(i->language) ? "C" : i->language);
items[n++] = *i;
}
assert(n == hashmap_size(h));
qsort_safe(items, n, sizeof(CatalogItem), catalog_compare_func);
r = write_catalog(database, h, sb, items, n);
if (r < 0)
log_error_errno(r, "Failed to write %s: %m", database);
else
log_debug("%s: wrote %u items, with %zu bytes of strings, %ld total size.",
database, n, sb->len, r);
finish:
if (sb)
strbuf_cleanup(sb);
return r < 0 ? r : 0;
}
static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p) {
const CatalogHeader *h;
int fd;
void *p;
struct stat st;
assert(_fd);
assert(_st);
assert(_p);
fd = open(database, O_RDONLY|O_CLOEXEC);
if (fd < 0)
return -errno;
if (fstat(fd, &st) < 0) {
safe_close(fd);
return -errno;
}
if (st.st_size < (off_t) sizeof(CatalogHeader)) {
safe_close(fd);
return -EINVAL;
}
p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
safe_close(fd);
return -errno;
}
h = p;
if (memcmp(h->signature, CATALOG_SIGNATURE, sizeof(h->signature)) != 0 ||
le64toh(h->header_size) < sizeof(CatalogHeader) ||
le64toh(h->catalog_item_size) < sizeof(CatalogItem) ||
h->incompatible_flags != 0 ||
le64toh(h->n_items) <= 0 ||
st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
safe_close(fd);
munmap(p, st.st_size);
return -EBADMSG;
}
*_fd = fd;
*_st = st;
*_p = p;
return 0;
}
static const char *find_id(void *p, sd_id128_t id) {
CatalogItem key, *f = NULL;
const CatalogHeader *h = p;
const char *loc;
zero(key);
key.id = id;
loc = setlocale(LC_MESSAGES, NULL);
if (loc && loc[0] && !streq(loc, "C") && !streq(loc, "POSIX")) {
strncpy(key.language, loc, sizeof(key.language));
key.language[strcspn(key.language, ".@")] = 0;
f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
if (!f) {
char *e;
e = strchr(key.language, '_');
if (e) {
*e = 0;
f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
}
}
}
if (!f) {
zero(key.language);
f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
}
if (!f)
return NULL;
return (const char*) p +
le64toh(h->header_size) +
le64toh(h->n_items) * le64toh(h->catalog_item_size) +
le64toh(f->offset);
}
int catalog_get(const char* database, sd_id128_t id, char **_text) {
_cleanup_close_ int fd = -1;
void *p = NULL;
struct stat st = {};
char *text = NULL;
int r;
const char *s;
assert(_text);
r = open_mmap(database, &fd, &st, &p);
if (r < 0)
return r;
s = find_id(p, id);
if (!s) {
r = -ENOENT;
goto finish;
}
text = strdup(s);
if (!text) {
r = -ENOMEM;
goto finish;
}
*_text = text;
r = 0;
finish:
if (p)
munmap(p, st.st_size);
return r;
}
static char *find_header(const char *s, const char *header) {
for (;;) {
const char *v, *e;
v = startswith(s, header);
if (v) {
v += strspn(v, WHITESPACE);
return strndup(v, strcspn(v, NEWLINE));
}
/* End of text */
e = strchr(s, '\n');
if (!e)
return NULL;
/* End of header */
if (e == s)
return NULL;
s = e + 1;
}
}
static void dump_catalog_entry(FILE *f, sd_id128_t id, const char *s, bool oneline) {
if (oneline) {
_cleanup_free_ char *subject = NULL, *defined_by = NULL;
subject = find_header(s, "Subject:");
defined_by = find_header(s, "Defined-By:");
fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n",
SD_ID128_FORMAT_VAL(id),
strna(defined_by), strna(subject));
} else
fprintf(f, "-- " SD_ID128_FORMAT_STR "\n%s\n",
SD_ID128_FORMAT_VAL(id), s);
}
int catalog_list(FILE *f, const char *database, bool oneline) {
_cleanup_close_ int fd = -1;
void *p = NULL;
struct stat st;
const CatalogHeader *h;
const CatalogItem *items;
int r;
unsigned n;
sd_id128_t last_id;
bool last_id_set = false;
r = open_mmap(database, &fd, &st, &p);
if (r < 0)
return r;
h = p;
items = (const CatalogItem*) ((const uint8_t*) p + le64toh(h->header_size));
for (n = 0; n < le64toh(h->n_items); n++) {
const char *s;
if (last_id_set && sd_id128_equal(last_id, items[n].id))
continue;
assert_se(s = find_id(p, items[n].id));
dump_catalog_entry(f, items[n].id, s, oneline);
last_id_set = true;
last_id = items[n].id;
}
munmap(p, st.st_size);
return 0;
}
int catalog_list_items(FILE *f, const char *database, bool oneline, char **items) {
char **item;
int r = 0;
STRV_FOREACH(item, items) {
sd_id128_t id;
int k;
_cleanup_free_ char *msg = NULL;
k = sd_id128_from_string(*item, &id);
if (k < 0) {
log_error_errno(k, "Failed to parse id128 '%s': %m", *item);
if (r == 0)
r = k;
continue;
}
k = catalog_get(database, id, &msg);
if (k < 0) {
log_full_errno(k == -ENOENT ? LOG_NOTICE : LOG_ERR, k,
"Failed to retrieve catalog entry for '%s': %m", *item);
if (r == 0)
r = k;
continue;
}
dump_catalog_entry(f, id, msg, oneline);
}
return r;
}