Struct openssh::Sftp[][src]

pub struct Sftp<'s> { /* fields omitted */ }
Expand description

A file-oriented channel to a remote host.

You likely want Sftp::write_to and Sftp::read_from.

Implementations

Check that the given file can be opened in the given mode.

This method does not change the remote file system, except where opening a file for read/write alone causes changes (like the “last accessed” timestamp).

Note that this function is potentially racy. The permissions on the server could change between when you check and when you start a subsequent operation, causing it to fail. It can also produce false positives, such as if you are checking whether you can write to a file on a read-only file system that you still have write permissions on.

Operations like read_from and write_to internally perform similar checking to this method, so you do not need to call can before calling those methods. The checking performed by write_to can also test its permissions by actually attemping to create the remote file (since it is about to create one anyway), so its checking is more reliable than what can can provide.

Open the remote file at path for writing.

If the remote file exists, it will be truncated. If it does not, it will be created.

Note that some errors may not propagate until you call close. This method internally performs similar checks to can though, so you should not need to call can before calling this method.

Examples

use std::io::prelude::*;

// connect to a remote host and get an sftp connection
let session = Session::connect("host", KnownHosts::Strict).await?;
let mut sftp = session.sftp();

// open a file for writing
let mut w = sftp.write_to("test_file").await?;

// write something to the file
use tokio::io::AsyncWriteExt;
w.write_all(b"hello world").await?;

// flush and close the remote file, absorbing any final errors
w.close().await?;

Open the remote file at path for appending.

If the remote file exists, it will be appended to. If it does not, it will be created.

Note that some errors may not propagate until you call close. This method internally performs similar checks to can though, so you should not need to call can before calling this method.

Examples

use std::io::prelude::*;

// connect to a remote host and get an sftp connection
let session = Session::connect("host", KnownHosts::Strict).await?;
let mut sftp = session.sftp();

// open a file for appending
let mut w = sftp.append_to("test_file").await?;

// write will append to the file
use tokio::io::AsyncWriteExt;
w.write_all(b"hello world").await?;

// flush and close the remote file, absorbing any final errors
w.close().await?;

Open the remote file at path for reading.

Note that some errors may not propagate until you call close. This method internally performs similar checks to can though, so you should not need to call can before calling this method.

Examples

use std::io::prelude::*;

// connect to a remote host and get an sftp connection
let session = Session::connect("host", KnownHosts::Strict).await?;
let mut sftp = session.sftp();

// open a file for reading
let mut r = sftp.read_from("/etc/hostname").await?;

// write something to the file
use tokio::io::AsyncReadExt;
let mut contents = String::new();
r.read_to_string(&mut contents).await?;

// close the remote file, absorbing any final errors
r.close().await?;

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.