Skip to content

Commit

Permalink
Auto-format code using cargo fmt
Browse files Browse the repository at this point in the history
Signed-off-by: Sandro-Alessio Gierens <sandro@gierens.de>
  • Loading branch information
gierens committed Aug 21, 2023
1 parent 8ce5ee9 commit 29b2670
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 249 deletions.
2 changes: 0 additions & 2 deletions .rustfmt.toml

This file was deleted.

25 changes: 16 additions & 9 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
extern crate uzers;
use uzers::{Users, Groups, UsersCache};
use uzers::{Groups, Users, UsersCache};

extern crate env_logger;


fn main() {
env_logger::init();

let cache = UsersCache::new();
let cache = UsersCache::new();

let current_uid = cache.get_current_uid();
println!("Your UID is {}", current_uid);
let current_uid = cache.get_current_uid();
println!("Your UID is {}", current_uid);

let you = cache.get_user_by_uid(current_uid).expect("No entry for current user!");
println!("Your username is {}", you.name().to_string_lossy());
let you = cache
.get_user_by_uid(current_uid)
.expect("No entry for current user!");
println!("Your username is {}", you.name().to_string_lossy());

let primary_group = cache.get_group_by_gid(you.primary_group_id()).expect("No entry for your primary group!");
println!("Your primary group has ID {} and name {}", primary_group.gid(), primary_group.name().to_string_lossy());
let primary_group = cache
.get_group_by_gid(you.primary_group_id())
.expect("No entry for your primary group!");
println!(
"Your primary group has ID {} and name {}",
primary_group.gid(),
primary_group.name().to_string_lossy()
);
}
25 changes: 16 additions & 9 deletions examples/groups.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
extern crate uzers;
use uzers::{Users, Group, UsersCache, get_user_groups, group_access_list};
use uzers::{get_user_groups, group_access_list, Group, Users, UsersCache};

extern crate env_logger;


fn main() {
env_logger::init();

let cache = UsersCache::new();

let user = cache.get_user_by_uid(cache.get_current_uid())
let user = cache
.get_user_by_uid(cache.get_current_uid())
.expect("No current user?");

let mut groups: Vec<Group> = get_user_groups(user.name(), user.primary_group_id())
.expect("No user groups?");
let mut groups: Vec<Group> =
get_user_groups(user.name(), user.primary_group_id()).expect("No user groups?");

groups.sort_by(|a, b| a.gid().cmp(&b.gid()));
for group in groups {
println!("Group {} has name {}", group.gid(), group.name().to_string_lossy());
println!(
"Group {} has name {}",
group.gid(),
group.name().to_string_lossy()
);
}

let mut groups = group_access_list()
.expect("Group access list");
let mut groups = group_access_list().expect("Group access list");

groups.sort_by(|a, b| a.gid().cmp(&b.gid()));
println!("\nGroup access list:");
for group in groups {
println!("Group {} has name {}", group.gid(), group.name().to_string_lossy());
println!(
"Group {} has name {}",
group.gid(),
group.name().to_string_lossy()
);
}
}
9 changes: 6 additions & 3 deletions examples/list.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
extern crate uzers;
use uzers::{User, all_users};
use uzers::{all_users, User};

extern crate env_logger;


fn main() {
env_logger::init();

let mut users: Vec<User> = unsafe { all_users() }.collect();
users.sort_by(|a, b| a.uid().cmp(&b.uid()));

for user in users {
println!("User {} has name {}", user.uid(), user.name().to_string_lossy());
println!(
"User {} has name {}",
user.uid(),
user.name().to_string_lossy()
);
}
}
51 changes: 30 additions & 21 deletions examples/os.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
extern crate uzers;
use uzers::{Users, Groups, UsersCache};
use uzers::os::unix::{UserExt, GroupExt};
use uzers::os::unix::{GroupExt, UserExt};
use uzers::{Groups, Users, UsersCache};
//use uzers::os::bsd::UserExt as BSDUserExt;

extern crate env_logger;


