From 8333d240549521dbb6f4906fdb4bb3256fb4ad40 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sun, 10 Mar 2024 08:48:59 -0600 Subject: [PATCH 1/2] Remove unused features from the Nix crate. And update it to the latest verison. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 41b330a..7556b19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ serialize = ["serde", "serde_json"] [dependencies] libc = "0.2" -nix = "0.26" +nix = { version = "0.28", default-features = false, features = [ "signal" ] } number_prefix = "0.4" sysctl = "0.5" serde = { version="1.0", features = ["derive"], optional=true } From 2c4b90d57e968e35a8cbdb4d4067b668522820bf Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sun, 10 Mar 2024 09:19:22 -0600 Subject: [PATCH 2/2] Eliminate dependency on the unmaintained "users" crate Use Nix's "user" feature instead, since we're already using Nix. Fixes #50 --- Cargo.toml | 3 +-- src/lib.rs | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7556b19..ee2e0b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,9 @@ serialize = ["serde", "serde_json"] [dependencies] libc = "0.2" -nix = { version = "0.28", default-features = false, features = [ "signal" ] } +nix = { version = "0.28", default-features = false, features = [ "signal", "user" ] } number_prefix = "0.4" sysctl = "0.5" serde = { version="1.0", features = ["derive"], optional=true } serde_json = { version="1.0", optional=true } thiserror = "1.0" -users = "0.11.0" diff --git a/src/lib.rs b/src/lib.rs index 334fe39..8c69eab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,32 +108,37 @@ pub enum Error { mod subject { use super::ParseError; use std::fmt; - use users::{get_user_by_name, get_user_by_uid}; + use nix::unistd::{self, Uid}; /// Represents a user subject #[derive(Clone, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serialize", derive(serde::Serialize))] - pub struct User(pub users::uid_t); + pub struct User(pub Uid); impl User { pub fn from_uid(uid: libc::uid_t) -> User { - User(uid as users::uid_t) + User(Uid::from_raw(uid)) } pub fn from_name(name: &str) -> Result { - let uid = get_user_by_name(name) + let uid = unistd::User::from_name(name) + // Note: the only documented error that getpwnam_r may return is + // ERANGE, and Nix is supposed to handle that one, so it should + // "never" return Err + .unwrap_or(None) .ok_or_else(|| ParseError::UnknownUser(name.into()))? - .uid(); + .uid; - Ok(User::from_uid(uid)) + Ok(User(uid)) } } impl fmt::Display for User { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match get_user_by_uid(self.0) { - Some(user) => write!(f, "user:{}", user.name().to_str().ok_or(fmt::Error)?), - None => write!(f, "user:{}", self.0), + match unistd::User::from_uid(self.0) { + Err(e) => write!(f, "user: <{}>", e), + Ok(Some(user)) => write!(f, "user:{}", user.name), + Ok(None) => write!(f, "user:{}", self.0), } } }