summaryrefslogtreecommitdiff
path: root/src/devices/file/operations.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2024-01-31 07:38:50 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2024-01-31 07:39:07 +1300
commit28101de56231252ca0cfa6a9f107b75112c9acad (patch)
treef5c82a6894562bfb8ed8ab94e9345cefaa6fb96b /src/devices/file/operations.rs
parent30d2f099c9edf4f59fbbdd6686988ae7b0622ba2 (diff)
downloadbedrock-pc-28101de56231252ca0cfa6a9f107b75112c9acad.zip
Implement new file device interface
This is a complete redesign of the file device. The most notable addition is the ability to ascend and descend the file tree.
Diffstat (limited to 'src/devices/file/operations.rs')
-rw-r--r--src/devices/file/operations.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/devices/file/operations.rs b/src/devices/file/operations.rs
new file mode 100644
index 0000000..f33509b
--- /dev/null
+++ b/src/devices/file/operations.rs
@@ -0,0 +1,43 @@
+use std::io::ErrorKind;
+use std::path::Path;
+
+pub fn entry_exists(source: &Path) -> bool {
+ std::fs::metadata(source).is_ok()
+}
+
+// Delete a file or folder, returning true if successful.
+pub fn delete_entry(source: &Path) -> bool {
+ match std::fs::remove_file(source) {
+ Ok(_) => true,
+ Err(e) => match e.kind() {
+ ErrorKind::NotFound => true,
+ ErrorKind::IsADirectory => match std::fs::remove_dir_all(source) {
+ Ok(_) => true,
+ Err(e) => match e.kind() {
+ ErrorKind::NotFound => true,
+ _ => false,
+ }
+ }
+ _ => false,
+ }
+ }
+}
+
+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()
+}
+
+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()
+ }
+}