fn main() {
env_logger::init();

let cache = UsersCache::new();
let cache = UsersCache::new();

let current_uid = cache.get_current_uid();
println!("Your UID is {}", current_uid);
let current_uid = cache.get_current_uid();
println!("Your UID is {}", current_uid);

let you = cache.get_user_by_uid(current_uid).expect("No entry for current user!");
println!("Your username is {}", you.name().to_string_lossy());
println!("Your shell is {}", you.shell().display());
println!("Your home directory is {}", you.home_dir().display());
let you = cache
.get_user_by_uid(current_uid)
.expect("No entry for current user!");
println!("Your username is {}", you.name().to_string_lossy());
println!("Your shell is {}", you.shell().display());
println!("Your home directory is {}", you.home_dir().display());

// The two fields below are only available on BSD systems.
// Linux systems don’t have the fields in their `passwd` structs!
//println!("Your password change timestamp is {}", you.password_change_time());
//println!("Your password expiry timestamp is {}", you.password_expire_time());

let primary_group = cache.get_group_by_gid(you.primary_group_id()).expect("No entry for your primary group!");
println!("Your primary group has ID {} and name {}", primary_group.gid(), primary_group.name().to_string_lossy());

if primary_group.members().is_empty() {
println!("There are no other members of that group.");
}
else {
for username in primary_group.members() {
println!("User {} is also a member of that group.", username.to_string_lossy());
}
}
let primary_group = cache
.get_group_by_gid(you.primary_group_id())
.expect("No entry for your primary group!");
println!(
"Your primary group has ID {} and name {}",
primary_group.gid(),
primary_group.name().to_string_lossy()
);

if primary_group.members().is_empty() {
println!("There are no other members of that group.");
} else {
for username in primary_group.members() {
println!(
"User {} is also a member of that group.",
username.to_string_lossy()
);
}
}
}
17 changes: 12 additions & 5 deletions examples/switching.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
extern crate uzers;
use uzers::{get_current_uid, get_current_gid, get_effective_uid, get_effective_gid, uid_t};
use uzers::switch::switch_user_group;
use std::mem::drop;
use uzers::switch::switch_user_group;
use uzers::{get_current_gid, get_current_uid, get_effective_gid, get_effective_uid, uid_t};

extern crate env_logger;


const SAMPLE_ID: uid_t = 502;

fn main() {
Expand All @@ -24,6 +23,14 @@ fn main() {
}

fn print_state() {
println!("Current UID/GID: {}/{}", get_current_uid(), get_current_gid());
println!("Effective UID/GID: {}/{}", get_effective_uid(), get_effective_gid());
println!(
"Current UID/GID: {}/{}",
get_current_uid(),
get_current_gid()
);
println!(
"Effective UID/GID: {}/{}",
get_effective_uid(),
get_effective_gid()
);
}
19 changes: 9 additions & 10 deletions examples/threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
// spew compile errors at you.

extern crate uzers;
use uzers::{Users, UsersCache, uid_t};
use uzers::{uid_t, Users, UsersCache};

extern crate env_logger;

use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::thread;
use std::time::Duration;

const LO: uid_t = 500;
const HI: uid_t = 510;


fn main() {
env_logger::init();

Expand All @@ -39,11 +38,11 @@ fn main() {

// Loop over the range and query all the users in the range. Although we
// could use the `&User` values returned, we just ignore them.
for uid in LO .. HI {
for uid in LO..HI {
let cache = Arc::clone(&cache);

thread::spawn(move || {
let cache = cache.lock().unwrap(); // Unlock the mutex
let cache = cache.lock().unwrap(); // Unlock the mutex
let _ = cache.get_user_by_uid(uid); // Query our users cache!
});
}
Expand All @@ -53,12 +52,12 @@ fn main() {

// Loop over the same range and print out all the users we find.
// These users will be retrieved from the cache.
for uid in LO .. HI {
let cache = cache.lock().unwrap(); // Re-unlock the mutex
if let Some(u) = cache.get_user_by_uid(uid) { // Re-query our cache!
for uid in LO..HI {
let cache = cache.lock().unwrap(); // Re-unlock the mutex
if let Some(u) = cache.get_user_by_uid(uid) {
// Re-query our cache!
println!("User #{} is {}", u.uid(), u.name().to_string_lossy())
}
else {
} else {
println!("User #{} does not exist", uid);
}
}
Expand Down
Loading

0 comments on commit 29b2670

Please sign in to comment.