virtual_fs/
tmp_fs.rs

1//! Wraps the memory file system implementation - this has been
2//! enhanced to support mounting file systems, shared static files,
3//! readonly files, etc...
4
5use std::{
6    path::{Path, PathBuf},
7    sync::Arc,
8};
9
10use crate::{
11    BoxFuture, FileSystem, Metadata, OpenOptions, ReadDir, Result, limiter::DynFsMemoryLimiter,
12    mem_fs,
13};
14
15#[derive(Debug, Default, Clone)]
16pub struct TmpFileSystem {
17    fs: mem_fs::FileSystem,
18}
19
20impl TmpFileSystem {
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    pub fn set_memory_limiter(&self, limiter: DynFsMemoryLimiter) {
26        self.fs.set_memory_limiter(limiter);
27    }
28
29    pub fn new_open_options_ext(&self) -> &mem_fs::FileSystem {
30        self.fs.new_open_options_ext()
31    }
32
33    pub fn union(&self, other: &Arc<dyn FileSystem + Send + Sync>) {
34        self.fs.union(other)
35    }
36
37    /// See [`mem_fs::FileSystem::mount_directory_entries`].
38    pub fn mount_directory_entries(
39        &self,
40        target_path: &Path,
41        other: &Arc<dyn crate::FileSystem + Send + Sync>,
42        source_path: &Path,
43    ) -> Result<()> {
44        self.fs
45            .mount_directory_entries(target_path, other, source_path)
46    }
47
48    pub fn mount(
49        &self,
50        src_path: PathBuf,
51        other: &Arc<dyn FileSystem + Send + Sync>,
52        dst_path: PathBuf,
53    ) -> Result<()> {
54        self.fs.mount(src_path, other, dst_path)
55    }
56
57    /// Canonicalize a path without validating that it actually exists.
58    pub fn canonicalize_unchecked(&self, path: &Path) -> Result<PathBuf> {
59        self.fs.canonicalize_unchecked(path)
60    }
61}
62
63impl FileSystem for TmpFileSystem {
64    fn readlink(&self, path: &Path) -> Result<PathBuf> {
65        self.fs.readlink(path)
66    }
67
68    fn read_dir(&self, path: &Path) -> Result<ReadDir> {
69        self.fs.read_dir(path)
70    }
71
72    fn create_dir(&self, path: &Path) -> Result<()> {
73        self.fs.create_dir(path)
74    }
75
76    fn remove_dir(&self, path: &Path) -> Result<()> {
77        self.fs.remove_dir(path)
78    }
79
80    fn rename<'a>(&'a self, from: &'a Path, to: &'a Path) -> BoxFuture<'a, Result<()>> {
81        Box::pin(async { self.fs.rename(from, to).await })
82    }
83
84    fn metadata(&self, path: &Path) -> Result<Metadata> {
85        self.fs.metadata(path)
86    }
87
88    fn symlink_metadata(&self, path: &Path) -> Result<Metadata> {
89        self.fs.symlink_metadata(path)
90    }
91
92    fn remove_file(&self, path: &Path) -> Result<()> {
93        self.fs.remove_file(path)
94    }
95
96    fn new_open_options(&self) -> OpenOptions<'_> {
97        self.fs.new_open_options()
98    }
99
100    fn mount(
101        &self,
102        name: String,
103        path: &Path,
104        fs: Box<dyn FileSystem + Send + Sync>,
105    ) -> Result<()> {
106        FileSystem::mount(&self.fs, name, path, fs)
107    }
108}