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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
use crate::*;
pub struct FileDevice {
pub base_path: PathBuf,
pub default_path: PathBuf,
pub entry_buffer: BedrockPathBuffer,
pub action_buffer: BedrockPathBuffer,
pub path_buffer: BedrockPathBuffer,
pub entry: Option<(Entry, BedrockFilePath)>,
pub cached_dir: Option<(Entry, BedrockFilePath)>,
pub success: bool,
pub pointer_write: u32,
pub length_write: u32,
pub enable_read: bool,
pub enable_write: bool,
pub enable_create: bool,
pub enable_move: bool,
pub enable_delete: bool,
}
impl Device for FileDevice {
fn read(&mut self, port: u8) -> u8 {
match port {
0x0 => read_b!(self.entry.is_some()),
0x1 => read_b!(self.success),
0x2 => self.path_buffer.read(),
0x3 => read_b!(self.entry_type()),
0x4 => self.read_byte(),
0x5 => self.read_byte(),
0x6 => self.read_child_path(),
0x7 => read_b!(self.child_type()),
0x8 => read_hh!(self.pointer()),
0x9 => read_hl!(self.pointer()),
0xa => read_lh!(self.pointer()),
0xb => read_ll!(self.pointer()),
0xc => read_hh!(self.length()),
0xd => read_hl!(self.length()),
0xe => read_lh!(self.length()),
0xf => read_ll!(self.length()),
_ => unreachable!(),
}
}
fn write(&mut self, port: u8, value: u8) -> Option<Signal> {
match port {
0x0 => self.write_to_entry_port(value),
0x1 => self.write_to_action_port(value),
0x2 => self.path_buffer.set_pointer(value),
0x3 => self.ascend_to_parent(),
0x4 => self.write_byte(value),
0x5 => self.write_byte(value),
0x6 => self.set_child_path(value),
0x7 => self.descend_to_child(),
0x8 => write_hh!(self.pointer_write, value),
0x9 => write_hl!(self.pointer_write, value),
0xa => write_lh!(self.pointer_write, value),
0xb => {write_ll!(self.pointer_write, value); self.commit_pointer()},
0xc => write_hh!(self.length_write, value),
0xd => write_hl!(self.length_write, value),
0xe => write_lh!(self.length_write, value),
0xf => {write_ll!(self.length_write, value); self.commit_length()},
_ => unreachable!(),
};
return None;
}
fn wake(&mut self) -> bool {
false
}
}
impl FileDevice {
pub fn new() -> Self {
#[cfg(target_family = "unix")]
let default_base: PathBuf = PathBuf::from("/");
#[cfg(target_family = "windows")]
let default_base: PathBuf = PathBuf::from("");
// TODO: I'm not at all confident that the default path is correct
// when not being set as the current directory.
Self {
base_path: default_base,
default_path: match std::env::current_dir() {
Ok(dir) => PathBuf::from(dir),
Err(_) => PathBuf::from(""),
},
entry_buffer: BedrockPathBuffer::new(),
action_buffer: BedrockPathBuffer::new(),
path_buffer: BedrockPathBuffer::new(),
entry: None,
cached_dir: None,
success: false,
pointer_write: 0,
length_write: 0,
enable_read: true,
enable_write: true,
enable_create: true,
enable_move: true,
enable_delete: false,
}
}
/// Safely close the current entry, cleaning up entry variables.
pub fn close(&mut self) {
self.entry_buffer.clear();
self.action_buffer.clear();
self.path_buffer.clear();
self.flush();
if let Some((Entry::Directory(mut dir), path)) = std::mem::take(&mut self.entry) {
// Prevent the selected child from persisting when loading from cache.
dir.deselect_child();
self.cached_dir = Some((Entry::Directory(dir), path));
}
}
/// Open the entry at the given Bedrock path.
pub fn open(&mut self, path: BedrockFilePath) -> Result<(), ()> {
match path.entry_type() {
Some(EntryType::File) => {
let open_result = std::fs::OpenOptions::new()
.read(self.enable_read)
.write(self.enable_write)
.open(path.as_path());
// Keep the current entry open if we can't open the new path.
if let Ok(file) = open_result {
self.close();
self.path_buffer.populate(path.as_buffer());
self.entry = Some((Entry::File(BufferedFile::new(file)), path));
return Ok(());
};
}
Some(EntryType::Directory) => {
// Attempt to use the cached directory.
if let Some((dir, cached_path)) = std::mem::take(&mut self.cached_dir) {
if cached_path == path {
self.close();
self.path_buffer.populate(cached_path.as_buffer());
self.entry = Some((dir, cached_path));
return Ok(());
}
}
// Keep the current entry open if we can't open the new path.
if let Some(listing) = DirectoryListing::from_path(&path) {
self.close();
self.path_buffer.populate(path.as_buffer());
self.entry = Some((Entry::Directory(listing), path));
return Ok(());
};
}
// The entry either doesn't exist or is not a file or directory.
None => (),
}
return Err(());
}
/// Process a byte received from the entry port.
pub fn write_to_entry_port(&mut self, byte: u8) {
if let Some(buffer) = self.entry_buffer.write(byte) {
self.close();
match BedrockFilePath::from_buffer(buffer, &self.base_path) {
Some(path) => self.success = self.open(path).is_ok(),
None => self.success = false,
};
}
}
/// Process a byte received from the action port.
pub fn write_to_action_port(&mut self, byte: u8) {
if let Some(buffer) = self.action_buffer.write(byte) {
let destination_blank = buffer[0] == 0x00;
let destination = BedrockFilePath::from_buffer(buffer, &self.base_path);
self.success = false;
if let Some((_, source)) = &self.entry {
if destination_blank {
if self.enable_delete {
self.success = delete_entry(&source.as_path());
}
} else if let Some(dest) = destination {
if self.enable_move {
self.success = move_entry(&source.as_path(), &dest.as_path());
}
}
} else if let Some(dest) = destination {
if self.enable_create {
self.success = create_file(&dest.as_path());
}
}
self.close();
}
}
/// Attempt to open the parent directory of the current entry.
pub fn ascend_to_parent(&mut self) {
if let Some((_, path)) = &self.entry {
match path.parent() {
Some(parent) => self.success = self.open(parent).is_ok(),
None => self.success = false,
};
} else {
match BedrockFilePath::from_path(&self.default_path, &self.base_path) {
Some(default) => self.success = self.open(default).is_ok(),
None => self.success = false,
};
}
}
/// Attempt to open the selected child of the current directory.
pub fn descend_to_child(&mut self) {
if let Some((Entry::Directory(dir), _)) = &self.entry {
match dir.child_path() {
Some(child) => self.success = self.open(child).is_ok(),
None => self.success = false,
};
} else {
self.success = false;
}
}
/// Return true if the current entry is a directory.
pub fn entry_type(&self) -> bool {
match self.entry {
Some((Entry::Directory(_), _)) => true,
_ => false,
}
}
/// Read a byte from the path buffer of the selected child.
pub fn read_child_path(&mut self) -> u8 {
match &mut self.entry {
Some((Entry::Directory(dir), _)) => dir.child_path_buffer().read(),
_ => 0,
}
}
pub fn set_child_path(&mut self, byte: u8) {
if let Some((Entry::Directory(dir), _)) = &mut self.entry {
dir.child_path_buffer().set_pointer(byte);
}
}
/// Return true if the selected child is a directory.
pub fn child_type(&self) -> bool {
match &self.entry {
Some((Entry::Directory(dir), _)) => match dir.child_type() {
Some(EntryType::Directory) => true,
_ => false,
}
_ => false,
}
}
/// Read a byte from the current file.
pub fn read_byte(&mut self) -> u8 {
match &mut self.entry {
Some((Entry::File(file), _)) => file.read(),
_ => 0,
}
}
/// Writes a byte to the currently-open file.
pub fn write_byte(&mut self, byte: u8) {
match &mut self.entry {
Some((Entry::File(file), _)) => file.write(byte),
_ => (),
}
}
pub fn pointer(&mut self) -> u32 {
match &mut self.entry {
Some((Entry::File(file), _)) => file.pointer(),
Some((Entry::Directory(dir), _)) => dir.selected(),
_ => 0,
}
}
pub fn commit_pointer(&mut self) {
match &mut self.entry {
Some((Entry::File(file), _)) => file.set_pointer(self.pointer_write),
Some((Entry::Directory(dir), _)) => dir.set_selected(self.pointer_write),
_ => (),
}
}
pub fn length(&mut self) -> u32 {
match &mut self.entry {
Some((Entry::File(file), _)) => file.length(),
Some((Entry::Directory(dir), _)) => dir.length(),
_ => 0,
}
}
pub fn commit_length(&mut self) {
match &mut self.entry {
Some((Entry::File(file), _)) => file.set_length(self.length_write),
_ => (),
}
}
pub fn flush(&mut self) {
if let Some((Entry::File(buffered_file), _)) = &mut self.entry {
let _ = buffered_file;
}
}
}
impl Drop for FileDevice {
fn drop(&mut self) {
self.flush();
}
}
/// Create a new file if it doesn't already exist, returning true if successful.
pub fn create_file(destination: &Path) -> bool {
if entry_exists(destination) {
false
} else {
if let Some(parent_path) = destination.parent() {
let _ = std::fs::create_dir_all(parent_path);
}
std::fs::OpenOptions::new().write(true).create_new(true)
.open(destination).is_ok()
}
}
/// Move an entry from one location to another, returning true if successful.
pub fn move_entry(source: &Path, destination: &Path) -> bool {
if !entry_exists(source) || entry_exists(destination) {
return false;
}
std::fs::rename(source, destination).is_ok()
}
/// Delete an entry, returning true if successful.
pub fn delete_entry(source: &Path) -> bool {
use std::fs::{remove_file, remove_dir_all};
use std::io::ErrorKind;
match remove_file(source) {
Ok(_) => true,
Err(error) => match error.kind() {
ErrorKind::NotFound => true,
ErrorKind::IsADirectory => match remove_dir_all(source) {
Ok(_) => true,
Err(error) => match error.kind() {
ErrorKind::NotFound => true,
_ => false,
}
}
_ => false,
}
}
}
/// Returns true if an entry already exists at the given path.
fn entry_exists(source: &Path) -> bool {
std::fs::metadata(source).is_ok()
}
|