Expand description
§gravityfile-ops
File operations for gravityfile: copy, move, rename, create, delete, undo and archives.
This crate exists for the grav binary and its sibling crates. Its API
follows their needs and can change in any minor release.
Long-running operations run on blocking threads and report through a
channel: best-effort OperationProgress snapshots, interactive
ConflictRequests, and one reliable completion message. Dropping the
receiver cancels the operation.
§Safety model
- New content is assembled in a private staging directory beside its destination and published with a single no-clobber rename, so a failed or cancelled operation never leaves a partial item at a visible path and never silently replaces anything.
- Overwrites the user approves move the replaced item to the system Trash (or delete it permanently, if configured) only after the replacement is complete.
- Copies skip entries they cannot read and report each one instead of discarding everything that did copy; cross-filesystem moves keep the source unless every entry arrived.
- Completed operations return what an undo needs as an
UndoEntry.execute_undomoves and renames items back, sends copies to the Trash, and removes created files and directories only while they are still empty.
use gravityfile_ops::{CancellationToken, TransferOptions, TransferResult, start_copy};
let dir = tempfile::tempdir()?;
let source = dir.path().join("notes.txt");
let backup = dir.path().join("backup");
std::fs::write(&source, "hello")?;
std::fs::create_dir(&backup)?;
let runtime = tokio::runtime::Runtime::new()?;
let complete = runtime.block_on(async {
let mut events = start_copy(
vec![source],
backup.clone(),
TransferOptions::default(),
CancellationToken::new(),
);
while let Some(event) = events.recv().await {
match event {
TransferResult::Progress(_) => {}
// Dropping a request skips that item.
TransferResult::Conflict(_) => {}
TransferResult::Complete(complete) => return Some(complete),
}
}
None
});
let complete = complete.expect("copy completes");
assert_eq!(complete.inner.succeeded, 1);
assert_eq!(std::fs::read_to_string(backup.join("notes.txt"))?, "hello");§Licence
MIT or Apache-2.0, at your option.
Re-exports§
pub use archive::ArchiveError;pub use archive::ArchiveFormat;pub use archive::ArchiveResult;pub use archive::ExtractLimits;pub use archive::create_archive;pub use archive::create_archive_cancellable;pub use archive::extract_archive;pub use archive::extract_archive_cancellable;pub use archive::extract_archive_with;
Modules§
- archive
- Archive extraction and creation.
Structs§
- Cancellation
Token - A token which can be used to signal a cancellation request to one or more tasks.
- Conflict
- A conflict detected during a file operation.
- Conflict
Request - A conflict awaiting an answer from the caller.
- Delete
Complete - Completion of a delete.
- Operation
Complete - Result of a completed operation.
- Operation
Error - A problem with one path during an operation.
- Operation
Progress - Progress information for an ongoing operation.
- Transfer
Complete - Completion of a copy or move.
- Transfer
Options - Options for copy and move.
- Undo
Complete - Completion of an undo.
- Undo
Entry - A recorded operation.
- UndoLog
- Bounded, most-recent-last undo history.
Enums§
- Conflict
Kind - The kind of conflict encountered.
- Conflict
Resolution - How to resolve a conflict.
- Create
Result - Result sent through the channel during create operations.
- Delete
Mode - Where deleted items go.
- Delete
Result - Message sent while a delete runs.
- Operation
Type - The type of operation being performed.
- Rename
Result - Message sent while a rename runs.
- Replace
Policy - How an existing destination is disposed of when the user chooses to overwrite it.
- Transfer
Result - Message sent while a copy or move runs.
- Undo
Result - Message sent while an undo runs.
- Undoable
Operation - An operation that can be reversed.
Constants§
- OPERATION_
CHANNEL_ SIZE - Channel capacity for operation messages.
- STAGING_
PREFIX - Prefix of every staging directory this crate creates.
Functions§
- auto_
rename_ path - Generate an auto-renamed path to avoid conflicts.
- execute_
undo - Reverse
entry. Items are processed independently; failures are reported and returned inUndoComplete::remaining. - rename_
paths - Rename a set of paths as one unit, including swaps (
a→b,b→a), cycles and case-only changes on case-insensitive filesystems. - start_
copy - Copy
sourcesinto thedestinationdirectory. - start_
create_ directory - Start directory creation with the same delivery semantics as file creation.
- start_
create_ file - Start file creation. Dropping the receiver prevents queued work from starting; active filesystem calls are awaited and cannot be interrupted.
- start_
delete - Delete
items, each given as(path, size); the size feeds progress. Symlinks are removed themselves, never their targets. - start_
move - Move
sourcesinto thedestinationdirectory. Same-filesystem moves are a single rename; cross-filesystem moves copy, then delete the source only when every entry arrived. - start_
rename - Rename
sourcetonew_namewithin its directory. - validate_
filename - Validate a new file name against portable rules.