pub struct FileManager { /* private fields */ }
Expand description
Safe file operation manager with rollback capabilities.
The FileManager
provides atomic file operations with automatic
rollback functionality. It tracks all file creations and deletions,
allowing complete operation reversal in case of errors.
§Use Cases
- Batch file operations that need to be atomic
- Temporary file creation during processing
- Safe file replacement with backup
§Examples
use subx_cli::core::file_manager::FileManager;
use std::path::Path;
let mut manager = FileManager::new();
// Create a new file (tracked for rollback)
manager.record_creation(Path::new("output.srt"));
// Remove an existing file (backed up for rollback)
manager.remove_file(Path::new("old_file.srt")).unwrap();
// If something goes wrong, rollback all operations
manager.rollback().unwrap();
§Safety
The manager ensures that:
- Created files are properly removed on rollback
- Removed files are backed up and restored on rollback
- No partial state is left after rollback completion
Implementations§
Source§impl FileManager
impl FileManager
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new FileManager
with an empty operation history.
The new manager starts with no tracked operations and is ready to begin recording file operations for potential rollback.
§Examples
use subx_cli::core::file_manager::FileManager;
let manager = FileManager::new();
Sourcepub fn record_creation<P: AsRef<Path>>(&mut self, path: P)
pub fn record_creation<P: AsRef<Path>>(&mut self, path: P)
Records the creation of a file for potential rollback.
This method should be called after successfully creating a file that may need to be removed if a rollback is performed. The file is not immediately affected, only tracked for future rollback.
§Arguments
path
: Path to the created file
§Examples
use subx_cli::core::file_manager::FileManager;
use std::path::Path;
let mut manager = FileManager::new();
// After creating a file...
manager.record_creation(Path::new("output.srt"));
// File will be removed if rollback() is called
Sourcepub fn remove_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
pub fn remove_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
Safely removes a file and tracks the operation for rollback.
The file is backed up before removal, allowing it to be restored
if a rollback is performed. The backup is created with a .bak
extension in the same directory as the original file.
§Arguments
path
: Path to the file to remove
§Returns
Returns Ok(())
if the file was successfully removed and backed up,
or an error if the file doesn’t exist or removal fails.
§Errors
SubXError::FileNotFound
if the file doesn’t existSubXError::FileOperationFailed
if backup creation or removal fails
§Examples
Sourcepub fn rollback(&mut self) -> Result<()>
pub fn rollback(&mut self) -> Result<()>
Rolls back all recorded operations in reverse execution order.
This method undoes all file operations that have been recorded, restoring the filesystem to its state before any operations were performed. Operations are reversed in LIFO order to maintain consistency.
§Rollback Behavior
- Created files: Removed from the filesystem
- Removed files: Restored from backup (if backup was created)
§Returns
Returns Ok(())
if all rollback operations succeed, or the first
error encountered during rollback.
§Errors
Returns SubXError::FileOperationFailed
if any rollback operation fails.
Note that partial rollback may occur if some operations succeed before
an error is encountered.
§Examples
use subx_cli::core::file_manager::FileManager;
let mut manager = FileManager::new();
// ... perform some file operations ...
// Rollback all operations
manager.rollback()?;
Sourcepub fn operation_count(&self) -> usize
pub fn operation_count(&self) -> usize
Returns the number of operations currently tracked.
This can be useful for testing or monitoring the state of the file manager.
§Examples
use subx_cli::core::file_manager::FileManager;
let manager = FileManager::new();
assert_eq!(manager.operation_count(), 0);