Simplify inodes & devices by removing Option

This code now won't compile on none-(windows/unix family)
systems. [unix family includes mac]

Removing this method allows us to remove an Option and simplify the code
slightly
This commit is contained in:
andy.boot
2020-02-09 14:07:06 +00:00
parent a8d700d530
commit 657858df16
2 changed files with 28 additions and 41 deletions
+22 -25
View File
@@ -154,8 +154,8 @@ fn examine_dir<P: AsRef<Path>>(
} }
match maybe_size_and_inode { match maybe_size_and_inode {
Some((size, maybe_inode)) => { Some((size, inode, device)) => {
if !should_ignore_file(apparent_size, filesystems, &mut inodes, maybe_inode) { if !should_ignore_file(apparent_size, filesystems, &mut inodes, inode, device) {
process_file_with_size_and_inode(top_dir, data, e, size) process_file_with_size_and_inode(top_dir, data, e, size)
} }
} }
@@ -171,23 +171,22 @@ fn should_ignore_file(
apparent_size: bool, apparent_size: bool,
restricted_filesystems: &Option<HashSet<u64>>, restricted_filesystems: &Option<HashSet<u64>>,
inodes: &mut HashSet<(u64, u64)>, inodes: &mut HashSet<(u64, u64)>,
maybe_inode: Option<(u64, u64)>, inode: u64,
device: u64,
) -> bool { ) -> bool {
if let Some(inode_dev_pair) = maybe_inode { // Ignore files on different devices (if flag applied)
// Ignore files on different devices (if flag applied) if let Some(rs) = restricted_filesystems {
if let Some(rs) = restricted_filesystems { if !rs.contains(&device) {
if !rs.contains(&inode_dev_pair.1) { return true;
return true;
}
} }
}
if !apparent_size { if !apparent_size {
// Ignore files already visited or symlinked // Ignore files already visited or symlinked
if inodes.contains(&inode_dev_pair) { if inodes.contains(&(inode, device)) {
return true; return true;
}
inodes.insert(inode_dev_pair);
} }
inodes.insert((inode, device));
} }
false false
} }
@@ -353,15 +352,14 @@ mod tests {
let mut files = HashSet::new(); let mut files = HashSet::new();
files.insert((10, 20)); files.insert((10, 20));
assert!(!should_ignore_file(true, &None, &mut files, None)); assert!(!should_ignore_file(true, &None, &mut files, 0, 0));
// New file is not known it will be inserted to the hashmp and should not be ignored // New file is not known it will be inserted to the hashmp and should not be ignored
let new_fd = (11, 12); assert!(!should_ignore_file(false, &None, &mut files, 11, 12));
assert!(!should_ignore_file(false, &None, &mut files, Some(new_fd))); assert!(files.contains(&(11, 12)));
assert!(files.contains(&new_fd));
// The same file will be ignored the second time // The same file will be ignored the second time
assert!(should_ignore_file(false, &None, &mut files, Some(new_fd))); assert!(should_ignore_file(false, &None, &mut files, 11, 12));
} }
#[test] #[test]
@@ -375,12 +373,11 @@ mod tests {
// If we are looking at a different device (disk) and the device flag is set // If we are looking at a different device (disk) and the device flag is set
// then apparent_size is irrelevant - we ignore files on other devices // then apparent_size is irrelevant - we ignore files on other devices
let new_file = (11, 12); assert!(should_ignore_file(false, &od, &mut files, 11, 12));
assert!(should_ignore_file(false, &od, &mut files, Some(new_file))); assert!(should_ignore_file(true, &od, &mut files, 11, 12));
assert!(should_ignore_file(true, &od, &mut files, Some(new_file)));
// We do not ignore files on the same device // We do not ignore files on the same device
assert!(!should_ignore_file(false, &od, &mut files, Some((2, 99)))); assert!(!should_ignore_file(false, &od, &mut files, 2, 99));
assert!(!should_ignore_file(true, &od, &mut files, Some((2, 99)))); assert!(!should_ignore_file(true, &od, &mut files, 2, 99));
} }
} }
+6 -16
View File
@@ -12,20 +12,19 @@ fn get_block_size() -> u64 {
} }
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, u64, u64)> {
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
d.metadata.as_ref().unwrap().as_ref().ok().map(|md| { d.metadata.as_ref().unwrap().as_ref().ok().map(|md| {
let inode = Some((md.ino(), md.dev()));
if use_apparent_size { if use_apparent_size {
(md.len(), inode) (md.len(), md.ino(), md.dev())
} else { } else {
(md.blocks() * get_block_size(), inode) (md.blocks() * get_block_size(), md.ino(), md.dev())
} }
}) })
} }
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
pub fn get_metadata(d: &DirEntry, _use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { pub fn get_metadata(d: &DirEntry, _use_apparent_size: bool) -> Option<(u64, u64, u64)> {
use winapi_util::file::information; use winapi_util::file::information;
use winapi_util::Handle; use winapi_util::Handle;
@@ -34,20 +33,11 @@ pub fn get_metadata(d: &DirEntry, _use_apparent_size: bool) -> Option<(u64, Opti
Some(( Some((
info.file_size(), info.file_size(),
Some((info.file_index(), info.volume_serial_number())), info.file_index(),
info.volume_serial_number(),
)) ))
} }
#[cfg(all(not(target_family = "windows"), not(target_family = "unix")))]
pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> {
d.metadata
.as_ref()
.unwrap()
.as_ref()
.ok()
.map(|md| (md.len(), None))
}
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
pub fn get_filesystem<P: AsRef<Path>>(file_path: P) -> Result<u64, io::Error> { pub fn get_filesystem<P: AsRef<Path>>(file_path: P) -> Result<u64, io::Error> {
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;