Skip to main content

UpdateGuard

Struct UpdateGuard 

Source
pub struct UpdateGuard { /* private fields */ }
Expand description

An exclusive hold on the writes of refs and config of one repository.

Repo::begin_update returns the guard. The guard holds the repository lock shared and the update lock. The update lock is exclusive across processes and inside the process.

The guard is a value: a caller holds it, passes it, and stores it. It is Send + Sync.

The guard reads and writes refs, collection refs, and ref aliases. It reads and writes config and the .conf files of the remotes directory. It stages no object. Each write of the guard is atomic and visible when the call returns.

§Remotes

The guard edits the remotes of config and of the .conf files of the remotes directory, Repo::remotes_dir, as a read-modify-write of the files on disk:

An add writes a new remote into the remotes directory if the handle has one and [core] add-remotes-config-dir of config is not false. Else it writes into config. Each other call edits the file that holds the remote, so a remote stays in its file.

Each of these calls reads config and the remotes directory again, and write_config reads the remotes directory again. A .conf file that an open refuses, and that comes into the directory after the open, makes each of these calls fail.

A write of a .conf file is atomic: the guard writes a temp file .ostrya-tmp-<pid>-XXXXXX in the directory, whose name does not end in .conf, and renames it over the file. With [core] fsync on, the write syncs the file and the directory with fsync before the call returns. A rewrite of a regular file keeps its permission bits, and does not keep its owner. A rewrite of a .conf file that is a symlink replaces the link with a regular file at the mode of a new file, and does not write through the link: the target keeps its content.

The update lock is the lock of one repository. Two repositories that share one remotes directory do not exclude each other. Each write changes one entry of the directory, so a race of the two loses at most the edit of one file.

These calls and write_config run one at a time on one guard.

§Writers that wait for the guard

A holder of the guard must write refs and config through the guard. These writers wait for the guard:

If [core] locking is on, Repo::prune also waits for the guard, because the guard holds the repository lock shared.

§A holder that waits for its own guard

No call detects a holder that waits for its own guard. This wait occurs if a task that holds the guard calls one of the writers that wait for the guard. It also occurs if that task waits for another task that calls one of these writers.

In both cases the wait lasts until [core] lock-timeout-secs passes. The writer then fails with Error::LockTimeout. If lock-timeout-secs is -1, the task waits forever.

§Release

finish releases the guard. A guard that drops without finish runs the directory syncs of finish synchronously, on the thread that drops the last reference to its state. It hides every error and then releases both locks.

That thread is the thread that drops the guard, or the blocking-pool thread of a write that still runs. The syncs block that thread, so an async caller releases the guard with finish.

Implementations§

Source§

impl UpdateGuard

Source

pub async fn read_ref(&self, r: &Ref) -> Result<Option<Checksum>>

Returns the commit that a ref names in the ref store.

The call reads the ref as Repo::resolve_ref_tip reads it. For a collection ref of the [core] collection-id of the repository, the call reads refs/heads/NAME, and it does not read refs/mirrors/ID/. If the store holds the ref as an alias, the call reads the ref that the alias names, by the alias rule of Repo::resolve_ref_tip. A ref path that passes through a file holds no ref. The result is None if the store holds no such ref.

§Errors
  • Error::InvalidRefspec if the ref fails Ref::validate. The call reads no file.
  • Error::NotARef if the ref path names a directory, a FIFO, a socket, or a device file, or an alias that breaks the alias rule.
  • Error::InvalidFormat if the ref file is not UTF-8 or is larger than 4096 bytes, or if a component of the ref path is a symlink.
  • Error::Core if the ref file holds no checksum.
  • Error::Io if the read fails, for example on a ref file that the process cannot read.
Source

pub async fn set_ref(&self, r: &Ref, checksum: Option<&Checksum>) -> Result<()>

Writes one ref atomically, as Repo::set_ref_immediate writes it.

The ref gives the path that Repo::set_ref_immediate writes: refs/heads/NAME for a collection ref of the [core] collection-id of the repository. If checksum is None, the call removes the ref file. The ref is visible when the call returns. The sync of each directory that changed waits for finish.

The id is the [core] collection-id that the open of the handle read. After write_config sets a new id NEW, the call writes @NEW:NAME to refs/mirrors/NEW/NAME until a new open. The ref resolve, the collection ref listings, and the summary of a new open ignore that file.

