cleanux 0.1.1

Linux cleaner CLI (CleanMyMac-like): scan & safely trash cache, find duplicates, list large files
Documentation
use crate::config::Config;
use anyhow::Result;
use globset::GlobSet;
use ignore::WalkBuilder;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct PathFilter {
    exclude_prefixes: Vec<PathBuf>,
    allow: Option<GlobSet>,
    deny: Option<GlobSet>,
}

impl PathFilter {
    pub fn from_config(cfg: &Config) -> Result<Self> {
        Ok(Self {
            exclude_prefixes: cfg.exclude_prefixes.clone(),
            allow: Config::build_globset(&cfg.allow_globs)?,
            deny: Config::build_globset(&cfg.deny_globs)?,
        })
    }

    pub fn allows(&self, path: &Path) -> bool {
        if self.exclude_prefixes.iter().any(|ex| path.starts_with(ex)) {
            return false;
        }

        if let Some(deny) = &self.deny {
            if deny.is_match(path) {
                return false;
            }
        }

        if let Some(allow) = &self.allow {
            return allow.is_match(path);
        }

        true
    }
}

pub fn walk_builder(root: &Path) -> WalkBuilder {
    let mut builder = WalkBuilder::new(root);
    builder
        .hidden(false)
        .follow_links(false)
        .same_file_system(true);
    builder
}

pub fn dir_size_bytes(root: &Path) -> Result<u64> {
    if !root.exists() {
        return Ok(0);
    }

    let mut total: u64 = 0;
    for dent in walk_builder(root).build().flatten() {
        if let Ok(md) = std::fs::symlink_metadata(dent.path()) {
            let file_type = md.file_type();
            if file_type.is_symlink() {
                continue;
            }
            if file_type.is_file() {
                total = total.saturating_add(md.len());
            }
        }
    }
    Ok(total)
}