Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Append to CopyOptions #40

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ include = [
]

[dependencies]
regex = "1"
62 changes: 62 additions & 0 deletions rusty-tags.vi
Original file line number Diff line number Diff line change
@@ -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<T> = ::std::result::Result<T, Error>;$/;" 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<P>(path: P, mut depth: u64) -> Result<DirContent>$/;" 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<P, Q>(from: P, to: Q, options: &CopyOptions) -> Result<u64>$/;" f
copy /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn copy<P, Q>(from: P, to: Q, options: &CopyOptions) -> Result<u64>$/;" f
copy_items /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn copy_items<P, Q>(from: &[P], to: Q, options: &dir::CopyOptions) -> Result<u64>$/;" f
copy_items_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn copy_items_with_progress<P, Q, F>($/;" f
copy_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn copy_with_progress<P, Q, F>($/;" f
copy_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn copy_with_progress<P, Q, F>($/;" f
create /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn create<P>(path: P, erase: bool) -> Result<()>$/;" f
create_all /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn create_all<P>(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<P>($/;" f
get_details_entry_with_meta /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^fn get_details_entry_with_meta<P>($/;" f
get_dir_content /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn get_dir_content<P>(path: P) -> Result<DirContent>$/;" f
get_dir_content2 /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn get_dir_content2<P>(path: P, options: &DirOptions) -> Result<DirContent>$/;" f
get_size /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn get_size<P>(path: P) -> Result<u64>$/;" f
ls /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn ls<P>(path: P, config: &HashSet<DirEntryAttr>) -> Result<LsResult>$/;" f
move_dir /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn move_dir<P, Q>(from: P, to: Q, options: &CopyOptions) -> Result<u64>$/;" f
move_dir_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn move_dir_with_progress<P, Q, F>($/;" f
move_file /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn move_file<P, Q>(from: P, to: Q, options: &CopyOptions) -> Result<u64>$/;" f
move_file_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn move_file_with_progress<P, Q, F>($/;" f
move_items /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn move_items<P, Q>(from_items: &[P], to: Q, options: &dir::CopyOptions) -> Result<u64>$/;" f
move_items_with_progress /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn move_items_with_progress<P, Q, F>($/;" 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<P>(path: P) -> Result<String>$/;" f
remove /home/artie/Documents/Repos/smallB_fs_extra/src/dir.rs /^pub fn remove<P: AsRef<Path>>(path: P) -> Result<()> {$/;" f
remove /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn remove<P>(path: P) -> Result<()>$/;" f
remove_items /home/artie/Documents/Repos/smallB_fs_extra/src/lib.rs /^pub fn remove_items<P>(from_items: &[P]) -> Result<()>$/;" f
write_all /home/artie/Documents/Repos/smallB_fs_extra/src/file.rs /^pub fn write_all<P>(path: P, content: &str) -> Result<()>$/;" f
50 changes: 39 additions & 11 deletions src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -39,6 +41,7 @@ impl CopyOptions {
CopyOptions {
overwrite: false,
skip_exist: false,
append: false,
buffer_size: 64000, // 64kb
copy_inside: false,
content_only: false,
Expand Down Expand Up @@ -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<u64>;
let mut work = true;
Expand Down Expand Up @@ -793,6 +797,7 @@ where
///
/// ```
pub fn copy_with_progress<P, Q, F>(
selected_mask:&String,
from: P,
to: Q,
options: &CopyOptions,
Expand Down Expand Up @@ -864,7 +869,15 @@ where
};

let mut options = options.clone();
let rg = regex::RegexSet::new(&selected_mask.split_ascii_whitespace().collect::<Vec::<_>>());//++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);
Expand All @@ -880,6 +893,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() {
Expand All @@ -896,11 +910,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);
Expand Down Expand Up @@ -1075,6 +1090,7 @@ where
overwrite: options.overwrite,
skip_exist: options.skip_exist,
buffer_size: options.buffer_size,
append: options.append,
};

let mut result_copy: Result<u64>;
Expand Down Expand Up @@ -1132,6 +1148,7 @@ where
///
/// ```
pub fn move_dir_with_progress<P, Q, F>(
selected_mask:&String,
from: P,
to: Q,
options: &CopyOptions,
Expand Down Expand Up @@ -1201,7 +1218,15 @@ where
};

let mut options = options.clone();
let rg = regex::RegexSet::new(&selected_mask.split_ascii_whitespace().collect::<Vec::<_>>());//++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);
Expand All @@ -1217,6 +1242,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() {
Expand All @@ -1233,11 +1259,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,
Expand Down Expand Up @@ -1300,7 +1327,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 => {}
Expand Down
21 changes: 17 additions & 4 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -29,6 +31,7 @@ impl CopyOptions {
CopyOptions {
overwrite: false,
skip_exist: false,
append: false,
buffer_size: 64000, //64kb
}
}
Expand Down Expand Up @@ -143,7 +146,7 @@ pub fn copy_with_progress<P, Q, F>(
where
P: AsRef<Path>,
Q: AsRef<Path>,
F: FnMut(TransitProcess),
F: FnMut(TransitProcess) -> crate::dir::TransitProcessResult,
{
let from = from.as_ref();
if !from.exists() {
Expand Down Expand Up @@ -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,
Expand All @@ -194,7 +201,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)),
Expand Down Expand Up @@ -275,7 +288,7 @@ pub fn move_file_with_progress<P, Q, F>(
where
P: AsRef<Path>,
Q: AsRef<Path>,
F: FnMut(TransitProcess),
F: FnMut(TransitProcess) -> crate::dir::TransitProcessResult,
{
let mut is_remove = true;
if options.skip_exist && to.as_ref().exists() && !options.overwrite {
Expand Down
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ impl Clone for TransitProcess {
/// ```
///
pub fn copy_items_with_progress<P, Q, F>(
selected_mask:&String,//++artie
from: &[P],
to: Q,
options: &dir::CopyOptions,
Expand Down Expand Up @@ -348,12 +349,13 @@ 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,
skip_exist: options.skip_exist,
buffer_size: options.buffer_size,
append: options.append,
};

if let Some(file_name) = item.file_name() {
Expand All @@ -376,10 +378,10 @@ where
let mut result_copy: Result<u64>;
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);
Expand Down Expand Up @@ -541,6 +543,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() {
Expand Down Expand Up @@ -599,6 +602,7 @@ where
/// ```
///
pub fn move_items_with_progress<P, Q, F>(
selected_mask:&String,//++artie
from_items: &[P],
to: Q,
options: &dir::CopyOptions,
Expand Down Expand Up @@ -660,12 +664,13 @@ 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,
skip_exist: options.skip_exist,
buffer_size: options.buffer_size,
append: options.append,
};

if let Some(file_name) = item.file_name() {
Expand All @@ -688,10 +693,10 @@ where
let mut result_copy: Result<u64>;
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);
Expand Down