master
Avril 4 years ago
parent 0fc93ee39e
commit 2f185cf454
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
.gitignore vendored

@ -1,2 +1,3 @@
/target /target
*~ *~
test-input

12
Cargo.lock generated

@ -435,6 +435,7 @@ dependencies = [
"futures", "futures",
"sha2", "sha2",
"tokio", "tokio",
"tokio-test",
] ]
[[package]] [[package]]
@ -554,6 +555,17 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "tokio-test"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed0049c119b6d505c4447f5c64873636c7af6c75ab0d45fd9f618d82acb8016d"
dependencies = [
"bytes",
"futures-core",
"tokio",
]
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.12.0" version = "1.12.0"

@ -14,6 +14,9 @@ codegen-units = 1
[features] [features]
threads = ["tokio", "futures"] threads = ["tokio", "futures"]
[dev-dependencies]
tokio-test = "0.2"
[dependencies] [dependencies]
tokio = { version = "0.2", features = ["full"], optional = true } tokio = { version = "0.2", features = ["full"], optional = true }
sha2 = "0.9" sha2 = "0.9"

@ -8,11 +8,19 @@ pub enum RecursionMode
N(usize), N(usize),
} }
#[derive(Debug, Clone)]
pub enum OperationMode
{
Print,
Delete,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Mode pub struct Mode
{ {
pub error_mode: error::Mode, pub error_mode: error::Mode,
pub recursion_mode: RecursionMode, pub recursion_mode: RecursionMode,
pub operation_mode: OperationMode,
} }
impl Default for Mode impl Default for Mode
@ -22,6 +30,7 @@ impl Default for Mode
Self { Self {
error_mode: error::Mode::Cancel, error_mode: error::Mode::Cancel,
recursion_mode: RecursionMode::None, recursion_mode: RecursionMode::None,
operation_mode: OperationMode::Print,
} }
} }
} }

@ -9,6 +9,48 @@ mod container;
mod config; mod config;
mod proc; mod proc;
#[cfg(test)]
mod test {
use super::*;
use std::{
path::Path,
};
#[test]
pub fn test() -> Result<(), error::Error>
{
let mut cont = container::DupeMap::new();
let mode = config::Mode::default();
let path = Path::new("test-input");
assert_eq!(proc::DupeCount{total:4, dupes:2}, proc::do_dir(path, 0, &mut cont, &mode)?);
Ok(())
}
#[cfg(feature="threads")]
pub async fn _test_async() -> Result<(), error::Error>
{
use std::sync::Arc;
use tokio::{
sync::Mutex,
};
let cont = Arc::new(Mutex::new(container::DupeMap::new()));
let mode = config::Mode::default();
let path = Path::new("test-input");
assert_eq!(proc::DupeCount{total:4, dupes:2}, proc::do_dir_async(path, 0, cont, mode).await?);
Ok(())
}
#[cfg(feature="threads")]
#[test]
pub fn test_async() -> Result<(), error::Error>
{
tokio_test::block_on(_test_async())
}
}
fn main() { fn main() {
} }

@ -18,6 +18,23 @@ use std::{
}, },
}; };
/// Handle a detected dupe
fn handle_dupe<P>(path: P, _mode: &config::Mode) -> Result<(), error::Error>
where P: AsRef<Path>
{
println!(" -> {:?}", path.as_ref());
Ok(())
}
/// Handle a detected dupe async
#[inline(always)]
#[cfg(feature="threads")]
async fn handle_dupe_async<P>(path: P, mode: &config::Mode) -> Result<(), error::Error>
where P: AsRef<Path>
{
handle_dupe(path, mode)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DupeCount pub struct DupeCount
{ {
@ -126,8 +143,7 @@ pub fn do_dir<P: AsRef<Path>>(dir: P, depth: usize, set: &mut container::DupeMap
count += if mode.handle(process_file(&obj, set))?.unwrap_or_default() { count += if mode.handle(process_file(&obj, set))?.unwrap_or_default() {
DupeCount{total: 1, dupes: 0} DupeCount{total: 1, dupes: 0}
} else { } else {
println!(" -> {:?}", obj); mode.handle(handle_dupe(obj, &cmode))?;
//TODO: Delete dupe?
DupeCount{total: 1, dupes: 1} DupeCount{total: 1, dupes: 1}
}; };
} }
@ -175,9 +191,20 @@ pub fn do_dir_async<P: AsRef<Path> + std::marker::Send + std::marker::Sync + 'st
} else { } else {
let set = Arc::clone(&set); let set = Arc::clone(&set);
let mode = mode.clone(); let mode = mode.clone();
let cmode = cmode.clone();
workers.push(tokio::task::spawn(async move { workers.push(tokio::task::spawn(async move {
match mode.handle(process_file_async(&obj, &set).await) { match mode.handle(process_file_async(&obj, &set).await) {
Ok(v) => Ok(v.unwrap_or_default()), Ok(v) => {
if v.unwrap_or_default() {
Ok(true)
} else {
if let Err(e) = mode.handle(handle_dupe_async(obj, &cmode).await) {
Err(e)
} else {
Ok(false)
}
}
},
Err(v) => Err(v), Err(v) => Err(v),
} }
})); }));

Loading…
Cancel
Save