From 0e40fccdecb71b2d054d91e23b519970ade7043d Mon Sep 17 00:00:00 2001 From: artie Date: Wed, 3 Feb 2021 15:15:04 +0100 Subject: [PATCH 1/8] Aborting during copying operation --- src/dir.rs | 22 ++++++++++++---------- src/file.rs | 12 +++++++++--- src/lib.rs | 8 ++++---- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index 9024c52..db5c377 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -896,11 +896,12 @@ where let copied_bytes = result; while work { { - let _progress_handler = |info: super::file::TransitProcess| { - info_process.copied_bytes = copied_bytes + info.copied_bytes; - info_process.file_bytes_copied = info.copied_bytes; - progress_handler(info_process.clone()); - }; + let _progress_handler = + |info: super::file::TransitProcess| -> TransitProcessResult { + info_process.copied_bytes = copied_bytes + info.copied_bytes; + info_process.file_bytes_copied = info.copied_bytes; + progress_handler(info_process.clone()) + }; result_copy = super::file::copy_with_progress(&file, &path, &file_options, _progress_handler); @@ -1233,11 +1234,12 @@ where let copied_bytes = result; while work { { - let _progress_handler = |info: super::file::TransitProcess| { - info_process.copied_bytes = copied_bytes + info.copied_bytes; - info_process.file_bytes_copied = info.copied_bytes; - progress_handler(info_process.clone()); - }; + let _progress_handler = + |info: super::file::TransitProcess| -> TransitProcessResult { + info_process.copied_bytes = copied_bytes + info.copied_bytes; + info_process.file_bytes_copied = info.copied_bytes; + progress_handler(info_process.clone()) + }; result_copy = super::file::move_file_with_progress( &file, diff --git a/src/file.rs b/src/file.rs index f9a384b..09f2f93 100644 --- a/src/file.rs +++ b/src/file.rs @@ -143,7 +143,7 @@ pub fn copy_with_progress( where P: AsRef, Q: AsRef, - F: FnMut(TransitProcess), + F: FnMut(TransitProcess) -> crate::dir::TransitProcessResult, { let from = from.as_ref(); if !from.exists() { @@ -194,7 +194,13 @@ where copied_bytes, total_bytes: file_size, }; - progress_handler(data); + let progres_result = progress_handler(data); + if progres_result as usize == crate::dir::TransitProcessResult::Abort as usize { + return Err(super::error::Error::new( + super::error::ErrorKind::Interrupted, + "Aborted by user", + )); + } } Err(ref e) if e.kind() == ::std::io::ErrorKind::Interrupted => {} Err(e) => return Err(::std::convert::From::from(e)), @@ -275,7 +281,7 @@ pub fn move_file_with_progress( where P: AsRef, Q: AsRef, - F: FnMut(TransitProcess), + F: FnMut(TransitProcess) -> crate::dir::TransitProcessResult, { let mut is_remove = true; if options.skip_exist && to.as_ref().exists() && !options.overwrite { diff --git a/src/lib.rs b/src/lib.rs index 2e7d4bd..c2610c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -376,10 +376,10 @@ where let mut result_copy: Result; while work { { - let handler = |info: file::TransitProcess| { + let handler = |info: file::TransitProcess| -> dir::TransitProcessResult { info_process.copied_bytes = copied_bytes + info.copied_bytes; info_process.file_bytes_copied = info.copied_bytes; - progress_handler(info_process.clone()); + progress_handler(info_process.clone()) }; result_copy = file::copy_with_progress(item, &file_name, &file_options, handler); @@ -688,10 +688,10 @@ where let mut result_copy: Result; while work { { - let handler = |info: file::TransitProcess| { + let handler = |info: file::TransitProcess| -> dir::TransitProcessResult { info_process.copied_bytes = copied_bytes + info.copied_bytes; info_process.file_bytes_copied = info.copied_bytes; - progress_handler(info_process.clone()); + progress_handler(info_process.clone()) }; result_copy = file::move_file_with_progress(item, &file_name, &file_options, handler); From 45a6e8fc0865d0796ceb1e26bbdef05f9169c22b Mon Sep 17 00:00:00 2001 From: artie Date: Thu, 25 Feb 2021 10:06:21 +0100 Subject: [PATCH 2/8] Added Append to CopyOptions --- src/dir.rs | 7 +++++++ src/file.rs | 9 ++++++++- src/lib.rs | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/dir.rs b/src/dir.rs index 9024c52..7cb3ffa 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -11,6 +11,8 @@ pub struct CopyOptions { pub overwrite: bool, /// Skip existing files if true (default: false). pub skip_exist: bool, + /// If set to true will append new file content to the old one + pub append: bool, /// Buffer size that specifies the amount of bytes to be moved or copied before the progress handler is called. This only affects functions with progress handlers. (default: 64000) pub buffer_size: usize, /// Recursively copy a directory with a new name or place it inside the destination (default: false, same behaviors as cp -r on Unix) @@ -39,6 +41,7 @@ impl CopyOptions { CopyOptions { overwrite: false, skip_exist: false, + append: false, buffer_size: 64000, // 64kb copy_inside: false, content_only: false, @@ -582,6 +585,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, + append: options.append, }; let mut result_copy: Result; let mut work = true; @@ -880,6 +884,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, + append: options.append, }; if let Some(file_name) = file_name.to_str() { @@ -1075,6 +1080,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, + append: options.append, }; let mut result_copy: Result; @@ -1217,6 +1223,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, + append: options.append, }; if let Some(file_name) = file_name.to_str() { diff --git a/src/file.rs b/src/file.rs index f9a384b..d78b2d4 100644 --- a/src/file.rs +++ b/src/file.rs @@ -10,6 +10,8 @@ pub struct CopyOptions { pub overwrite: bool, /// Sets the option true for skip existing files. pub skip_exist: bool, + /// If set to true appends content of new file to the old one + pub append: bool, /// Sets buffer size for copy/move work only with receipt information about process work. pub buffer_size: usize, } @@ -29,6 +31,7 @@ impl CopyOptions { CopyOptions { overwrite: false, skip_exist: false, + append: false, buffer_size: 64000, //64kb } } @@ -180,7 +183,11 @@ where let file_size = file_from.metadata()?.len(); let mut copied_bytes: u64 = 0; - let mut file_to = File::create(to)?; + let mut file_to = std::fs::OpenOptions::new() + .write(true) + .create(true) + .append(options.append) + .open(to)?; while !buf.is_empty() { match file_from.read(&mut buf) { Ok(0) => break, diff --git a/src/lib.rs b/src/lib.rs index 118643a..0e65ebc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -354,6 +354,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, + append: options.append, }; if let Some(file_name) = item.file_name() { @@ -541,6 +542,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, + append: options.append, }; if let Some(file_name) = item.file_name() { @@ -666,6 +668,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, + append: options.append, }; if let Some(file_name) = item.file_name() { From c0ee170587cc1a09a2864a26c9b516f88c05ce20 Mon Sep 17 00:00:00 2001 From: artie Date: Thu, 25 Feb 2021 10:16:25 +0100 Subject: [PATCH 3/8] Formatting --- src/dir.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index 7cb3ffa..4c7a27c 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -884,7 +884,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, - append: options.append, + append: options.append, }; if let Some(file_name) = file_name.to_str() { @@ -1080,7 +1080,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, - append: options.append, + append: options.append, }; let mut result_copy: Result; @@ -1223,7 +1223,7 @@ where overwrite: options.overwrite, skip_exist: options.skip_exist, buffer_size: options.buffer_size, - append: options.append, + append: options.append, }; if let Some(file_name) = file_name.to_str() { From 4add8ab4d0f135323d4aebf9eaa93fb127dc7ec4 Mon Sep 17 00:00:00 2001 From: artie Date: Thu, 4 Mar 2021 09:35:45 +0100 Subject: [PATCH 4/8] Regex in cpy_dir --- Cargo.toml | 1 + rusty-tags.vi | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/dir.rs | 8 +++++++ src/lib.rs | 3 ++- 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 rusty-tags.vi diff --git a/Cargo.toml b/Cargo.toml index 1cfb325..9ba74f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ include = [ ] [dependencies] +regex = "1" diff --git a/rusty-tags.vi b/rusty-tags.vi new file mode 100644 index 0000000..eaac351 --- /dev/null +++ b/rusty-tags.vi @@ -0,0 +1,62 @@ +!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ +!_TAG_PROGRAM_NAME Exuberant Ctags // +!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ +!_TAG_PROGRAM_VERSION 5.9~svn20110310 // +CopyOptions /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub struct CopyOptions {$/;" s +CopyOptions /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub struct CopyOptions {$/;" s +DirContent /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub struct DirContent {$/;" s +DirEntryAttr /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub enum DirEntryAttr {$/;" g +DirEntryValue /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub enum DirEntryValue {$/;" g +DirOptions /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub struct DirOptions {$/;" s +Error /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^pub struct Error {$/;" s +ErrorKind /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^pub enum ErrorKind {$/;" g +LsResult /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub struct LsResult {$/;" s +Result /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^pub type Result = ::std::result::Result;$/;" T +TransitProcess /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub struct TransitProcess {$/;" s +TransitProcess /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub struct TransitProcess {$/;" s +TransitProcess /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub struct TransitProcess {$/;" s +TransitProcessResult /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub enum TransitProcessResult {$/;" g +TransitState /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub enum TransitState {$/;" g +_get_dir_content /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^fn _get_dir_content

(path: P, mut depth: u64) -> Result$/;" f +as_str /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^ fn as_str(&self) -> &str {$/;" f +clone /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^ fn clone(&self) -> TransitProcess {$/;" f +clone /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^ fn clone(&self) -> TransitProcess {$/;" f +copy /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn copy(from: P, to: Q, options: &CopyOptions) -> Result$/;" f +copy /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn copy(from: P, to: Q, options: &CopyOptions) -> Result$/;" f +copy_items /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn copy_items(from: &[P], to: Q, options: &dir::CopyOptions) -> Result$/;" f +copy_items_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn copy_items_with_progress($/;" f +copy_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn copy_with_progress($/;" f +copy_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn copy_with_progress($/;" f +create /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn create

(path: P, erase: bool) -> Result<()>$/;" f +create_all /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn create_all

(path: P, erase: bool) -> Result<()>$/;" f +default /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^ fn default() -> Self {$/;" f +default /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^ fn default() -> Self {$/;" f +description /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^ fn description(&self) -> &str {$/;" f +err /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^macro_rules! err {$/;" d +fmt /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {$/;" f +from /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^ fn from(err: IoError) -> Error {$/;" f +from /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^ fn from(err: OsString) -> Error {$/;" f +from /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^ fn from(err: StripPrefixError) -> Error {$/;" f +get_details_entry /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn get_details_entry

($/;" f +get_details_entry_with_meta /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^fn get_details_entry_with_meta

($/;" f +get_dir_content /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn get_dir_content

(path: P) -> Result$/;" f +get_dir_content2 /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn get_dir_content2

(path: P, options: &DirOptions) -> Result$/;" f +get_size /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn get_size

(path: P) -> Result$/;" f +ls /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn ls

(path: P, config: &HashSet) -> Result$/;" f +move_dir /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn move_dir(from: P, to: Q, options: &CopyOptions) -> Result$/;" f +move_dir_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn move_dir_with_progress($/;" f +move_file /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn move_file(from: P, to: Q, options: &CopyOptions) -> Result$/;" f +move_file_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn move_file_with_progress($/;" f +move_items /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn move_items(from_items: &[P], to: Q, options: &dir::CopyOptions) -> Result$/;" f +move_items_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn move_items_with_progress($/;" f +new /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^ pub fn new() -> CopyOptions {$/;" f +new /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^ pub fn new() -> DirOptions {$/;" f +new /home/artie/Documents/Repos/smallB_fs_extra/src/error.rs /^ pub fn new(kind: ErrorKind, message: &str) -> Error {$/;" f +new /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^ pub fn new() -> CopyOptions {$/;" f +read_to_string /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn read_to_string

(path: P) -> Result$/;" f +remove /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn remove>(path: P) -> Result<()> {$/;" f +remove /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn remove

(path: P) -> Result<()>$/;" f +remove_items /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn remove_items

(from_items: &[P]) -> Result<()>$/;" f +write_all /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn write_all

(path: P, content: &str) -> Result<()>$/;" f diff --git a/src/dir.rs b/src/dir.rs index da447f0..d015db4 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -797,6 +797,7 @@ where /// /// ``` pub fn copy_with_progress( + selected_mask:&String, from: P, to: Q, options: &CopyOptions, @@ -868,7 +869,14 @@ where }; let mut options = options.clone(); + let rg = regex::Regex::new(&selected_mask).unwrap();//++artie for file in dir_content.files { + /*++artie */ + if !rg.is_match(&file) + { + continue; + } + //--artie let mut to = to.to_path_buf(); let tp = Path::new(&file).strip_prefix(from)?; let path = to.join(&tp); diff --git a/src/lib.rs b/src/lib.rs index 7e9caad..b0fff86 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -287,6 +287,7 @@ impl Clone for TransitProcess { /// ``` /// pub fn copy_items_with_progress( + selected_mask:&String,//++artie from: &[P], to: Q, options: &dir::CopyOptions, @@ -348,7 +349,7 @@ where } result }; - result += dir::copy_with_progress(item, &to, &dir_options, handler)?; + result += dir::copy_with_progress(selected_mask/*++artie */,item, &to, &dir_options, handler)?; } else { let mut file_options = file::CopyOptions { overwrite: options.overwrite, From a635cc42fa714fc4f12df4ec147c977539b7c1be Mon Sep 17 00:00:00 2001 From: artie Date: Thu, 4 Mar 2021 10:04:09 +0100 Subject: [PATCH 5/8] Fix panic on unwrap --- src/dir.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index d015db4..ada910f 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -869,10 +869,11 @@ where }; let mut options = options.clone(); - let rg = regex::Regex::new(&selected_mask).unwrap();//++artie + let rg = regex::Regex::new(&selected_mask);//++artie + let rg_ok = rg.is_ok(); for file in dir_content.files { /*++artie */ - if !rg.is_match(&file) + if rg_ok && !rg.as_ref().unwrap().is_match(&file) { continue; } From 9e4c30094ebf6edcc32fcdd7b403463c32a4b964 Mon Sep 17 00:00:00 2001 From: artie Date: Thu, 4 Mar 2021 13:00:37 +0100 Subject: [PATCH 6/8] Fix for panic on * --- src/dir.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dir.rs b/src/dir.rs index ada910f..52f6465 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -869,7 +869,7 @@ where }; let mut options = options.clone(); - let rg = regex::Regex::new(&selected_mask);//++artie + let rg = regex::RegexSet::new(&selected_mask.split_ascii_whitespace().collect::>());//++artie let rg_ok = rg.is_ok(); for file in dir_content.files { /*++artie */ From 948a2f7520d4e7f78ef24fea1d563f6917aa3f10 Mon Sep 17 00:00:00 2001 From: artie Date: Mon, 8 Mar 2021 08:37:04 +0100 Subject: [PATCH 7/8] SkipAll remove false set --- src/dir.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dir.rs b/src/dir.rs index 52f6465..864c1c7 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -1318,7 +1318,8 @@ where file_options.skip_exist = true; } TransitProcessResult::SkipAll => { - file_options.skip_exist = true; + is_remove = false; + file_options.skip_exist = true; options.skip_exist = true; } TransitProcessResult::Retry => {} From df7a839783e40dc0da03631542074126f4636394 Mon Sep 17 00:00:00 2001 From: artie Date: Mon, 8 Mar 2021 09:42:03 +0100 Subject: [PATCH 8/8] updated move with regex --- src/dir.rs | 9 +++++++++ src/lib.rs | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/dir.rs b/src/dir.rs index 864c1c7..34ed300 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -1148,6 +1148,7 @@ where /// /// ``` pub fn move_dir_with_progress( + selected_mask:&String, from: P, to: Q, options: &CopyOptions, @@ -1217,7 +1218,15 @@ where }; let mut options = options.clone(); + let rg = regex::RegexSet::new(&selected_mask.split_ascii_whitespace().collect::>());//++artie + let rg_ok = rg.is_ok(); for file in dir_content.files { + /*++artie */ + if rg_ok && !rg.as_ref().unwrap().is_match(&file) + { + continue; + } + //--artie let mut to = to.to_path_buf(); let tp = Path::new(&file).strip_prefix(from)?; let path = to.join(&tp); diff --git a/src/lib.rs b/src/lib.rs index b0fff86..ad35a1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -602,6 +602,7 @@ where /// ``` /// pub fn move_items_with_progress( + selected_mask:&String,//++artie from_items: &[P], to: Q, options: &dir::CopyOptions, @@ -663,7 +664,7 @@ where } result }; - result += dir::move_dir_with_progress(item, &to, &dir_options, handler)?; + result += dir::move_dir_with_progress(selected_mask/*++artie */,item, &to, &dir_options, handler)?; } else { let mut file_options = file::CopyOptions { overwrite: options.overwrite,