Skip to main content

update_package/target/
image.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use thiserror::Error;
6use zx_status::Status;
7
8use fidl_fuchsia_io as fio;
9use fidl_fuchsia_mem as fmem;
10
11/// An error encountered while opening an image.
12#[derive(Debug, Error)]
13#[allow(missing_docs)]
14pub enum OpenImageError {
15    #[error("while opening the file path {path:?}")]
16    OpenPath {
17        path: String,
18        #[source]
19        err: fuchsia_fs::node::OpenError,
20    },
21
22    #[error("while calling get_backing_memory for {path:?}")]
23    FidlGetBackingMemory {
24        path: String,
25        #[source]
26        err: fidl::Error,
27    },
28
29    #[error("while obtaining vmo of file for {path:?}: {status}")]
30    GetBackingMemory { path: String, status: Status },
31}
32
33/// Opens the given `path` as a VMO buffer and returns the buffer on success.
34pub(crate) async fn open_from_path(
35    proxy: &fio::DirectoryProxy,
36    path: &str,
37) -> Result<fmem::Buffer, OpenImageError> {
38    let file = fuchsia_fs::directory::open_file(proxy, path, fio::PERM_READABLE)
39        .await
40        .map_err(|err| OpenImageError::OpenPath { path: path.to_string(), err })?;
41
42    let vmo = file
43        .get_backing_memory(fio::VmoFlags::READ)
44        .await
45        .map_err(|err| OpenImageError::FidlGetBackingMemory { path: path.to_string(), err })?
46        .map_err(Status::err_from_raw)
47        .map_err(|status| OpenImageError::GetBackingMemory { path: path.to_string(), status })?;
48
49    let size = vmo
50        .get_content_size()
51        .map_err(|status| OpenImageError::GetBackingMemory { path: path.to_string(), status })?;
52
53    Ok(fmem::Buffer { vmo, size })
54}