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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::storage::{DiskSpace, Drive, DriveFactory, DriveFiles, FileAcls, Metadata};
use async_trait::async_trait;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::io;
use std::str;
#[derive(Default)]
pub struct InMemoryDrive {
programs: HashMap<String, (String, HashSet<String>)>,
pub(crate) fake_disk_quota: Option<DiskSpace>,
pub(crate) fake_disk_free: Option<DiskSpace>,
}
#[async_trait(?Send)]
impl Drive for InMemoryDrive {
async fn delete(&mut self, name: &str) -> io::Result<()> {
match self.programs.remove(name) {
Some(_) => Ok(()),
None => Err(io::Error::new(io::ErrorKind::NotFound, "Entry not found")),
}
}
async fn enumerate(&self) -> io::Result<DriveFiles> {
let date = time::OffsetDateTime::from_unix_timestamp(1_588_757_875).unwrap();
let mut entries = BTreeMap::new();
for (name, (contents, _readers)) in &self.programs {
entries.insert(name.clone(), Metadata { date, length: contents.len() as u64 });
}
Ok(DriveFiles::new(entries, self.fake_disk_quota, self.fake_disk_free))
}
async fn get(&self, name: &str) -> io::Result<String> {
match self.programs.get(name) {
Some((content, _readers)) => Ok(content.to_owned()),
None => Err(io::Error::new(io::ErrorKind::NotFound, "Entry not found")),
}
}
async fn get_acls(&self, name: &str) -> io::Result<FileAcls> {
match self.programs.get(name) {
Some((_content, readers)) => {
let mut readers = readers.iter().map(String::to_owned).collect::<Vec<String>>();
readers.sort();
Ok(FileAcls::default().with_readers(readers))
}
None => Err(io::Error::new(io::ErrorKind::NotFound, "Entry not found")),
}
}
async fn put(&mut self, name: &str, content: &str) -> io::Result<()> {
if let Some((prev_content, _readers)) = self.programs.get_mut(name) {
*prev_content = content.to_owned();
return Ok(());
};
self.programs.insert(name.to_owned(), (content.to_owned(), HashSet::new()));
Ok(())
}
async fn update_acls(
&mut self,
name: &str,
add: &FileAcls,
remove: &FileAcls,
) -> io::Result<()> {
let readers = match self.programs.get_mut(name) {
Some((_content, readers)) => readers,
None => return Err(io::Error::new(io::ErrorKind::NotFound, "Entry not found")),
};
for reader in remove.readers() {
readers.remove(reader);
}
for reader in add.readers() {
readers.insert(reader.to_owned());
}
Ok(())
}
}
#[derive(Default)]
pub struct InMemoryDriveFactory {}
impl DriveFactory for InMemoryDriveFactory {
fn create(&self, target: &str) -> io::Result<Box<dyn Drive>> {
if target.is_empty() {
Ok(Box::from(InMemoryDrive::default()))
} else {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Cannot specify a path to mount an in-memory drive",
))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn readers(r: &[&str]) -> FileAcls {
FileAcls::default().with_readers(r.iter().map(|x| x.to_string()).collect::<Vec<String>>())
}
#[tokio::test]
async fn test_inmemorydrive_put_respects_acls() {
let mut drive = InMemoryDrive::default();
drive.put("untouched", "some content").await.unwrap();
drive.put("file", "before").await.unwrap();
drive.update_acls("file", &readers(&["r1"]), &FileAcls::default()).await.unwrap();
assert_eq!("before", drive.get("file").await.unwrap());
assert_eq!(readers(&["r1"]), drive.get_acls("file").await.unwrap());
drive.put("file", "after").await.unwrap();
assert_eq!("after", drive.get("file").await.unwrap());
assert_eq!(readers(&["r1"]), drive.get_acls("file").await.unwrap());
}
#[tokio::test]
async fn test_inmemorydrive_get_update_acls() {
let mut drive = InMemoryDrive::default();
drive.put("untouched", "some content").await.unwrap();
let err =
drive.update_acls("file", &readers(&["r0"]), &FileAcls::default()).await.unwrap_err();
assert_eq!(io::ErrorKind::NotFound, err.kind());
drive.put("file", "some content").await.unwrap();
assert_eq!(FileAcls::default(), drive.get_acls("file").await.unwrap());
drive.update_acls("file", &readers(&["r1", "r2"]), &readers(&["r3"])).await.unwrap();
assert_eq!(readers(&["r1", "r2"]), drive.get_acls("file").await.unwrap());
drive.update_acls("file", &readers(&["r2", "r2", "r3"]), &readers(&["r1"])).await.unwrap();
assert_eq!(readers(&["r2", "r3"]), drive.get_acls("file").await.unwrap());
assert_eq!(FileAcls::default(), drive.get_acls("untouched").await.unwrap());
}
#[test]
fn test_inmemorydrive_system_path() {
let drive = InMemoryDrive::default();
assert!(drive.system_path("foo").is_none());
}
}