From 657858df16ca1d2ff062f69c4f24da7fcd9451ce Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Sun, 9 Feb 2020 14:07:06 +0000 Subject: [PATCH] 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 --- src/utils/mod.rs | 47 ++++++++++++++++++++----------------------- src/utils/platform.rs | 22 ++++++-------------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2ed4dfe..1c34960 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -154,8 +154,8 @@ fn examine_dir>( } match maybe_size_and_inode { - Some((size, maybe_inode)) => { - if !should_ignore_file(apparent_size, filesystems, &mut inodes, maybe_inode) { + Some((size, inode, device)) => { + if !should_ignore_file(apparent_size, filesystems, &mut inodes, inode, device) { process_file_with_size_and_inode(top_dir, data, e, size) } } @@ -171,23 +171,22 @@ fn should_ignore_file( apparent_size: bool, restricted_filesystems: &Option>, inodes: &mut HashSet<(u64, u64)>, - maybe_inode: Option<(u64, u64)>, + inode: u64, + device: u64, ) -> bool { - if let Some(inode_dev_pair) = maybe_inode { - // Ignore files on different devices (if flag applied) - if let Some(rs) = restricted_filesystems { - if !rs.contains(&inode_dev_pair.1) { - return true; - } + // Ignore files on different devices (if flag applied) + if let Some(rs) = restricted_filesystems { + if !rs.contains(&device) { + return true; } + } - if !apparent_size { - // Ignore files already visited or symlinked - if inodes.contains(&inode_dev_pair) { - return true; - } - inodes.insert(inode_dev_pair); + if !apparent_size { + // Ignore files already visited or symlinked + if inodes.contains(&(inode, device)) { + return true; } + inodes.insert((inode, device)); } false } @@ -353,15 +352,14 @@ mod tests { let mut files = HashSet::new(); 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 - let new_fd = (11, 12); - assert!(!should_ignore_file(false, &None, &mut files, Some(new_fd))); - assert!(files.contains(&new_fd)); + assert!(!should_ignore_file(false, &None, &mut files, 11, 12)); + assert!(files.contains(&(11, 12))); // 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] @@ -375,12 +373,11 @@ mod tests { // 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 - let new_file = (11, 12); - assert!(should_ignore_file(false, &od, &mut files, Some(new_file))); - assert!(should_ignore_file(true, &od, &mut files, Some(new_file))); + assert!(should_ignore_file(false, &od, &mut files, 11, 12)); + assert!(should_ignore_file(true, &od, &mut files, 11, 12)); // We do not ignore files on the same device - assert!(!should_ignore_file(false, &od, &mut files, Some((2, 99)))); - assert!(!should_ignore_file(true, &od, &mut files, Some((2, 99)))); + assert!(!should_ignore_file(false, &od, &mut files, 2, 99)); + assert!(!should_ignore_file(true, &od, &mut files, 2, 99)); } } diff --git a/src/utils/platform.rs b/src/utils/platform.rs index d2b1720..144f9ce 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -12,20 +12,19 @@ fn get_block_size() -> u64 { } #[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; d.metadata.as_ref().unwrap().as_ref().ok().map(|md| { - let inode = Some((md.ino(), md.dev())); if use_apparent_size { - (md.len(), inode) + (md.len(), md.ino(), md.dev()) } else { - (md.blocks() * get_block_size(), inode) + (md.blocks() * get_block_size(), md.ino(), md.dev()) } }) } #[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::Handle; @@ -34,20 +33,11 @@ pub fn get_metadata(d: &DirEntry, _use_apparent_size: bool) -> Option<(u64, Opti Some(( 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")] pub fn get_filesystem>(file_path: P) -> Result { use std::os::unix::fs::MetadataExt;