Skip to main content

secure_exec_vfs_core/engine/
vfs.rs

1use crate::engine::error::{VfsError, VfsResult};
2use crate::engine::types::{Dentry, SnapshotId, VirtualStat};
3use async_trait::async_trait;
4
5#[async_trait]
6pub trait VirtualFileSystem: Send + Sync {
7    async fn read_file(&self, path: &str) -> VfsResult<Vec<u8>>;
8
9    async fn read_text(&self, path: &str) -> VfsResult<String> {
10        String::from_utf8(self.read_file(path).await?)
11            .map_err(|_| VfsError::einval(format!("file is not valid UTF-8: {path}")))
12    }
13
14    async fn read_dir(&self, path: &str) -> VfsResult<Vec<String>>;
15    async fn read_dir_with_types(&self, path: &str) -> VfsResult<Vec<Dentry>>;
16    async fn write_file(&self, path: &str, content: &[u8]) -> VfsResult<()>;
17    async fn create_dir(&self, path: &str) -> VfsResult<()>;
18    async fn mkdir(&self, path: &str, recursive: bool) -> VfsResult<()>;
19    async fn exists(&self, path: &str) -> bool;
20    async fn stat(&self, path: &str) -> VfsResult<VirtualStat>;
21    async fn lstat(&self, path: &str) -> VfsResult<VirtualStat>;
22    async fn remove_file(&self, path: &str) -> VfsResult<()>;
23    async fn remove_dir(&self, path: &str) -> VfsResult<()>;
24    async fn rename(&self, old_path: &str, new_path: &str) -> VfsResult<()>;
25    async fn realpath(&self, path: &str) -> VfsResult<String>;
26    async fn symlink(&self, target: &str, link_path: &str) -> VfsResult<()>;
27    async fn readlink(&self, path: &str) -> VfsResult<String>;
28    async fn link(&self, old_path: &str, new_path: &str) -> VfsResult<()>;
29    async fn chmod(&self, path: &str, mode: u32) -> VfsResult<()>;
30    async fn chown(&self, path: &str, uid: u32, gid: u32) -> VfsResult<()>;
31    async fn utimes(&self, path: &str, atime_ms: u64, mtime_ms: u64) -> VfsResult<()>;
32    async fn truncate(&self, path: &str, length: u64) -> VfsResult<()>;
33    async fn pread(&self, path: &str, offset: u64, length: usize) -> VfsResult<Vec<u8>>;
34    async fn pwrite(&self, path: &str, content: &[u8], offset: u64) -> VfsResult<()>;
35    async fn append(&self, path: &str, content: &[u8]) -> VfsResult<u64>;
36}
37
38#[async_trait]
39pub trait Snapshottable: Send + Sync {
40    async fn snapshot(&self, root: u64) -> VfsResult<SnapshotId>;
41    async fn fork(&self, snap: SnapshotId) -> VfsResult<u64>;
42}