From: Sylvestre Ledru Subject: Speedup locale reads Install locales to a specific directory and read from that instead of testing different directories. --- The information above should follow the Patch Tagging Guidelines, please checkout https://dep.debian.net/deps/dep3/ to learn about the format. Here are templates for supplementary fields that you might want to add: Origin: upstream, https://salsa.debian.org/rust-team/coreutils/-/blob/master/debian/patches/fix-locale-path.diff?ref_type=heads Forwarded: not-needed Reviewed-By: Simon Johnsson =================================================================== --- a/GNUmakefile +++ b/GNUmakefile @@ -35,6 +35,7 @@ PREFIX ?= /usr DESTDIR ?= BINDIR ?= $(PREFIX)/bin DATAROOTDIR ?= $(PREFIX)/share +LOCALEDIR ?= $(DATAROOTDIR)/coreutils/locales LIBSTDBUF_DIR ?= $(PREFIX)/libexec/$(PROG_PREFIX)coreutils # Export variable so that it is used during the build export LIBSTDBUF_DIR @@ -230,7 +231,7 @@ endif ifeq ($(LOCALES),y) locales: - @# Copy uucore common locales + # Copy uucore common locales @if [ -d "$(BASEDIR)/src/uucore/locales" ]; then \ $(INSTALL) -d "$(BUILDDIR)/locales/uucore"; \ for locale_file in "$(BASEDIR)"/src/uucore/locales/*.ftl; do \ @@ -259,31 +260,38 @@ locales: # - *sum binaries need the files from checksum_common INSTALLEES_WITH_EXTRA_LOCALE = \ $(INSTALLEES) \ - $(if $(findstring sum, $(INSTALLEES)),checksum_common, ) + $(if $(findstring sum,$(INSTALLEES)),checksum_common,) + +UU_LOCALES := $(wildcard $(BASEDIR)/l10n/src/uu/*/locales/*.ftl) +UUC_CORE_LOCALES := $(wildcard $(BASEDIR)/l10n/src/uucore/locales/*) + +UU_LOCALES_NO_EN := $(filter-out %/en-US.ftl,$(UU_LOCALES)) + +define uu_install_locale +$(eval prog := $(notdir $(patsubst %/locales/,%,$(dir $(dir $(1)))))) +mkdir -p $(DESTDIR)$(LOCALEDIR)/$(prog) +$(INSTALL) -m 644 $(1) $(DESTDIR)$(LOCALEDIR)/$(prog)/ +endef + install-locales: + @mkdir -p $(DESTDIR)$(LOCALEDIR) @# Install lazy error locales shared by all utilities @if [ -d "$(BASEDIR)/src/uucore/locales/errors" ]; then \ - $(INSTALL) -d "$(DESTDIR)$(DATAROOTDIR)/locales/uucore/errors"; \ + $(INSTALL) -d "$(DESTDIR)$(LOCALEDIR)/uucore/errors"; \ for locale_file in "$(BASEDIR)"/src/uucore/locales/errors/*.ftl; do \ if [ "$$(basename "$$locale_file")" != "en-US.ftl" ]; then \ - $(INSTALL) -m 644 "$$locale_file" "$(DESTDIR)$(DATAROOTDIR)/locales/uucore/errors/"; \ + $(INSTALL) -m 644 "$$locale_file" "$(DESTDIR)$(LOCALEDIR)/uucore/errors/"; \ fi; \ done; \ fi - @for prog in $(INSTALLEES_WITH_EXTRA_LOCALE); do \ - if [ -d "$(BASEDIR)/l10n/src/uu/$$prog/locales" ]; then \ - $(INSTALL) -d "$(DESTDIR)$(DATAROOTDIR)/locales/$$prog"; \ - for locale_file in "$(BASEDIR)"/l10n/src/uu/$$prog/locales/*.ftl; do \ - if [ "$$(basename "$$locale_file")" != "en-US.ftl" ]; then \ - $(INSTALL) -m 644 "$$locale_file" "$(DESTDIR)$(DATAROOTDIR)/locales/$$prog/"; \ - fi; \ - done; \ - fi; \ - done - if [ -d "$(BASEDIR)/l10n/src/uucore/locales" ]; then \ - mkdir -p "$(DESTDIR)$(DATAROOTDIR)/locales/uucore"; \ - $(INSTALL) -m 644 "$(BASEDIR)"/l10n/src/uucore/locales/* "$(DESTDIR)$(DATAROOTDIR)/locales/uucore/"; \ - fi; + @$(foreach f,$(UU_LOCALES_NO_EN),$(call uu_install_locale,$(f));) + @if [ -d "$(BASEDIR)/l10n/src/uucore/locales" ]; then \ + mkdir -p "$(DESTDIR)$(LOCALEDIR)/uucore"; \ + for f in $(UUC_CORE_LOCALES); do \ + [ -e "$$f" ] || continue; \ + $(INSTALL) -m 644 "$$f" "$(DESTDIR)$(LOCALEDIR)/uucore/"; \ + done \ + fi else locales: install-locales: =================================================================== --- a/src/uucore/src/lib/mods/locale.rs +++ b/src/uucore/src/lib/mods/locale.rs @@ -16,6 +16,7 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::OnceLock; +#[cfg(any(debug_assertions, test))] use os_display::Quotable; use thiserror::Error; use unic_langid::LanguageIdentifier; @@ -221,23 +222,33 @@ thread_local! { const { OnceLock::new() }; } -/// Helper function to find the uucore locales directory from a utility's locales directory +/// Helper function to find the uucore locales directory fn find_uucore_locales_dir(utility_locales_dir: &Path) -> Option { - // Normalize the path to get absolute path - let normalized_dir = utility_locales_dir - .canonicalize() - .unwrap_or_else(|_| utility_locales_dir.to_path_buf()); - - // Walk up: locales -> printenv -> uu -> src - let uucore_locales = normalized_dir - .parent()? // printenv - .parent()? // uu - .parent()? // src - .join("uucore") - .join("locales"); + #[cfg(debug_assertions)] + { + // During development, walk up from the utility's locales directory + let normalized_dir = utility_locales_dir + .canonicalize() + .unwrap_or_else(|_| utility_locales_dir.to_path_buf()); + + // Walk up: locales -> printenv -> uu -> src + let uucore_locales = normalized_dir + .parent()? // printenv + .parent()? // uu + .parent()? // src + .join("uucore") + .join("locales"); - // Only return if the directory actually exists - uucore_locales.exists().then_some(uucore_locales) + // Only return if the directory actually exists + uucore_locales.exists().then_some(uucore_locales) + } + + #[cfg(not(debug_assertions))] + { + let _ = utility_locales_dir; + let uucore_locales = PathBuf::from("/usr/share/coreutils/locales/uucore"); + uucore_locales.exists().then_some(uucore_locales) + } } /// Create a bundle that combines common and utility-specific strings @@ -666,25 +677,11 @@ pub fn setup_localization(p: &str) -> Re } #[cfg(not(debug_assertions))] -fn resolve_locales_dir_from_exe_dir(exe_dir: &Path, p: &str) -> Option { - // 1. /locales/ - let coreutils = exe_dir.join("locales").join(p); - if coreutils.is_dir() { - return Some(coreutils); - } - - // 2. /share/locales/ - if let Some(prefix) = exe_dir.parent() { - let fhs = prefix.join("share").join("locales").join(p); - if fhs.is_dir() { - return Some(fhs); - } - } - - // 3. / (legacy fall-back) - let fallback = exe_dir.join(p); - if fallback.is_dir() { - return Some(fallback); +fn resolve_locales_dir(p: &str) -> Option { + // Only look in /usr/share/coreutils/locales/ + let locales_dir = PathBuf::from("/usr/share/coreutils/locales").join(p); + if locales_dir.exists() { + return Some(locales_dir) } None @@ -728,23 +725,13 @@ fn get_locales_dir(p: &str) -> Result + if let Some(dir) = resolve_locales_dir(p) { return Ok(dir); } Err(LocalizationError::LocalesDirNotFound(format!( - "Release locales directory not found starting from {}", - exe_dir.quote() + "Locales directory not found at /usr/share/coreutils/locales/{p}" ))) } } @@ -1849,24 +1836,18 @@ invalid-syntax = This is { $missing #[cfg(all(test, not(debug_assertions)))] mod fhs_tests { use super::*; - use tempfile::TempDir; #[test] - fn resolves_fhs_share_locales_layout() { - // 1. Set up a fake installation prefix in a temp directory - let prefix = TempDir::new().unwrap(); // e.g. /tmp/xyz - let bin_dir = prefix.path().join("bin"); // /tmp/xyz/bin - let share_dir = prefix.path().join("share").join("locales").join("cut"); // /tmp/xyz/share/locales/cut - std::fs::create_dir_all(&share_dir).unwrap(); - std::fs::create_dir_all(&bin_dir).unwrap(); - - // 2. Pretend the executable lives in /bin - let exe_dir = bin_dir.as_path(); - - // 3. Ask the helper to resolve the locales dir - let result = resolve_locales_dir_from_exe_dir(exe_dir, "cut") - .expect("should find locales via FHS path"); - - assert_eq!(result, share_dir); + fn resolves_fixed_locales_path() { + // Test that resolve_locales_dir returns the expected fixed path + // This test only passes if /usr/share/coreutils/locales/ exists on the system + let result = resolve_locales_dir("cut"); + if let Some(dir) = result { + assert_eq!( + dir, + PathBuf::from("/usr/share/coreutils/locales/cut") + ); + } + // If the directory doesn't exist, result is None which is expected } }