summaryrefslogtreecommitdiffstats
path: root/src/commands/file_operations.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/file_operations.rs')
-rw-r--r--src/commands/file_operations.rs137
1 files changed, 35 insertions, 102 deletions
diff --git a/src/commands/file_operations.rs b/src/commands/file_operations.rs
index 570cc99..0db7f41 100644
--- a/src/commands/file_operations.rs
+++ b/src/commands/file_operations.rs
@@ -186,9 +186,6 @@ impl PasteFiles {
&self,
context: &mut JoshutoContext,
) -> Result<FileOperationThread, std::io::Error> {
- use std::os::linux::fs::MetadataExt;
-
- let src_ino;
let paths = SELECTED_FILES.lock().unwrap().take();
match paths {
Some(paths) => {
@@ -198,25 +195,15 @@ impl PasteFiles {
"no files selected",
));
}
- src_ino = paths[0].metadata()?.st_dev();
let tab_src = TAB_SRC.load(atomic::Ordering::SeqCst);
let tab_dest = context.curr_tab_index;
let destination = context.tabs[tab_dest].curr_path.clone();
- let dest_ino = destination.metadata()?.st_dev();
let options = self.options.clone();
let (tx, rx) = mpsc::channel();
- #[cfg(target_os = "linux")]
- let handle = if dest_ino == src_ino {
- thread::spawn(move || fs_rename_thread(options, tx, destination, paths))
- } else {
- thread::spawn(move || fs_cut_thread(options, tx, destination, paths))
- };
-
- #[cfg(not(target_os = "linux"))]
let handle = thread::spawn(move || fs_cut_thread(options, tx, destination, paths));
let thread = FileOperationThread {
@@ -227,12 +214,10 @@ impl PasteFiles {
};
Ok(thread)
}
- None => {
- Err(std::io::Error::new(
- std::io::ErrorKind::Other,
- "no files selected",
- ))
- }
+ None => Err(std::io::Error::new(
+ std::io::ErrorKind::Other,
+ "no files selected",
+ )),
}
}
@@ -277,15 +262,15 @@ impl PasteFiles {
}
}
-fn fs_rename_thread(
+fn fs_cut_thread(
options: fs_extra::dir::CopyOptions,
tx: mpsc::Sender<ProgressInfo>,
dest: path::PathBuf,
paths: Vec<path::PathBuf>,
) -> std::result::Result<(), std::io::Error> {
let mut progress_info = ProgressInfo {
- bytes_finished: 1,
- total_bytes: paths.len() as u64 + 1,
+ bytes_finished: 4,
+ total_bytes: paths.len() as u64 + 4,
};
let mut destination = dest;
@@ -306,86 +291,36 @@ fn fs_rename_thread(
i += 1;
}
}
- std::fs::rename(&path, &destination)?;
- destination.pop();
-
- progress_info.bytes_finished += 1;
- match tx.send(progress_info.clone()) {
- _ => {}
- }
- }
- Ok(())
-}
-
-fn fs_cut_thread(options: fs_extra::dir::CopyOptions,
- tx: mpsc::Sender<ProgressInfo>,
- dest: path::PathBuf,
- paths: Vec<path::PathBuf>,
-) -> std::result::Result<(), std::io::Error> {
- let mut progress_info = ProgressInfo {
- bytes_finished: 4,
- total_bytes: paths.len() as u64 + 4,
- };
-
- let mut destination = dest;
-
- for path in paths {
- let file_name = path.file_name().unwrap().to_os_string();
-
- if path.symlink_metadata()?.is_dir() {
- destination.push(file_name.clone());
- if !options.skip_exist {
- let mut i = 0;
- while destination.exists() {
- destination.pop();
-
- let mut file_name = file_name.clone();
- file_name.push(&format!("_{}", i));
-
- destination.push(file_name);
- i += 1;
- }
- }
- std::fs::create_dir(&destination)?;
- let path: Vec<path::PathBuf> = std::fs::read_dir(path)?
- .filter_map(|s| {
- match s {
+ match std::fs::rename(&path, &destination) {
+ Ok(_) => {}
+ Err(_) => {
+ if path.symlink_metadata()?.is_dir() {
+ std::fs::create_dir(&destination)?;
+ let path: Vec<path::PathBuf> = std::fs::read_dir(path)?
+ .filter_map(|s| match s {
Ok(s) => Some(s.path()),
- _ => None
+ _ => None,
+ })
+ .collect();
+
+ match fs_extra::move_items(&path, &destination, &options) {
+ Err(e) => {
+ let err =
+ std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e));
+ return Err(err);
}
- })
- .collect();
-
- match fs_extra::move_items(&path, &destination, &options) {
- Err(e) => {
- let err = std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e));
- return Err(err);
- }
- _ => {}
- }
- } else {
- destination.push(file_name.clone());
- if !options.skip_exist {
- let mut i = 0;
- while destination.exists() {
- destination.pop();
-
- let mut file_name = file_name.clone();
- file_name.push(&format!("_{}", i));
-
- destination.push(file_name);
- i += 1;
+ _ => {}
+ }
+ } else {
+ std::fs::copy(&path, &destination)?;
+ std::fs::remove_file(&path)?;
}
}
- std::fs::copy(&path, &destination)?;
- std::fs::remove_file(&path)?;
}
-
destination.pop();
+
progress_info.bytes_finished += 1;
- match tx.send(progress_info.clone()) {
- _ => {}
- }
+ tx.send(progress_info.clone());
}
return Ok(());
}
@@ -422,13 +357,11 @@ fn fs_copy_thread(
}
std::fs::create_dir(&destination)?;
let path: Vec<path::PathBuf> = std::fs::read_dir(path)?
- .filter_map(|s| {
- match s {
- Ok(s) => Some(s.path()),
- _ => None
- }
- })
- .collect();
+ .filter_map(|s| match s {
+ Ok(s) => Some(s.path()),
+ _ => None,
+ })
+ .collect();
match fs_extra::copy_items(&path, &destination, &options) {
Err(e) => {