0

I want to pass a ssh2::Sftp struct between functions. This structure depends on the ssh2::Session, which in turn depends on the std::net::TcpStream.

extern crate ssh2;

use std::path::Path;
use std::net::TcpStream;

fn new_sftp<'a>() -> ssh2::Sftp<'a> {
    let tcp = TcpStream::connect("localhost").unwrap();
    let mut session = ssh2::Session::new().unwrap();
    session.handshake(&tcp).unwrap();
    session.userauth_pubkey_file("user", None, Path::new("/home/user/.ssh/id_rsa"), None)
           .unwrap();
    session.sftp().unwrap()
}

fn main() {
    let sftp = new_sftp();
}

error:

error: `session` does not live long enough
    session.sftp().unwrap()
    ^~~~~~~

I tried to do in this way, but I don't understand which option lifetime pass for the ssh2::Sftp:

extern crate ssh2;

use std::path::Path;
use std::net::TcpStream;

struct Sftp {
    tcp: TcpStream,
    session: ssh2::Session,
    sftp: ssh2::Sftp,
}

fn main() {
    let tcp = TcpStream::connect("localhost").unwrap();
    let mut session = ssh2::Session::new().unwrap();
    session.handshake(&tcp).unwrap();
    session.userauth_pubkey_file("user", None, Path::new("/home/user/.ssh/id_rsa"), None)
           .unwrap();
    let sftp = session.sftp().unwrap();

    let sftp = Sftp {
        tcp: tcp,
        session: session,
        sftp: sftp,
    };
}

Update
I have several functions which work with files. I want to create a new type, which would have a function "open", which returns a file descriptor, regardless of where the file (on the local computer or on the remote). Now I'm trying to figure out how do I keep ssh::Sftp within this new type. I think it is not the best to create a new structure every time I call function "open", because the creation of something new always requires resources. Perhaps the compiler understands what I want and optimize this place, but I'm not sure.

vessd
  • 215
  • 3
  • 7
  • You may also be interested in [Rust: ssh2::Session lifetime error](http://stackoverflow.com/q/32298278/155423) and [Return local String as a slice (&str)](http://stackoverflow.com/q/29428227/155423). – Shepmaster Jul 26 '16 at 15:09
  • @Shepmaster Thank you, but I haven't found a solution of my problem. I know that the code that I gave as an example is incorrect. I thought about the possibility of creating Sftp object just before use, and came to the conclusion that in my case it is not optimal, because I have to create it before each call to open the file. It is planned to open at least a few thousand files. – vessd Jul 26 '16 at 21:33
  • Perhaps you can rephrase your question to explain what parts of the three linked questions are confusing? You may also want to consider showing how you attempted to solve the problem using the linked solutions and how those attempts have failed. I would also include the profiling you have done that shows that creating thousands of `Sftp` objects is not performant enough. [I'd probably just pass in a reference to the `Sftp` object](https://play.rust-lang.org/?gist=32d1c4c976b4306d6fd86537e5b4377c). – Shepmaster Jul 26 '16 at 22:43
  • @Shepmaster updated – vessd Jul 27 '16 at 17:44
  • The question is indeed updated, but it doesn't *explain what parts of the three linked questions are confusing* or *show how you attempted to solve the problem using the linked solutions* or *include the profiling*. No one will be able to give you better help than the 3 linked questions until we understand what the problem with them is. Otherwise, we'd just copy and paste the answer directly from those to here which helps no one. – Shepmaster Jul 27 '16 at 22:38

0 Answers0