#[non_exhaustive]pub struct MoveAll { /* private fields */ }Expand description
Transactionally install a set of files: either every (source -> dest) move is applied, or
— on the first failure — all already-applied moves are rolled back, restoring every
destination to its prior contents. Use it to update a tool that ships more than one file (a
binary plus sidecar libraries/resources) without risking a half-applied update.
This is the multi-file analogue of Move. It relies on rename, so every source, every
destination, and the temp directory must live on the same filesystem (the same constraint
Move::replace_using_temp has) — in particular the staging dir holding the files you add
must be co-located with the destinations, not in $TMPDIR. The temp directory is used to
stash each displaced destination so it can be restored on rollback; a tempfile::TempDir is a
convenient choice.
commit drains the queued moves as it applies them, so a MoveAll is
single-use: a second commit has nothing left to do and is a no-op returning Ok(()). Rollback
is best-effort — if a rollback step itself fails it is logged via log::error! rather than
surfaced, and the error returned to the caller is always the original one that triggered the
rollback.
// The stash dir must be on the same filesystem as the destinations (rename can't cross
// filesystems), so create it next to them rather than in $TMPDIR.
let tmp = tempfile::TempDir::new_in("/usr/local")?;
// `new_bin` / `new_lib` are files you already extracted into a temp dir.
let new_bin = std::path::Path::new("/tmp/extracted/app");
let new_lib = std::path::Path::new("/tmp/extracted/libapp.so");
self_update::MoveAll::from_temp(tmp.path())
.add(new_bin, "/usr/local/bin/app")
.add(new_lib, "/usr/local/lib/libapp.so")
.commit()?; // all-or-nothing- Errors:
- Io - renaming a source into place or stashing an existing destination
Implementations§
Source§impl MoveAll
impl MoveAll
Sourcepub fn from_temp(temp: impl AsRef<Path>) -> Self
pub fn from_temp(temp: impl AsRef<Path>) -> Self
Start a transactional install, stashing displaced destinations under temp so they can be
restored if a later move fails. temp must be on the same filesystem as every destination.
Accepts anything path-like, storing an owned PathBuf.
Sourcepub fn add(
&mut self,
source: impl AsRef<Path>,
dest: impl AsRef<Path>,
) -> &mut Self
pub fn add( &mut self, source: impl AsRef<Path>, dest: impl AsRef<Path>, ) -> &mut Self
Queue a source -> dest move. Moves are applied by commit in the order
added.
Sourcepub fn commit(&mut self) -> Result<()>
pub fn commit(&mut self) -> Result<()>
Apply every queued move. On success all destinations have been replaced. On the first failure, every already-applied move (and the failing one’s partial state) is rolled back so each destination is left with its original contents, and the underlying error is returned.
The queued moves are drained as they are applied, so calling commit again is a no-op that
returns Ok(()).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MoveAll
impl RefUnwindSafe for MoveAll
impl Send for MoveAll
impl Sync for MoveAll
impl Unpin for MoveAll
impl UnsafeUnpin for MoveAll
impl UnwindSafe for MoveAll
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more