1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::env;
use std::io::Result;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};
use FileSystem;
#[cfg(unix)]
use UnixFileSystem;
#[cfg(feature = "temp")]
use {TempDir, TempFileSystem};
#[cfg(feature = "temp")]
pub use self::tempdir::FakeTempDir;
use self::file::{Dir, FakeFile, File};
use self::registry::Registry;
mod file;
mod registry;
#[cfg(feature = "temp")]
mod tempdir;
#[derive(Clone, Debug, Default)]
pub struct FakeFileSystem {
registry: Arc<Mutex<Registry>>,
}
impl FakeFileSystem {
pub fn new() -> Self {
let registry = Registry::new();
FakeFileSystem { registry: Arc::new(Mutex::new(registry)) }
}
fn apply<F, T>(&self, path: &Path, f: F) -> T
where F: FnOnce(&MutexGuard<Registry>, &Path) -> T
{
let registry = self.registry.lock().unwrap();
let storage;
let path = if path.is_relative() {
storage = registry.current_dir()
.unwrap_or_else(|_| PathBuf::from("/"))
.join(path);
&storage
} else {
path
};
f(®istry, path)
}
fn apply_mut<F, T>(&self, path: &Path, mut f: F) -> T
where F: FnMut(&mut MutexGuard<Registry>, &Path) -> T
{
let mut registry = self.registry.lock().unwrap();
let storage;
let path = if path.is_relative() {
storage = registry.current_dir()
.unwrap_or_else(|_| PathBuf::from("/"))
.join(path);
&storage
} else {
path
};
f(&mut registry, path)
}
}
impl FileSystem for FakeFileSystem {
fn current_dir(&self) -> Result<PathBuf> {
let registry = self.registry.lock().unwrap();
registry.current_dir()
}
fn set_current_dir<P: AsRef<Path>>(&self, path: P) -> Result<()> {
self.apply_mut(path.as_ref(), |mut r, p| r.set_current_dir(p.to_path_buf()))
}
fn is_dir<P: AsRef<Path>>(&self, path: P) -> bool {
self.apply(path.as_ref(), |r, p| r.is_dir(p))
}
fn is_file<P: AsRef<Path>>(&self, path: P) -> bool {
self.apply(path.as_ref(), |r, p| r.is_file(p))
}
fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<()> {
self.apply_mut(path.as_ref(), |r, p| r.create_dir(p))
}
fn create_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()> {
self.apply_mut(path.as_ref(), |r, p| r.create_dir_all(p))
}
fn remove_dir<P: AsRef<Path>>(&self, path: P) -> Result<()> {
self.apply_mut(path.as_ref(), |r, p| r.remove_dir(p))
}
fn remove_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()> {
self.apply_mut(path.as_ref(), |r, p| r.remove_dir_all(p))
}
fn create_file<P, B>(&self, path: P, buf: B) -> Result<()>
where P: AsRef<Path>,
B: AsRef<[u8]>
{
self.apply_mut(path.as_ref(), |r, p| r.create_file(p, buf.as_ref()))
}
fn write_file<P, B>(&self, path: P, buf: B) -> Result<()>
where P: AsRef<Path>,
B: AsRef<[u8]>
{
self.apply_mut(path.as_ref(), |r, p| r.write_file(p, buf.as_ref()))
}
fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>> {
self.apply(path.as_ref(), |r, p| r.read_file(p))
}
fn readonly<P: AsRef<Path>>(&self, path: P) -> Result<bool> {
self.apply(path.as_ref(), |r, p| r.readonly(p))
}
fn set_readonly<P: AsRef<Path>>(&self, path: P, readonly: bool) -> Result<()> {
self.apply_mut(path.as_ref(), |r, p| r.set_readonly(p, readonly))
}
}
#[cfg(unix)]
impl UnixFileSystem for FakeFileSystem {
fn mode<P: AsRef<Path>>(&self, path: P) -> Result<u32> {
self.apply(path.as_ref(), |r, p| r.mode(p))
}
fn set_mode<P: AsRef<Path>>(&self, path: P, mode: u32) -> Result<()> {
self.apply_mut(path.as_ref(), |r, p| r.set_mode(p, mode))
}
}
#[cfg(feature = "temp")]
impl TempFileSystem for FakeFileSystem {
type TempDir = FakeTempDir;
fn temp_dir<S: AsRef<str>>(&self, prefix: S) -> Result<Self::TempDir> {
let base = env::temp_dir();
let dir = FakeTempDir::new(Arc::downgrade(&self.registry), &base, prefix.as_ref());
self.create_dir_all(&dir.path())
.and(Ok(dir))
}
}