locale-util: allow overriding locale directory via environment

This commit is contained in:
andre4ik3
2026-02-16 08:09:33 +09:00
committed by Yu Watanabe
parent a267656b07
commit 4afa6cf07a
2 changed files with 25 additions and 8 deletions
+17 -4
View File
@@ -55,6 +55,15 @@ static char* normalize_locale(const char *name) {
return strdup(name);
}
static const char* get_locale_dir(void) {
return secure_getenv("SYSTEMD_LOCALE_DIRECTORY") ?:
#ifdef __GLIBC__
"/usr/lib/locale/";
#else
"/usr/share/i18n/locales/musl/";
#endif
}
#ifdef __GLIBC__
static int add_locales_from_archive(Set *locales) {
/* Stolen from glibc... */
@@ -94,7 +103,11 @@ static int add_locales_from_archive(Set *locales) {
assert(locales);
_cleanup_close_ int fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
_cleanup_free_ char *locale_archive_file = path_join(get_locale_dir(), "locale-archive");
if (!locale_archive_file)
return -ENOMEM;
_cleanup_close_ int fd = open(locale_archive_file, O_RDONLY|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return errno == ENOENT ? 0 : -errno;
@@ -162,7 +175,7 @@ static int add_locales_from_libdir(Set *locales) {
assert(locales);
dir = opendir("/usr/lib/locale");
dir = opendir(get_locale_dir());
if (!dir)
return errno == ENOENT ? 0 : -errno;
@@ -191,7 +204,7 @@ static int add_locales_for_musl(Set *locales) {
assert(locales);
_cleanup_closedir_ DIR *dir = opendir("/usr/share/i18n/locales/musl/");
_cleanup_closedir_ DIR *dir = opendir(get_locale_dir());
if (!dir)
return errno == ENOENT ? 0 : -errno;
@@ -313,7 +326,7 @@ int locale_is_installed(const char *name) {
/* musl's newlocale() always succeeds and provides a fake locale object even when the locale does
* not exist. Hence, we need to explicitly check if the locale file exists. */
_cleanup_free_ char *p = path_join("/usr/share/i18n/locales/musl/", name);
_cleanup_free_ char *p = path_join(get_locale_dir(), name);
if (!p)
return -ENOMEM;