diff --git a/wezterm-ssh/src/dirwrap.rs b/wezterm-ssh/src/dirwrap.rs index 5c324d3a9..0716dd09a 100644 --- a/wezterm-ssh/src/dirwrap.rs +++ b/wezterm-ssh/src/dirwrap.rs @@ -1,10 +1,12 @@ use crate::sftp::types::Metadata; use crate::sftp::{SftpChannelError, SftpChannelResult}; use camino::Utf8PathBuf; +use libssh_rs as libssh; use std::convert::TryFrom; pub(crate) enum DirWrap { Ssh2(ssh2::File), + LibSsh(libssh::SftpDir), } impl DirWrap { @@ -21,6 +23,21 @@ impl DirWrap { ))), }) } + Self::LibSsh(dir) => match dir.read_dir() { + None => Err(SftpChannelError::from(std::io::Error::new( + std::io::ErrorKind::UnexpectedEof, + "no more files", + ))), + Some(Err(err)) => Err(SftpChannelError::from(err)), + Some(Ok(metadata)) => { + let path: Utf8PathBuf = metadata + .name() + .expect("name to be present in read_dir") + .into(); + let md: Metadata = metadata.into(); + Ok((path, md)) + } + }, } } } diff --git a/wezterm-ssh/src/sftp/error.rs b/wezterm-ssh/src/sftp/error.rs index e830e5f43..96693b97b 100644 --- a/wezterm-ssh/src/sftp/error.rs +++ b/wezterm-ssh/src/sftp/error.rs @@ -105,34 +105,30 @@ impl TryFrom for SftpError { } } -mod ssh2_impl { - use super::*; +impl TryFrom for SftpError { + type Error = ssh2::Error; - impl TryFrom for SftpError { - type Error = ssh2::Error; - - fn try_from(err: ssh2::Error) -> Result { - match err.code() { - ssh2::ErrorCode::SFTP(x) => match Self::from_error_code(x) { - Some(err) => Ok(err), - None => Err(err), - }, - _ => Err(err), - } + fn try_from(err: ssh2::Error) -> Result { + match err.code() { + ssh2::ErrorCode::SFTP(x) => match Self::from_error_code(x) { + Some(err) => Ok(err), + None => Err(err), + }, + _ => Err(err), } } +} - impl TryFrom for SftpError { - type Error = ssh2::ErrorCode; +impl TryFrom for SftpError { + type Error = ssh2::ErrorCode; - fn try_from(code: ssh2::ErrorCode) -> Result { - match code { - ssh2::ErrorCode::SFTP(x) => match Self::from_error_code(x) { - Some(err) => Ok(err), - None => Err(code), - }, - x => Err(x), - } + fn try_from(code: ssh2::ErrorCode) -> Result { + match code { + ssh2::ErrorCode::SFTP(x) => match Self::from_error_code(x) { + Some(err) => Ok(err), + None => Err(code), + }, + x => Err(x), } } } diff --git a/wezterm-ssh/src/sftp/mod.rs b/wezterm-ssh/src/sftp/mod.rs index c3e053d81..4245c80b2 100644 --- a/wezterm-ssh/src/sftp/mod.rs +++ b/wezterm-ssh/src/sftp/mod.rs @@ -4,7 +4,9 @@ use crate::sftp::file::{File, FileRequest}; use crate::sftp::types::{Metadata, OpenFileType, OpenOptions, RenameOptions, WriteMode}; use camino::Utf8PathBuf; use error::SftpError; +use libssh_rs as libssh; use smol::channel::{bounded, RecvError, Sender}; +use std::convert::TryFrom; use std::convert::TryInto; use std::io; use thiserror::Error; @@ -42,6 +44,9 @@ pub enum SftpChannelError { #[error("Library-specific error: {}", .0)] Ssh2(#[source] ssh2::Error), + #[error("Library-specific error: {}", .0)] + LibSsh(#[source] libssh::Error), + #[error("Not Implemented")] NotImplemented, } @@ -464,16 +469,17 @@ pub(crate) struct RemoveFile { pub reply: Sender>, } -mod ssh2_impl { - use super::*; - use std::convert::TryFrom; - - impl From for SftpChannelError { - fn from(err: ssh2::Error) -> Self { - match SftpError::try_from(err) { - Ok(x) => Self::Sftp(x), - Err(x) => Self::Ssh2(x), - } +impl From for SftpChannelError { + fn from(err: ssh2::Error) -> Self { + match SftpError::try_from(err) { + Ok(x) => Self::Sftp(x), + Err(x) => Self::Ssh2(x), } } } + +impl From for SftpChannelError { + fn from(err: libssh::Error) -> Self { + Self::LibSsh(err) + } +} diff --git a/wezterm-ssh/src/sftp/types.rs b/wezterm-ssh/src/sftp/types.rs index 5239f4ddc..15a7a1178 100644 --- a/wezterm-ssh/src/sftp/types.rs +++ b/wezterm-ssh/src/sftp/types.rs @@ -1,4 +1,6 @@ use bitflags::bitflags; +use libssh_rs as libssh; +use std::time::SystemTime; bitflags! { struct FileTypeFlags: u32 { @@ -348,3 +350,39 @@ mod ssh2_impl { } } } + +impl From for FileType { + fn from(ft: libssh::FileType) -> Self { + match ft { + libssh::FileType::Directory => Self::Dir, + libssh::FileType::Regular => Self::File, + libssh::FileType::Symlink => Self::Symlink, + _ => Self::Other, + } + } +} + +impl From for Metadata { + fn from(stat: libssh::Metadata) -> Self { + Self { + ty: stat + .file_type() + .map(FileType::from) + .unwrap_or(FileType::Other), + permissions: stat.permissions().map(FilePermissions::from_unix_mode), + size: stat.len(), + uid: stat.uid(), + gid: stat.gid(), + accessed: stat.accessed().map(|t| { + t.duration_since(SystemTime::UNIX_EPOCH) + .expect("UNIX_EPOCH < SystemTime") + .as_secs() + }), + modified: stat.modified().map(|t| { + t.duration_since(SystemTime::UNIX_EPOCH) + .expect("UNIX_EPOCH < SystemTime") + .as_secs() + }), + } + } +}