From d5ba67ed93bb62fb34454f16545b782dad809f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 15 May 2024 15:05:20 +0100 Subject: [PATCH 1/2] rust: do not crash if the file-based locales database is empty --- rust/agama-server/src/l10n/locale.rs | 30 ++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/rust/agama-server/src/l10n/locale.rs b/rust/agama-server/src/l10n/locale.rs index 98a013c701..04def8b22f 100644 --- a/rust/agama-server/src/l10n/locale.rs +++ b/rust/agama-server/src/l10n/locale.rs @@ -42,10 +42,7 @@ impl LocalesDatabase { /// /// * `ui_language`: language to translate the descriptions (e.g., "en"). pub fn read(&mut self, ui_language: &str) -> Result<(), Error> { - self.known_locales = Self::get_locales_list()? - .lines() - .filter_map(|line| TryInto::::try_into(line).ok()) - .collect(); + self.known_locales = Self::get_locales_list()?; self.locales = self.get_locales(ui_language)?; Ok(()) } @@ -108,18 +105,35 @@ impl LocalesDatabase { Ok(result) } - fn get_locales_list() -> Result { + fn get_locales_list() -> Result, Error> { const LOCALES_LIST_PATH: &str = "/etc/agama.d/locales"; - if let Ok(locales) = fs::read_to_string(LOCALES_LIST_PATH) { - return Ok(locales); + let locales = fs::read_to_string(LOCALES_LIST_PATH) + .map(|content| Self::get_locales_from_string(content)); + + if let Ok(locales) = locales { + if !locales.is_empty() { + return Ok(locales); + } } let result = Command::new("localectl") .args(["list-locales"]) .output() .context("Failed to get the list of locales")?; - Ok(String::from_utf8(result.stdout).context("Invalid UTF-8 sequence from list-locales")?) + + let locales = String::from_utf8(result.stdout) + .map(|content| Self::get_locales_from_string(content)) + .context("Invalid UTF-8 sequence from list-locales")?; + + Ok(locales) + } + + fn get_locales_from_string(locales: String) -> Vec { + locales + .lines() + .filter_map(|line| TryInto::::try_into(line).ok()) + .collect() } } From 71728a284017dca58a67a61cc0373d8e5f1a1c2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 15 May 2024 15:09:44 +0100 Subject: [PATCH 2/2] rust: update changes file --- rust/package/agama.changes | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/package/agama.changes b/rust/package/agama.changes index 70203c5966..9857943e3d 100644 --- a/rust/package/agama.changes +++ b/rust/package/agama.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Wed May 15 14:08:26 UTC 2024 - Imobach Gonzalez Sosa + +- Do not crash if the /etc/agama.d/locales file does not contain + any valid locale (gh#openSUSE/agama#1213). + ------------------------------------------------------------------- Tue May 14 12:39:49 UTC 2024 - Imobach Gonzalez Sosa