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:
@@ -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