From 840a2685bfadef67c8a7835691f6d09bdc67fb25 Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Sun, 29 Nov 2020 12:35:51 +0100 Subject: [PATCH] Removed progress callback (not a priority atm) --- src/filetransfer/mod.rs | 47 +++++------ src/filetransfer/sftp_transfer.rs | 67 ++++------------ src/ui/activities/filetransfer_activity.rs | 92 ++++++++-------------- 3 files changed, 75 insertions(+), 131 deletions(-) diff --git a/src/filetransfer/mod.rs b/src/filetransfer/mod.rs index 9b2e1b5..f047a0b 100644 --- a/src/filetransfer/mod.rs +++ b/src/filetransfer/mod.rs @@ -23,17 +23,14 @@ * */ -use std::path::{Path, PathBuf}; use std::fs::File; +use std::path::{Path, PathBuf}; use crate::fs::FsEntry; // Transfers pub mod sftp_transfer; -// Types -pub type ProgressCallback = dyn Fn(usize, usize); - /// ## FileTransferProtocol /// /// This enum defines the different transfer protocol available in TermSCP @@ -65,7 +62,9 @@ pub enum FileTransferError { impl std::fmt::Display for FileTransferError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let err: String = match self { - FileTransferError::AuthenticationFailed => String::from("Authentication failed: bad credentials"), + FileTransferError::AuthenticationFailed => { + String::from("Authentication failed: bad credentials") + } FileTransferError::BadAddress => String::from("Bad address syntax"), FileTransferError::ConnectionError => String::from("Connection error"), FileTransferError::DirStatFailed => String::from("Could not stat directory"), @@ -82,71 +81,75 @@ impl std::fmt::Display for FileTransferError { } /// ## FileTransfer -/// +/// /// File transfer trait must be implemented by all the file transfers and defines the method used by a generic file transfer pub trait FileTransfer { - /// ### connect - /// + /// /// Connect to the remote server - fn connect(&mut self, address: String, port: u16, username: Option, password: Option) -> Result<(), FileTransferError>; + fn connect( + &mut self, + address: String, + port: u16, + username: Option, + password: Option, + ) -> Result<(), FileTransferError>; /// ### disconnect - /// + /// /// Disconnect from the remote server fn disconnect(&mut self) -> Result<(), FileTransferError>; /// ### is_connected - /// + /// /// Indicates whether the client is connected to remote fn is_connected(&self) -> bool; /// ### pwd - /// + /// /// Print working directory fn pwd(&self) -> Result; /// ### change_dir - /// + /// /// Change working directory fn change_dir(&mut self, dir: &Path) -> Result; /// ### list_dir - /// + /// /// List directory entries fn list_dir(&self, path: &Path) -> Result, FileTransferError>; /// ### mkdir - /// + /// /// Make directory fn mkdir(&self, dir: &Path) -> Result<(), FileTransferError>; /// ### remove - /// + /// /// Remove a file or a directory fn remove(&self, file: &FsEntry) -> Result<(), FileTransferError>; /// ### rename - /// + /// /// Rename file or a directory fn rename(&self, file: &FsEntry, dst: &Path) -> Result<(), FileTransferError>; /// ### send_file - /// + /// /// Send file to remote /// File name is referred to the name of the file as it will be saved /// Data contains the file data - fn send_file(&self, file_name: &Path, file: &mut File, prog_cb: Option>) -> Result<(), FileTransferError>; + fn send_file(&self, file_name: &Path, file: &mut File) -> Result<(), FileTransferError>; /// ### recv_file - /// + /// /// Receive file from remote with provided name - fn recv_file(&self, file_name: &Path, dest_file: &mut File, prog_cb: Option>) -> Result<(), FileTransferError>; - + fn recv_file(&self, file_name: &Path, dest_file: &mut File) -> Result<(), FileTransferError>; } diff --git a/src/filetransfer/sftp_transfer.rs b/src/filetransfer/sftp_transfer.rs index 9d3488d..d05d587 100644 --- a/src/filetransfer/sftp_transfer.rs +++ b/src/filetransfer/sftp_transfer.rs @@ -27,7 +27,7 @@ extern crate ssh2; // Locals -use super::{FileTransfer, FileTransferError, ProgressCallback}; +use super::{FileTransfer, FileTransferError}; use crate::fs::{FsDirectory, FsEntry, FsFile}; // Includes @@ -178,7 +178,7 @@ impl FileTransfer for SftpFileTransfer { } /// ### is_connected - /// + /// /// Indicates whether the client is connected to remote fn is_connected(&self) -> bool { self.session.is_some() @@ -369,7 +369,7 @@ impl FileTransfer for SftpFileTransfer { } /// ### rename - /// + /// /// Rename file or a directory fn rename(&self, file: &FsEntry, dst: &Path) -> Result<(), FileTransferError> { match self.sftp.as_ref() { @@ -380,11 +380,11 @@ impl FileTransfer for SftpFileTransfer { // Get abs path of entry let abs_src: PathBuf = match file { FsEntry::Directory(dir) => dir.abs_path.clone(), - FsEntry::File(file) => file.abs_path.clone() + FsEntry::File(file) => file.abs_path.clone(), }; match sftp.rename(abs_src.as_path(), abs_dst.as_path(), None) { Ok(_) => Ok(()), - Err(_) => Err(FileTransferError::FileCreateDenied) + Err(_) => Err(FileTransferError::FileCreateDenied), } } } @@ -395,18 +395,13 @@ impl FileTransfer for SftpFileTransfer { /// Send file to remote /// File name is referred to the name of the file as it will be saved /// Data contains the file data - fn send_file( - &self, - file_name: &Path, - file: &mut File, - prog_cb: Option>, - ) -> Result<(), FileTransferError> { + fn send_file(&self, file_name: &Path, file: &mut File) -> Result<(), FileTransferError> { match self.sftp.as_ref() { None => Err(FileTransferError::UninitializedSession), Some(sftp) => { let remote_path: PathBuf = self.get_abs_path(file_name); // Get file size - let file_size: usize = file.seek(std::io::SeekFrom::End(0)).unwrap_or(0) as usize; + //let file_size: usize = file.seek(std::io::SeekFrom::End(0)).unwrap_or(0) as usize; // rewind if let Err(err) = file.seek(std::io::SeekFrom::Start(0)) { return Err(FileTransferError::IoErr(err)); @@ -414,13 +409,13 @@ impl FileTransfer for SftpFileTransfer { // Open remote file match sftp.create(remote_path.as_path()) { Ok(mut rhnd) => { - let mut total_bytes_written: usize = 0; + //let mut total_bytes_written: usize = 0; loop { // Read till you can let mut buffer: [u8; 8192] = [0; 8192]; match file.read(&mut buffer) { Ok(bytes_read) => { - total_bytes_written += bytes_read; + //total_bytes_written += bytes_read; if bytes_read == 0 { break; } else { @@ -428,10 +423,6 @@ impl FileTransfer for SftpFileTransfer { if let Err(err) = rhnd.write(&buffer) { return Err(FileTransferError::IoErr(err)); } - // Call callback - if let Some(ref cb) = prog_cb { - cb(total_bytes_written, file_size); - } } } Err(err) => return Err(FileTransferError::IoErr(err)), @@ -448,12 +439,7 @@ impl FileTransfer for SftpFileTransfer { /// ### recv_file /// /// Receive file from remote with provided name - fn recv_file( - &self, - file_name: &Path, - dest_file: &mut File, - prog_cb: Option>, - ) -> Result<(), FileTransferError> { + fn recv_file(&self, file_name: &Path, dest_file: &mut File) -> Result<(), FileTransferError> { match self.sftp.as_ref() { None => Err(FileTransferError::UninitializedSession), Some(sftp) => { @@ -465,20 +451,19 @@ impl FileTransfer for SftpFileTransfer { // Open remote file match sftp.open(remote_path.as_path()) { Ok(mut rhnd) => { - let file_size: usize = - rhnd.seek(std::io::SeekFrom::End(0)).unwrap_or(0) as usize; + // let file_size: usize = rhnd.seek(std::io::SeekFrom::End(0)).unwrap_or(0) as usize; // rewind if let Err(err) = rhnd.seek(std::io::SeekFrom::Start(0)) { return Err(FileTransferError::IoErr(err)); }; // Write local file - let mut total_bytes_written: usize = 0; + // let mut total_bytes_written: usize = 0; loop { // Read till you can let mut buffer: [u8; 8192] = [0; 8192]; match rhnd.read(&mut buffer) { Ok(bytes_read) => { - total_bytes_written += bytes_read; + // total_bytes_written += bytes_read; if bytes_read == 0 { break; } else { @@ -486,10 +471,6 @@ impl FileTransfer for SftpFileTransfer { if let Err(err) = dest_file.write(&buffer) { return Err(FileTransferError::IoErr(err)); } - // Call callback - if let Some(ref cb) = prog_cb { - cb(total_bytes_written, file_size); - } } } Err(err) => return Err(FileTransferError::IoErr(err)), @@ -696,11 +677,7 @@ mod tests { .unwrap(); // Receive file assert!(client - .recv_file( - PathBuf::from("readme.txt").as_path(), - &mut dst_file_hnd, - Some(Box::new(progress_callback)) - ) + .recv_file(PathBuf::from("readme.txt").as_path(), &mut dst_file_hnd) .is_ok()); // Disconnect assert!(client.disconnect().is_ok()); @@ -730,11 +707,7 @@ mod tests { .unwrap(); // Receive file assert!(client - .recv_file( - PathBuf::from("omar.txt").as_path(), - &mut dst_file_hnd, - Some(Box::new(progress_callback)) - ) + .recv_file(PathBuf::from("omar.txt").as_path(), &mut dst_file_hnd) .is_err()); // Disconnect assert!(client.disconnect().is_ok()); @@ -767,11 +740,7 @@ mod tests { let mut dst_file_hnd: File = OpenOptions::new().read(true).open(dst_file.path()).unwrap(); // Receive file assert!(client - .recv_file( - PathBuf::from("readme.txt").as_path(), - &mut dst_file_hnd, - Some(Box::new(progress_callback)) - ) + .recv_file(PathBuf::from("readme.txt").as_path(), &mut dst_file_hnd) .is_err()); // Disconnect assert!(client.disconnect().is_ok()); @@ -794,8 +763,4 @@ mod tests { assert!(client.disconnect().is_ok()); } */ - - fn progress_callback(it: usize, max: usize) { - assert!(it <= max); - } } diff --git a/src/ui/activities/filetransfer_activity.rs b/src/ui/activities/filetransfer_activity.rs index 4fc3f34..3a2252e 100644 --- a/src/ui/activities/filetransfer_activity.rs +++ b/src/ui/activities/filetransfer_activity.rs @@ -34,7 +34,7 @@ use crate::filetransfer::FileTransferProtocol; // File transfer use crate::filetransfer::sftp_transfer::SftpFileTransfer; -use crate::filetransfer::{FileTransfer, ProgressCallback}; +use crate::filetransfer::FileTransfer; use crate::fs::FsEntry; // Includes @@ -55,9 +55,6 @@ use tui::{ }; use unicode_width::UnicodeWidthStr; -// Holder for current upload progress -static mut UPLOAD_PROGRESS: f64 = 0.0; // VERY VERY BAD CODING - // Types type DialogCallback = fn(&mut FileTransferActivity); type OnInputSubmitCallback = fn(&mut FileTransferActivity, String); @@ -315,22 +312,15 @@ impl FileTransferActivity { FsEntry::Directory(dir) => dir.name.clone(), FsEntry::File(file) => file.name.clone(), }; - self.input_mode = InputMode::Popup(PopupType::Progress(format!( - "Uploading \"{}\"...", - file_name - ))); + self.input_mode = + InputMode::Popup(PopupType::Wait(format!("Uploading \"{}\"...", file_name))); + // Draw + self.draw(); + // Get remote path let remote_path: PathBuf = match dst_name { Some(s) => PathBuf::from(s.as_str()), None => PathBuf::from(file_name.as_str()), }; - let prog_cb: Box = Box::new(|c, sz| { - // Progress callback - let percentage: f64 = ((c as f64) * 100.0) / (sz as f64); - unsafe { - UPLOAD_PROGRESS = percentage; - } - // FIXME: can't draw here... - }); // Match entry match entry { FsEntry::File(file) => { @@ -343,31 +333,26 @@ impl FileTransferActivity { .local .open_file_read(file.abs_path.as_path()) { - Ok(mut f) => { - match self - .client - .send_file(remote_path.as_path(), &mut f, Some(prog_cb)) - { - Ok(_) => self.log( - LogLevel::Info, - format!( - "Saved file \"{}\" to \"{}\"", - file.abs_path.display(), - remote_path.display() - ) - .as_ref(), - ), - Err(err) => self.log( - LogLevel::Error, - format!( - "Failed to upload file \"{}\": {}", - file.abs_path.display(), - err - ) - .as_ref(), - ), - } - } + Ok(mut f) => match self.client.send_file(remote_path.as_path(), &mut f) { + Ok(_) => self.log( + LogLevel::Info, + format!( + "Saved file \"{}\" to \"{}\"", + file.abs_path.display(), + remote_path.display() + ) + .as_ref(), + ), + Err(err) => self.log( + LogLevel::Error, + format!( + "Failed to upload file \"{}\": {}", + file.abs_path.display(), + err + ) + .as_ref(), + ), + }, Err(err) => { // Report error self.log( @@ -441,18 +426,10 @@ impl FileTransferActivity { FsEntry::Directory(dir) => dir.name.clone(), FsEntry::File(file) => file.name.clone(), }; - self.input_mode = InputMode::Popup(PopupType::Progress(format!( - "Downloading \"{}\"...", - file_name - ))); - let prog_cb: Box = Box::new(|c, sz| { - // Progress callback - let percentage: f64 = ((c as f64) * 100.0) / (sz as f64); - unsafe { - UPLOAD_PROGRESS = percentage; - } - // FIXME: can't draw here... - }); + self.input_mode = + InputMode::Popup(PopupType::Wait(format!("Downloading \"{}\"...", file_name))); + // Draw + self.draw(); // Match entry match entry { FsEntry::File(file) => { @@ -473,11 +450,10 @@ impl FileTransferActivity { { Ok(mut local_file) => { // Download file from remote - match self.client.recv_file( - file.abs_path.as_path(), - &mut local_file, - Some(prog_cb), - ) { + match self + .client + .recv_file(file.abs_path.as_path(), &mut local_file) + { Ok(_) => self.log( LogLevel::Info, format!(