Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find: make InodeMatcher & LinksMatcher unix-only #393

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/find/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod prune;
mod quit;
mod regex;
mod size;
#[cfg(unix)]
mod stat;
mod time;
mod type_matcher;
Expand Down Expand Up @@ -51,6 +52,7 @@ use self::prune::PruneMatcher;
use self::quit::QuitMatcher;
use self::regex::RegexMatcher;
use self::size::SizeMatcher;
#[cfg(unix)]
use self::stat::{InodeMatcher, LinksMatcher};
use self::time::{
FileAgeRangeMatcher, FileTimeMatcher, FileTimeType, NewerMatcher, NewerOptionMatcher,
Expand Down Expand Up @@ -495,21 +497,33 @@ fn build_matcher_tree(
.into_box(),
)
}
#[cfg(unix)]
"-inum" => {
if i >= args.len() - 1 {
return Err(From::from(format!("missing argument to {}", args[i])));
}
let inum = convert_arg_to_comparable_value(args[i], args[i + 1])?;
i += 1;
Some(InodeMatcher::new(inum)?.into_box())
Some(InodeMatcher::new(inum).into_box())
}
#[cfg(not(unix))]
"-inum" => {
return Err(From::from(
"Inode numbers are not available on this platform",
));
}
#[cfg(unix)]
"-links" => {
if i >= args.len() - 1 {
return Err(From::from(format!("missing argument to {}", args[i])));
}
let inum = convert_arg_to_comparable_value(args[i], args[i + 1])?;
i += 1;
Some(LinksMatcher::new(inum)?.into_box())
Some(LinksMatcher::new(inum).into_box())
}
#[cfg(not(unix))]
"-links" => {
return Err(From::from("Link counts are not available on this platform"));
}
"-user" => {
if i >= args.len() - 1 {
Expand Down
48 changes: 10 additions & 38 deletions src/find/matchers/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

#[cfg(unix)]
use std::os::unix::fs::MetadataExt;

use std::error::Error;
use walkdir::DirEntry;

use super::{ComparableValue, Matcher, MatcherIO};
Expand All @@ -18,32 +16,18 @@ pub struct InodeMatcher {
}

impl InodeMatcher {
#[cfg(unix)]
pub fn new(ino: ComparableValue) -> Result<Self, Box<dyn Error>> {
Ok(Self { ino })
}

#[cfg(not(unix))]
pub fn new(_ino: ComparableValue) -> Result<Self, Box<dyn Error>> {
Err(From::from(
"Inode numbers are not available on this platform",
))
pub fn new(ino: ComparableValue) -> Self {
Self { ino }
}
}

impl Matcher for InodeMatcher {
#[cfg(unix)]
fn matches(&self, file_info: &DirEntry, _: &mut MatcherIO) -> bool {
match file_info.metadata() {
Ok(metadata) => self.ino.matches(metadata.ino()),
Err(_) => false,
}
}

#[cfg(not(unix))]
fn matches(&self, _: &DirEntry, _: &mut MatcherIO) -> bool {
unreachable!("Inode numbers are not available on this platform")
}
}

/// Link count matcher.
Expand All @@ -52,30 +36,18 @@ pub struct LinksMatcher {
}

impl LinksMatcher {
#[cfg(unix)]
pub fn new(nlink: ComparableValue) -> Result<Self, Box<dyn Error>> {
Ok(Self { nlink })
}

#[cfg(not(unix))]
pub fn new(_nlink: ComparableValue) -> Result<Self, Box<dyn Error>> {
Err(From::from("Link counts are not available on this platform"))
pub fn new(nlink: ComparableValue) -> Self {
Self { nlink }
}
}

impl Matcher for LinksMatcher {
#[cfg(unix)]
fn matches(&self, file_info: &DirEntry, _: &mut MatcherIO) -> bool {
match file_info.metadata() {
Ok(metadata) => self.nlink.matches(metadata.nlink()),
Err(_) => false,
}
}

#[cfg(not(unix))]
fn matches(&self, _: &DirEntry, _: &mut MatcherIO) -> bool {
unreachable!("Link counts are not available on this platform")
}
}

#[cfg(test)]
Expand All @@ -92,19 +64,19 @@ mod tests {
let metadata = file_info.metadata().unwrap();
let deps = FakeDependencies::new();

let matcher = InodeMatcher::new(ComparableValue::EqualTo(metadata.ino())).unwrap();
let matcher = InodeMatcher::new(ComparableValue::EqualTo(metadata.ino()));
assert!(
matcher.matches(&file_info, &mut deps.new_matcher_io()),
"inode number should match"
);

let matcher = InodeMatcher::new(ComparableValue::LessThan(metadata.ino())).unwrap();
let matcher = InodeMatcher::new(ComparableValue::LessThan(metadata.ino()));
assert!(
!matcher.matches(&file_info, &mut deps.new_matcher_io()),
"inode number should not match"
);

let matcher = InodeMatcher::new(ComparableValue::MoreThan(metadata.ino())).unwrap();
let matcher = InodeMatcher::new(ComparableValue::MoreThan(metadata.ino()));
assert!(
!matcher.matches(&file_info, &mut deps.new_matcher_io()),
"inode number should not match"
Expand All @@ -116,19 +88,19 @@ mod tests {
let file_info = get_dir_entry_for("test_data/simple", "abbbc");
let deps = FakeDependencies::new();

let matcher = LinksMatcher::new(ComparableValue::EqualTo(1)).unwrap();
let matcher = LinksMatcher::new(ComparableValue::EqualTo(1));
assert!(
matcher.matches(&file_info, &mut deps.new_matcher_io()),
"link count should match"
);

let matcher = LinksMatcher::new(ComparableValue::LessThan(1)).unwrap();
let matcher = LinksMatcher::new(ComparableValue::LessThan(1));
assert!(
!matcher.matches(&file_info, &mut deps.new_matcher_io()),
"link count should not match"
);

let matcher = LinksMatcher::new(ComparableValue::MoreThan(1)).unwrap();
let matcher = LinksMatcher::new(ComparableValue::MoreThan(1));
assert!(
!matcher.matches(&file_info, &mut deps.new_matcher_io()),
"link count should not match"
Expand Down
24 changes: 24 additions & 0 deletions tests/find_cmd_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,18 @@ fn find_inum() {
.stdout(predicate::str::contains("abbbc"));
}

#[cfg(not(unix))]
#[test]
fn find_inum() {
Command::cargo_bin("find")
.expect("found binary")
.args(["test_data", "-inum", "1"])
.assert()
.failure()
.stderr(predicate::str::contains("not available on this platform"))
.stdout(predicate::str::is_empty());
}

#[cfg(unix)]
#[serial(working_dir)]
#[test]
Expand All @@ -454,6 +466,18 @@ fn find_links() {
.stdout(predicate::str::contains("abbbc"));
}

#[cfg(not(unix))]
#[test]
fn find_links() {
Command::cargo_bin("find")
.expect("found binary")
.args(["test_data", "-links", "1"])
.assert()
.failure()
.stderr(predicate::str::contains("not available on this platform"))
.stdout(predicate::str::is_empty());
}

#[serial(working_dir)]
#[test]
fn find_mount_xdev() {
Expand Down
Loading