Merge pull request #1402 from tmccombs/nix-users
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thayne McCombs · committed by GitHub · parents 8f32a75, 054bae0 · Browse files · 4 files·+26 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- maint: Remove use of unmaintained users crate
Diff stats
4 files+26 added−29 removed
- tests:+10 added−8 removed
- other:+16 added−21 removed
Provenance
- Commit
5e0018f- Tree
01afed1- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
8f32a75- Parent 2
054bae0
4 files changed+26 added−29 removed
modified:src/filter/owner.rs+15 added-8 removed
1 use anyhow::{anyhow, Result};2+use nix::unistd::{Group, User};3 use std::fs;4 5 #[derive(Clone, Copy, Debug, PartialEq, Eq)]6 pub struct OwnerFilter {28 unmodified lines35 ));36 }37 38 let uid = Check::parse(fst, |s| {38- s.parse()39- .ok()40- .or_else(|| users::get_user_by_name(s).map(|user| user.uid()))41- .ok_or_else(|| anyhow!("'{}' is not a recognized user name", s))39+ if let Ok(uid) = s.parse() {40+ Ok(uid)41+ } else {42+ User::from_name(s)?43+ .map(|user| user.uid.as_raw())44+ .ok_or_else(|| anyhow!("'{}' is not a recognized user name", s))45+ }46 })?;47 let gid = Check::parse(snd, |s| {44- s.parse()45- .ok()46- .or_else(|| users::get_group_by_name(s).map(|group| group.gid()))47- .ok_or_else(|| anyhow!("'{}' is not a recognized group name", s))48+ if let Ok(gid) = s.parse() {49+ Ok(gid)50+ } else {51+ Group::from_name(s)?52+ .map(|group| group.gid.as_raw())53+ .ok_or_else(|| anyhow!("'{}' is not a recognized group name", s))54+ }55 })?;56 57 Ok(OwnerFilter { uid, gid })58 }82 unmodified linesmodified:tests/tests.rs+10 added-8 removed
1 mod testenv;2 3+#[cfg(unix)]4+use nix::unistd::{Gid, Group, Uid, User};5 use std::fs;6 use std::io::Write;7 use std::path::Path;8 use std::time::{Duration, SystemTime};1294 unmodified lines1303 1304 // This test assumes the current user isn't root1305 // (otherwise if the executable bit is set for any level, it is executable for the current1306 // user)1305- if users::get_current_uid() == 0 {1307+ if Uid::current().is_root() {1308 return;1309 }1310 1311 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);950 unmodified lines2262 #[cfg(unix)]2263 #[test]2264 fn test_owner_current_user() {2265 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);2264- let uid = users::get_current_uid();2266+ let uid = Uid::current();2267 te.assert_output(&["--owner", &uid.to_string(), "a.foo"], "a.foo");2266- if let Some(username) = users::get_current_username().map(|u| u.into_string().unwrap()) {2267- te.assert_output(&["--owner", &username, "a.foo"], "a.foo");2268+ if let Ok(Some(user)) = User::from_uid(uid) {2269+ te.assert_output(&["--owner", &user.name, "a.foo"], "a.foo");2270 }2271 }2272 2273 #[cfg(unix)]2274 #[test]2275 fn test_owner_current_group() {2276 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);2275- let gid = users::get_current_gid();2277+ let gid = Gid::current();2278 te.assert_output(&["--owner", &format!(":{}", gid), "a.foo"], "a.foo");2277- if let Some(groupname) = users::get_current_groupname().map(|u| u.into_string().unwrap()) {2278- te.assert_output(&["--owner", &format!(":{}", groupname), "a.foo"], "a.foo");2279+ if let Ok(Some(group)) = Group::from_gid(gid) {2280+ te.assert_output(&["--owner", &format!(":{}", group.name), "a.foo"], "a.foo");2281 }2282 }2283 2284 #[cfg(target_os = "linux")]2285 #[test]2286 fn test_owner_root() {2287 // This test assumes the current user isn't root2286- if users::get_current_uid() == 0 || users::get_current_gid() == 0 {2288+ if Uid::current().is_root() || Gid::current() == Gid::from_raw(0) {2289 return;2290 }2291 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);2292 te.assert_output(&["--owner", "root", "a.foo"], "");266 unmodified linesmodified:Cargo.lock-11 removed
315 unmodified lines316 "regex",317 "regex-syntax 0.7.5",318 "tempfile",319 "test-case",320- "users",320 "version_check",321 ]322 323 [[package]]467 unmodified lines791 source = "registry+https://github.com/rust-lang/crates.io-index"792 checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"793 794 [[package]]796-name = "users"797-version = "0.11.0"798-source = "registry+https://github.com/rust-lang/crates.io-index"799-checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"800-dependencies = [801- "libc",802- "log",803-]804-805-[[package]]795 name = "utf8parse"796 version = "0.2.1"797 source = "registry+https://github.com/rust-lang/crates.io-index"798 checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"176 unmodified linesmodified:Cargo.toml+1 added-2 removed
64 unmodified lines656667686970697071727322 unmodified lines64 unmodified linesdefault-features = falsefeatures = ["nu-ansi-term"]
[target.'cfg(unix)'.dependencies]users = "0.11.0"nix = { version = "0.26.2", default-features = false, features = ["signal"] }nix = { version = "0.26.2", default-features = false, features = ["signal", "user"] }
[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]libc = "0.2"
22 unmodified lines