summaryrefslogtreecommitdiff
path: root/rust-bindings/src/sysroot.rs
blob: d7b2b76790c02b8d5cda68e37f292538561a8085 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::gio;
use crate::Sysroot;
use std::path::PathBuf;

#[derive(Clone, Debug, Default)]
/// Builder object for `Sysroot`.
pub struct SysrootBuilder {
    path: Option<PathBuf>,
    mount_namespace_in_use: bool,
}

impl SysrootBuilder {
    /// Create a new builder for `Sysroot`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the path to the sysroot location.
    pub fn path(mut self, path: Option<PathBuf>) -> Self {
        self.path = path;
        self
    }

    #[cfg(any(feature = "v2020_1", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2020_1")))]
    /// Set whether the logic is running in its own mount namespace.
    pub fn mount_namespace_in_use(mut self, mount_namespace_in_use: bool) -> Self {
        self.mount_namespace_in_use = mount_namespace_in_use;
        self
    }

    /// Load an existing `Sysroot` from disk, finalizing this builder.
    pub fn load(self, cancellable: Option<&gio::Cancellable>) -> Result<Sysroot, glib::Error> {
        let sysroot = self.configure_common();
        sysroot.load(cancellable)?;

        Ok(sysroot)
    }

    /// Create a new `Sysroot` on disk, finalizing this builder.
    pub fn create(self, cancellable: Option<&gio::Cancellable>) -> Result<Sysroot, glib::Error> {
        let sysroot = self.configure_common();
        sysroot.ensure_initialized(cancellable)?;
        sysroot.load(cancellable)?;

        Ok(sysroot)
    }

    /// Perform common configuration steps, returning a not-yet-fully-loaded `Sysroot`.
    fn configure_common(self) -> Sysroot {
        let sysroot = {
            let opt_file = self.path.map(gio::File::for_path);
            Sysroot::new(opt_file.as_ref())
        };

        #[cfg(feature = "v2020_1")]
        if self.mount_namespace_in_use {
            sysroot.set_mount_namespace_in_use();
        }

        sysroot
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sysroot_create_load_empty() {
        // Create and load an empty sysroot. Make sure it can be properly
        // inspected as empty, without panics.
        let tmpdir = tempfile::tempdir().unwrap();

        let path_created = {
            let tmp_path = Some(tmpdir.path().to_path_buf());
            let builder = SysrootBuilder::new().path(tmp_path);

            let sysroot = builder.create(gio::Cancellable::NONE).unwrap();

            assert!(sysroot.fd() >= 0);
            assert_eq!(sysroot.deployments().len(), 0);
            assert_eq!(sysroot.booted_deployment(), None);
            assert_eq!(sysroot.bootversion(), 0);
            assert_eq!(sysroot.subbootversion(), 0);
            sysroot.cleanup(gio::Cancellable::NONE).unwrap();

            sysroot.path().unwrap()
        };
        let path_loaded = {
            let tmp_path = Some(tmpdir.path().to_path_buf());
            let builder = SysrootBuilder::new().path(tmp_path);

            let sysroot = builder.create(gio::Cancellable::NONE).unwrap();

            assert!(sysroot.fd() >= 0);
            assert_eq!(sysroot.deployments().len(), 0);
            assert_eq!(sysroot.booted_deployment(), None);
            assert_eq!(sysroot.bootversion(), 0);
            assert_eq!(sysroot.subbootversion(), 0);
            sysroot.cleanup(gio::Cancellable::NONE).unwrap();

            sysroot.path().unwrap()
        };
        assert_eq!(path_created.to_string(), path_loaded.to_string());
    }
}