vmgs_broker/
client.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! The Vmgs worker will send messages to the Vmgs dispatch, allowing
5//! tasks to queue for the dispatcher to handle synchronously
6
7use crate::broker::VmgsBrokerRpc;
8use inspect::Inspect;
9use mesh_channel::rpc::RpcError;
10use mesh_channel::rpc::RpcSend;
11use thiserror::Error;
12use tracing::instrument;
13use vmgs::VmgsFileInfo;
14use vmgs_format::FileId;
15
16/// VMGS broker errors.
17#[derive(Debug, Error)]
18#[non_exhaustive]
19pub enum VmgsClientError {
20    /// VMGS broker is offline
21    #[error("broker is offline")]
22    BrokerOffline(#[from] RpcError),
23    /// VMGS error
24    #[error("vmgs error")]
25    Vmgs(#[from] vmgs::Error),
26}
27
28impl From<RpcError<vmgs::Error>> for VmgsClientError {
29    fn from(value: RpcError<vmgs::Error>) -> Self {
30        match value {
31            RpcError::Call(e) => VmgsClientError::Vmgs(e),
32            RpcError::Channel(e) => VmgsClientError::BrokerOffline(RpcError::Channel(e)),
33        }
34    }
35}
36
37/// Client to interact with a backend-agnostic VMGS instance.
38#[derive(Clone, Inspect)]
39pub struct VmgsClient {
40    #[inspect(flatten, send = "VmgsBrokerRpc::Inspect")]
41    pub(crate) control: mesh_channel::Sender<VmgsBrokerRpc>,
42}
43
44impl VmgsClient {
45    /// Get allocated and valid bytes from File Control Block for file_id.
46    #[instrument(skip_all, fields(file_id))]
47    pub async fn get_file_info(&self, file_id: FileId) -> Result<VmgsFileInfo, VmgsClientError> {
48        let res = self
49            .control
50            .call_failable(VmgsBrokerRpc::GetFileInfo, file_id)
51            .await?;
52
53        Ok(res)
54    }
55
56    /// Reads the specified `file_id`.
57    #[instrument(skip_all, fields(file_id))]
58    pub async fn read_file(&self, file_id: FileId) -> Result<Vec<u8>, VmgsClientError> {
59        let res = self
60            .control
61            .call_failable(VmgsBrokerRpc::ReadFile, file_id)
62            .await?;
63
64        Ok(res)
65    }
66
67    /// Writes `buf` to a file_id.
68    ///
69    /// NOTE: It is an error to overwrite a previously encrypted FileId with
70    /// plaintext data.
71    #[instrument(skip_all, fields(file_id))]
72    pub async fn write_file(&self, file_id: FileId, buf: Vec<u8>) -> Result<(), VmgsClientError> {
73        self.control
74            .call_failable(VmgsBrokerRpc::WriteFile, (file_id, buf))
75            .await?;
76
77        Ok(())
78    }
79
80    /// If VMGS has been configured with encryption, encrypt + write `bug` to
81    /// the specified `file_id`. Otherwise, perform a regular plaintext write
82    /// instead.
83    #[cfg(with_encryption)]
84    #[instrument(skip_all, fields(file_id))]
85    pub async fn write_file_encrypted(
86        &self,
87        file_id: FileId,
88        buf: Vec<u8>,
89    ) -> Result<(), VmgsClientError> {
90        self.control
91            .call_failable(VmgsBrokerRpc::WriteFileEncrypted, (file_id, buf))
92            .await?;
93
94        Ok(())
95    }
96
97    /// Save the in-memory VMGS file metadata.
98    ///
99    /// This saved state can be used alongside `open_from_saved` to obtain a
100    /// new `Vmgs` instance _without_ needing to invoke any IOs on the
101    /// underlying storage.
102    pub async fn save(&self) -> Result<vmgs::save_restore::state::SavedVmgsState, VmgsClientError> {
103        let res = self.control.call(VmgsBrokerRpc::Save, ()).await?;
104        Ok(res)
105    }
106}