Struct FtpStream

Source
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

Source

pub fn connect<A: ToSocketAddrs>(addr: A) -> Result<FtpStream>

Creates an FTP Stream.

Examples found in repository?
examples/connecting.rs (line 8)
7fn test_ftp(addr: &str, user: &str, pass: &str) -> Result<(), FtpError> {
8    let mut ftp_stream = FtpStream::connect((addr, 21)).unwrap();
9    ftp_stream.login(user, pass).unwrap();
10    println!("current dir: {}", ftp_stream.pwd().unwrap());
11
12    ftp_stream.cwd("test_data").unwrap();
13
14    // An easy way to retrieve a file
15    let cursor = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
16    let vec = cursor.into_inner();
17    let text = str::from_utf8(&vec).unwrap();
18    println!("got data: {}", text);
19
20    // Store a file
21    let file_data = format!("Some awesome file data man!!");
22    let mut reader = Cursor::new(file_data.into_bytes());
23    ftp_stream.put("my_random_file.txt", &mut reader).unwrap();
24
25    ftp_stream.quit()
26}
Source

pub fn get_ref(&self) -> &TcpStream

Returns a reference to the underlying TcpStream.

Example:

use std::net::TcpStream;

let stream = FtpStream::connect("127.0.0.1:21")
                       .expect("Couldn't connect to the server...");
stream.get_ref().set_read_timeout(Duration::from_secs(10))
                .expect("set_read_timeout call failed");
Source

pub fn login(&mut self, user: &str, password: &str) -> Result<()>

Log in to the FTP server.

Examples found in repository?
examples/connecting.rs (line 9)
7fn test_ftp(addr: &str, user: &str, pass: &str) -> Result<(), FtpError> {
8    let mut ftp_stream = FtpStream::connect((addr, 21)).unwrap();
9    ftp_stream.login(user, pass).unwrap();
10    println!("current dir: {}", ftp_stream.pwd().unwrap());
11
12    ftp_stream.cwd("test_data").unwrap();
13
14    // An easy way to retrieve a file
15    let cursor = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
16    let vec = cursor.into_inner();
17    let text = str::from_utf8(&vec).unwrap();
18    println!("got data: {}", text);
19
20    // Store a file
21    let file_data = format!("Some awesome file data man!!");
22    let mut reader = Cursor::new(file_data.into_bytes());
23    ftp_stream.put("my_random_file.txt", &mut reader).unwrap();
24
25    ftp_stream.quit()
26}
Source

pub fn cwd(&mut self, path: &str) -> Result<()>

Change the current directory to the path specified.

Examples found in repository?
examples/connecting.rs (line 12)
7fn test_ftp(addr: &str, user: &str, pass: &str) -> Result<(), FtpError> {
8    let mut ftp_stream = FtpStream::connect((addr, 21)).unwrap();
9    ftp_stream.login(user, pass).unwrap();
10    println!("current dir: {}", ftp_stream.pwd().unwrap());
11
12    ftp_stream.cwd("test_data").unwrap();
13
14    // An easy way to retrieve a file
15    let cursor = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
16    let vec = cursor.into_inner();
17    let text = str::from_utf8(&vec).unwrap();
18    println!("got data: {}", text);
19
20    // Store a file
21    let file_data = format!("Some awesome file data man!!");
22    let mut reader = Cursor::new(file_data.into_bytes());
23    ftp_stream.put("my_random_file.txt", &mut reader).unwrap();
24
25    ftp_stream.quit()
26}
Source

pub fn cdup(&mut self) -> Result<()>

Move the current directory to the parent directory.

Source

pub fn pwd(&mut self) -> Result<String>

Gets the current directory

Examples found in repository?
examples/connecting.rs (line 10)
7fn test_ftp(addr: &str, user: &str, pass: &str) -> Result<(), FtpError> {
8    let mut ftp_stream = FtpStream::connect((addr, 21)).unwrap();
9    ftp_stream.login(user, pass).unwrap();
10    println!("current dir: {}", ftp_stream.pwd().unwrap());
11
12    ftp_stream.cwd("test_data").unwrap();
13
14    // An easy way to retrieve a file
15    let cursor = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
16    let vec = cursor.into_inner();
17    let text = str::from_utf8(&vec).unwrap();
18    println!("got data: {}", text);
19
20    // Store a file
21    let file_data = format!("Some awesome file data man!!");
22    let mut reader = Cursor::new(file_data.into_bytes());
23    ftp_stream.put("my_random_file.txt", &mut reader).unwrap();
24
25    ftp_stream.quit()
26}
Source

pub fn noop(&mut self) -> Result<()>

This does nothing. This is usually just used to keep the connection open.

