[ownership] depend on users 0.10.0 on unix; parse user and group names
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alexandru Macovei · committed by David Peter · parent 4f9a024 · Browse files · 4 files·+57 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+57 added−2 removed
- other:+57 added−2 removed
Provenance
- Commit
d7cc34e- Tree
4ba0bf8- Author
Alexandru Macovei <alexnmaco@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
4f9a024
4 files changed+57 added−2 removed
modified:src/filter/owner.rs+30 added-2 removed
1 use anyhow::{anyhow, Result};2+use std::fs;3 4 #[derive(Clone, Copy, Debug, PartialEq)]5 pub struct OwnerFilter {6 uid: Option<u32>,6 unmodified lines13 let (fst, snd) = (it.next(), it.next());14 15 let uid = match fst {16 Some("") | None => None,16- Some(s) => Some(s.parse()?),17+ Some(s) => {18+ let maybe_uid = s19+ .parse()20+ .ok()21+ .or_else(|| users::get_user_by_name(s).map(|user| user.uid()));22+ match maybe_uid {23+ Some(uid) => Some(uid),24+ _ => return Err(anyhow!("'{}' is not a recognized user name", s)),25+ }26+ }27 };28 let gid = match snd {29 Some("") | None => None,20- Some(s) => Some(s.parse()?),30+ Some(s) => {31+ let maybe_gid = s32+ .parse()33+ .ok()34+ .or_else(|| users::get_group_by_name(s).map(|group| group.gid()));35+ match maybe_gid {36+ Some(gid) => Some(gid),37+ _ => return Err(anyhow!("'{}' is not a recognized group name", s)),38+ }39+ }40 };41 42 if uid.is_none() && gid.is_none() {43 Err(anyhow!(3 unmodified lines47 } else {48 Ok(OwnerFilter { uid, gid })49 }50 }51+52+ pub fn matches(&self, md: &fs::Metadata) -> bool {53+ use std::os::unix::fs::MetadataExt;54+55+ let uid_ok = self.uid.map(|u| u == md.uid()).unwrap_or(true);56+ let gid_ok = self.gid.map(|g| g == md.gid()).unwrap_or(true);57+58+ uid_ok && gid_ok59+ }60 }61 62 #[cfg(test)]63 mod owner_parsing {26 unmodified linesmodified:src/walk.rs+13 added
409 unmodified lines410 return ignore::WalkState::Continue;411 }412 }413 414+ #[cfg(unix)]415+ {416+ if let Some(ref owner_constraint) = config.owner_constraint {417+ if let Ok(ref metadata) = entry_path.metadata() {418+ if !owner_constraint.matches(&metadata) {419+ return ignore::WalkState::Continue;420+ }421+ } else {422+ return ignore::WalkState::Continue;423+ }424+ }425+ }426+427 // Filter out unwanted sizes if it is a file and we have been given size constraints.428 if !config.size_constraints.is_empty() {429 if entry_path.is_file() {430 if let Ok(metadata) = entry_path.metadata() {42 unmodified linesmodified:Cargo.lock+11 added
135 unmodified lines136 "num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)",137 "regex 1.3.6 (registry+https://github.com/rust-lang/crates.io-index)",138 "regex-syntax 0.6.17 (registry+https://github.com/rust-lang/crates.io-index)",139 "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",140+ "users 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",141 "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",142 ]143 144 [[package]]266 unmodified lines411 version = "0.1.7"412 source = "registry+https://github.com/rust-lang/crates.io-index"413 414 [[package]]415+name = "users"416+version = "0.10.0"417+source = "registry+https://github.com/rust-lang/crates.io-index"418+dependencies = [419+ "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)",420+ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",421+]422+423+[[package]]424 name = "vec_map"425 version = "0.8.1"426 source = "registry+https://github.com/rust-lang/crates.io-index"427 103 unmodified lines531 "checksum term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5b9a66db815dcfd2da92db471106457082577c3c278d4138ab3e3b4e189327"532 "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"533 "checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"534 "checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"535+"checksum users 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aa4227e95324a443c9fcb06e03d4d85e91aabe9a5a02aa818688b6918b6af486"536 "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"537 "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"538 "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"539 "checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d"6 unmodified linesmodified:Cargo.toml+3 added
49 unmodified lines505152535455565758596010 unmodified lines49 unmodified lines[dependencies.clap]version = "2.31.2"features = ["suggestions", "color", "wrap_help"]
[target.'cfg(unix)'.dependencies]users = "0.10.0"
[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]libc = "0.2"
[target.'cfg(all(not(windows), not(target_env = "musl")))'.dependencies]10 unmodified lines