]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
musl: locale-util: explicitly check existence of locale file 39689/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 11 Nov 2025 22:30:01 +0000 (07:30 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 12 Nov 2025 02:43:13 +0000 (11:43 +0900)
musl's newlocale() always provides a locale object even the requested
locale does not exist. Let's explicitly check the existence of the
requested locale file.

src/basic/locale-util.c

index 36fe153379fb0215ed5ec1cdc4ab559375fa9718..ebccc6a8bd5ce712d603346025b1e604b7b793ae 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include <unistd.h>
 
 #include "dirent-util.h"
 #include "env-util.h"
@@ -299,11 +300,25 @@ int locale_is_installed(const char *name) {
         if (STR_IN_SET(name, "C", "POSIX")) /* These ones are always OK */
                 return true;
 
+#ifdef __GLIBC__
         _cleanup_(freelocalep) locale_t loc = newlocale(LC_ALL_MASK, name, (locale_t) 0);
         if (loc == (locale_t) 0)
                 return errno == ENOMEM ? -ENOMEM : false;
 
         return true;
+#else
+        /* musl also has C.UTF-8 as builtin */
+        if (streq(name, "C.UTF-8"))
+                return true;
+
+        /* 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);
+        if (!p)
+                return -ENOMEM;
+
+        return access(p, F_OK) >= 0;
+#endif
 }
 
 static bool is_locale_utf8_impl(void) {