From 8f410d1ead74b979481f1488a4dcddd33ea829c7 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 25 Aug 2022 21:27:39 +1200 Subject: Initial commit --- src/operations/cp.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ src/operations/ls.rs | 51 ++++++++++++++++++++++++++++++++++++++++ src/operations/mkdir.rs | 20 ++++++++++++++++ src/operations/rm.rs | 23 ++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 src/operations/cp.rs create mode 100644 src/operations/ls.rs create mode 100644 src/operations/mkdir.rs create mode 100644 src/operations/rm.rs (limited to 'src/operations') diff --git a/src/operations/cp.rs b/src/operations/cp.rs new file mode 100644 index 0000000..be43826 --- /dev/null +++ b/src/operations/cp.rs @@ -0,0 +1,62 @@ +use crate::make_parent_directory; +use crate::{get_entry, get_optional_entry, list_directory}; +use crate::{EntryType, EntryWriteError}; +use std::path::Path; + +pub fn copy(source_path: P, target_path: Q) -> Result<(), EntryWriteError> +where + P: AsRef, + Q: AsRef, +{ + let source = get_entry(&source_path)?; + let target = get_optional_entry(&target_path)?; + let target_type = target.and_then(|e| Some(e.entry_type)); + + match (source.entry_type, target_type) { + (EntryType::File, Some(EntryType::File)) => { + copy_file(source_path, target_path)?; + } + (EntryType::File, Some(EntryType::Directory)) => { + let target_path = target_path.as_ref().join(source.name); + copy_file(source_path, target_path)?; + } + (EntryType::File, None) => { + make_parent_directory(&target_path)?; + copy_file(source_path, target_path)?; + } + (EntryType::Directory, Some(EntryType::File)) => { + std::fs::remove_file(&target_path)?; + copy_directory(&source_path, &target_path)?; + } + (EntryType::Directory, Some(EntryType::Directory)) => { + let target_path = target_path.as_ref().join(source.name); + copy_directory(source_path, target_path)?; + } + (EntryType::Directory, None) => { + make_parent_directory(&target_path)?; + copy_directory(source_path, target_path)?; + } + } + Ok(()) +} + +fn copy_file(source_path: P, target_path: Q) -> Result<(), EntryWriteError> +where + P: AsRef, + Q: AsRef, +{ + std::fs::copy(source_path, target_path)?; + Ok(()) +} + +fn copy_directory(source_path: P, target_path: Q) -> Result<(), EntryWriteError> +where + P: AsRef, + Q: AsRef, +{ + for entry in list_directory(&source_path)? { + let target_path = target_path.as_ref().join(entry.name); + copy(entry.path, &target_path)?; + } + Ok(()) +} diff --git a/src/operations/ls.rs b/src/operations/ls.rs new file mode 100644 index 0000000..2d2da5d --- /dev/null +++ b/src/operations/ls.rs @@ -0,0 +1,51 @@ +use crate::{Entry, EntryReadError, EntryType}; +use std::path::Path; + +pub fn get_entry

(path: P) -> Result +where + P: AsRef, +{ + Entry::from_path(path) +} + +pub fn get_optional_entry

(path: P) -> Result, EntryReadError> +where + P: AsRef, +{ + match get_entry(path) { + Ok(e) => Ok(Some(e)), + Err(EntryReadError::NotFound) => Ok(None), + Err(other) => Err(other), + } +} + +pub fn list_directory

(path: P) -> Result, EntryReadError> +where + P: AsRef, +{ + let mut entries = Vec::new(); + for dir_entry in std::fs::read_dir(path)? { + let entry = match Entry::from_path(&dir_entry?.path()) { + Ok(v) => v, + Err(_) => continue, + }; + entries.push(entry); + } + return Ok(entries); +} + +/// Recursively descend into a directory and all sub-directories, +/// returning an [`Entry`](struct.Entry.html) for each discovered file. +pub fn traverse_directory

(path: P) -> Result, EntryReadError> +where + P: AsRef, +{ + let mut file_entries = Vec::new(); + for entry in list_directory(path)? { + match entry.entry_type { + EntryType::File => file_entries.push(entry), + EntryType::Directory => file_entries.extend(traverse_directory(&entry.path)?), + } + } + return Ok(file_entries); +} diff --git a/src/operations/mkdir.rs b/src/operations/mkdir.rs new file mode 100644 index 0000000..011b8cf --- /dev/null +++ b/src/operations/mkdir.rs @@ -0,0 +1,20 @@ +use crate::EntryWriteError; +use std::path::Path; + +pub fn make_directory

(path: P) -> Result<(), EntryWriteError> +where + P: AsRef, +{ + std::fs::DirBuilder::new().recursive(true).create(path)?; + Ok(()) +} + +pub fn make_parent_directory

(path: P) -> Result<(), EntryWriteError> +where + P: AsRef, +{ + match path.as_ref().parent() { + Some(parent) => make_directory(parent), + None => Ok(()), + } +} diff --git a/src/operations/rm.rs b/src/operations/rm.rs new file mode 100644 index 0000000..846a094 --- /dev/null +++ b/src/operations/rm.rs @@ -0,0 +1,23 @@ +use crate::EntryWriteError; +use crate::{get_entry, EntryType}; +use std::path::Path; + +pub fn remove

(path: P) -> Result<(), EntryWriteError> +where + P: AsRef, +{ + let entry = get_entry(&path)?; + match entry.entry_type { + EntryType::File => std::fs::remove_file(&path)?, + EntryType::Directory => std::fs::remove_dir_all(&path)?, + } + Ok(()) +} + +pub fn remove_file

(path: P) -> Result<(), EntryWriteError> +where + P: AsRef, +{ + std::fs::remove_file(path)?; + Ok(()) +} -- cgit v1.2.3-70-g09d2