From 8a9d6f90c94fab8a6cd56c556a3257e79bf26676 Mon Sep 17 00:00:00 2001 From: Ben S Date: Sun, 3 May 2015 12:42:37 +0100 Subject: [PATCH] Start using uid_t and gid_t They were i32 and u32 before. Or was that the other way around? Anyway, this changes the interface, so I'm bumping the version. --- Cargo.toml | 2 +- README.md | 2 +- src/lib.rs | 53 +++++++++++++++++++++++++++-------------------------- src/mock.rs | 15 ++++++++------- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d5def57..629fd6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ documentation = "http://bsago.me/doc/users/" homepage = "https://github.com/ogham/rust-users" license = "MIT" readme = "README.md" -version = "0.3.2" +version = "0.4.0" [dependencies] libc = "0.1.1" diff --git a/README.md b/README.md index 34d7eb9..5091f48 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ read-only. ## Users -The function `get_current_uid` returns a `i32` value representing the user +The function `get_current_uid` returns a `uid_t` value representing the user currently running the program, and the `get_user_by_uid` function scans the users database and returns a User object with the user's information. This function returns `None` when there is no user for that ID. diff --git a/src/lib.rs b/src/lib.rs index 5600b99..845ea36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ //! Users //! ----- //! -//! The function `get_current_uid` returns a `i32` value representing the user +//! The function `get_current_uid` returns a `uid_t` value representing the user //! currently running the program, and the `get_user_by_uid` function scans the //! users database and returns a User object with the user's information. This //! function returns `None` when there is no user for that ID. @@ -113,7 +113,8 @@ //! edge cases. extern crate libc; -use libc::{c_char, c_int, uid_t, gid_t, time_t}; +pub use libc::{uid_t, gid_t}; +use libc::{c_char, time_t}; extern crate collections; use collections::borrow::ToOwned; @@ -130,19 +131,19 @@ pub mod mock; pub trait Users { /// Return a User object if one exists for the given user ID; otherwise, return None. - fn get_user_by_uid(&mut self, uid: i32) -> Option; + fn get_user_by_uid(&mut self, uid: uid_t) -> Option; /// Return a User object if one exists for the given username; otherwise, return None. fn get_user_by_name(&mut self, username: &str) -> Option; /// Return a Group object if one exists for the given group ID; otherwise, return None. - fn get_group_by_gid(&mut self, gid: u32) -> Option; + fn get_group_by_gid(&mut self, gid: gid_t) -> Option; /// Return a Group object if one exists for the given groupname; otherwise, return None. fn get_group_by_name(&mut self, group_name: &str) -> Option; /// Return the user ID for the user running the process. - fn get_current_uid(&mut self) -> i32; + fn get_current_uid(&mut self) -> uid_t; /// Return the username of the user running the process. fn get_current_username(&mut self) -> Option; @@ -152,8 +153,8 @@ pub trait Users { struct c_passwd { pub pw_name: *const c_char, // login name pub pw_passwd: *const c_char, - pub pw_uid: c_int, // user ID - pub pw_gid: c_int, // group ID + pub pw_uid: uid_t, // user ID + pub pw_gid: gid_t, // group ID pub pw_change: time_t, pub pw_class: *const c_char, pub pw_gecos: *const c_char, // full name @@ -171,13 +172,13 @@ struct c_group { } extern { - fn getpwuid(uid: c_int) -> *const c_passwd; + fn getpwuid(uid: uid_t) -> *const c_passwd; fn getpwnam(user_name: *const c_char) -> *const c_passwd; - fn getgrgid(gid: uid_t) -> *const c_group; + fn getgrgid(gid: gid_t) -> *const c_group; fn getgrnam(group_name: *const c_char) -> *const c_group; - fn getuid() -> c_int; + fn getuid() -> uid_t; } #[derive(Clone)] @@ -185,13 +186,13 @@ extern { pub struct User { /// This user's ID - pub uid: i32, + pub uid: uid_t, /// This user's name pub name: String, /// The ID of this user's primary group - pub primary_group: u32, + pub primary_group: gid_t, } /// Information about a particular group. @@ -199,7 +200,7 @@ pub struct User { pub struct Group { /// This group's ID - pub gid: u32, + pub gid: uid_t, /// This group's name pub name: String, @@ -211,13 +212,13 @@ pub struct Group { /// A producer of user and group instances that caches every result. #[derive(Clone)] pub struct OSUsers { - users: HashMap>, - users_back: HashMap>, + users: HashMap>, + users_back: HashMap>, - groups: HashMap>, - groups_back: HashMap>, + groups: HashMap>, + groups_back: HashMap>, - uid: Option, + uid: Option, } unsafe fn from_raw_buf(p: *const i8) -> String { @@ -227,7 +228,7 @@ unsafe fn from_raw_buf(p: *const i8) -> String { unsafe fn passwd_to_user(pointer: *const c_passwd) -> Option { if !pointer.is_null() { let pw = read(pointer); - Some(User { uid: pw.pw_uid, name: from_raw_buf(pw.pw_name as *const i8), primary_group: pw.pw_gid as u32 }) + Some(User { uid: pw.pw_uid as uid_t, name: from_raw_buf(pw.pw_name as *const i8), primary_group: pw.pw_gid as gid_t }) } else { None @@ -273,10 +274,10 @@ unsafe fn members(groups: *const *const c_char) -> Vec { } impl Users for OSUsers { - fn get_user_by_uid(&mut self, uid: i32) -> Option { + fn get_user_by_uid(&mut self, uid: uid_t) -> Option { match self.users.entry(uid) { Vacant(entry) => { - let user = unsafe { passwd_to_user(getpwuid(uid as i32)) }; + let user = unsafe { passwd_to_user(getpwuid(uid)) }; match user { Some(user) => { entry.insert(Some(user.clone())); @@ -318,7 +319,7 @@ impl Users for OSUsers { } } - fn get_group_by_gid(&mut self, gid: u32) -> Option { + fn get_group_by_gid(&mut self, gid: gid_t) -> Option { match self.groups.clone().entry(gid) { Vacant(entry) => { let group = unsafe { struct_to_group(getgrgid(gid)) }; @@ -363,7 +364,7 @@ impl Users for OSUsers { } } - fn get_current_uid(&mut self) -> i32 { + fn get_current_uid(&mut self) -> uid_t { match self.uid { Some(uid) => uid, None => { @@ -395,7 +396,7 @@ impl OSUsers { } /// Return a User object if one exists for the given user ID; otherwise, return None. -pub fn get_user_by_uid(uid: i32) -> Option { +pub fn get_user_by_uid(uid: uid_t) -> Option { OSUsers::empty_cache().get_user_by_uid(uid) } @@ -405,7 +406,7 @@ pub fn get_user_by_name(username: &str) -> Option { } /// Return a Group object if one exists for the given group ID; otherwise, return None. -pub fn get_group_by_gid(gid: u32) -> Option { +pub fn get_group_by_gid(gid: gid_t) -> Option { OSUsers::empty_cache().get_group_by_gid(gid) } @@ -415,7 +416,7 @@ pub fn get_group_by_name(group_name: &str) -> Option { } /// Return the user ID for the user running the process. -pub fn get_current_uid() -> i32 { +pub fn get_current_uid() -> uid_t { OSUsers::empty_cache().get_current_uid() } diff --git a/src/mock.rs b/src/mock.rs index 98a6937..f8d44fe 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -52,17 +52,18 @@ pub use super::{Users, User, Group}; use std::collections::HashMap; +use libc::{uid_t, gid_t}; /// A mocking users object that you can add your own users and groups to. pub struct MockUsers { - users: HashMap, - groups: HashMap, - uid: i32, + users: HashMap, + groups: HashMap, + uid: uid_t, } impl MockUsers { /// Create a new, empty mock users object. - pub fn with_current_uid(current_uid: i32) -> MockUsers { + pub fn with_current_uid(current_uid: uid_t) -> MockUsers { MockUsers { users: HashMap::new(), groups: HashMap::new(), @@ -82,7 +83,7 @@ impl MockUsers { } impl Users for MockUsers { - fn get_user_by_uid(&mut self, uid: i32) -> Option { + fn get_user_by_uid(&mut self, uid: uid_t) -> Option { self.users.get(&uid).cloned() } @@ -90,7 +91,7 @@ impl Users for MockUsers { self.users.values().find(|u| u.name == username).cloned() } - fn get_group_by_gid(&mut self, gid: u32) -> Option { + fn get_group_by_gid(&mut self, gid: gid_t) -> Option { self.groups.get(&gid).cloned() } @@ -98,7 +99,7 @@ impl Users for MockUsers { self.groups.values().find(|g| g.name == group_name).cloned() } - fn get_current_uid(&mut self) -> i32 { + fn get_current_uid(&mut self) -> uid_t { self.uid }