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
428 downloads per month
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 therand
dependency.)mmap_support
: Enables memory mapping of temporary files via thememmap2
crate.regex_support
: Enables regex-based filtering and searching of temporary files using theregex
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 therand_gen
feature, you can use methods likeTempDir::random
andTempFile::new_random
to create temporary resources with random names. -
Regex-Based Filtering:
When theregex_support
feature is enabled, you can filter temporary files usingTempDir::find_files_by_pattern
or its mutable counterpart. -
Memory Mapping:
With themmap_support
feature enabled, you can create memory maps of temporary files usingTempFile::mmap
andTempFile::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