disk-cleaner 0.1.5

A disk usage cli similar to windirstat
Documentation
use rayon::prelude::*;
use serde::Serialize;
use std::error::Error;
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::sync::Arc;

mod ffi;
pub mod projects;
pub mod tui;

pub use projects::{
    analyze, clean, dir_size, scan, Project, ProjectAnalysis, ProjectType, ScanError, ScanOptions,
};

#[derive(Serialize, Clone)]
pub struct DiskItem {
    pub name: String,
    pub disk_size: u64,
    pub children: Option<Vec<DiskItem>>,
}

impl DiskItem {
    pub fn from_analyze(
        path: &Path,
        apparent: bool,
        root_dev: u64,
    ) -> Result<Self, Box<dyn Error>> {
        // One shared hardlink-dedup set spans the whole tree, so a file linked
        // under multiple paths (Cargo's target/ does this for every build
        // script and binary) is counted once — matching `du`/`df`, not once per
        // link. Without this the tree's per-directory and total sizes overshoot
        // what is actually on disk.
        let counter = Arc::new(projects::FileSizeCounter::new(apparent));
        Self::from_analyze_inner(path, root_dev, &counter)
    }

    fn from_analyze_inner(
        path: &Path,
        root_dev: u64,
        counter: &Arc<projects::FileSizeCounter>,
    ) -> Result<Self, Box<dyn Error>> {
        let name = path
            .file_name()
            .unwrap_or(OsStr::new("."))
            .to_string_lossy()
            .to_string();

        let md = path.symlink_metadata()?;
        if md.is_dir() {
            let volume_id = volume_id_of(&md, path)?;
            if volume_id != root_dev {
                return Err("Filesystem boundary crossed".into());
            }

            let sub_entries = fs::read_dir(path)?
                .filter_map(Result::ok)
                .collect::<Vec<_>>();

            let mut sub_items = sub_entries
                .par_iter()
                .filter_map(|entry| {
                    DiskItem::from_analyze_inner(&entry.path(), root_dev, counter).ok()
                })
                .collect::<Vec<_>>();

            sub_items.sort_unstable_by(|a, b| a.disk_size.cmp(&b.disk_size).reverse());

            Ok(DiskItem {
                name,
                disk_size: sub_items.iter().map(|di| di.disk_size).sum(),
                children: Some(sub_items),
            })
        } else {
            // Files: count through the shared counter so hardlinks are deduped.
            let size = counter.add(&md, path);
            Ok(DiskItem {
                name,
                disk_size: size,
                children: None,
            })
        }
    }

    /// Build a fully-recursive `DiskItem` tree for `path`.
    ///
    /// Despite the historical name, this is *not* a shallow scan: it performs a
    /// complete recursive analysis of the entire subtree, identical to
    /// [`DiskItem::from_analyze`]. The separate entry point is kept only so the
    /// TUI's `do_shallow_scan` call site reads naturally. New code should call
    /// [`DiskItem::from_analyze`] directly.
    pub fn from_shallow_scan(
        path: &Path,
        apparent: bool,
        root_dev: u64,
    ) -> Result<Self, Box<dyn Error>> {
        Self::from_analyze(path, apparent, root_dev)
    }
}

/// The device/volume id of `md`'s filesystem, used to detect filesystem
/// boundaries during a recursive walk. On Unix this is `st_dev` from the same
/// `symlink_metadata` call [`DiskItem::from_analyze`] already made; on Windows
/// the volume serial number requires reopening the path, so it is fetched only
/// for directories (files don't need a boundary check).
fn volume_id_of(md: &fs::Metadata, path: &Path) -> Result<u64, Box<dyn Error>> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        let _ = path;
        Ok(md.dev())
    }
    #[cfg(windows)]
    {
        use winapi_util::{file, Handle};
        let _ = md;
        let info = file::information(Handle::from_path_any(path)?)?;
        Ok(info.volume_serial_number())
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = (md, path);
        Ok(0)
    }
}

