system_image/
system_image.rs

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::{
    get_system_image_hash, CachePackages, CachePackagesInitError, StaticPackages,
    StaticPackagesInitError,
};
use anyhow::Context as _;
use fuchsia_hash::Hash;
use package_directory::RootDir;
use std::sync::Arc;

static DISABLE_RESTRICTIONS_FILE_PATH: &str = "data/pkgfs_disable_executability_restrictions";

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ExecutabilityRestrictions {
    Enforce,
    DoNotEnforce,
}

/// System image package.
pub struct SystemImage {
    root_dir: Arc<RootDir<blobfs::Client>>,
}

impl SystemImage {
    pub async fn new(
        blobfs: blobfs::Client,
        boot_args: &fidl_fuchsia_boot::ArgumentsProxy,
    ) -> Result<Self, anyhow::Error> {
        let hash = get_system_image_hash(boot_args).await.context("getting system_image hash")?;
        let root_dir = RootDir::new(blobfs, hash)
            .await
            .with_context(|| format!("creating RootDir for system_image: {hash}"))?;
        Ok(SystemImage { root_dir })
    }

    /// Make a `SystemImage` from a `RootDir` for the `system_image` package.
    pub fn from_root_dir(root_dir: Arc<RootDir<blobfs::Client>>) -> Self {
        Self { root_dir }
    }

    pub fn load_executability_restrictions(&self) -> ExecutabilityRestrictions {
        match self.root_dir.has_file(DISABLE_RESTRICTIONS_FILE_PATH) {
            true => ExecutabilityRestrictions::DoNotEnforce,
            false => ExecutabilityRestrictions::Enforce,
        }
    }

    /// The hash of the `system_image` package.
    pub fn hash(&self) -> &Hash {
        self.root_dir.hash()
    }

    /// Load `data/cache_packages.json`.
    pub async fn cache_packages(&self) -> Result<CachePackages, CachePackagesInitError> {
        self.root_dir
            .read_file("data/cache_packages.json")
            .await
            .map_err(CachePackagesInitError::ReadCachePackagesJson)
            .and_then(|content| CachePackages::from_json(content.as_slice()))
    }

    /// Load `data/static_packages`.
    pub async fn static_packages(&self) -> Result<StaticPackages, StaticPackagesInitError> {
        StaticPackages::deserialize(
            self.root_dir
                .read_file("data/static_packages")
                .await
                .map_err(StaticPackagesInitError::ReadStaticPackages)?
                .as_slice(),
        )
        .map_err(StaticPackagesInitError::ProcessingStaticPackages)
    }

    /// Consume self and return the contained `package_directory::RootDir`.
    pub fn into_root_dir(self) -> Arc<RootDir<blobfs::Client>> {
        self.root_dir
    }

    /// The package path of the system image package.
    pub fn package_path() -> fuchsia_pkg::PackagePath {
        fuchsia_pkg::PackagePath::from_name_and_variant(
            "system_image".parse().expect("valid package name"),
            fuchsia_pkg::PackageVariant::zero(),
        )
    }
}

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

    struct TestEnv {
        _blobfs: blobfs_ramdisk::BlobfsRamdisk,
    }

    impl TestEnv {
        async fn new(system_image: SystemImageBuilder) -> (Self, SystemImage) {
            let blobfs = blobfs_ramdisk::BlobfsRamdisk::start().await.unwrap();
            let system_image = system_image.build().await;
            system_image.write_to_blobfs(&blobfs).await;
            let root_dir = RootDir::new(blobfs.client(), *system_image.hash()).await.unwrap();
            (Self { _blobfs: blobfs }, SystemImage { root_dir })
        }
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn cache_packages_fails_without_config_files() {
        let (_env, system_image) = TestEnv::new(SystemImageBuilder::new()).await;
        assert_matches!(
            system_image.cache_packages().await,
            Err(CachePackagesInitError::ReadCachePackagesJson(
                package_directory::ReadFileError::NoFileAtPath { .. }
            ))
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn cache_packages_deserialize_valid_line_oriented() {
        let (_env, system_image) = TestEnv::new(
            SystemImageBuilder::new()
                .cache_package("name/variant".parse().unwrap(), [0; 32].into()),
        )
        .await;

        assert_eq!(
            system_image.cache_packages().await.unwrap(),
            CachePackages::from_entries(
                vec!["fuchsia-pkg://fuchsia.com/name/variant?hash=0000000000000000000000000000000000000000000000000000000000000000"
                    .parse()
                    .unwrap()
                ]
            )
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn static_packages_deserialize_valid_line_oriented() {
        let (_env, system_image) = TestEnv::new(
            SystemImageBuilder::new()
                .static_package("name/variant".parse().unwrap(), [0; 32].into()),
        )
        .await;

        assert_eq!(
            system_image.static_packages().await.unwrap(),
            StaticPackages::from_entries(vec![("name/variant".parse().unwrap(), [0; 32].into())])
        );
    }
}