Merge pull request #1 from Arcterus/master

Use passwd and group from libc rather than custom definitions
This commit is contained in:
mpkh
2018-03-11 12:03:28 +04:00
committed by GitHub
10 changed files with 29 additions and 58 deletions
+6 -2
View File
@@ -1,3 +1,7 @@
language: rust
rust: nightly
matrix:
include:
- rust: nightly
os: linux
- rust: nightly
os: osx
+2 -2
View File
@@ -1,5 +1,5 @@
extern crate users;
use users::{Users, Groups, UsersCache};
extern crate rust_users;
use rust_users::{Users, Groups, UsersCache};
fn main() {
let cache = UsersCache::new();
+2 -2
View File
@@ -1,5 +1,5 @@
extern crate users;
use users::{User, AllUsers};
extern crate rust_users;
use rust_users::{User, AllUsers};
fn main() {
let mut users: Vec<User> = unsafe { AllUsers::new() }.collect();
+3 -3
View File
@@ -1,6 +1,6 @@
extern crate users;
use users::{Users, Groups, UsersCache};
use users::os::unix::{UserExt, GroupExt};
extern crate rust_users;
use rust_users::{Users, Groups, UsersCache};
use rust_users::os::unix::{UserExt, GroupExt};
//use users::os::bsd::UserExt as BSDUserExt;
fn main() {
+2 -2
View File
@@ -15,8 +15,8 @@
// 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 rust_users;
use rust_users::{Users, UsersCache, uid_t};
use std::sync::{Arc, Mutex};
use std::time::Duration;
+3 -36
View File
@@ -38,6 +38,8 @@ use std::ptr::read;
use std::sync::Arc;
use libc::{uid_t, gid_t};
use libc::passwd as c_passwd;
use libc::group as c_group;
#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
use libc::{c_char, time_t};
@@ -46,41 +48,6 @@ use libc::{c_char, time_t};
use libc::c_char;
#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
#[repr(C)]
pub struct c_passwd {
pw_name: *const c_char, // user name
pw_passwd: *const c_char, // password field
pw_uid: uid_t, // user ID
pw_gid: gid_t, // group ID
pw_change: time_t, // password change time
pw_class: *const c_char,
pw_gecos: *const c_char,
pw_dir: *const c_char, // user's home directory
pw_shell: *const c_char, // user's shell
pw_expire: time_t, // password expiry time
}
#[cfg(target_os = "linux")]
#[repr(C)]
pub struct c_passwd {
pw_name: *const c_char, // user name
pw_passwd: *const c_char, // password field
pw_uid: uid_t, // user ID
pw_gid: gid_t, // group ID
pw_gecos: *const c_char,
pw_dir: *const c_char, // user's home directory
pw_shell: *const c_char, // user's shell
}
#[repr(C)]
pub struct c_group {
gr_name: *const c_char, // group name
gr_passwd: *const c_char, // password
gr_gid: gid_t, // group id
gr_mem: *const *const c_char, // names of users in the group
}
extern {
fn getpwuid(uid: uid_t) -> *const c_passwd;
fn getpwnam(user_name: *const c_char) -> *const c_passwd;
@@ -277,7 +244,7 @@ unsafe fn struct_to_group(pointer: *const c_group) -> Option<Group> {
/// `members[1]`, and so on, until that null pointer is reached. It doesn't
/// specify whether we should expect a null pointer or a pointer to a null
/// pointer, so we check for both here!
unsafe fn members(groups: *const *const c_char) -> Vec<String> {
unsafe fn members(groups: *mut *mut c_char) -> Vec<String> {
let mut members = Vec::new();
for i in 0.. {
+2 -2
View File
@@ -24,7 +24,7 @@
//! value. In this case, switching to `&mut self` would only allow for one user
//! to be read at a time!
//!
//! ``norun
//! ```norun
//! let mut cache = UsersCache::empty_cache();
//! let uid = cache.get_current_uid(); // OK...
//! let user = cache.get_user_by_uid(uid).unwrap() // OK...
@@ -43,7 +43,7 @@
//! were just trying the same trick as earlier. A simplified implementation of
//! a user cache lookup would look something like this:
//!
//! ``norun
//! ```norun
//! fn get_user_by_uid(&self, uid: uid_t) -> Option<&User> {
//! let users = self.users.borrow_mut();
//! users.get(uid)
+3 -3
View File
@@ -32,7 +32,7 @@
//! Here is a complete example that prints out the current users name:
//!
//! ```rust
//! use users::{get_user_by_uid, get_current_uid};
//! use rust_users::{get_user_by_uid, get_current_uid};
//! let user = get_user_by_uid(get_current_uid()).unwrap();
//! println!("Hello, {}!", user.name());
//! ```
@@ -63,7 +63,7 @@
//! methods on it. For example:
//!
//! ```rust
//! use users::{Users, Groups, UsersCache};
//! use rust_users::{Users, Groups, UsersCache};
//! let mut cache = UsersCache::new();
//! let uid = cache.get_current_uid();
//! let user = cache.get_user_by_uid(uid).unwrap();
@@ -87,7 +87,7 @@
//! And again, a complete example:
//!
//! ```no_run
//! use users::{Users, Groups, UsersCache};
//! use rust_users::{Users, Groups, UsersCache};
//! let mut cache = UsersCache::new();
//! let group = cache.get_group_by_name("admin").expect("No such group 'admin'!");
//! println!("The '{}' group has the ID {}", group.name(), group.gid());
+5 -5
View File
@@ -17,8 +17,8 @@
//! `add_user` and `add_group` to the object:
//!
//! ```rust
//! use users::mock::{MockUsers, User, Group};
//! use users::os::unix::{UserExt, GroupExt};
//! use rust_users::mock::{MockUsers, User, Group};
//! use rust_users::os::unix::{UserExt, GroupExt};
//! use std::sync::Arc;
//!
//! let mut users = MockUsers::with_current_uid(1000);
@@ -39,9 +39,9 @@
//! Here's a complete example:
//!
//! ```rust
//! use users::{Users, UsersCache, User};
//! use users::os::unix::UserExt;
//! use users::mock::MockUsers;
//! use rust_users::{Users, UsersCache, User};
//! use rust_users::os::unix::UserExt;
//! use rust_users::mock::MockUsers;
//! use std::sync::Arc;
//!
//! fn print_current_username<U: Users>(users: &mut U) {
+1 -1
View File
@@ -125,7 +125,7 @@ impl Drop for SwitchUserGuard {
/// ### Examples
///
/// ```no_run
/// use users::switch::switch_user_group;
/// use rust_users::switch::switch_user_group;
///
/// {
/// let _guard = switch_user_group(1001, 1001);