Skip to content

Commit

Permalink
make group member list a hashset
Browse files Browse the repository at this point in the history
vec::dedup() won't work if the input is not sorted.
  • Loading branch information
Mic92 committed Feb 1, 2025
1 parent 6da85a6 commit fe5f592
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion rust/userborn/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::{fs::File, io::Read, path::Path};

use anyhow::{Context, Result};
Expand Down Expand Up @@ -48,7 +49,7 @@ pub struct Group {
pub gid: Option<u32>,
/// The members of this group
#[serde(default)]
pub members: Vec<String>,
pub members: HashSet<String>,
}

#[derive(Deserialize, Debug)]
Expand Down
15 changes: 8 additions & 7 deletions rust/userborn/src/group.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::{
collections::{BTreeMap, BTreeSet},
fs,
Expand All @@ -13,12 +14,12 @@ pub struct Entry {
name: String,
password: String,
gid: u32,
user_list: Vec<String>,
user_list: HashSet<String>,
}

impl Entry {
/// Create a new /etc/group entry.
pub fn new(name: String, gid: u32, user_list: Vec<String>) -> Self {
pub fn new(name: String, gid: u32, user_list: HashSet<String>) -> Self {
Self {
name,
password: "x".into(),
Expand All @@ -28,7 +29,7 @@ impl Entry {
}

/// Update an /etc/group entry.
pub fn update(&mut self, user_list: Vec<String>) {
pub fn update(&mut self, user_list: HashSet<String>) {
if self.user_list != user_list {
log::info!(
"Updating members of group {} from {:?} to {user_list:?}...",
Expand Down Expand Up @@ -76,16 +77,16 @@ impl Entry {
}

/// Split a string containing group members separated by `,` into a list.
fn split_group_members(s: &str) -> Vec<String> {
fn split_group_members(s: &str) -> HashSet<String> {
if s.is_empty() {
return Vec::new();
return HashSet::new();
}
s.split(',').map(ToString::to_string).collect()
}

/// Join a list of group members into a string separating each group name with a `,`.
fn join_group_members(v: &[String]) -> String {
v.join(",")
fn join_group_members(v: &HashSet<String>) -> String {
v.clone().into_iter().collect::<Vec<_>>().join(",")
}

#[derive(Default)]
Expand Down
7 changes: 3 additions & 4 deletions rust/userborn/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod passwd;
mod password;
mod shadow;

use std::collections::HashSet;
use std::{collections::BTreeSet, io::Write, process::ExitCode};

use anyhow::{anyhow, Context, Result};
Expand Down Expand Up @@ -98,9 +99,7 @@ fn update_users_and_groups(
) {
for group_config in &config.groups {
if let Some(existing_entry) = group_db.get_mut(&group_config.name) {
let mut members = group_config.members.clone();
members.dedup();
existing_entry.update(members);
existing_entry.update(group_config.members.clone());
} else if let Err(e) = create_group(group_config, group_db) {
log::error!("Failed to create group {}: {e:#}", group_config.name);
};
Expand Down Expand Up @@ -187,7 +186,7 @@ fn create_user(
is_normal: user_config.is_normal,
name: user_config.name.clone(),
gid,
members: vec![user_config.name.clone()],
members: HashSet::from([user_config.name.clone()]),
};

create_group(&group_config, group_db)
Expand Down

0 comments on commit fe5f592

Please sign in to comment.