summaryrefslogtreecommitdiff
path: root/src/operations/cp.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2023-12-19 16:23:29 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2023-12-19 16:23:29 +1300
commit47b25c05a6be51b93c909d38a19440d1c04ba2f8 (patch)
tree448c23c6d58a8407de45a4730cf972bd83713aa0 /src/operations/cp.rs
parent8f410d1ead74b979481f1488a4dcddd33ea829c7 (diff)
downloadvagabond-47b25c05a6be51b93c909d38a19440d1c04ba2f8.zip
Large collection of changesv1.0.0
Diffstat (limited to 'src/operations/cp.rs')
-rw-r--r--src/operations/cp.rs65
1 files changed, 29 insertions, 36 deletions
diff --git a/src/operations/cp.rs b/src/operations/cp.rs
index be43826..27be3ce 100644
--- a/src/operations/cp.rs
+++ b/src/operations/cp.rs
@@ -1,62 +1,55 @@
-use crate::make_parent_directory;
-use crate::{get_entry, get_optional_entry, list_directory};
-use crate::{EntryType, EntryWriteError};
-use std::path::Path;
+use crate::*;
-pub fn copy<P, Q>(source_path: P, target_path: Q) -> Result<(), EntryWriteError>
-where
- P: AsRef<Path>,
- Q: AsRef<Path>,
-{
+#[must_use]
+pub fn copy(source_path: impl AsRef<Path>, destination_path: impl AsRef<Path>) -> WriteResult<()> {
let source = get_entry(&source_path)?;
- let target = get_optional_entry(&target_path)?;
- let target_type = target.and_then(|e| Some(e.entry_type));
+ let destination = get_optional_entry(&destination_path)?;
+ let destination_type = destination.and_then(|e| Some(e.entry_type));
- match (source.entry_type, target_type) {
+ match (source.entry_type, destination_type) {
(EntryType::File, Some(EntryType::File)) => {
- copy_file(source_path, target_path)?;
+ copy_file(source_path, destination_path)?;
}
(EntryType::File, Some(EntryType::Directory)) => {
- let target_path = target_path.as_ref().join(source.name);
- copy_file(source_path, target_path)?;
+ let destination_path = destination_path.as_ref().join(source.name);
+ copy_file(source_path, destination_path)?;
}
(EntryType::File, None) => {
- make_parent_directory(&target_path)?;
- copy_file(source_path, target_path)?;
+ make_parent_directory(&destination_path)?;
+ copy_file(&source_path, &destination_path)?;
}
(EntryType::Directory, Some(EntryType::File)) => {
- std::fs::remove_file(&target_path)?;
- copy_directory(&source_path, &target_path)?;
+ // The file is replaced by a copy of the source directory
+ remove_file(&destination_path)?;
+ copy_directory(&source_path, &destination_path)?;
}
(EntryType::Directory, Some(EntryType::Directory)) => {
- let target_path = target_path.as_ref().join(source.name);
- copy_directory(source_path, target_path)?;
+ // The destination is filled with the contents of the source directory
+ for entry in list_directory(source_path)? {
+ let destination_path = destination_path.as_ref().join(&entry.name);
+ copy_file(entry.path, destination_path)?;
+ }
}
(EntryType::Directory, None) => {
- make_parent_directory(&target_path)?;
- copy_directory(source_path, target_path)?;
+ // The destination is created as a copy of the source directory
+ copy_directory(&source_path, &destination_path)?;
}
}
Ok(())
}
-fn copy_file<P, Q>(source_path: P, target_path: Q) -> Result<(), EntryWriteError>
-where
- P: AsRef<Path>,
- Q: AsRef<Path>,
-{
- std::fs::copy(source_path, target_path)?;
+#[must_use]
+fn copy_file(source_path: impl AsRef<Path>, destination_path: impl AsRef<Path>) -> WriteResult<()> {
+ let copy_result = std::fs::copy(&source_path, &destination_path);
+ io_result_to_write_result(copy_result, &destination_path.as_ref())?;
Ok(())
}
-fn copy_directory<P, Q>(source_path: P, target_path: Q) -> Result<(), EntryWriteError>
-where
- P: AsRef<Path>,
- Q: AsRef<Path>,
-{
+#[must_use]
+fn copy_directory(source_path: impl AsRef<Path>, destination_path: impl AsRef<Path>) -> WriteResult<()> {
for entry in list_directory(&source_path)? {
- let target_path = target_path.as_ref().join(entry.name);
- copy(entry.path, &target_path)?;
+ let destination_path = destination_path.as_ref().join(entry.name);
+ copy(entry.path, &destination_path)?;
}
Ok(())
}