ssh: stub out DirWrap::read_dir for libssh

This commit is contained in:
Wez Furlong committed 2021-10-19 20:33:30 -07:00
1 parent e3f5174dcc
commit 390fcc56ca
4 files changed
+90 -33

No files matched your search

+17
View File
@@ -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))
}
},
}
}
}
+19 -23
View File
@@ -105,34 +105,30 @@ impl TryFrom<i32> for SftpError {
}
}
mod ssh2_impl {
use super::*;
impl TryFrom<ssh2::Error> for SftpError {
type Error = ssh2::Error;
impl TryFrom<ssh2::Error> for SftpError {
type Error = ssh2::Error;
fn try_from(err: ssh2::Error) -> Result<Self, Self::Error> {
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<Self, Self::Error> {
match err.code() {
ssh2::ErrorCode::SFTP(x) => match Self::from_error_code(x) {
Some(err) => Ok(err),
None => Err(err),
},
_ => Err(err),
}
}
}
impl TryFrom<ssh2::ErrorCode> for SftpError {
type Error = ssh2::ErrorCode;
impl TryFrom<ssh2::ErrorCode> for SftpError {
type Error = ssh2::ErrorCode;
fn try_from(code: ssh2::ErrorCode) -> Result<Self, Self::Error> {
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<Self, Self::Error> {
match code {
ssh2::ErrorCode::SFTP(x) => match Self::from_error_code(x) {
Some(err) => Ok(err),
None => Err(code),
},
x => Err(x),
}
}
}
+16 -10
View File
@@ -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<SftpChannelResult<()>>,
}
mod ssh2_impl {
use super::*;
use std::convert::TryFrom;
impl From<ssh2::Error> 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<ssh2::Error> 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<libssh::Error> for SftpChannelError {
fn from(err: libssh::Error) -> Self {
Self::LibSsh(err)
}
}
+38
View File
@@ -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<libssh::FileType> 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<libssh::Metadata> 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()
}),
}
}
}