Skip to content

Commit

Permalink
Do not crash if the file-based locales database is empty (#1213)
Browse files Browse the repository at this point in the history
Improve #1205 so that it does not crash if there are no valid locales in
the `/etc/agama.d/locales` file.
  • Loading branch information
imobachgs authored May 15, 2024
2 parents ac03083 + 71728a2 commit 3ef5965
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
30 changes: 22 additions & 8 deletions rust/agama-server/src/l10n/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<LocaleId>::try_into(line).ok())
.collect();
self.known_locales = Self::get_locales_list()?;
self.locales = self.get_locales(ui_language)?;
Ok(())
}
Expand Down Expand Up @@ -108,18 +105,35 @@ impl LocalesDatabase {
Ok(result)
}

fn get_locales_list() -> Result<String, Error> {
fn get_locales_list() -> Result<Vec<LocaleId>, 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<LocaleId> {
locales
.lines()
.filter_map(|line| TryInto::<LocaleId>::try_into(line).ok())
.collect()
}
}

Expand Down
6 changes: 6 additions & 0 deletions rust/package/agama.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Wed May 15 14:08:26 UTC 2024 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

- 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 <igonzalezsosa@suse.com>

Expand Down

0 comments on commit 3ef5965

Please sign in to comment.