Vendor dependencies

Let's see how I like this workflow.
This commit is contained in:
John Doty 2022-12-19 08:27:18 -08:00
parent 34d1830413
commit 9c435dc440
7500 changed files with 1665121 additions and 99 deletions

20
vendor/users/examples/example.rs vendored Normal file
View file

@ -0,0 +1,20 @@
extern crate users;
use users::{Users, Groups, UsersCache};
extern crate env_logger;
fn main() {
env_logger::init();
let cache = UsersCache::new();
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 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());
}

31
vendor/users/examples/groups.rs vendored Normal file
View file

@ -0,0 +1,31 @@
extern crate users;
use users::{Users, Group, UsersCache, get_user_groups, group_access_list};
extern crate env_logger;
fn main() {
env_logger::init();
let cache = UsersCache::new();
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?");
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());
}
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());
}
}

16
vendor/users/examples/list.rs vendored Normal file
View file

@ -0,0 +1,16 @@
extern crate users;
use users::{User, all_users};
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());
}
}

38
vendor/users/examples/os.rs vendored Normal file
View file

@ -0,0 +1,38 @@
extern crate users;
use users::{Users, Groups, UsersCache};
use users::os::unix::{UserExt, GroupExt};
//use users::os::bsd::UserExt as BSDUserExt;
extern crate env_logger;
fn main() {
env_logger::init();
let cache = UsersCache::new();
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());
// The two fields below are only available on BSD systems.
// Linux systems dont 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());
}
}
}

29
vendor/users/examples/switching.rs vendored Normal file
View file

@ -0,0 +1,29 @@
extern crate users;
use users::{get_current_uid, get_current_gid, get_effective_uid, get_effective_gid, uid_t};
use users::switch::switch_user_group;
use std::mem::drop;
extern crate env_logger;
const SAMPLE_ID: uid_t = 502;
fn main() {
env_logger::init();
println!("\nInitial values:");
print_state();
println!("\nValues after switching:");
let guard = switch_user_group(SAMPLE_ID, SAMPLE_ID);
print_state();
println!("\nValues after switching back:");
drop(guard);
print_state();
}
fn print_state() {
println!("Current UID/GID: {}/{}", get_current_uid(), get_current_gid());
println!("Effective UID/GID: {}/{}", get_effective_uid(), get_effective_gid());
}

65
vendor/users/examples/threading.rs vendored Normal file
View file

@ -0,0 +1,65 @@
//! This example demonstrates how to use a `UsersCache` cache in a
//! multi-threaded situation. The cache uses `RefCell`s internally, so it
//! is distinctly not thread-safe. Instead, youll need to place it within
//! some kind of lock in order to have threads access it one-at-a-time.
//!
//! It queries all the users it can find in the range 500..510. This is the
//! default uid range on my Apple laptop -- Linux starts counting from 1000,
//! but I cant include both in the range! It spawns one thread per user to
//! query, with each thread accessing the same cache.
//!
//! Then, afterwards, it retrieves references to the users that had been
//! cached earlier.
// For extra fun, try uncommenting some of the lines of code below, making
// the code try to access the users cache *without* a Mutex, and see it
// spew compile errors at you.
extern crate users;
use users::{Users, UsersCache, uid_t};
extern crate env_logger;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::thread;
const LO: uid_t = 500;
const HI: uid_t = 510;
fn main() {
env_logger::init();
// For thread-safely, our users cache needs to be within a Mutex, so
// only one thread can access it once. This Mutex needs to be within an
// Arc, so multiple threads can access the Mutex.
let cache = Arc::new(Mutex::new(UsersCache::new()));
// let cache = UsersCache::empty_cache();
// 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 {
let cache = Arc::clone(&cache);
thread::spawn(move || {
let cache = cache.lock().unwrap(); // Unlock the mutex
let _ = cache.get_user_by_uid(uid); // Query our users cache!
});
}
// Wait for all the threads to finish.
thread::sleep(Duration::from_millis(100));
// 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!
println!("User #{} is {}", u.uid(), u.name().to_string_lossy())
}
else {
println!("User #{} does not exist", uid);
}
}
}