Merge pull request #67 from bootandy/ab-simplify-inodes2

Simplify inodes & devices by removing Option
This commit is contained in:
andy.boot
2020-02-10 22:11:35 +00:00
committed by GitHub
2 changed files with 28 additions and 41 deletions

View File

@@ -154,8 +154,8 @@ fn examine_dir<P: AsRef<Path>>(
}
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<HashSet<u64>>,
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));
}
}

View File

@@ -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<P: AsRef<Path>>(file_path: P) -> Result<u64, io::Error> {
use std::os::unix::fs::MetadataExt;