cleanux 0.1.0

Linux cleaner CLI (CleanMyMac-like): scan & safely trash cache, find duplicates, list large files
Documentation
use anyhow::Result;
use blake3::Hasher;
use ignore::WalkBuilder;
use indicatif::{ProgressBar, ProgressStyle};
use rayon::prelude::*;
use std::{
    collections::HashMap,
    fs::File,
    io::Read,
    path::{Path, PathBuf},
};

fn blake3_file(path: &Path) -> Result<[u8; 32]> {
    let mut f = File::open(path)?;
    let mut hasher = Hasher::new();
    let mut buf = [0u8; 1024 * 1024];

    loop {
        let n = f.read(&mut buf)?;
        if n == 0 {
            break;
        }
        hasher.update(&buf[..n]);
    }

    Ok(*hasher.finalize().as_bytes())
}

pub fn run_dups(root: &Path, move_to_trash: bool) -> Result<()> {
    let pb = ProgressBar::new_spinner();
    pb.set_style(ProgressStyle::with_template("{spinner} {msg}")?);

    pb.set_message("Scanning files...");

    // 1) Group files by size.
    let mut by_size: HashMap<u64, Vec<PathBuf>> = HashMap::new();
    for dent in WalkBuilder::new(root).hidden(false).build() {
        let dent = dent?;
        if dent.file_type().map(|t| t.is_file()).unwrap_or(false) {
            let md = dent.metadata()?;
            by_size.entry(md.len()).or_default().push(dent.into_path());
        }
    }

    let candidates: Vec<(u64, Vec<PathBuf>)> =
        by_size.into_iter().filter(|(_, v)| v.len() > 1).collect();

    if candidates.is_empty() {
        pb.finish_and_clear();
        println!("No duplicates found.");
        return Ok(());
    }

    pb.set_message("Hashing candidates...");

    // 2) For each size-group, compute full BLAKE3 hash for all files.
    let hashed: Vec<(u64, [u8; 32], PathBuf)> = candidates
        .par_iter()
        .flat_map(|(size, paths)| {
            paths
                .par_iter()
                .filter_map(|p| blake3_file(p).ok().map(|h| (*size, h, p.clone())))
                .collect::<Vec<_>>()
        })
        .collect();

    // 3) Group by (size, hash)
    let mut by_hash: HashMap<(u64, [u8; 32]), Vec<PathBuf>> = HashMap::new();
    for (size, hash, p) in hashed {
        by_hash.entry((size, hash)).or_default().push(p);
    }

    pb.finish_and_clear();

    let mut groups: Vec<Vec<PathBuf>> = by_hash.into_values().filter(|v| v.len() > 1).collect();

    if groups.is_empty() {
        println!("No duplicates found.");
        return Ok(());
    }

    groups.sort_by_key(|g| std::cmp::Reverse(g.len()));
    println!("Found {} duplicate groups.", groups.len());

    for (i, g) in groups.iter().enumerate() {
        println!("\n[Group {}] {} files", i + 1, g.len());
        for p in g {
            println!("  {}", p.display());
        }
    }

    if move_to_trash {
        // Keep the first file in each group, trash the rest.
        let mut to_trash: Vec<PathBuf> = Vec::new();
        for g in &groups {
            to_trash.extend(g.iter().skip(1).cloned());
        }

        if !to_trash.is_empty() {
            trash::delete_all(&to_trash)?;
            println!("\nMoved {} files to Trash.", to_trash.len());
        }
    }

    Ok(())
}