1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! Helpers for interacting with sysroots.

use std::ops::Deref;

use anyhow::Result;

/// A locked system root.
#[derive(Debug)]
pub struct SysrootLock {
    /// The underlying sysroot value.
    pub sysroot: ostree::Sysroot,
}

impl Drop for SysrootLock {
    fn drop(&mut self) {
        self.sysroot.unlock();
    }
}

impl Deref for SysrootLock {
    type Target = ostree::Sysroot;

    fn deref(&self) -> &Self::Target {
        &self.sysroot
    }
}

impl SysrootLock {
    /// Asynchronously acquire a sysroot lock.  If the lock cannot be acquired
    /// immediately, a status message will be printed to standard output.
    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(),
                });
            }
            if !printed {
                println!("Waiting for sysroot lock...");
                printed = true;
            }
            tokio::time::sleep(std::time::Duration::from_secs(3)).await;
        }
    }
}