Skip to main content

MoveAll

Struct MoveAll 

Source
#[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

Source

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.

Source

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.

Source

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§

Source§

impl Debug for MoveAll

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more