Newer
Older
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use resource::FileResource;
use redoxfs::{FileSystem, Node};
use std::collections::BTreeMap;
use system::error::{Error, Result, EEXIST, EISDIR, ENOTDIR, EPERM, ENOENT, EBADF};
use system::scheme::Scheme;
use system::syscall::{Stat, O_CREAT};
pub struct FileScheme {
fs: FileSystem,
next_id: isize,
files: BTreeMap<usize, FileResource>
}
impl FileScheme {
pub fn new(fs: FileSystem) -> FileScheme {
FileScheme {
fs: fs,
next_id: 1,
files: BTreeMap::new()
}
}
fn path_nodes(&mut self, path: &str, nodes: &mut Vec<(u64, Node)>) -> Result<(u64, Node)> {
let mut block = self.fs.header.1.root;
nodes.push(try!(self.fs.node(block)));
for part in path.split('/') {
if ! part.is_empty() {
let node = try!(self.fs.find_node(part, block));
block = node.0;
nodes.push(node);
}
}
Ok(nodes.pop().unwrap())
}
}
impl Scheme for FileScheme {
fn open(&mut self, url: &str, flags: usize, _mode: usize) -> Result<usize> {
let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');
let mut nodes = Vec::new();
let node_result = self.path_nodes(path, &mut nodes);
let mut data = Vec::new();
match node_result {
Ok(node) => if node.1.is_dir() {
let mut children = Vec::new();
try!(self.fs.child_nodes(&mut children, node.0));
for child in children.iter() {
if let Ok(name) = child.1.name() {
if ! data.is_empty() {
data.push('\n' as u8);
}
data.extend_from_slice(&name.as_bytes());
if child.1.is_dir() {
data.push(b'/');
}
}
}
} else {
for i in 0..try!(self.fs.node_len(node.0)) {
let mut sector = [0; 512];
try!(self.fs.read_node(node.0, i as usize * 512, &mut sector));
data.extend_from_slice(§or);
}
},
Err(err) => if err.errno == ENOENT && flags & O_CREAT == O_CREAT {
let mut last_part = String::new();
for part in path.split('/') {
if ! part.is_empty() {
last_part = part.to_string();
}
}
if ! last_part.is_empty() {
if let Some(parent) = nodes.last() {
try!(self.fs.create_node(Node::MODE_FILE, &last_part, parent.0));
} else {
return Err(Error::new(EPERM));
}
} else {
return Err(Error::new(EPERM));
}
} else {
return Err(err);
}
}
/*
if let Some(arg) = args.next() {
match {
Ok(node) => println!("{}: {:#?}", node.0, node.1),
Err(err) => println!("mk: failed to create {}: {}", arg, err)
}
} else {
println!("mk <file>");
}
*/
let id = self.next_id as usize;
self.next_id += 1;
if self.next_id < 0 {
self.next_id = 1;
}
self.files.insert(id, FileResource::new(url, data));
Ok(id)
}
fn mkdir(&mut self, url: &str, _mode: usize) -> Result<usize> {
let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');
let mut nodes = Vec::new();
match self.path_nodes(path, &mut nodes) {
Ok(_node) => Err(Error::new(EEXIST)),
Err(err) => if err.errno == ENOENT {
let mut last_part = String::new();
for part in path.split('/') {
if ! part.is_empty() {
last_part = part.to_string();
}
}
if ! last_part.is_empty() {
if let Some(parent) = nodes.last() {
self.fs.create_node(Node::MODE_DIR, &last_part, parent.0).and(Ok(0))
} else {
Err(Error::new(EPERM))
}
} else {
Err(Error::new(EPERM))
}
} else {
Err(err)
}
}
}
fn rmdir(&mut self, url: &str) -> Result<usize> {
let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');
let mut nodes = Vec::new();
let child = try!(self.path_nodes(path, &mut nodes));
if let Some(parent) = nodes.last() {
if child.1.is_dir() {
if let Ok(child_name) = child.1.name() {
self.fs.remove_node(Node::MODE_DIR, child_name, parent.0).and(Ok(0))
} else {
Err(Error::new(ENOENT))
}
} else {
Err(Error::new(ENOTDIR))
}
} else {
Err(Error::new(EPERM))
}
}
fn unlink(&mut self, url: &str) -> Result<usize> {
let path = url.split(':').nth(1).unwrap_or("").trim_matches('/');
let mut nodes = Vec::new();
let child = try!(self.path_nodes(path, &mut nodes));
if let Some(parent) = nodes.last() {
if ! child.1.is_dir() {
if let Ok(child_name) = child.1.name() {
self.fs.remove_node(Node::MODE_FILE, child_name, parent.0).and(Ok(0))
} else {
Err(Error::new(ENOENT))
}
} else {
Err(Error::new(EISDIR))
}
} else {
Err(Error::new(EPERM))
}
}
/* Resource operations */
#[allow(unused_variables)]
fn read(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
if let Some(mut file) = self.files.get_mut(&id) {
file.read(buf)
} else {
Err(Error::new(EBADF))
}
}
fn write(&mut self, id: usize, buf: &[u8]) -> Result<usize> {
if let Some(mut file) = self.files.get_mut(&id) {
file.write(buf)
} else {
Err(Error::new(EBADF))
}
}
fn seek(&mut self, id: usize, pos: usize, whence: usize) -> Result<usize> {
if let Some(mut file) = self.files.get_mut(&id) {
file.seek(pos, whence)
} else {
Err(Error::new(EBADF))
}
}
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
if let Some(file) = self.files.get(&id) {
file.path(buf)
} else {
Err(Error::new(EBADF))
}
}
fn fstat(&self, id: usize, stat: &mut Stat) -> Result<usize> {
println!("fstat {}, {:X}", id, stat as *mut Stat as usize);
if let Some(file) = self.files.get(&id) {
file.stat(stat)
} else {
Err(Error::new(EBADF))
}
}
fn fsync(&mut self, id: usize) -> Result<usize> {
if let Some(mut file) = self.files.get_mut(&id) {
file.sync()
} else {
Err(Error::new(EBADF))
}
}
fn ftruncate(&mut self, id: usize, len: usize) -> Result<usize> {
if let Some(mut file) = self.files.get_mut(&id) {
file.truncate(len)
} else {
Err(Error::new(EBADF))
}
}
fn close(&mut self, id: usize) -> Result<usize> {
if self.files.remove(&id).is_some() {
Ok(0)
} else {
Err(Error::new(EBADF))
}
}
}