The call does not write or remove an alias. To remove an alias, use remove_ref_alias.

§Errors
  • Error::InvalidRefspec if the ref fails Ref::validate. The call writes nothing.
  • Error::RefNotWritable if the path of the ref names an alias. The call writes nothing, and the alias stays. The call gives the same error, with the reason passes through an alias, if refs or a component above the ref is a symlink, also a dangling one. It then writes nothing through the symlink.
  • Error::InvalidFormat if a file is at a directory component of the path, if a symlink is at tmp, or if a writer puts a symlink at a component of the path after the check of the path. The call then writes nothing through the symlink.
  • Error::Io if a file system operation fails.

If a directory of refs/mirrors/ has a name that fails the rule, a ref under it cannot be written or removed through this call. Remove such a ref by hand.

Source

pub async fn set_ref_alias(&self, alias: &Ref, target: &Ref) -> Result<()>

Writes alias as an alias of target, as Repo::set_ref_alias_immediate writes it.

The alias is a relative symlink. It replaces the entry that alias named before. The call checks both refs, and the two refs do not have to exist. An alias whose target is an alias is refused, as Repo::set_ref_alias_immediate states.

Each ref gives its path as set_ref states: refs/heads/NAME for a collection ref of the [core] collection-id, and refs/mirrors/ID/NAME for a collection ref of each other id. The id is the one that the open of the handle read, also after write_config sets a new id. An error names a ref by the text of its canonical form, as Ref::canonical gives it.

§Errors
  • Error::InvalidRefspec if alias or target fails Ref::validate. The call checks alias first.
  • Error::RefNotWritable if refs or a component above the path of alias is a symlink, also a dangling one. The reason is passes through an alias, and the call writes nothing.
  • Error::RefNotWritable for alias if target names an alias or the path of alias itself (reason targets an alias), or if refs or a component above the path of target is a symlink (reason passes through an alias). The call writes nothing.
  • Error::InvalidFormat if a file is at a directory component of the path, if a symlink is at tmp, or if a writer puts a symlink at a component of the path after the check of the path. The call then writes nothing through the symlink.
  • Error::Io if a file system operation fails.
Source

pub async fn remove_ref_alias(&self, alias: &Ref) -> Result<()>

Removes the alias alias, as Repo::remove_ref_alias_immediate removes it.

The ref gives the path as set_ref_alias states. The ref that the alias names stays. The removal of an absent path is not an error. The sync of the directory that held the link waits for finish.

§Errors
Source

pub async fn read_config(&self) -> Result<RepoConfig>

Reads the config file and the .conf files of the remotes directory of the handle from disk and parses them.

The repository handle keeps the config that it read at open. A read-modify-write of config under the guard reads the file with this call. The call reads the directory of Repo::remotes_dir, and does not read the key [ex-ostrya] remotes-config-dir again.

§Errors
Source

pub async fn write_config(&self, keyfile: &KeyFile) -> Result<()>

Replaces config with the document that keyfile holds.

The call writes the file as Repo::write_config writes it. The new file is visible when the call returns. The sync of the repository directory waits for finish.

The call writes the document as given. The repository handle and the guard keep the values that they read before. A new open of the repository reads the new values. This also applies to [core] collection-id, as set_ref states. The write does not interleave with a remote call of the same guard.

The call refuses a document with a [remote "NAME"] group when a .conf file of the remotes directory defines NAME, as Repo::write_config does. It reads the directory again for this check.

§Errors
  • Error::InvalidFormat if the document is larger than 1 MiB, the size that an open accepts. The call then writes nothing.
  • Error::RemoteExists if the document has a group of a remote of the remotes directory. The call then writes nothing.
  • The errors of OpenOptions::open for the remotes directory and its .conf files.
  • Error::InvalidFormat if tmp or tmp/cache is a symlink or is not a directory. The write of config makes its temp file in tmp/.
  • Error::Io if a file system operation fails.
Source

pub async fn add_remote(&self, name: &str, keys: &[(&str, &str)]) -> Result<()>

Adds the remote name, with the keys of keys in order.

