-
Notifications
You must be signed in to change notification settings - Fork 62
find: Implement -fstype
#408
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
13202e9
Add nix deps and impl fs type matcher.
hanbings 45df782
Add -fstype support.
hanbings c2c22db
Add tests for -fstype.
hanbings fce0794
Merge branch 'main' into implement-374
hanbings 8313d40
Merge duplicate nix dependencies.
hanbings 280fd0a
Encapsulate functions and simplify code.
hanbings 5850f5c
Run clippy.
hanbings aa2b551
Merge branch 'main' into implement-374
sylvestre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// This file is part of the uutils findutils package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
use std::path::Path; | ||
use std::{ | ||
error::Error, | ||
io::{stderr, Write}, | ||
}; | ||
|
||
use super::Matcher; | ||
|
||
/// Get the filesystem type of a file. | ||
/// 1. get the metadata of the file | ||
/// 2. get the device ID of the metadata | ||
/// 3. search the filesystem list | ||
/// | ||
/// Returns an empty string when no file system list matches. | ||
/// | ||
/// # Errors | ||
/// Returns an error if the metadata could not be read. | ||
/// Returns an error if the filesystem list could not be read. | ||
/// | ||
/// This is only supported on Unix. | ||
#[cfg(unix)] | ||
pub fn get_file_system_type(path: &Path) -> Result<String, Box<dyn Error>> { | ||
use std::os::unix::fs::MetadataExt; | ||
|
||
let metadata = match path.metadata() { | ||
Ok(metadata) => metadata, | ||
Err(err) => Err(err)?, | ||
}; | ||
let dev_id = metadata.dev().to_string(); | ||
let fs_list = match uucore::fsext::read_fs_list() { | ||
Ok(fs_list) => fs_list, | ||
Err(err) => Err(err)?, | ||
}; | ||
let result = fs_list | ||
.into_iter() | ||
.find(|fs| fs.dev_id == dev_id) | ||
.map_or_else(String::new, |fs| fs.fs_type); | ||
|
||
Ok(result) | ||
} | ||
|
||
/// This matcher handles the -fstype argument. | ||
/// It matches the filesystem type of the file. | ||
/// | ||
/// This is only supported on Unix. | ||
pub struct FileSystemMatcher { | ||
fs_text: String, | ||
} | ||
|
||
impl FileSystemMatcher { | ||
pub fn new(fs_text: String) -> Self { | ||
Self { fs_text } | ||
} | ||
} | ||
|
||
impl Matcher for FileSystemMatcher { | ||
fn matches(&self, file_info: &walkdir::DirEntry, _: &mut super::MatcherIO) -> bool { | ||
#[cfg(not(unix))] | ||
{ | ||
false | ||
} | ||
#[cfg(unix)] | ||
{ | ||
match get_file_system_type(file_info.path()) { | ||
Ok(result) => result == self.fs_text, | ||
Err(_) => { | ||
writeln!( | ||
&mut stderr(), | ||
"Error getting filesystem type for {}", | ||
file_info.path().to_string_lossy() | ||
) | ||
.unwrap(); | ||
|
||
false | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
#[cfg(unix)] | ||
fn test_fs_matcher() { | ||
use crate::find::{ | ||
matchers::{fs::get_file_system_type, tests::get_dir_entry_for, Matcher}, | ||
tests::FakeDependencies, | ||
}; | ||
use std::fs::File; | ||
use tempfile::Builder; | ||
|
||
let deps = FakeDependencies::new(); | ||
let mut matcher_io = deps.new_matcher_io(); | ||
|
||
// create temp file and get its fs type | ||
// We pass this file and the corresponding file system type into the Matcher for comparison. | ||
let temp_dir = Builder::new().prefix("fs_matcher").tempdir().unwrap(); | ||
let foo_path = temp_dir.path().join("foo"); | ||
let _ = File::create(foo_path).expect("create temp file"); | ||
let file_info = get_dir_entry_for(&temp_dir.path().to_string_lossy(), "foo"); | ||
|
||
let target_fs_type = get_file_system_type(file_info.path()).unwrap(); | ||
|
||
// should match fs type | ||
let matcher = super::FileSystemMatcher::new(target_fs_type.clone()); | ||
assert!( | ||
matcher.matches(&file_info, &mut matcher_io), | ||
"{} should match {}", | ||
file_info.path().to_string_lossy(), | ||
target_fs_type | ||
); | ||
|
||
// should not match fs type | ||
let matcher = super::FileSystemMatcher::new(target_fs_type.clone() + "foo"); | ||
assert!( | ||
!matcher.matches(&file_info, &mut matcher_io), | ||
"{} should not match {}", | ||
file_info.path().to_string_lossy(), | ||
target_fs_type | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.