Skip to content
Myr

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

4 files+26 added−29 removed

modified:src/filter/owner.rs+15 added-8 removed
1use anyhow::{anyhow, Result};2use nix::unistd::{Group, User};3use std::fs;45#[derive(Clone, Copy, Debug, PartialEq, Eq)]6pub struct OwnerFilter {
28 unmodified lines
35 ));36 }3738 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 })?;5657 Ok(OwnerFilter { uid, gid })58 }
82 unmodified lines
modified:tests/tests.rs+10 added-8 removed
1mod testenv;23#[cfg(unix)]4use nix::unistd::{Gid, Group, Uid, User};5use std::fs;6use std::io::Write;7use std::path::Path;8use std::time::{Duration, SystemTime};
1294 unmodified lines
13031304 // 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 }13101311 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
950 unmodified lines
2262#[cfg(unix)]2263#[test]2264fn 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}22722273#[cfg(unix)]2274#[test]2275fn 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}22832284#[cfg(target_os = "linux")]2285#[test]2286fn 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 lines
modified:Cargo.lock-11 removed
315 unmodified lines
316 "regex",317 "regex-syntax 0.7.5",318 "tempfile",319 "test-case",320 "users",320 "version_check",321]322323[[package]]
467 unmodified lines
791source = "registry+https://github.com/rust-lang/crates.io-index"792checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"793794[[package]]796name = "users"797version = "0.11.0"798source = "registry+https://github.com/rust-lang/crates.io-index"799checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"800dependencies = [801 "libc",802 "log",803]804805[[package]]795name = "utf8parse"796version = "0.2.1"797source = "registry+https://github.com/rust-lang/crates.io-index"798checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
176 unmodified lines
modified:Cargo.toml+1 added-2 removed