openssh_sftp_client

Struct Sftp

Source
pub struct Sftp { /* private fields */ }
Expand description

A file-oriented channel to a remote host.

Implementations§

Source§

impl Sftp

Source

pub async fn from_session( session: Session, options: SftpOptions, ) -> Result<Self, Error>

Available on crate feature openssh only.

Create Sftp from openssh::Session.

Calling Sftp::close on sftp instances created using this function would also await on openssh::RemoteChild::wait and openssh::Session::close and propagate their error in Sftp::close.

Source

pub async fn from_session_with_check_connection( session: Session, options: SftpOptions, check_openssh_connection: impl CheckOpensshConnection + Send + Sync + 'static, ) -> Result<Self, Error>

Available on crate feature openssh only.

Similar to Sftp::from_session, but takes an additional parameter for checking if the connection is still alive.

§Example
fn check_connection<'session>(
    session: &'session openssh::Session,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), openssh::Error>> + Send + Sync + 'session>> {
    Box::pin(async move {
        loop {
            tokio::time::sleep(std::time::Duration::from_secs(10)).await;
            session.check().await?;
        }
        Ok(())
    })
}

openssh_sftp_client::Sftp::from_session_with_check_connection(
    openssh::Session::connect_mux("me@ssh.example.com", openssh::KnownHosts::Strict).await?,
    openssh_sftp_client::SftpOptions::default(),
    check_connection,
).await?;
Source

pub async fn from_clonable_session( session: impl Deref<Target = Session> + Clone + Debug + Send + Sync + 'static, options: SftpOptions, ) -> Result<Self, Error>

Available on crate feature openssh only.

Create Sftp from any type that can be dereferenced to openssh::Session and is clonable.

Source

pub async fn from_clonable_session_with_check_connection( session: impl Deref<Target = Session> + Clone + Debug + Send + Sync + 'static, options: SftpOptions, check_openssh_connection: impl CheckOpensshConnection + Send + Sync + 'static, ) -> Result<Self, Error>

Available on crate feature openssh only.

Similar to Sftp::from_session_with_check_connection, but takes an additional parameter for checking if the connection is still alive.

§Example
fn check_connection<'session>(
    session: &'session openssh::Session,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), openssh::Error>> + Send + Sync + 'session>> {
    Box::pin(async move {
        loop {
            tokio::time::sleep(std::time::Duration::from_secs(10)).await;
            session.check().await?;
        }
        Ok(())
    })
}

openssh_sftp_client::Sftp::from_clonable_session_with_check_connection(
    std::sync::Arc::new(openssh::Session::connect_mux("me@ssh.example.com", openssh::KnownHosts::Strict).await?),
    openssh_sftp_client::SftpOptions::default(),
    check_connection,
).await?;
Source§

impl Sftp

Source

pub async fn new<W: AsyncWrite + Send + 'static, R: AsyncRead + Send + 'static>( stdin: W, stdout: R, options: SftpOptions, ) -> Result<Self, Error>

Create Sftp.

Source

pub async fn new_with_auxiliary<W: AsyncWrite + Send + 'static, R: AsyncRead + Send + 'static>( stdin: W, stdout: R, options: SftpOptions, auxiliary: SftpAuxiliaryData, ) -> Result<Self, Error>

Create Sftp with some auxiliary data.

The auxiliary data will be dropped after all sftp requests has been sent(flush_task), all responses processed (read_task) and Sftp has been dropped.

If you want to get back the data, you can simply use SftpAuxiliaryData::Arced and then stores an Arc elsewhere.

Once the sftp tasks is completed and Sftp is dropped, you can call Arc::try_unwrap to get back the exclusive ownership of it.

Source

pub async fn close(self) -> Result<(), Error>

Close sftp connection

If sftp is created using Sftp::from_session, then calling this function would also await on openssh::RemoteChild::wait and openssh::Session::close and propagate their error in Sftp::close.

Source

pub fn options(&self) -> OpenOptions

Return a new OpenOptions object.

Source

pub async fn create(&self, path: impl AsRef<Path>) -> Result<File, Error>

Opens a file in write-only mode.

This function will create a file if it does not exist, and will truncate it if it does.

Source

pub async fn open(&self, path: impl AsRef<Path>) -> Result<File, Error>

Attempts to open a file in read-only mode.

Source

pub fn fs(&self) -> Fs

Fs defaults to the current working dir set by remote sftp-server, which usually is the home directory.

Source

pub fn support_expand_path(&self) -> bool

Check if the remote server supports the expand path extension.

If it returns true, then Fs::canonicalize with expand path is supported.

Source

pub fn support_fsync(&self) -> bool

Check if the remote server supports the fsync extension.

If it returns true, then File::sync_all is supported.

Check if the remote server supports the hardlink extension.

If it returns true, then Fs::hard_link is supported.

Source

pub fn support_posix_rename(&self) -> bool

Check if the remote server supports the posix rename extension.

If it returns true, then Fs::rename will use posix rename.

Source

pub fn support_copy(&self) -> bool

Check if the remote server supports the copy data extension.

If it returns true, then File::copy_to and File::copy_all_to are supported.

Trait Implementations§

Source§

impl Debug for Sftp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Sftp

§

impl !RefUnwindSafe for Sftp

§

impl Send for Sftp

§

impl Sync for Sftp

§

impl Unpin for Sftp

§

impl !UnwindSafe for Sftp

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more