Skip to main content

Crate remove_except

Crate remove_except 

Source
Expand description

Safe removal plan generation for cleaning directories while keeping specified items.

remove-except scans beneath a chosen processing root, compares discovered filesystem items against keep patterns and optional modification-time bounds, and creates an in-memory RemovalPlan. The plan partitions items into kept entries (including required ancestor directories) and deletion targets.

§Safety by Design

  • Separation of Planning and Deletion: build_plan is strictly read-only. It records an in-memory plan from entries observed during traversal. The traversal is not an atomic or protected filesystem snapshot and does not lock or prevent concurrent changes. It does not create, modify, or delete any files or directories.
  • Root Protection: The processing root itself is never included in the plan, and filesystem roots (such as / on Unix or C:\ on Windows) are rejected to prevent catastrophic deletion.
  • No Symlink Traversal: Symbolic links encountered beneath the root are never followed. Each link is evaluated as an individual leaf item; if targeted for deletion, only the symbolic link itself is deleted, never its target.
  • Unambiguous Paths: The normalized root and every scanned entry path must be representable as Unicode. Otherwise, planning fails without returning a partial plan.

§Pattern Matching Syntax and Semantics

build_plan requires at least one keep pattern. build_plan_with_options also accepts a modification-time range without patterns. Multiple patterns use OR, while the time range is combined with the pattern condition using AND. When a time range is present, only matching regular files and their ancestors are kept. The following pattern retention rules describe behavior without a time range:

  • * matches any sequence of non-separator characters within a single directory component.
  • ? matches one Unicode scalar value other than a separator. A character class also matches one Unicode scalar value; combining sequences can therefore require multiple wildcards.
  • ** matches zero or more directory levels.
  • [...] matches any character in the character class, adhering to standard glob rules.
  • {a,b} matches any of the comma-separated alternatives.
  • . or ./ represents the processing root and keeps everything beneath it.
  • A directory path without glob syntax keeps that directory and all of its contents recursively. Glob syntax is *, ?, [, {, and }; on non-Windows platforms, \ is also glob syntax because it escapes the next character. On Windows, \ is a path separator.
  • A glob match keeps the matching item and all required ancestor directories up to the root, but does not recursively keep unmatched descendants. For instance, dir/* matches items directly beneath dir/, but does not recursively keep unmatched contents of subdirectories beneath them. Likewise, dir/** does not match dir itself, and an escaped name such as backup[[]2025] keeps only that directory; append /** to keep its contents.
  • Relative patterns are resolved relative to the processing root. Patterns containing a .. component are rejected.
  • Leading and trailing whitespace in patterns is preserved.

§Platform Differences

  • Windows: Path matching uses Unicode simple case folding (non-Turkic), ignoring per-directory NTFS case-sensitivity settings.
  • Ubuntu: Path matching is strictly case-sensitive. Windows and Ubuntu are the supported platforms.

§Examples

use remove_except::build_plan;
use std::fs::{self, File};

let temp_dir = assert_fs::TempDir::new().unwrap();
let root = temp_dir.path();
fs::create_dir_all(root.join("src")).unwrap();
File::create(root.join("src/main.rs")).unwrap();
File::create(root.join("notes.txt")).unwrap();

// Build a removal plan keeping only files inside `src/`:
let plan = build_plan(root, &["src/**".to_owned()])
    .expect("valid root and keep patterns");

// The plan identifies kept items and ancestors:
assert!(plan.keep_items().iter().any(|item| item.relative_path() == "src/main.rs"));
assert!(plan.keep_items().iter().any(|item| item.relative_path() == "src"));

// Deletion roots identify top-level removal targets:
assert!(plan.delete_roots().iter().any(|item| item.relative_path() == "notes.txt"));

Structs§

ModifiedRange
A half-open range of modification instants.
PlanOptions
Optional conditions used while generating a removal plan.
PlannedItem
A filesystem item observed during the traversal that created a removal plan.
RemovalPlan
An in-memory plan containing items to keep or delete beneath a root directory.
ValidationError
Marks an error caused by an invalid processing root or invalid keep conditions.

Enums§

ItemType
The type of a filesystem item found during traversal.

Functions§

build_plan
Scans beneath the specified root directory and constructs a RemovalPlan.
build_plan_with_options
Builds a read-only removal plan with optional modification-time filtering.