diff options
author | Canop <cano.petrole@gmail.com> | 2020-06-25 19:23:05 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-06-25 19:23:05 +0200 |
commit | ec9bf7a54cb5a2e8881ef2c503daa798381f2fe7 (patch) | |
tree | d1b73192390261336f5331645485187205ce55e8 /src/file_sum | |
parent | f35ee714052f0bdbe3dbd6cc673e883be882fe1f (diff) |
file_sum small optimizations
(still a temporary implementation)
Diffstat (limited to 'src/file_sum')
-rw-r--r-- | src/file_sum/mod.rs | 26 | ||||
-rw-r--r-- | src/file_sum/sum_computation.rs | 35 |
2 files changed, 39 insertions, 22 deletions
diff --git a/src/file_sum/mod.rs b/src/file_sum/mod.rs index fa6eaf5..894f457 100644 --- a/src/file_sum/mod.rs +++ b/src/file_sum/mod.rs @@ -11,15 +11,13 @@ use { crate::task_sync::Dam, std::{ collections::HashMap, - fs::Metadata, ops::AddAssign, path::{Path, PathBuf}, sync::Mutex, - time::UNIX_EPOCH, }, }; -const SUM_NAMES: &[&str] = &["", "K", "M", "G", "T", "P", "E", "Z", "Y"]; +const SIZE_NAMES: &[&str] = &["", "K", "M", "G", "T", "P", "E", "Z", "Y"]; lazy_static! { static ref SUM_CACHE_MUTEX: Mutex<HashMap<PathBuf, FileSum>> = Mutex::new(HashMap::new()); @@ -30,19 +28,13 @@ pub fn clear_cache() { sum_cache.clear(); } -pub fn extract_seconds(md: &Metadata) -> u64 { - md.modified().map_or( - 0, - |st| st.duration_since(UNIX_EPOCH).map_or(0, |d| d.as_secs()) - ) -} - +/// Reduction of counts, dates and sizes on a file or directory #[derive(Debug, Copy, Clone)] pub struct FileSum { - real_size: u64, // bytes, the space it takes on disk - sparse: bool, // only for non directories: tells whether the file is sparse + real_size: u64, // bytes, the space it takes on disk count: usize, // number of files - modified: u64, // seconds from Epoch to last modification, or 0 if there was an error + modified: u32, // seconds from Epoch to last modification, or 0 if there was an error + sparse: bool, // only for non directories: tells whether the file is sparse } impl FileSum { @@ -50,7 +42,7 @@ impl FileSum { real_size: u64, sparse: bool, count: usize, - modified: u64, + modified: u32, ) -> Self { Self { real_size, sparse, count, modified } } @@ -102,11 +94,11 @@ impl FileSum { pub fn to_size_string(self) -> String { let mut v = self.real_size; let mut i = 0; - while v >= 5000 && i < SUM_NAMES.len() - 1 { + while v >= 5000 && i < SIZE_NAMES.len() - 1 { v /= 1000; i += 1; } - format!("{}{}", v, &SUM_NAMES[i]) + format!("{}{}", v, &SIZE_NAMES[i]) } /// return the number of files (normally at least 1) pub fn to_count(self) -> usize { @@ -114,7 +106,7 @@ impl FileSum { } /// return the number of seconds from Epoch to last modification, /// or 0 if the computation failed - pub fn to_seconds(self) -> u64 { + pub fn to_seconds(self) -> u32 { self.modified } /// return the size in bytes diff --git a/src/file_sum/sum_computation.rs b/src/file_sum/sum_computation.rs index 6e7a099..7e46ad2 100644 --- a/src/file_sum/sum_computation.rs +++ b/src/file_sum/sum_computation.rs @@ -1,23 +1,30 @@ use { - super::{extract_seconds, FileSum}, + super::FileSum, crate::task_sync::Dam, crossbeam::channel, std::{ - collections::HashSet, + convert::TryInto, fs, path::{Path, PathBuf}, sync::{ atomic::{AtomicIsize, Ordering}, - Arc, Mutex, + Arc, }, thread, + }, }; #[cfg(unix)] -use std::os::unix::fs::MetadataExt; +use { + std::{ + collections::HashSet, + os::unix::fs::MetadataExt, + sync::Mutex, + }, +}; -const THREADS_COUNT: usize = 8; +const THREADS_COUNT: usize = 4; /// compute the consolidated numbers for a directory, with implementation /// varying depending on the OS: @@ -150,3 +157,21 @@ pub fn compute_file_sum(path: &Path) -> FileSum { Err(_) => FileSum::new(0, false, 1, 0), } } + +#[cfg(unix)] +fn extract_seconds(md: &fs::Metadata) -> u32 { + md.mtime().try_into().unwrap_or(0) +} + +#[cfg(not(unix))] +fn extract_seconds(md: &fs::Metadata) -> u32 { + if let Ok(st) = md.modified() { + if let Ok(d) = st.duration_since(std::time::UNIX_EPOCH) { + if let Ok(secs) = d.as_secs().try_into() { + return secs + } + } + } + 0 +} + |