From f31b0476718342c60a9b6834ef5cc76f471840c8 Mon Sep 17 00:00:00 2001 From: veeso Date: Sat, 15 May 2021 19:09:58 +0200 Subject: [PATCH] Copy command support for SFTP --- CHANGELOG.md | 1 + src/filetransfer/scp_transfer.rs | 2 +- src/filetransfer/sftp_transfer.rs | 41 +++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 567b2ba..e133471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Released on FIXME: ?? - Protocol input as first field in UI - Port is now updated to standard for selected protocol - when you change the protocol in the authentication form and the current port is standard (`< 1024`), the port will be automatically changed to default value for the selected protocol (e.g. current port: `123`, protocol is changes to `FTP`, port becomes `21`) + - Added **COPY** command to SFTP (Please note that Copy command is not supported by SFTP natively, so here it just uses the `cp` shell command as it does in SCP). - Bugfix: - Fixed wrong text wrap in log box - Fixed error message not being shown after an upload failure diff --git a/src/filetransfer/scp_transfer.rs b/src/filetransfer/scp_transfer.rs index fdc9d5e..8a7011f 100644 --- a/src/filetransfer/scp_transfer.rs +++ b/src/filetransfer/scp_transfer.rs @@ -729,7 +729,7 @@ impl FileTransfer for ScpFileTransfer { Some(p) => PathBuf::from(p), None => { return Err(FileTransferError::new_ex( - FileTransferErrorType::UnsupportedFeature, + FileTransferErrorType::DirStatFailed, String::from("Path has no parent"), )) } diff --git a/src/filetransfer/sftp_transfer.rs b/src/filetransfer/sftp_transfer.rs index 28dfdca..7e2b62f 100644 --- a/src/filetransfer/sftp_transfer.rs +++ b/src/filetransfer/sftp_transfer.rs @@ -437,11 +437,42 @@ impl FileTransfer for SftpFileTransfer { /// ### copy /// /// Copy file to destination - fn copy(&mut self, _src: &FsEntry, _dst: &Path) -> Result<(), FileTransferError> { - // SFTP doesn't support file copy - Err(FileTransferError::new( - FileTransferErrorType::UnsupportedFeature, - )) + fn copy(&mut self, src: &FsEntry, dst: &Path) -> Result<(), FileTransferError> { + // NOTE: use SCP command to perform copy (UNSAFE) + match self.is_connected() { + true => { + let dst: PathBuf = self.get_abs_path(dst); + // Run `cp -rf` + match self.perform_shell_cmd_with_path( + format!( + "cp -rf \"{}\" \"{}\"; echo $?", + src.get_abs_path().display(), + dst.display() + ) + .as_str(), + ) { + Ok(output) => + // Check if output is 0 + { + match output.as_str().trim() == "0" { + true => Ok(()), // File copied + false => Err(FileTransferError::new_ex( + // Could not copy file + FileTransferErrorType::FileCreateDenied, + format!("\"{}\"", dst.display()), + )), + } + } + Err(err) => Err(FileTransferError::new_ex( + FileTransferErrorType::ProtocolError, + err.to_string(), + )), + } + } + false => Err(FileTransferError::new( + FileTransferErrorType::UninitializedSession, + )), + } } /// ### list_dir