summaryrefslogtreecommitdiff
path: root/src/sftp.rs
blob: 08391fa43a38a128d9c954e6ca7049864dfd0817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::kinds::marker;
use libc::c_int;

use {raw, Session, Error};

pub struct Sftp<'a> {
    raw: *mut raw::LIBSSH2_SFTP,
    marker1: marker::NoSync,
    marker2: marker::ContravariantLifetime<'a>,
    marker3: marker::NoSend,
}

impl<'a> Sftp<'a> {
    /// Wraps a raw pointer in a new Sftp structure tied to the lifetime of the
    /// given session.
    ///
    /// This consumes ownership of `raw`.
    pub unsafe fn from_raw(_sess: &Session,
                           raw: *mut raw::LIBSSH2_SFTP) -> Sftp {
        Sftp {
            raw: raw,
            marker1: marker::NoSync,
            marker2: marker::ContravariantLifetime,
            marker3: marker::NoSend,
        }
    }

    /// Peel off the last error to happen on this SFTP instance.
    pub fn last_error(&self) -> Error {
        let code = unsafe { raw::libssh2_sftp_last_error(self.raw) };
        Error::from_errno(code as c_int)
    }
}

#[unsafe_destructor]
impl<'a> Drop for Sftp<'a> {
    fn drop(&mut self) {
        unsafe { assert_eq!(raw::libssh2_sftp_shutdown(self.raw), 0) }
    }
}