virtual_fs/
tmp_fs.rs

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