From 65eaa44b0d464c917fdb4b3770a27e557aa2f04f Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 18 Oct 2021 07:42:53 -0700 Subject: [PATCH] ssh: split FileWrap out --- wezterm-ssh/src/filewrap.rs | 60 +++++++++++++++++++++++++++++++ wezterm-ssh/src/lib.rs | 1 + wezterm-ssh/src/session/inner.rs | 4 +-- wezterm-ssh/src/session/mod.rs | 61 +------------------------------- 4 files changed, 64 insertions(+), 62 deletions(-) create mode 100644 wezterm-ssh/src/filewrap.rs diff --git a/wezterm-ssh/src/filewrap.rs b/wezterm-ssh/src/filewrap.rs new file mode 100644 index 000000000..189d6a114 --- /dev/null +++ b/wezterm-ssh/src/filewrap.rs @@ -0,0 +1,60 @@ +use crate::sftp::{Metadata, SftpChannelError, SftpChannelResult}; +use camino::Utf8PathBuf; +use std::convert::TryFrom; + +pub(crate) enum FileWrap { + Ssh2(ssh2::File), +} + +impl FileWrap { + pub fn reader(&mut self) -> impl std::io::Read + '_ { + match self { + Self::Ssh2(file) => file, + } + } + + pub fn writer(&mut self) -> impl std::io::Write + '_ { + match self { + Self::Ssh2(file) => file, + } + } + + pub fn set_metadata(&mut self, metadata: Metadata) -> SftpChannelResult<()> { + match self { + Self::Ssh2(file) => file + .setstat(metadata.into()) + .map_err(SftpChannelError::from), + } + } + + pub fn metadata(&mut self) -> SftpChannelResult { + match self { + Self::Ssh2(file) => file + .stat() + .map(Metadata::from) + .map_err(SftpChannelError::from), + } + } + + pub fn read_dir(&mut self) -> SftpChannelResult<(Utf8PathBuf, Metadata)> { + match self { + Self::Ssh2(file) => { + file.readdir() + .map_err(SftpChannelError::from) + .and_then(|(path, stat)| match Utf8PathBuf::try_from(path) { + Ok(path) => Ok((path, Metadata::from(stat))), + Err(x) => Err(SftpChannelError::from(std::io::Error::new( + std::io::ErrorKind::InvalidData, + x, + ))), + }) + } + } + } + + pub fn fsync(&mut self) -> SftpChannelResult<()> { + match self { + Self::Ssh2(file) => file.fsync().map_err(SftpChannelError::from), + } + } +} diff --git a/wezterm-ssh/src/lib.rs b/wezterm-ssh/src/lib.rs index b2e991e95..41dd6e0ea 100644 --- a/wezterm-ssh/src/lib.rs +++ b/wezterm-ssh/src/lib.rs @@ -1,6 +1,7 @@ mod auth; mod channelwrap; mod config; +mod filewrap; mod host; mod pty; mod session; diff --git a/wezterm-ssh/src/session/inner.rs b/wezterm-ssh/src/session/inner.rs index e02460115..c0b57477f 100644 --- a/wezterm-ssh/src/session/inner.rs +++ b/wezterm-ssh/src/session/inner.rs @@ -1,8 +1,8 @@ use crate::config::ConfigMap; +use crate::filewrap::FileWrap; use crate::pty::*; use crate::session::{ - ChannelWrap, Exec, ExecResult, FileWrap, SessionEvent, SessionRequest, SessionWrap, - SignalChannel, + ChannelWrap, Exec, ExecResult, SessionEvent, SessionRequest, SessionWrap, SignalChannel, }; use crate::sftp::{ self, File, FileId, FileRequest, Metadata, SftpChannelError, SftpChannelResult, SftpRequest, diff --git a/wezterm-ssh/src/session/mod.rs b/wezterm-ssh/src/session/mod.rs index 08fa6583f..02c3a3221 100644 --- a/wezterm-ssh/src/session/mod.rs +++ b/wezterm-ssh/src/session/mod.rs @@ -4,8 +4,7 @@ use crate::config::ConfigMap; use crate::host::*; use crate::pty::*; pub(crate) use crate::session::inner::*; -use crate::sftp::{Metadata, Sftp, SftpChannelError, SftpChannelResult, SftpRequest}; -use camino::Utf8PathBuf; +use crate::sftp::{Sftp, SftpRequest}; use filedescriptor::{ socketpair, AsRawSocketDescriptor, FileDescriptor, SocketDescriptor, POLLIN, POLLOUT, }; @@ -14,7 +13,6 @@ use portable_pty::PtySize; use smol::channel::{bounded, Receiver, Sender}; use ssh2::BlockDirections; use std::collections::HashMap; -use std::convert::TryFrom; use std::io::Write; use std::sync::{Arc, Mutex}; @@ -76,63 +74,6 @@ pub(crate) struct Exec { pub reply: Sender, } -pub(crate) enum FileWrap { - Ssh2(ssh2::File), -} - -impl FileWrap { - pub fn reader(&mut self) -> impl std::io::Read + '_ { - match self { - Self::Ssh2(file) => file, - } - } - - pub fn writer(&mut self) -> impl std::io::Write + '_ { - match self { - Self::Ssh2(file) => file, - } - } - - pub fn set_metadata(&mut self, metadata: Metadata) -> SftpChannelResult<()> { - match self { - Self::Ssh2(file) => file - .setstat(metadata.into()) - .map_err(SftpChannelError::from), - } - } - - pub fn metadata(&mut self) -> SftpChannelResult { - match self { - Self::Ssh2(file) => file - .stat() - .map(Metadata::from) - .map_err(SftpChannelError::from), - } - } - - pub fn read_dir(&mut self) -> SftpChannelResult<(Utf8PathBuf, Metadata)> { - match self { - Self::Ssh2(file) => { - file.readdir() - .map_err(SftpChannelError::from) - .and_then(|(path, stat)| match Utf8PathBuf::try_from(path) { - Ok(path) => Ok((path, Metadata::from(stat))), - Err(x) => Err(SftpChannelError::from(std::io::Error::new( - std::io::ErrorKind::InvalidData, - x, - ))), - }) - } - } - } - - pub fn fsync(&mut self) -> SftpChannelResult<()> { - match self { - Self::Ssh2(file) => file.fsync().map_err(SftpChannelError::from), - } - } -} - pub(crate) struct Ssh2Session { sess: ssh2::Session, sftp: Option,