mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Removed progress callback (not a priority atm)
This commit is contained in:
@@ -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<String>, password: Option<String>) -> Result<(), FileTransferError>;
|
||||
fn connect(
|
||||
&mut self,
|
||||
address: String,
|
||||
port: u16,
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
) -> 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<PathBuf, FileTransferError>;
|
||||
|
||||
/// ### change_dir
|
||||
///
|
||||
///
|
||||
/// Change working directory
|
||||
|
||||
fn change_dir(&mut self, dir: &Path) -> Result<PathBuf, FileTransferError>;
|
||||
|
||||
/// ### list_dir
|
||||
///
|
||||
///
|
||||
/// List directory entries
|
||||
|
||||
fn list_dir(&self, path: &Path) -> Result<Vec<FsEntry>, 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<Box<ProgressCallback>>) -> 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<Box<ProgressCallback>>) -> Result<(), FileTransferError>;
|
||||
|
||||
fn recv_file(&self, file_name: &Path, dest_file: &mut File) -> Result<(), FileTransferError>;
|
||||
}
|
||||
|
||||
@@ -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<Box<ProgressCallback>>,
|
||||
) -> 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<Box<ProgressCallback>>,
|
||||
) -> 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user