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
use crate::alloc_prelude::*;
use crate::{FsDirectory, FsError, FsFile, FsFileHandle, FsNode, FsReadDir, FsRoot, FsWriteDir};
#[derive(Debug, PartialEq)]
pub struct SimpleMemoryFilesystem {
pub files: Vec<SimpleMemoryFilesystemFile>,
pub current_inode: usize,
}
impl SimpleMemoryFilesystem {
pub fn new() -> Self {
Self {
files: Vec::new(),
current_inode: 0,
}
}
fn has_filename(&self, name: &str) -> bool {
for f in self.files.iter() {
if &f.name == name {
return true;
}
}
false
}
}
impl FsReadDir for SimpleMemoryFilesystem {
fn readdir(&mut self) -> Result<Vec<&mut dyn FsNode>, FsError> {
let mut res = Vec::new();
for file in self.files.iter_mut() {
res.push(file as &mut dyn FsNode);
}
Ok(res)
}
}
impl FsWriteDir for SimpleMemoryFilesystem {
fn touch(&mut self, name: &str) -> Result<&mut dyn FsNode, FsError> {
let name = String::from(name);
if self.has_filename(&name) {
return Err(FsError::FileExists);
}
self.current_inode += 1;
let f = SimpleMemoryFilesystemFile {
parent_index: None,
inode: self.current_inode,
name: name,
content: Vec::new(),
};
self.files.push(f);
Ok(self.files.last_mut().unwrap() as &mut dyn FsNode)
}
}
impl FsNode for SimpleMemoryFilesystem {
fn inode(&self) -> usize {
0
}
fn name(&self) -> &str {
""
}
fn permissions(&self) -> u16 {
0o777
}
fn try_root(&mut self) -> Option<&mut dyn FsRoot> {
Some(self as &mut dyn FsRoot)
}
fn try_directory(&mut self) -> Option<&mut dyn FsDirectory> {
Some(self as &mut dyn FsDirectory)
}
fn try_file(&mut self) -> Option<&mut dyn FsFile> {
None
}
}
impl FsDirectory for SimpleMemoryFilesystem {}
impl FsRoot for SimpleMemoryFilesystem {}
#[derive(Debug, Default, PartialEq)]
pub struct SimpleMemoryFilesystemFile {
pub parent_index: Option<usize>,
pub inode: usize,
pub name: String,
pub content: Vec<u8>,
}
impl FsNode for SimpleMemoryFilesystemFile {
fn inode(&self) -> usize {
self.inode
}
fn name(&self) -> &str {
&self.name
}
fn permissions(&self) -> u16 {
0o777
}
fn try_root(&mut self) -> Option<&mut dyn FsRoot> {
None
}
fn try_directory(&mut self) -> Option<&mut dyn FsDirectory> {
None
}
fn try_file(&mut self) -> Option<&mut dyn FsFile> {
Some(self as &mut dyn FsFile)
}
}
impl FsFile for SimpleMemoryFilesystemFile {
fn open(&mut self) -> Result<&mut dyn FsFileHandle, FsError> {
Ok(self as &mut dyn FsFileHandle)
}
}
impl FsFileHandle for SimpleMemoryFilesystemFile {
fn raw_read(&mut self, offset: usize, length: Option<usize>) -> Result<Vec<u8>, FsError> {
if offset > self.content.len() {
return Err(FsError::EndOfFile);
}
let final_length = if length.is_none() {
self.content.len() - offset
} else {
let length = length.unwrap();
let mut final_length = self.content.len() - offset;
if length < final_length {
final_length = length;
}
final_length
};
return Ok(Vec::from(&self.content[offset..(offset + final_length)]));
}
fn raw_write(&mut self, offset: usize, data: &[u8]) -> Result<(), FsError> {
if (offset + data.len()) > self.content.len() {
for _ in self.content.len()..(offset + data.len()) {
self.content.push(0);
}
}
for (byte, idx) in data.iter().zip(offset..) {
self.content[idx] = *byte;
}
Ok(())
}
}