TFIO is a library that provides a Transaction-like interface traditionally used in databases, applied to FileIO operations. It gives the flexibility to execute and rollback singular operations as well as transactions on the fly. The library also provides a builder-pattern interface to chain operations and execute them in one go.
Features
- 100% safe code (thanks to Rust)
- 11 rollback-able File/Directory operations
- Transparent cross-filesystem move fallback
- Pre-existing destinations backed up and restored on rollback
- All
Errorsexposed for handling - 100% tests passing
- Optional async support via the
tokiofeature flag
Minimum Supported Rust Version (MSRV): 1.85
Usage
Import the library in your Rust project:
[]
= "0.3"
Create a transaction and execute it. If any Error is encountered, rollback the entire transaction:
use io;
use *;
Transaction::new() uses the OS temp directory for backups. To specify a custom backup directory:
let mut tr = with_temp_dir
.delete_file
.delete_dir;
You can also use single operations directly:
use io;
use ;
Async Support
Enable the tokio feature in Cargo.toml:
[]
= { = "0.3", = ["tokio"] }
Then use AsyncTransaction which mirrors the sync API:
use AsyncTransaction;
async
Available Operations
| Operation | Sync | Async |
|---|---|---|
CreateFile |
✓ | ✓ |
CreateDirectory |
✓ | ✓ |
DeleteFile |
✓ | ✓ |
DeleteDirectory |
✓ | ✓ |
CopyFile |
✓ | ✓ |
CopyDirectory |
✓ | ✓ |
MoveFile |
✓ | ✓ |
MoveDirectory |
✓ | ✓ |
WriteFile |
✓ | ✓ |
AppendFile |
✓ | ✓ |
TouchFile |
✓ | ✓ |
Move operations automatically fall back to copy-then-delete when source and destination are on different filesystems. Copy and delete operations store a backup in temp_dir and restore the original on rollback.
Notes
CreateFilefails if the target file already exists (useWriteFileto overwrite).rollback()is safe to call even ifexecute()was never called or failed partway through.- All operations that require a backup directory default to the OS temp dir. Use the
::with_temp_dir(...)constructor to specify a custom location (e.g. same filesystem as the target for atomic moves). - Async types (
AsyncTransaction,AsyncCopyFile, etc.) are re-exported directly from the crate root under thetokiofeature — no need to import fromtfio::async_::*.
Running Tests
git clone https://github.com/MovAh13h/tfio
cd tfio
cargo test # sync tests
cargo test --features tokio # sync + async tests