pub enum FileInfo {
    File { size: u64, volume_id: u64 },
    Directory { volume_id: u64 },
}

impl FileInfo {
    #[cfg(unix)]
    pub fn from_path(path: &Path, apparent: bool) -> Result<Self, Box<dyn Error>> {
        use std::os::unix::fs::MetadataExt;

        let md = path.symlink_metadata()?;
        if md.is_dir() {
            Ok(FileInfo::Directory {
                volume_id: md.dev(),
            })
        } else {
            let size = if apparent {
                md.len()
            } else {
                md.blocks() * 512
            };
            Ok(FileInfo::File {
                size,
                volume_id: md.dev(),
            })
        }
    }

    #[cfg(windows)]
    pub fn from_path(path: &Path, apparent: bool) -> Result<Self, Box<dyn Error>> {
        use winapi_util::{file, Handle};
        const FILE_ATTRIBUTE_DIRECTORY: u64 = 0x10;

        let h = Handle::from_path_any(path)?;
        let md = file::information(h)?;

        if md.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0 {
            Ok(FileInfo::Directory {
                volume_id: md.volume_serial_number(),
            })
        } else {
            let size = if apparent {
                md.file_size()
            } else {
                ffi::compressed_size(path)?
            };
            Ok(FileInfo::File {
                size,
                volume_id: md.volume_serial_number(),
            })
        }
    }
}

#[cfg(all(test, unix))]
mod tests {
    use super::{FileInfo, DiskItem};
    use std::error::Error;
    use std::fs::{self, File};
    use std::os::unix::fs::MetadataExt;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn apparent_size_uses_logical_file_length() -> Result<(), Box<dyn Error>> {
        let dir = std::env::temp_dir().join(format!(
            "disk-cleaner-{}",
            SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos()
        ));
        fs::create_dir(&dir)?;
        let path = dir.join("sparse.bin");
        let file = File::create(&path)?;
        let sparse_len = 1024 * 1024;
        file.set_len(sparse_len)?;
        drop(file);

        let apparent_size = match FileInfo::from_path(&path, true)? {
            FileInfo::File { size, .. } => size,
            FileInfo::Directory { .. } => panic!("test path should be a file"),
        };
        let disk_size = match FileInfo::from_path(&path, false)? {
            FileInfo::File { size, .. } => size,
            FileInfo::Directory { .. } => panic!("test path should be a file"),
        };

        fs::remove_file(&path)?;
        fs::remove_dir(&dir)?;

        assert_eq!(apparent_size, sparse_len);
        assert!(disk_size <= apparent_size);
        Ok(())
    }

    /// The `du`-style tree must count a hardlinked file once per inode, not once
    /// per directory entry. Without this the tree (and the TUI disk view built
    /// from it) reports inflated sizes for Cargo `target/` directories, where
    /// every build script and binary is hardlinked under two paths.
    #[test]
    fn disk_item_dedups_hardlinked_files() -> Result<(), Box<dyn Error>> {
        let dir = std::env::temp_dir().join(format!(
            "disk-cleaner-{}",
            SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos()
        ));
        fs::create_dir(&dir)?;

        // A 1 MiB file, then a second name (hardlink) pointing at the same inode.
        let primary = dir.join("a.bin");
        let linked = dir.join("b.bin");
        let f = File::create(&primary)?;
        f.set_len(1024 * 1024)?;
        drop(f);
        fs::hard_link(&primary, &linked)?;

        // Sanity: one inode, two links.
        assert_eq!(fs::metadata(&primary)?.nlink(), 2);

        let dev = fs::metadata(&dir)?.dev();
        let item = DiskItem::from_analyze(&dir, true, dev)?;

        // Counted once: 1 MiB apparent, not 2 MiB.
        assert_eq!(item.disk_size, 1024 * 1024);

        fs::remove_dir_all(&dir)?;
        Ok(())
    }
}