mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Merged 0.6.1; added new DirectoryAlreadyExists; return new variant for SFTP/SCP
This commit is contained in:
@@ -189,7 +189,7 @@ impl FtpFileTransfer {
|
||||
FsEntry::Directory(FsDirectory {
|
||||
name: p
|
||||
.file_name()
|
||||
.unwrap_or(&std::ffi::OsStr::new(""))
|
||||
.unwrap_or_else(|| std::ffi::OsStr::new(""))
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
abs_path: p.clone(),
|
||||
@@ -206,7 +206,7 @@ impl FtpFileTransfer {
|
||||
false => FsEntry::File(FsFile {
|
||||
name: p
|
||||
.file_name()
|
||||
.unwrap_or(&std::ffi::OsStr::new(""))
|
||||
.unwrap_or_else(|| std::ffi::OsStr::new(""))
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
abs_path: p.clone(),
|
||||
@@ -659,7 +659,7 @@ impl FileTransfer for FtpFileTransfer {
|
||||
// Remove recursively files
|
||||
debug!("Removing {} entries from directory...", files.len());
|
||||
for file in files.iter() {
|
||||
if let Err(err) = self.remove(&file) {
|
||||
if let Err(err) = self.remove(file) {
|
||||
return Err(FileTransferError::new_ex(
|
||||
FileTransferErrorType::PexError,
|
||||
err.to_string(),
|
||||
|
||||
@@ -44,7 +44,7 @@ pub use params::FileTransferParams;
|
||||
///
|
||||
/// This enum defines the different transfer protocol available in termscp
|
||||
|
||||
#[derive(PartialEq, std::fmt::Debug, std::clone::Clone, Copy)]
|
||||
#[derive(PartialEq, Debug, std::clone::Clone, Copy)]
|
||||
pub enum FileTransferProtocol {
|
||||
Sftp,
|
||||
Scp,
|
||||
@@ -54,7 +54,7 @@ pub enum FileTransferProtocol {
|
||||
/// ## FileTransferError
|
||||
///
|
||||
/// FileTransferError defines the possible errors available for a file transfer
|
||||
#[derive(std::fmt::Debug)]
|
||||
#[derive(Debug)]
|
||||
pub struct FileTransferError {
|
||||
code: FileTransferErrorType,
|
||||
msg: Option<String>,
|
||||
@@ -84,6 +84,8 @@ pub enum FileTransferErrorType {
|
||||
SslError,
|
||||
#[error("Could not stat directory")]
|
||||
DirStatFailed,
|
||||
#[error("Directory already exists")]
|
||||
DirectoryAlreadyExists,
|
||||
#[error("Failed to create file")]
|
||||
FileCreateDenied,
|
||||
#[error("No such file or directory")]
|
||||
@@ -180,7 +182,7 @@ pub trait FileTransfer {
|
||||
/// ### mkdir
|
||||
///
|
||||
/// Make directory
|
||||
/// You must return error in case the directory already exists
|
||||
/// It MUSTN'T return error in case the directory already exists
|
||||
fn mkdir(&mut self, dir: &Path) -> Result<(), FileTransferError>;
|
||||
|
||||
/// ### remove
|
||||
|
||||
@@ -169,7 +169,7 @@ impl ScpFileTransfer {
|
||||
// Get symlink; PATH mustn't be equal to filename
|
||||
let symlink: Option<Box<FsEntry>> = match symlink_path {
|
||||
None => None,
|
||||
Some(p) => match p.file_name().unwrap_or(&std::ffi::OsStr::new(""))
|
||||
Some(p) => match p.file_name().unwrap_or_else(|| std::ffi::OsStr::new(""))
|
||||
== file_name.as_str()
|
||||
{
|
||||
// If name is equal, don't stat path; otherwise it would get stuck
|
||||
@@ -339,7 +339,7 @@ impl FileTransfer for ScpFileTransfer {
|
||||
// Try addresses
|
||||
for socket_addr in socket_addresses.iter() {
|
||||
debug!("Trying socket address {}", socket_addr);
|
||||
match TcpStream::connect_timeout(&socket_addr, Duration::from_secs(30)) {
|
||||
match TcpStream::connect_timeout(socket_addr, Duration::from_secs(30)) {
|
||||
Ok(stream) => {
|
||||
debug!("{} succeded", socket_addr);
|
||||
tcp = Some(stream);
|
||||
@@ -643,13 +643,20 @@ impl FileTransfer for ScpFileTransfer {
|
||||
/// ### mkdir
|
||||
///
|
||||
/// Make directory
|
||||
/// You must return error in case the directory already exists
|
||||
/// It MUSTN'T return error in case the directory already exists
|
||||
fn mkdir(&mut self, dir: &Path) -> Result<(), FileTransferError> {
|
||||
match self.is_connected() {
|
||||
true => {
|
||||
let dir: PathBuf = Self::resolve(dir);
|
||||
info!("Making directory {}", dir.display());
|
||||
let p: PathBuf = self.wrkdir.clone();
|
||||
// If directory already exists, return Err
|
||||
if let Ok(_) = self.stat(dir.as_path()) {
|
||||
error!("Directory {} already exists", dir.display());
|
||||
return Err(FileTransferError::new(
|
||||
FileTransferErrorType::DirectoryAlreadyExists,
|
||||
));
|
||||
}
|
||||
// Mkdir dir && echo 0
|
||||
match self.perform_shell_cmd_with_path(
|
||||
p.as_path(),
|
||||
|
||||
@@ -282,7 +282,7 @@ impl FileTransfer for SftpFileTransfer {
|
||||
// Try addresses
|
||||
for socket_addr in socket_addresses.iter() {
|
||||
debug!("Trying socket address {}", socket_addr);
|
||||
match TcpStream::connect_timeout(&socket_addr, Duration::from_secs(30)) {
|
||||
match TcpStream::connect_timeout(socket_addr, Duration::from_secs(30)) {
|
||||
Ok(stream) => {
|
||||
tcp = Some(stream);
|
||||
break;
|
||||
@@ -559,6 +559,13 @@ impl FileTransfer for SftpFileTransfer {
|
||||
Some(sftp) => {
|
||||
// Make directory
|
||||
let path: PathBuf = self.get_abs_path(PathBuf::from(dir).as_path());
|
||||
// If directory already exists, return Err
|
||||
if let Ok(_) = sftp.stat(path.as_path()) {
|
||||
error!("Directory {} already exists", path.display());
|
||||
return Err(FileTransferError::new(
|
||||
FileTransferErrorType::DirectoryAlreadyExists,
|
||||
));
|
||||
}
|
||||
info!("Making directory {}", path.display());
|
||||
match sftp.mkdir(path.as_path(), 0o775) {
|
||||
Ok(_) => Ok(()),
|
||||
@@ -602,7 +609,7 @@ impl FileTransfer for SftpFileTransfer {
|
||||
// Get directory files
|
||||
let directory_content: Vec<FsEntry> = self.list_dir(d.abs_path.as_path())?;
|
||||
for entry in directory_content.iter() {
|
||||
if let Err(err) = self.remove(&entry) {
|
||||
if let Err(err) = self.remove(entry) {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user