return log_oom();
i->id = id;
- strscpy(i->language, sizeof(i->language), language);
+ if (language) {
+ assert(strlen(language) > 1 && strlen(language) < 32);
+ strcpy(i->language, language);
+ }
i->offset = htole64((uint64_t) offset);
r = hashmap_put(h, i, i);
return 0;
}
+int catalog_file_lang(const char* filename, char **lang) {
+ char *beg, *end, *_lang;
+
+ end = endswith(filename, ".catalog");
+ if (!end)
+ return 0;
+
+ beg = end - 1;
+ while (beg > filename && *beg != '.')
+ beg --;
+
+ if (beg <= filename || end <= beg + 1 || end - beg > 32)
+ return 0;
+
+ _lang = strndup(beg + 1, end - beg - 1);
+ if (!_lang)
+ return -ENOMEM;
+
+ *lang = _lang;
+ return 1;
+}
+
int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *payload = NULL;
unsigned n = 0;
sd_id128_t id;
- char language[32];
+ _cleanup_free_ char *deflang = NULL, *lang = NULL;
bool got_id = false, empty_line = true;
int r;
return -errno;
}
+ r = catalog_file_lang(path, &deflang);
+ if (r < 0)
+ log_error("Failed to determine language for file %s: %m", path);
+ if (r == 1)
+ log_debug("File %s has language %s.", path, deflang);
+
for (;;) {
char line[LINE_MAX];
size_t a, b, c;
if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
if (got_id) {
- r = finish_item(h, sb, id, language, payload);
+ r = finish_item(h, sb, id, lang ?: deflang, payload);
if (r < 0)
return r;
+
+ free(lang);
+ lang = NULL;
}
if (with_language) {
log_error("[%s:%u] Language too short.", path, n);
return -EINVAL;
}
- if (c > sizeof(language) - 1) {
+ if (c > 31) {
log_error("[%s:%u] language too long.", path, n);
return -EINVAL;
}
- strscpy(language, sizeof(language), t);
- } else
- language[0] = '\0';
+ if (deflang) {
+ log_warning("[%s:%u] language %s", path, n,
+ streq(t, deflang) ?
+ "specified unnecessarily" :
+ "differs from default for file");
+ lang = strdup(t);
+ if (!lang)
+ return -ENOMEM;
+ }
+ }
got_id = true;
empty_line = false;
}
if (got_id) {
- r = finish_item(h, sb, id, language, payload);
+ r = finish_item(h, sb, id, lang ?: deflang, payload);
if (r < 0)
return r;
}
assert(r >= 0);
}
+static void test_catalog_file_lang(void) {
+ _cleanup_free_ char *lang = NULL, *lang2 = NULL, *lang3 = NULL;
+
+ assert_se(catalog_file_lang("systemd.de_DE.catalog", &lang) == 1);
+ assert_se(streq(lang, "de_DE"));
+
+ assert_se(catalog_file_lang("systemd..catalog", &lang2) == 0);
+ assert_se(lang2 == NULL);
+
+ assert_se(catalog_file_lang("systemd.fr.catalog", &lang2) == 1);
+ assert_se(streq(lang2, "fr"));
+
+ assert_se(catalog_file_lang("systemd.fr.catalog.gz", &lang3) == 0);
+ assert_se(lang3 == NULL);
+
+ assert_se(catalog_file_lang("systemd.01234567890123456789012345678901.catalog", &lang3) == 0);
+ assert_se(lang3 == NULL);
+
+ assert_se(catalog_file_lang("systemd.0123456789012345678901234567890.catalog", &lang3) == 1);
+ assert_se(streq(lang3, "0123456789012345678901234567890"));
+}
+
int main(int argc, char *argv[]) {
_cleanup_free_ char *text = NULL;
int r;
if (database)
unlink(database);
+ test_catalog_file_lang();
+
return 0;
}