Source

pub fn mkdir(&mut self, pathname: &str) -> Result<()>

This creates a new directory on the server.

Source

pub fn transfer_type(&mut self, file_type: FileType) -> Result<()>

Sets the type of file to be transferred. That is the implementation of TYPE command.

Source

pub fn quit(&mut self) -> Result<()>

Quits the current FTP session.

Examples found in repository?
examples/connecting.rs (line 25)
7fn test_ftp(addr: &str, user: &str, pass: &str) -> Result<(), FtpError> {
8    let mut ftp_stream = FtpStream::connect((addr, 21)).unwrap();
9    ftp_stream.login(user, pass).unwrap();
10    println!("current dir: {}", ftp_stream.pwd().unwrap());
11
12    ftp_stream.cwd("test_data").unwrap();
13
14    // An easy way to retrieve a file
15    let cursor = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
16    let vec = cursor.into_inner();
17    let text = str::from_utf8(&vec).unwrap();
18    println!("got data: {}", text);
19
20    // Store a file
21    let file_data = format!("Some awesome file data man!!");
22    let mut reader = Cursor::new(file_data.into_bytes());
23    ftp_stream.put("my_random_file.txt", &mut reader).unwrap();
24
25    ftp_stream.quit()
26}
Source

pub 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.

Source

pub fn rename(&mut self, from_name: &str, to_name: &str) -> Result<()>

Renames the file from_name to to_name

Source

pub fn retr<F, T>(&mut self, filename: &str, reader: F) -> Result<T>
where F: Fn(&mut dyn Read) -> Result<T>,

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.

assert!(conn.retr("retr.txt", |stream| {
    let mut buf = Vec::new();
    stream.read_to_end(&mut buf).map(|_|
        assert_eq!(buf, "hello, world!".as_bytes())
    ).map_err(|e| FtpError::ConnectionError(e))
}).is_ok());
Source

pub 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.

let cursor = conn.simple_retr("simple_retr.txt").unwrap();
// do something with bytes
assert_eq!(cursor.into_inner(), "hello, world!".as_bytes());
Examples found in repository?
examples/connecting.rs (line 15)
7fn test_ftp(addr: &str, user: &str, pass: &str) -> Result<(), FtpError> {
8    let mut ftp_stream = FtpStream::connect((addr, 21)).unwrap();
9    ftp_stream.login(user, pass).unwrap();
10    println!("current dir: {}", ftp_stream.pwd().unwrap());
11
12    ftp_stream.cwd("test_data").unwrap();
13
14    // An easy way to retrieve a file
15    let cursor = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
16    let vec = cursor.into_inner();
17    let text = str::from_utf8(&vec).unwrap();
18    println!("got data: {}", text);
19
20    // Store a file
21    let file_data = format!("Some awesome file data man!!");
22    let mut reader = Cursor::new(file_data.into_bytes());
23    ftp_stream.put("my_random_file.txt", &mut reader).unwrap();
24
25    ftp_stream.quit()
26}
Source

pub fn rmdir(&mut self, pathname: &str) -> Result<()>

Removes the remote pathname from the server.

Source

pub fn rm(&mut self, filename: &str) -> Result<()>

Remove the remote file from the server.

Source

pub fn put<R: Read>(&mut self, filename: &str, r: &mut R) -> Result<()>

This stores a file on the server.

Examples found in repository?
examples/connecting.rs (line 23)
7fn test_ftp(addr: &str, user: &str, pass: &str) -> Result<(), FtpError> {
8    let mut ftp_stream = FtpStream::connect((addr, 21)).unwrap();
9    ftp_stream.login(user, pass).unwrap();
10    println!("current dir: {}", ftp_stream.pwd().unwrap());
11
12    ftp_stream.cwd("test_data").unwrap();
13
14    // An easy way to retrieve a file
15    let cursor = ftp_stream.simple_retr("ftpext-charter.txt").unwrap();
16    let vec = cursor.into_inner();
17    let text = str::from_utf8(&vec).unwrap();
18    println!("got data: {}", text);
19
20    // Store a file
21    let file_data = format!("Some awesome file data man!!");
22    let mut reader = Cursor::new(file_data.into_bytes());
23    ftp_stream.put("my_random_file.txt", &mut reader).unwrap();
24
25    ftp_stream.quit()
26}
Source

pub 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.

Source

pub 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.

Source

pub 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.

Source

pub 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.

Source

pub fn read_response(&mut self, expected_code: u32) -> Result<Line>

Source

pub fn read_response_in(&mut self, expected_code: &[u32]) -> Result<Line>

Retrieve single line response

Trait Implementations§

Source§

impl Debug for FtpStream

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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.