rename in filetransfer trait

This commit is contained in:
ChristianVisintin
2020-11-27 18:19:10 +01:00
parent d63dc2e2c8
commit 1557c068d5
2 changed files with 27 additions and 0 deletions

View File

@@ -132,6 +132,11 @@ pub trait FileTransfer {
/// Remove a file or a directory /// Remove a file or a directory
fn remove(&self, file: &FsEntry) -> Result<(), FileTransferError>; 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
/// ///
/// Send file to remote /// Send file to remote

View File

@@ -368,6 +368,28 @@ impl FileTransfer for SftpFileTransfer {
} }
} }
/// ### rename
///
/// Rename file or a directory
fn rename(&self, file: &FsEntry, dst: &Path) -> Result<(), FileTransferError> {
match self.sftp.as_ref() {
None => Err(FileTransferError::UninitializedSession),
Some(sftp) => {
// Resolve destination path
let abs_dst: PathBuf = self.get_abs_path(dst);
// 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()
};
match sftp.rename(abs_src.as_path(), abs_dst.as_path(), None) {
Ok(_) => Ok(()),
Err(_) => Err(FileTransferError::FileCreateDenied)
}
}
}
}
/// ### send_file /// ### send_file
/// ///
/// Send file to remote /// Send file to remote