steady-fs 0.1.0

Small, boring, reliable filesystem helpers for Rust CLI tools and developer apps.
Documentation
use std::path::{Component, Path, PathBuf};

use crate::{Error, Result};

/// Starts a directory clean operation for `path`.
///
/// Cleaning removes the directory contents while leaving the directory itself
/// in place.
pub fn clean_dir<P>(path: P) -> CleanDir
where
    P: AsRef<Path>,
{
    CleanDir {
        path: path.as_ref().to_path_buf(),
        dry_run: false,
    }
}

/// Builder for [`clean_dir`].
#[derive(Debug, Clone)]
pub struct CleanDir {
    path: PathBuf,
    dry_run: bool,
}

impl CleanDir {
    /// Sets whether the operation should validate guardrails but skip filesystem changes.
    pub fn dry_run(mut self, dry_run: bool) -> Self {
        self.dry_run = dry_run;
        self
    }

    /// Removes the contents of the directory.
    pub fn run(self) -> Result<()> {
        if is_dangerous_path(&self.path) {
            return Err(Error::DangerousCleanDir { path: self.path });
        }

        if self.dry_run {
            return Ok(());
        }

        let entries = std::fs::read_dir(&self.path).map_err(|source| Error::CleanDir {
            path: self.path.clone(),
            source,
        })?;

        for entry in entries {
            let entry = entry.map_err(|source| Error::CleanDir {
                path: self.path.clone(),
                source,
            })?;
            let entry_path = entry.path();
            let file_type = entry.file_type().map_err(|source| Error::CleanDir {
                path: self.path.clone(),
                source,
            })?;

            if file_type.is_dir() {
                std::fs::remove_dir_all(&entry_path).map_err(|source| Error::CleanDir {
                    path: self.path.clone(),
                    source,
                })?;
            } else {
                std::fs::remove_file(&entry_path).map_err(|source| Error::CleanDir {
                    path: self.path.clone(),
                    source,
                })?;
            }
        }

        Ok(())
    }
}

fn is_dangerous_path(path: &Path) -> bool {
    if path.as_os_str().is_empty() {
        return true;
    }

    let mut has_normal = false;

    for component in path.components() {
        match component {
            Component::Normal(_) => has_normal = true,
            Component::ParentDir | Component::CurDir => return true,
            Component::RootDir | Component::Prefix(_) => {}
        }
    }

    !has_normal
}