If the handle has a remotes directory and [core] add-remotes-config-dir of config is not false, the call writes the file <name>.conf into the directory. The file holds the one group [remote "<name>"]. Its mode is 0666 with the umask applied. If the directory is absent, the call creates it and each absent parent, at mode 0777 with the umask applied. In a bare-user-shared repository, each directory that the call creates for an InRepo value gets the mode 02770. The call refuses the name if the directory holds an entry <name>.conf of any type, also a symlink. The check and the write are one step: the move of the temp file to <name>.conf does not replace an entry.

Else the [remote "<name>"] group goes at the end of config. The call does not touch a trusted keyring of the name. A refusal writes nothing.

The call reads config and the remotes directory from disk, edits the target, and writes it in one step on the blocking pool. It writes config as write_config does: atomically, with the sync of the repository directory left to finish. It writes a .conf file as UpdateGuard states in “Remotes”. Repo::config keeps the copy that the handle read at open.

The rewrite of config keeps the groups and keys of the file in their order. It drops the comment lines and the blank lines of the file.

The remote calls and write_config of one guard run one at a time, so concurrent calls on one guard lose no edit. If the future of a call drops after its first poll, the call makes the whole edit or none of it. An edit that runs completes, and finish waits for it.

§Errors
Source

pub async fn set_remote_key( &self, name: &str, key: &str, value: &str, ) -> Result<()>

Sets the key key of the remote name to value, in the file that holds the remote: config or a .conf file of the remotes directory.

A key that exists keeps its position. A new key goes at the end of the group. If the key holds value already, the call writes nothing.

The call reads the files and writes config as add_remote does. A rewrite of a .conf file keeps the other groups of the file and the groups and keys in their order, and drops the comment lines and the blank lines. A refusal writes nothing.

§Errors
Source

pub async fn unset_remote_key(&self, name: &str, key: &str) -> Result<bool>

Removes the key key of the remote name from the file that holds the remote.

The call returns true if it removed the key. If the key is absent, the call returns false and writes nothing. The group stays, also when it has no key left.

The call reads and writes the files as set_remote_key does.

§Errors
Source

pub async fn replace_remote( &self, name: &str, keys: &[(&str, &str)], ) -> Result<()>

Replaces the remote name with a remote of the keys of keys, in order, in the file that holds it.

The call removes the [remote "<name>"] group from its source, config or a .conf file of the remotes directory, and writes a new group with keys at the end of the same source. The other groups of the source stay. The call does not touch a trusted keyring of the name. A refusal writes nothing.

The call reads and writes the source as set_remote_key does.

§Errors
Source

pub async fn delete_remote(&self, name: &str) -> Result<()>

Deletes the remote name from the file that holds it, together with its repository keyring.

The call removes the trusted keyring <name>.trustedkeys.gpg at the repository root, and then the group of the remote from its file. For a symlink at the keyring, it removes the link, and the target stays. An absent keyring is not an error. A keyring name that is too long for the file system is not an error, because no file can have that name. The call does not remove a keyring of the remotes directory.

In a .conf file of the remotes directory, the call removes the group. If the file then holds no [remote "NAME"] group, the call removes the file, together with each other group of it. For a symlink, it removes the link, and the target stays. Else it writes the file back, as set_remote_key does.

The repository keyring goes with the remote. A keyring of the remotes directory stays, as the ostree command keeps it. A later remote of the same name trusts the keys of that keyring while the repository holds no keyring of that name. The repository keyring goes first, so a failure between the two steps can leave a remote with no repository keyring. It cannot leave a repository keyring with no remote.

The removal is visible when the call returns. The sync of the repository directory waits for finish. The call reads the files and writes config as add_remote does. A refusal removes no file and writes nothing.

§Errors
Source

pub async fn finish(self) -> Result<()>

Syncs the directories that the writes changed, and releases both locks.

The call first waits until each write of the guard ends. It then runs fsync once on each directory that a write changed, deepest first. It then releases the update lock and the repository lock. If [core] fsync is off, the guard records no directory, and the call runs no sync.

The wait covers a write whose future was dropped while its work on the blocking pool still ran. That work ends before the syncs start, so the call syncs the directories of each write of the guard. Both locks are free when the call returns.

The call runs each sync, also after a sync that fails. The release of the locks comes after the syncs, also if the returned future drops before it completes.

§Errors
  • Error::Io if a sync fails. The call returns the error of the first sync that fails.

Trait Implementations§

Source§

impl Debug for UpdateGuard

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. 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> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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