| 1 |
From: Sylvestre Ledru <[email protected]> |
| 2 |
Subject: Speedup locale reads |
| 3 |
|
| 4 |
Install locales to a specific directory and read from that |
| 5 |
instead of testing different directories. |
| 6 |
|
| 7 |
--- |
| 8 |
The information above should follow the Patch Tagging Guidelines, please |
| 9 |
checkout https://dep.debian.net/deps/dep3/ to learn about the format. Here |
| 10 |
are templates for supplementary fields that you might want to add: |
| 11 |
|
| 12 |
Origin: upstream, https://salsa.debian.org/rust-team/coreutils/-/blob/master/debian/patches/fix-locale-path.diff?ref_type=heads |
| 13 |
Forwarded: not-needed |
| 14 |
Reviewed-By: Simon Johnsson <[email protected]> |
| 15 |
|
| 16 |
=================================================================== |
| 17 |
--- a/GNUmakefile |
| 18 |
+++ b/GNUmakefile |
| 19 |
@@ -35,6 +35,7 @@ PREFIX ?= /usr |
| 20 |
DESTDIR ?= |
| 21 |
BINDIR ?= $(PREFIX)/bin |
| 22 |
DATAROOTDIR ?= $(PREFIX)/share |
| 23 |
+LOCALEDIR ?= $(DATAROOTDIR)/coreutils/locales |
| 24 |
LIBSTDBUF_DIR ?= $(PREFIX)/libexec/$(PROG_PREFIX)coreutils |
| 25 |
# Export variable so that it is used during the build |
| 26 |
export LIBSTDBUF_DIR |
| 27 |
@@ -230,7 +231,7 @@ endif |
| 28 |
|
| 29 |
ifeq ($(LOCALES),y) |
| 30 |
locales: |
| 31 |
- @# Copy uucore common locales |
| 32 |
+ # Copy uucore common locales |
| 33 |
@if [ -d "$(BASEDIR)/src/uucore/locales" ]; then \ |
| 34 |
$(INSTALL) -d "$(BUILDDIR)/locales/uucore"; \ |
| 35 |
for locale_file in "$(BASEDIR)"/src/uucore/locales/*.ftl; do \ |
| 36 |
@@ -259,31 +260,38 @@ locales: |
| 37 |
# - *sum binaries need the files from checksum_common |
| 38 |
INSTALLEES_WITH_EXTRA_LOCALE = \ |
| 39 |
$(INSTALLEES) \ |
| 40 |
- $(if $(findstring sum, $(INSTALLEES)),checksum_common, ) |
| 41 |
+ $(if $(findstring sum,$(INSTALLEES)),checksum_common,) |
| 42 |
+ |
| 43 |
+UU_LOCALES := $(wildcard $(BASEDIR)/l10n/src/uu/*/locales/*.ftl) |
| 44 |
+UUC_CORE_LOCALES := $(wildcard $(BASEDIR)/l10n/src/uucore/locales/*) |
| 45 |
+ |
| 46 |
+UU_LOCALES_NO_EN := $(filter-out %/en-US.ftl,$(UU_LOCALES)) |
| 47 |
+ |
| 48 |
+define uu_install_locale |
| 49 |
+$(eval prog := $(notdir $(patsubst %/locales/,%,$(dir $(dir $(1)))))) |
| 50 |
+mkdir -p $(DESTDIR)$(LOCALEDIR)/$(prog) |
| 51 |
+$(INSTALL) -m 644 $(1) $(DESTDIR)$(LOCALEDIR)/$(prog)/ |
| 52 |
+endef |
| 53 |
+ |
| 54 |
install-locales: |
| 55 |
+ @mkdir -p $(DESTDIR)$(LOCALEDIR) |
| 56 |
@# Install lazy error locales shared by all utilities |
| 57 |
@if [ -d "$(BASEDIR)/src/uucore/locales/errors" ]; then \ |
| 58 |
- $(INSTALL) -d "$(DESTDIR)$(DATAROOTDIR)/locales/uucore/errors"; \ |
| 59 |
+ $(INSTALL) -d "$(DESTDIR)$(LOCALEDIR)/uucore/errors"; \ |
| 60 |
for locale_file in "$(BASEDIR)"/src/uucore/locales/errors/*.ftl; do \ |
| 61 |
if [ "$$(basename "$$locale_file")" != "en-US.ftl" ]; then \ |
| 62 |
- $(INSTALL) -m 644 "$$locale_file" "$(DESTDIR)$(DATAROOTDIR)/locales/uucore/errors/"; \ |
| 63 |
+ $(INSTALL) -m 644 "$$locale_file" "$(DESTDIR)$(LOCALEDIR)/uucore/errors/"; \ |
| 64 |
fi; \ |
| 65 |
done; \ |
| 66 |
fi |
| 67 |
- @for prog in $(INSTALLEES_WITH_EXTRA_LOCALE); do \ |
| 68 |
- if [ -d "$(BASEDIR)/l10n/src/uu/$$prog/locales" ]; then \ |
| 69 |
- $(INSTALL) -d "$(DESTDIR)$(DATAROOTDIR)/locales/$$prog"; \ |
| 70 |
- for locale_file in "$(BASEDIR)"/l10n/src/uu/$$prog/locales/*.ftl; do \ |
| 71 |
- if [ "$$(basename "$$locale_file")" != "en-US.ftl" ]; then \ |
| 72 |
- $(INSTALL) -m 644 "$$locale_file" "$(DESTDIR)$(DATAROOTDIR)/locales/$$prog/"; \ |
| 73 |
- fi; \ |
| 74 |
- done; \ |
| 75 |
- fi; \ |
| 76 |
- done |
| 77 |
- if [ -d "$(BASEDIR)/l10n/src/uucore/locales" ]; then \ |
| 78 |
- mkdir -p "$(DESTDIR)$(DATAROOTDIR)/locales/uucore"; \ |
| 79 |
- $(INSTALL) -m 644 "$(BASEDIR)"/l10n/src/uucore/locales/* "$(DESTDIR)$(DATAROOTDIR)/locales/uucore/"; \ |
| 80 |
- fi; |
| 81 |
+ @$(foreach f,$(UU_LOCALES_NO_EN),$(call uu_install_locale,$(f));) |
| 82 |
+ @if [ -d "$(BASEDIR)/l10n/src/uucore/locales" ]; then \ |
| 83 |
+ mkdir -p "$(DESTDIR)$(LOCALEDIR)/uucore"; \ |
| 84 |
+ for f in $(UUC_CORE_LOCALES); do \ |
| 85 |
+ [ -e "$$f" ] || continue; \ |
| 86 |
+ $(INSTALL) -m 644 "$$f" "$(DESTDIR)$(LOCALEDIR)/uucore/"; \ |
| 87 |
+ done \ |
| 88 |
+ fi |
| 89 |
else |
| 90 |
locales: |
| 91 |
install-locales: |
| 92 |
=================================================================== |
| 93 |
--- a/src/uucore/src/lib/mods/locale.rs |
| 94 |
+++ b/src/uucore/src/lib/mods/locale.rs |
| 95 |
@@ -16,6 +16,7 @@ use std::path::{Path, PathBuf}; |
| 96 |
use std::str::FromStr; |
| 97 |
use std::sync::OnceLock; |
| 98 |
|
| 99 |
+#[cfg(any(debug_assertions, test))] |
| 100 |
use os_display::Quotable; |
| 101 |
use thiserror::Error; |
| 102 |
use unic_langid::LanguageIdentifier; |
| 103 |
@@ -221,23 +222,33 @@ thread_local! { |
| 104 |
const { OnceLock::new() }; |
| 105 |
} |
| 106 |
|
| 107 |
-/// Helper function to find the uucore locales directory from a utility's locales directory |
| 108 |
+/// Helper function to find the uucore locales directory |
| 109 |
fn find_uucore_locales_dir(utility_locales_dir: &Path) -> Option<PathBuf> { |
| 110 |
- // Normalize the path to get absolute path |
| 111 |
- let normalized_dir = utility_locales_dir |
| 112 |
- .canonicalize() |
| 113 |
- .unwrap_or_else(|_| utility_locales_dir.to_path_buf()); |
| 114 |
- |
| 115 |
- // Walk up: locales -> printenv -> uu -> src |
| 116 |
- let uucore_locales = normalized_dir |
| 117 |
- .parent()? // printenv |
| 118 |
- .parent()? // uu |
| 119 |
- .parent()? // src |
| 120 |
- .join("uucore") |
| 121 |
- .join("locales"); |
| 122 |
+ #[cfg(debug_assertions)] |
| 123 |
+ { |
| 124 |
+ // During development, walk up from the utility's locales directory |
| 125 |
+ let normalized_dir = utility_locales_dir |
| 126 |
+ .canonicalize() |
| 127 |
+ .unwrap_or_else(|_| utility_locales_dir.to_path_buf()); |
| 128 |
+ |
| 129 |
+ // Walk up: locales -> printenv -> uu -> src |
| 130 |
+ let uucore_locales = normalized_dir |
| 131 |
+ .parent()? // printenv |
| 132 |
+ .parent()? // uu |
| 133 |
+ .parent()? // src |
| 134 |
+ .join("uucore") |
| 135 |
+ .join("locales"); |
| 136 |
|
| 137 |
- // Only return if the directory actually exists |
| 138 |
- uucore_locales.exists().then_some(uucore_locales) |
| 139 |
+ // Only return if the directory actually exists |
| 140 |
+ uucore_locales.exists().then_some(uucore_locales) |
| 141 |
+ } |
| 142 |
+ |
| 143 |
+ #[cfg(not(debug_assertions))] |
| 144 |
+ { |
| 145 |
+ let _ = utility_locales_dir; |
| 146 |
+ let uucore_locales = PathBuf::from("/usr/share/coreutils/locales/uucore"); |
| 147 |
+ uucore_locales.exists().then_some(uucore_locales) |
| 148 |
+ } |
| 149 |
} |
| 150 |
|
| 151 |
/// Create a bundle that combines common and utility-specific strings |
| 152 |
@@ -666,25 +677,11 @@ pub fn setup_localization(p: &str) -> Re |
| 153 |
} |
| 154 |
|
| 155 |
#[cfg(not(debug_assertions))] |
| 156 |
-fn resolve_locales_dir_from_exe_dir(exe_dir: &Path, p: &str) -> Option<PathBuf> { |
| 157 |
- // 1. <bindir>/locales/<prog> |
| 158 |
- let coreutils = exe_dir.join("locales").join(p); |
| 159 |
- if coreutils.is_dir() { |
| 160 |
- return Some(coreutils); |
| 161 |
- } |
| 162 |
- |
| 163 |
- // 2. <prefix>/share/locales/<prog> |
| 164 |
- if let Some(prefix) = exe_dir.parent() { |
| 165 |
- let fhs = prefix.join("share").join("locales").join(p); |
| 166 |
- if fhs.is_dir() { |
| 167 |
- return Some(fhs); |
| 168 |
- } |
| 169 |
- } |
| 170 |
- |
| 171 |
- // 3. <bindir>/<prog> (legacy fall-back) |
| 172 |
- let fallback = exe_dir.join(p); |
| 173 |
- if fallback.is_dir() { |
| 174 |
- return Some(fallback); |
| 175 |
+fn resolve_locales_dir(p: &str) -> Option<PathBuf> { |
| 176 |
+ // Only look in /usr/share/coreutils/locales/<prog> |
| 177 |
+ let locales_dir = PathBuf::from("/usr/share/coreutils/locales").join(p); |
| 178 |
+ if locales_dir.exists() { |
| 179 |
+ return Some(locales_dir) |
| 180 |
} |
| 181 |
|
| 182 |
None |
| 183 |
@@ -728,23 +725,13 @@ fn get_locales_dir(p: &str) -> Result<Pa |
| 184 |
|
| 185 |
#[cfg(not(debug_assertions))] |
| 186 |
{ |
| 187 |
- use std::env; |
| 188 |
- // In release builds, look relative to executable |
| 189 |
- let exe_path = env::current_exe().map_err(|e| { |
| 190 |
- LocalizationError::PathResolution(format!("Failed to get executable path: {e}")) |
| 191 |
- })?; |
| 192 |
- |
| 193 |
- let exe_dir = exe_path.parent().ok_or_else(|| { |
| 194 |
- LocalizationError::PathResolution("Failed to get executable directory".to_string()) |
| 195 |
- })?; |
| 196 |
- |
| 197 |
- if let Some(dir) = resolve_locales_dir_from_exe_dir(exe_dir, p) { |
| 198 |
+ // In release builds, only look in /usr/share/coreutils/locales/<prog> |
| 199 |
+ if let Some(dir) = resolve_locales_dir(p) { |
| 200 |
return Ok(dir); |
| 201 |
} |
| 202 |
|
| 203 |
Err(LocalizationError::LocalesDirNotFound(format!( |
| 204 |
- "Release locales directory not found starting from {}", |
| 205 |
- exe_dir.quote() |
| 206 |
+ "Locales directory not found at /usr/share/coreutils/locales/{p}" |
| 207 |
))) |
| 208 |
} |
| 209 |
} |
| 210 |
@@ -1849,24 +1836,18 @@ invalid-syntax = This is { $missing |
| 211 |
#[cfg(all(test, not(debug_assertions)))] |
| 212 |
mod fhs_tests { |
| 213 |
use super::*; |
| 214 |
- use tempfile::TempDir; |
| 215 |
|
| 216 |
#[test] |
| 217 |
- fn resolves_fhs_share_locales_layout() { |
| 218 |
- // 1. Set up a fake installation prefix in a temp directory |
| 219 |
- let prefix = TempDir::new().unwrap(); // e.g. /tmp/xyz |
| 220 |
- let bin_dir = prefix.path().join("bin"); // /tmp/xyz/bin |
| 221 |
- let share_dir = prefix.path().join("share").join("locales").join("cut"); // /tmp/xyz/share/locales/cut |
| 222 |
- std::fs::create_dir_all(&share_dir).unwrap(); |
| 223 |
- std::fs::create_dir_all(&bin_dir).unwrap(); |
| 224 |
- |
| 225 |
- // 2. Pretend the executable lives in <prefix>/bin |
| 226 |
- let exe_dir = bin_dir.as_path(); |
| 227 |
- |
| 228 |
- // 3. Ask the helper to resolve the locales dir |
| 229 |
- let result = resolve_locales_dir_from_exe_dir(exe_dir, "cut") |
| 230 |
- .expect("should find locales via FHS path"); |
| 231 |
- |
| 232 |
- assert_eq!(result, share_dir); |
| 233 |
+ fn resolves_fixed_locales_path() { |
| 234 |
+ // Test that resolve_locales_dir returns the expected fixed path |
| 235 |
+ // This test only passes if /usr/share/coreutils/locales/<prog> exists on the system |
| 236 |
+ let result = resolve_locales_dir("cut"); |
| 237 |
+ if let Some(dir) = result { |
| 238 |
+ assert_eq!( |
| 239 |
+ dir, |
| 240 |
+ PathBuf::from("/usr/share/coreutils/locales/cut") |
| 241 |
+ ); |
| 242 |
+ } |
| 243 |
+ // If the directory doesn't exist, result is None which is expected |
| 244 |
} |
| 245 |
} |