From 65b53003e8de9543ba25a3b3d3cace399b92dc1d Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 6 Aug 2024 19:11:46 +1200 Subject: Refactor the file device The major feature of this refactor is the creation of BedrockFilePath, a type which can be losslessly converted into an operating-system specific path or a Bedrock-style byte path. It also prevents file paths which can't be represented as Bedrock-style byte paths from being constructed, and allows the use of a base directory which acts as an inescapable sandbox. BedrockFilePath will support the creation of a virtual root directory on Windows which will allow navigation between drive letters via the standard hierarchical navigation ports. This commit has been tested on Linux, but not yet Windows. Further work is expected before the new code will work on Windows. --- src/devices/file/operations.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/devices/file/operations.rs') diff --git a/src/devices/file/operations.rs b/src/devices/file/operations.rs index f33509b..0593ac8 100644 --- a/src/devices/file/operations.rs +++ b/src/devices/file/operations.rs @@ -1,11 +1,13 @@ use std::io::ErrorKind; use std::path::Path; + +/// Returns true if an entry already exists at the given path. pub fn entry_exists(source: &Path) -> bool { std::fs::metadata(source).is_ok() } -// Delete a file or folder, returning true if successful. +/// Delete an entry, returning true if successful. pub fn delete_entry(source: &Path) -> bool { match std::fs::remove_file(source) { Ok(_) => true, @@ -23,6 +25,7 @@ pub fn delete_entry(source: &Path) -> bool { } } +/// 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; @@ -30,6 +33,7 @@ pub fn move_entry(source: &Path, destination: &Path) -> bool { std::fs::rename(source, destination).is_ok() } +/// 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 -- cgit v1.2.3-70-g09d2