diff --git a/src/utils.rs b/src/utils/mod.rs similarity index 81% rename from src/utils.rs rename to src/utils/mod.rs index c0215ba..0e7cdd7 100644 --- a/src/utils.rs +++ b/src/utils/mod.rs @@ -1,5 +1,5 @@ use std::collections::HashSet; -use std; + use std::fs::{self, ReadDir}; use std::io; @@ -7,6 +7,9 @@ use std::cmp; use dust::{DirEnt, Node}; +mod platform; +use self::platform::*; + extern crate ansi_term; use self::ansi_term::Colour::Fixed; @@ -34,72 +37,6 @@ fn examine_dir_str(loc: &str, apparent_size: bool) -> (bool, Node) { (hp, Node::new(DirEnt::new(loc, new_size), result)) } -#[cfg(not(target_os = "macos"))] -fn get_block_size() -> u64 { - 1024 -} - -#[cfg(target_os = "macos")] -fn get_block_size() -> u64 { - 512 -} - -#[cfg(target_os = "linux")] -fn get_metadata(d: &std::fs::DirEntry, s: bool) -> Option<(u64, Option<(u64, u64)>)> { - use std::os::linux::fs::MetadataExt; - match d.metadata().ok() { - Some(md) => { - let inode = Some((md.st_ino(), md.st_dev())); - if s { - Some((md.len(), inode)) - } else { - Some((md.st_blocks() * get_block_size(), inode)) - } - } - None => None, - } -} - -#[cfg(target_os = "unix")] -fn get_metadata(d: &std::fs::DirEntry, s: bool) -> Option<(u64, Option<(u64, u64)>)> { - use std::os::unix::fs::MetadataExt; - match d.metadata().ok() { - Some(md) => { - let inode = Some((md.ino(), md.dev())); - if s { - Some((md.len(), inode)) - } else { - Some((md.blocks() * get_block_size(), inode)) - } - } - None => None, - } -} - -#[cfg(target_os = "macos")] -fn get_metadata(d: &std::fs::DirEntry, s: bool) -> Option<(u64, Option<(u64, u64)>)> { - use std::os::macos::fs::MetadataExt; - match d.metadata().ok() { - Some(md) => { - let inode = Some((md.st_ino(), md.st_dev())); - if s { - Some((md.len(), inode)) - } else { - Some((md.st_blocks() * get_block_size(), inode)) - } - } - None => None, - } -} - -#[cfg(not(any(target_os = "linux", target_os = "unix", target_os = "macos")))] -fn get_metadata(d: &std::fs::DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> { - match d.metadata().ok() { - Some(md) => Some((md.len(), None)), - None => None, - } -} - fn examine_dir( a_dir: io::Result, apparent_size: bool, diff --git a/src/utils/platform.rs b/src/utils/platform.rs new file mode 100644 index 0000000..bcfee1d --- /dev/null +++ b/src/utils/platform.rs @@ -0,0 +1,67 @@ +use std; + +#[cfg(not(target_os = "macos"))] +pub fn get_block_size() -> u64 { + 1024 +} + +#[cfg(target_os = "macos")] +pub fn get_block_size() -> u64 { + 512 +} + +#[cfg(target_os = "linux")] +pub fn get_metadata(d: &std::fs::DirEntry, s: bool) -> Option<(u64, Option<(u64, u64)>)> { + use std::os::linux::fs::MetadataExt; + match d.metadata().ok() { + Some(md) => { + let inode = Some((md.st_ino(), md.st_dev())); + if s { + Some((md.len(), inode)) + } else { + Some((md.st_blocks() * get_block_size(), inode)) + } + } + None => None, + } +} + +#[cfg(target_os = "unix")] +pub fn get_metadata(d: &std::fs::DirEntry, s: bool) -> Option<(u64, Option<(u64, u64)>)> { + use std::os::unix::fs::MetadataExt; + match d.metadata().ok() { + Some(md) => { + let inode = Some((md.ino(), md.dev())); + if s { + Some((md.len(), inode)) + } else { + Some((md.blocks() * get_block_size(), inode)) + } + } + None => None, + } +} + +#[cfg(target_os = "macos")] +pub fn get_metadata(d: &std::fs::DirEntry, s: bool) -> Option<(u64, Option<(u64, u64)>)> { + use std::os::macos::fs::MetadataExt; + match d.metadata().ok() { + Some(md) => { + let inode = Some((md.st_ino(), md.st_dev())); + if s { + Some((md.len(), inode)) + } else { + Some((md.st_blocks() * get_block_size(), inode)) + } + } + None => None, + } +} + +#[cfg(not(any(target_os = "linux", target_os = "unix", target_os = "macos")))] +pub fn get_metadata(d: &std::fs::DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> { + match d.metadata().ok() { + Some(md) => Some((md.len(), None)), + None => None, + } +}