#temp-dir #temporary-files #temporary #directories #file

tempfs

A lightweight Rust crate for managing temporary files and directories with automatic cleanup

12 releases (6 breaking)

new 0.13.11 Feb 23, 2025
0.12.11 Feb 23, 2025
0.11.9 Feb 22, 2025
0.10.9 Feb 22, 2025
0.7.6 Feb 21, 2025

#356 in Filesystem

Download history

428 downloads per month

MIT/Apache

86KB
1.5K SLoC

tempfs

tempfs is a lightweight Rust crate that provides utilities for managing temporary files and directories. It makes working with temporary resources easier by automatically cleaning up files and directories when they go out of scope. The crate offers a flexible API with optional support for features such as random name generation, memory mapping, and regex-based file filtering.

Features

  • Temporary Directory (TempDir):
    Create and manage a temporary directory whose contents are automatically removed when the directory is dropped.

  • Temporary File (TempFile):
    Create temporary files with support for writing, reading, renaming, persisting, and even memory mapping (if enabled).

  • Optional Feature Flags:

    • rand_gen: Enables random name generation for temporary files and directories. (Requires the rand dependency.)
    • mmap_support: Enables memory mapping of temporary files via the memmap2 crate.
    • regex_support: Enables regex-based filtering and searching of temporary files using the regex crate.
    • virt_fs: Enables the new virt_fs module, providing a virtual, in-memory filesystem which mimics a Linux filesystem.
    • full: Activates all optional features at once.

Installation

Add tempfs to your Cargo.toml manually or use cargo add tempfs [-F <feature>]*.

Usage

Below is a simple example demonstrating how to create a temporary directory and file:

use std::fs::File;
use tempfs::{TempDir, TempFile};
use std::io::Write;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a temporary directory at the specified path.
    let mut temp_dir = TempDir::new("temp_directory")?;

    // Create a temporary file within the directory.
    let mut temp_file = temp_dir.create_file("example.txt")?;

    // Write data to the temporary file.
    writeln!(temp_file, "Hello, tempfs!")?;

    // Optionally, persist the file to prevent deletion and get the inner File.
    let _persisted_file: File = temp_file.persist()?;

    // The temporary directory will clean up any remaining temporary files on drop.
    Ok(())
}

Advanced Usage

  • Random Naming:
    If you enable the rand_gen feature, you can use methods like TempDir::random and TempFile::new_random to create temporary resources with random names.

  • Regex-Based Filtering:
    When the regex_support feature is enabled, you can filter temporary files using TempDir::find_files_by_pattern or its mutable counterpart.

  • Memory Mapping:
    With the mmap_support feature enabled, you can create memory maps of temporary files using TempFile::mmap and TempFile::mmap_mut.

Documentation

Full API documentation is available on docs.rs.

License

This project is dual licensed under the MIT and Apache 2.0 Licenses. See the MIT license and Apache license files for details.

Contributing

Contributions, issues, and feature requests are welcome! Please check the issues page for existing issues before creating new ones. Pull requests are also welcome.

Dependencies

~0–0.9MB
~15K SLoC