Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #526 from cgwalters/add-force-sysroot-lock
Browse files Browse the repository at this point in the history
sysroot: Add `from_assume_locked`
  • Loading branch information
jmarrero authored Aug 31, 2023
2 parents bd77743 + e4e8d18 commit a45512a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/src/sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ use anyhow::Result;
pub struct SysrootLock {
/// The underlying sysroot value.
pub sysroot: ostree::Sysroot,
/// True if we didn't actually lock
unowned: bool,
}

impl Drop for SysrootLock {
fn drop(&mut self) {
if self.unowned {
return;
}
self.sysroot.unlock();
}
}
Expand All @@ -28,12 +33,14 @@ impl Deref for SysrootLock {
impl SysrootLock {
/// Asynchronously acquire a sysroot lock. If the lock cannot be acquired
/// immediately, a status message will be printed to standard output.
/// The lock will be unlocked when this object is dropped.
pub async fn new_from_sysroot(sysroot: &ostree::Sysroot) -> Result<Self> {
let mut printed = false;
loop {
if sysroot.try_lock()? {
return Ok(Self {
sysroot: sysroot.clone(),
unowned: false,
});
}
if !printed {
Expand All @@ -43,4 +50,13 @@ impl SysrootLock {
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
}
}

/// This function should only be used when you have locked the sysroot
/// externally (e.g. in C/C++ code). This also does not unlock on drop.
pub fn from_assumed_locked(sysroot: &ostree::Sysroot) -> Self {
Self {
sysroot: sysroot.clone(),
unowned: true,
}
}
}

0 comments on commit a45512a

Please sign in to comment.