pub struct FtpStream { /* private fields */ }
Expand description
Stream to interface with the FTP server. This interface is only for the command stream.
Implementations§
Source§impl FtpStream
impl FtpStream
Sourcepub async fn connect<A: ToSocketAddrs>(addr: A) -> Result<FtpStream>
pub async fn connect<A: ToSocketAddrs>(addr: A) -> Result<FtpStream>
Creates an FTP Stream.
Sourcepub fn get_ref(&self) -> &TcpStream
pub fn get_ref(&self) -> &TcpStream
Returns a reference to the underlying TcpStream.
Example:
use tokio::net::TcpStream;
use std::time::Duration;
use async_ftp::FtpStream;
async {
let stream = FtpStream::connect("172.25.82.139:21").await
.expect("Couldn't connect to the server...");
let s: &TcpStream = stream.get_ref();
};
Sourcepub fn get_welcome_msg(&self) -> Option<&str>
pub fn get_welcome_msg(&self) -> Option<&str>
Get welcome message from the server on connect.
Sourcepub async fn login(&mut self, user: &str, password: &str) -> Result<()>
pub async fn login(&mut self, user: &str, password: &str) -> Result<()>
Log in to the FTP server.
Sourcepub async fn cwd(&mut self, path: &str) -> Result<()>
pub async fn cwd(&mut self, path: &str) -> Result<()>
Change the current directory to the path specified.
Sourcepub async fn noop(&mut self) -> Result<()>
pub async fn noop(&mut self) -> Result<()>
This does nothing. This is usually just used to keep the connection open.
Sourcepub async fn mkdir(&mut self, pathname: &str) -> Result<()>
pub async fn mkdir(&mut self, pathname: &str) -> Result<()>
This creates a new directory on the server.
Sourcepub async fn transfer_type(&mut self, file_type: FileType) -> Result<()>
pub async fn transfer_type(&mut self, file_type: FileType) -> Result<()>
Sets the type of file to be transferred. That is the implementation
of TYPE
command.
Sourcepub async fn restart_from(&mut self, offset: u64) -> Result<()>
pub async fn restart_from(&mut self, offset: u64) -> Result<()>
Sets the byte from which the transfer is to be restarted.
Sourcepub async fn get(&mut self, file_name: &str) -> Result<BufReader<DataStream>>
pub async fn get(&mut self, file_name: &str) -> Result<BufReader<DataStream>>
Retrieves the file name specified from the server. This method is a more complicated way to retrieve a file. The reader returned should be dropped. Also you will have to read the response to make sure it has the correct value.
Sourcepub async fn rename(&mut self, from_name: &str, to_name: &str) -> Result<()>
pub async fn rename(&mut self, from_name: &str, to_name: &str) -> Result<()>
Renames the file from_name to to_name
Sourcepub async fn retr<F, T, P, E>(
&mut self,
filename: &str,
reader: F,
) -> Result<T, E>
pub async fn retr<F, T, P, E>( &mut self, filename: &str, reader: F, ) -> Result<T, E>
The implementation of RETR
command where filename
is the name of the file
to download from FTP and reader
is the function which operates with the
data stream opened.
use async_ftp::{FtpStream, DataStream, FtpError};
use tokio::io::{AsyncReadExt, BufReader};
use std::io::Cursor;
async {
let mut conn = FtpStream::connect("172.25.82.139:21").await.unwrap();
conn.login("Doe", "mumble").await.unwrap();
let mut reader = Cursor::new("hello, world!".as_bytes());
conn.put("retr.txt", &mut reader).await.unwrap();
async fn lambda(mut reader: BufReader<DataStream>) -> Result<Vec<u8>, FtpError> {
let mut buffer = Vec::new();
reader
.read_to_end(&mut buffer)
.await
.map_err(FtpError::ConnectionError)?;
assert_eq!(buffer, "hello, world!".as_bytes());
Ok(buffer)
};
assert!(conn.retr("retr.txt", lambda).await.is_ok());
assert!(conn.rm("retr.txt").await.is_ok());
};
Sourcepub async fn simple_retr(&mut self, file_name: &str) -> Result<Cursor<Vec<u8>>>
pub async fn simple_retr(&mut self, file_name: &str) -> Result<Cursor<Vec<u8>>>
Simple way to retr a file from the server. This stores the file in memory.
use async_ftp::{FtpStream, FtpError};
use std::io::Cursor;
async {
let mut conn = FtpStream::connect("172.25.82.139:21").await?;
conn.login("Doe", "mumble").await?;
let mut reader = Cursor::new("hello, world!".as_bytes());
conn.put("simple_retr.txt", &mut reader).await?;
let cursor = conn.simple_retr("simple_retr.txt").await?;
assert_eq!(cursor.into_inner(), "hello, world!".as_bytes());
assert!(conn.rm("simple_retr.txt").await.is_ok());
Ok::<(), FtpError>(())
};
Sourcepub async fn rmdir(&mut self, pathname: &str) -> Result<()>
pub async fn rmdir(&mut self, pathname: &str) -> Result<()>
Removes the remote pathname from the server.
Sourcepub async fn rm(&mut self, filename: &str) -> Result<()>
pub async fn rm(&mut self, filename: &str) -> Result<()>
Remove the remote file from the server.
Sourcepub async fn put<R: AsyncRead + Unpin>(
&mut self,
filename: &str,
r: &mut R,
) -> Result<()>
pub async fn put<R: AsyncRead + Unpin>( &mut self, filename: &str, r: &mut R, ) -> Result<()>
This stores a file on the server.
Sourcepub async fn list(&mut self, pathname: Option<&str>) -> Result<Vec<String>>
pub async fn list(&mut self, pathname: Option<&str>) -> Result<Vec<String>>
Execute LIST
command which returns the detailed file listing in human readable format.
If pathname
is omited then the list of files in the current directory will be
returned otherwise it will the list of files on pathname
.
Sourcepub async fn nlst(&mut self, pathname: Option<&str>) -> Result<Vec<String>>
pub async fn nlst(&mut self, pathname: Option<&str>) -> Result<Vec<String>>
Execute NLST
command which returns the list of file names only.
If pathname
is omited then the list of files in the current directory will be
returned otherwise it will the list of files on pathname
.
Sourcepub async fn mdtm(&mut self, pathname: &str) -> Result<Option<DateTime<Utc>>>
pub async fn mdtm(&mut self, pathname: &str) -> Result<Option<DateTime<Utc>>>
Retrieves the modification time of the file at pathname
if it exists.
In case the file does not exist None
is returned.
Sourcepub async fn size(&mut self, pathname: &str) -> Result<Option<usize>>
pub async fn size(&mut self, pathname: &str) -> Result<Option<usize>>
Retrieves the size of the file in bytes at pathname
if it exists.
In case the file does not exist None
is returned.
pub async fn read_response(&mut self, expected_code: u32) -> Result<Line>
Sourcepub async fn read_response_in(&mut self, expected_code: &[u32]) -> Result<Line>
pub async fn read_response_in(&mut self, expected_code: &[u32]) -> Result<Line>
Retrieve single line response