diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-28 20:25:01 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-28 20:29:12 +1300 |
commit | 1a830a3d1b9d99653322d5ae49ea8165de7ed9d0 (patch) | |
tree | 798e77b6fcf2438b1c2538a67efe856a2f7cb979 /src/devices/file_device/operations.rs | |
parent | 03c4b069e1806af256730639cefdae115b24401a (diff) | |
download | bedrock-pc-1a830a3d1b9d99653322d5ae49ea8165de7ed9d0.zip |
Rewrite emulatorv1.0.0-alpha1
This is a complete rewrite and restructure of the entire emulator
project, as part of the effort in locking down the Bedrock specification
and in creating much better tooling for creating and using Bedrock
programs.
This commit adds a command-line argument scheme, an embedded assembler,
a headless emulator for use in non-graphical environments, deferred
window creation for programs that do not access the screen device,
and new versions of phosphor and bedrock-core. The new version of
phosphor supports multi-window programs, which will make it possible to
implement program forking in the system device later on, and the new
version of bedrock-core implements the final core specification.
Diffstat (limited to 'src/devices/file_device/operations.rs')
-rw-r--r-- | src/devices/file_device/operations.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/devices/file_device/operations.rs b/src/devices/file_device/operations.rs new file mode 100644 index 0000000..3a3f81b --- /dev/null +++ b/src/devices/file_device/operations.rs @@ -0,0 +1,47 @@ +use std::io::ErrorKind; +use std::path::Path; + + +/// 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 { + 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, + } + } +} + +/// Returns true if an entry already exists at the given path. +fn entry_exists(source: &Path) -> bool { + std::fs::metadata(source).is_ok() +} |