Skip to content

Instantly share code, notes, and snippets.

@opensourcegeek
Created February 21, 2018 22:48
Show Gist options
  • Save opensourcegeek/59292253dd06364dd06d82ade0aa51ef to your computer and use it in GitHub Desktop.
Save opensourcegeek/59292253dd06364dd06d82ade0aa51ef to your computer and use it in GitHub Desktop.
Lifetime issue with bufwriter
extern crate ssh2;
use std::net::TcpStream;
use std::path::Path;
use std::io::BufWriter;
use ssh2::Session;
use ssh2::{OpenType, WRITE, TRUNCATE};
fn create_session<'a>() -> (TcpStream, Session, BufWriter<ssh2::File<'a>>) {
let tcp = TcpStream::connect("test:22").expect("Cannot connect");
let mut sess = Session::new().expect("cannot start session");
sess.handshake(&tcp).expect("handshake failed");
sess.userauth_password("test", "boo").expect("Cannot authenticate");
assert!(sess.authenticated());
let sftp = sess.sftp().expect("Cannot start sftp subsystem");
sftp.create(Path::new("sample.txt")).expect("Cannot create file in target directory");
let f = sftp.open_mode(Path::new("sample.txt"), WRITE | TRUNCATE, 0o644, OpenType::File).expect("Cannot open file file for writing");
let mut buf_file = BufWriter::new(f);
(tcp, sess, buf_file)
}
struct Sample<'b> {
// session: Session,
// sftp: ssh2::Sftp<'a>
buf_file: BufWriter<ssh2::File<'b>>
}
impl <'b> Sample<'b> {
fn new(buf_file: BufWriter<ssh2::File<'b>>) -> Sample<'b> {
Sample {
// session: s,
buf_file
}
}
fn print_some(&self) -> () {
println!("Got it!!");
}
}
fn main() {
let (stream, sess, buf_file) = create_session();
let s = Sample::new(buf_file);
s.print_some();
}
@opensourcegeek
Copy link
Author

opensourcegeek commented Feb 21, 2018

extern crate ssh2;

use std::net::TcpStream;
use std::path::Path;
use std::io::BufWriter;
use std::io::Write;

use ssh2::Session;
use ssh2::{OpenType, WRITE, TRUNCATE};


fn create_session() -> (TcpStream, Session) {
    let tcp = TcpStream::connect("test:22").expect("Cannot connect");
    let mut sess = Session::new().expect("cannot start session");
    sess.handshake(&tcp).expect("handshake failed");

    sess.userauth_password("test", "boo").expect("Cannot authenticate");
    assert!(sess.authenticated());

    (tcp, sess)
}

struct Sample<'b> {
//    session: Session,
//    sftp: ssh2::Sftp<'a>
    buf_file: BufWriter<ssh2::File<'b>>
}

impl <'b> Sample<'b> {
    fn new(buf_file: BufWriter<ssh2::File<'b>>) -> Sample<'b> {
        Sample {
//            session: s,
            buf_file
        }
    }

    fn write_to_file(&mut self, contents: &[u8]) {
        self.buf_file.write_all(&contents).expect("Cannot write to file");
    }

    fn print_some(&self) -> () {
        println!("Got it!!");
    }
}


fn handle_file() {
    let (_stream, sess) = create_session();
    let sftp = sess.sftp().expect("Cannot start sftp subsystem");

    sftp.create(Path::new("sample.txt")).expect("Cannot create file in target directory");
    let f = sftp.open_mode(Path::new("sample.txt"), WRITE | TRUNCATE, 0o644, OpenType::File).expect("Cannot open file file for writing");
    let buf_file = BufWriter::new(f);

    let mut s = Sample::new(buf_file);
    s.print_some();
    s.write_to_file("Nooooo ".as_bytes());
    s.write_to_file("but yesssssssss".as_bytes());
}



fn main() {
    handle_file()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment