vmbfs/
backing.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Defines the trait for the backing store of the vmbus file system.
5
6use crate::protocol;
7use inspect::InspectMut;
8
9/// The backing store for the vmbus file system.
10pub trait VmbfsIo: Send + InspectMut {
11    /// Returns information about a file or directory.
12    fn file_info(&mut self, path: &str) -> Result<FileInfo, FileError>;
13    /// Reads the contents of a file.
14    fn read_file(&mut self, path: &str, offset: u64, buf: &mut [u8]) -> Result<(), FileError>;
15}
16
17/// Information about a file or directory.
18pub struct FileInfo {
19    /// Whether the path is a directory.
20    pub directory: bool,
21    /// The size of the file in bytes.
22    pub file_size: u64,
23}
24
25/// An error that can occur when interacting with the file system.
26pub enum FileError {
27    /// The file was not found.
28    NotFound,
29    /// The read operation reached the end of the file.
30    EndOfFile,
31    /// An I/O error occurred.
32    Error(std::io::Error),
33}
34
35impl FileError {
36    pub(crate) fn to_protocol(&self) -> protocol::Status {
37        match self {
38            FileError::NotFound => protocol::Status::NOT_FOUND,
39            FileError::EndOfFile => protocol::Status::END_OF_FILE,
40            FileError::Error(_) => protocol::Status::ERROR,
41        }
42    }
43}
44
45impl From<std::io::Error> for FileError {
46    fn from(err: std::io::Error) -> Self {
47        match err.kind() {
48            std::io::ErrorKind::NotFound => FileError::NotFound,
49            std::io::ErrorKind::UnexpectedEof => FileError::EndOfFile,
50            _ => FileError::Error(err),
51        }
52    }
53}