Skip to content
Snippets Groups Projects
Commit d6fd7592 authored by Leo Lung's avatar Leo Lung Committed by CQ bot account: commit-bot@chromium.org
Browse files

[component_manager] Implement fuchsia-boot

TEST=fx run-test-component component_manager_tests

CF-536 #comment

"Despite its stocky shape and short legs, it can easily outrun most
humans. Hippos have been clocked at 30 km/h over short distances"

Change-Id: I157e83298f2437982068c272c08527bb6ab7c570
parent f1a828e0
No related branches found
No related tags found
No related merge requests found
......@@ -72,6 +72,13 @@ test_package("component_manager_tests") {
},
]
meta = [
{
path = rebase_path("meta/component_manager_tests_hello_world.cml")
dest = "component_manager_tests_hello_world.cm"
},
]
binaries = [
{
name = "hello_world"
......
{
"program": {
"binary": "bin/hello_world"
}
}
......@@ -3,10 +3,14 @@
// found in the LICENSE file.
use {
crate::io_util,
crate::model::{Resolver, ResolverError},
failure::format_err,
fidl_fuchsia_data as fdata, fidl_fuchsia_sys2 as fsys,
cm_fidl_translator::translate,
fidl::endpoints::ClientEnd,
fidl_fuchsia_sys2 as fsys,
fuchsia_uri::boot_uri::FuchsiaBootUri,
futures::future::FutureObj,
std::path::PathBuf,
};
pub static SCHEME: &str = "fuchsia-boot";
......@@ -14,7 +18,7 @@ pub static SCHEME: &str = "fuchsia-boot";
/// Resolves component URIs with the "fuchsia-boot" scheme.
///
/// URI syntax:
/// - fuchsia-boot:///path/to/directory#meta/component.cm
/// - fuchsia-boot:///directory#meta/component.cm
pub struct FuchsiaBootResolver {}
impl FuchsiaBootResolver {
......@@ -26,32 +30,36 @@ impl FuchsiaBootResolver {
&'a self,
component_uri: &'a str,
) -> Result<fsys::Component, ResolverError> {
// TODO: Actually resolve and parse CM files from the boot filesystem.
println!("FuchsiaBootResolver: pretending to resolve '{}'", component_uri);
if component_uri == "fuchsia-boot:///boot#meta/scaffold.cm" {
Ok(fsys::Component {
resolved_uri: Some(component_uri.to_string()),
decl: Some(fsys::ComponentDecl {
program: Some(fdata::Dictionary {
entries: vec![fdata::Entry {
key: "binary".to_string(),
value: Some(Box::new(fdata::Value::Str("bin/scaffold".to_string()))),
}],
}),
uses: None,
exposes: None,
offers: None,
facets: None,
children: None,
}),
package: None,
})
} else {
Err(ResolverError::component_not_available(
component_uri,
format_err!("unknown component"),
))
}
// Parse URI.
let uri = FuchsiaBootUri::parse(component_uri)
.map_err(|e| ResolverError::component_not_available(component_uri, e))?;
let res = uri.resource().ok_or(ResolverError::uri_missing_resource_error(component_uri))?;
let res_path = PathBuf::from(uri.path()).join(PathBuf::from(res));
let res_path_str =
res_path.to_str().ok_or(ResolverError::uri_missing_resource_error(component_uri))?;
// Read component manifest from resource into a component decl.
let cm_file = await!(io_util::open_file_in_namespace(&res_path_str))
.map_err(|e| ResolverError::component_not_available(component_uri, e))?;
let cm_str = await!(io_util::read_file(&cm_file))
.map_err(|e| ResolverError::component_not_available(component_uri, e))?;
let component_decl = translate(&cm_str)
.map_err(|e| ResolverError::component_not_available(component_uri, e))?;
// Set up the fuchsia-boot path as the component's "package" namespace.
let package_path = uri.path();
let path_proxy = await!(io_util::open_directory_in_namespace(&package_path))
.map_err(|e| ResolverError::component_not_available(component_uri, e))?;
let package = fsys::Package {
package_uri: Some(uri.root_uri().to_string()),
package_dir: Some(ClientEnd::new(path_proxy.into_channel().unwrap().into_zx_channel())),
};
Ok(fsys::Component {
resolved_uri: Some(component_uri.to_string()),
decl: Some(component_decl),
package: Some(package),
})
}
}
......@@ -63,3 +71,23 @@ impl Resolver for FuchsiaBootResolver {
FutureObj::new(Box::new(self.resolve_async(component_uri)))
}
}
#[cfg(test)]
mod tests {
use {super::*, fuchsia_async as fasync};
#[test]
fn hello_world_test() {
let mut executor = fasync::Executor::new().unwrap();
executor.run_singlethreaded(
async {
let resolver = FuchsiaBootResolver::new();
let component = await!(resolver.resolve_async(
"fuchsia-boot:///pkg#meta/component_manager_tests_hello_world.cm"
))
.unwrap();
assert_eq!("fuchsia-boot:///pkg", component.package.unwrap().package_uri.unwrap());
},
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment