summaryrefslogtreecommitdiff
path: root/src/channel.rs
diff options
context:
space:
mode:
authorWez Furlong <wez@wezfurlong.org>2019-07-23 16:39:28 -0700
committerWez Furlong <wez@wezfurlong.org>2019-07-29 08:55:06 -0700
commit18dc0f59e0b29c50da525dbe85d36fb5cb0ecf62 (patch)
tree5f424e77c3bc053a39aea417fea6fe75b963f1d9 /src/channel.rs
parent78d2e3d9dbf1530cdf4bc152b91f0ddd7ce4895b (diff)
downloadssh2-rs-18dc0f59e0b29c50da525dbe85d36fb5cb0ecf62.zip
cargo fmt
No functional changes, just formatting
Diffstat (limited to 'src/channel.rs')
-rw-r--r--src/channel.rs190
1 files changed, 103 insertions, 87 deletions
diff --git a/src/channel.rs b/src/channel.rs
index 32930d8..6328938 100644
--- a/src/channel.rs
+++ b/src/channel.rs
@@ -1,11 +1,11 @@
+use libc::{c_char, c_int, c_uchar, c_uint, c_ulong, c_void, size_t};
use std::cmp;
use std::io::prelude::*;
use std::io::{self, ErrorKind};
use std::slice;
-use libc::{c_uint, c_int, size_t, c_char, c_void, c_uchar, c_ulong};
-use {raw, Session, Error};
use util::{Binding, SessionBinding};
+use {raw, Error, Session};
/// A channel represents a portion of an SSH connection on which data can be
/// read and written.
@@ -68,11 +68,13 @@ impl<'sess> Channel<'sess> {
/// ignored by the server despite returning success.
pub fn setenv(&mut self, var: &str, val: &str) -> Result<(), Error> {
unsafe {
- self.sess.rc(raw::libssh2_channel_setenv_ex(self.raw,
- var.as_ptr() as *const _,
- var.len() as c_uint,
- val.as_ptr() as *const _,
- val.len() as c_uint))
+ self.sess.rc(raw::libssh2_channel_setenv_ex(
+ self.raw,
+ var.as_ptr() as *const _,
+ var.len() as c_uint,
+ val.as_ptr() as *const _,
+ val.len() as c_uint,
+ ))
}
}
@@ -83,40 +85,46 @@ impl<'sess> Channel<'sess> {
///
/// The dimensions argument is a tuple of (width, height, width_px,
/// height_px)
- pub fn request_pty(&mut self, term: &str,
- mode: Option<&str>,
- dim: Option<(u32, u32, u32, u32)>)
- -> Result<(), Error>{
+ pub fn request_pty(
+ &mut self,
+ term: &str,
+ mode: Option<&str>,
+ dim: Option<(u32, u32, u32, u32)>,
+ ) -> Result<(), Error> {
self.sess.rc(unsafe {
- let (width, height, width_px, height_px) =
- dim.unwrap_or((80, 24, 0, 0));
- raw::libssh2_channel_request_pty_ex(self.raw,
- term.as_ptr() as *const _,
- term.len() as c_uint,
- mode.map(|s| s.as_ptr())
- .unwrap_or(0 as *const _)
- as *const _,
- mode.map(|s| s.len())
- .unwrap_or(0) as c_uint,
- width as c_int,
- height as c_int,
- width_px as c_int,
- height_px as c_int)
+ let (width, height, width_px, height_px) = dim.unwrap_or((80, 24, 0, 0));
+ raw::libssh2_channel_request_pty_ex(
+ self.raw,
+ term.as_ptr() as *const _,
+ term.len() as c_uint,
+ mode.map(|s| s.as_ptr()).unwrap_or(0 as *const _) as *const _,
+ mode.map(|s| s.len()).unwrap_or(0) as c_uint,
+ width as c_int,
+ height as c_int,
+ width_px as c_int,
+ height_px as c_int,
+ )
})
}
/// Request a PTY of a specified size
- pub fn request_pty_size(&mut self, width: u32, height: u32,
- width_px: Option<u32>, height_px: Option<u32>)
- -> Result<(), Error> {
+ pub fn request_pty_size(
+ &mut self,
+ width: u32,
+ height: u32,
+ width_px: Option<u32>,
+ height_px: Option<u32>,
+ ) -> Result<(), Error> {
let width_px = width_px.unwrap_or(0);
let height_px = height_px.unwrap_or(0);
self.sess.rc(unsafe {
- raw::libssh2_channel_request_pty_size_ex(self.raw,
- width as c_int,
- height as c_int,
- width_px as c_int,
- height_px as c_int)
+ raw::libssh2_channel_request_pty_size_ex(
+ self.raw,
+ width as c_int,
+ height as c_int,
+ width_px as c_int,
+ height_px as c_int,
+ )
})
}
@@ -161,14 +169,17 @@ impl<'sess> Channel<'sess> {
///
/// The SSH2 protocol currently defines shell, exec, and subsystem as
/// standard process services.
- pub fn process_startup(&mut self, request: &str, message: Option<&str>)
- -> Result<(), Error> {
+ pub fn process_startup(&mut self, request: &str, message: Option<&str>) -> Result<(), Error> {
let message_len = message.map(|s| s.len()).unwrap_or(0);
let message = message.map(|s| s.as_ptr()).unwrap_or(0 as *const _);
unsafe {
- let rc = raw::libssh2_channel_process_startup(self.raw,
- request.as_ptr() as *const _, request.len() as c_uint,
- message as *const _, message_len as c_uint);
+ let rc = raw::libssh2_channel_process_startup(
+ self.raw,
+ request.as_ptr() as *const _,
+ request.len() as c_uint,
+ message as *const _,
+ message_len as c_uint,
+ );
self.sess.rc(rc)
}
}
@@ -190,7 +201,10 @@ impl<'sess> Channel<'sess> {
/// * FLUSH_EXTENDED_DATA - Flush all extended data substreams
/// * FLUSH_ALL - Flush all substreams
pub fn stream<'a>(&'a mut self, stream_id: i32) -> Stream<'a, 'sess> {
- Stream { channel: self, id: stream_id }
+ Stream {
+ channel: self,
+ id: stream_id,
+ }
}
/// Returns the exit code raised by the process running on the remote host
@@ -215,22 +229,27 @@ impl<'sess> Channel<'sess> {
let mut msglen = 0;
let mut lang = 0 as *mut _;
let mut langlen = 0;
- let rc = raw::libssh2_channel_get_exit_signal(self.raw,
- &mut sig, &mut siglen,
- &mut msg, &mut msglen,
- &mut lang,
- &mut langlen);
+ let rc = raw::libssh2_channel_get_exit_signal(
+ self.raw,
+ &mut sig,
+ &mut siglen,
+ &mut msg,
+ &mut msglen,
+ &mut lang,
+ &mut langlen,
+ );
try!(self.sess.rc(rc));
return Ok(ExitSignal {
exit_signal: convert(self, sig, siglen),
error_message: convert(self, msg, msglen),
lang_tag: convert(self, lang, langlen),
- })
+ });
}
- unsafe fn convert(chan: &Channel, ptr: *mut c_char,
- len: size_t) -> Option<String> {
- if ptr.is_null() { return None }
+ unsafe fn convert(chan: &Channel, ptr: *mut c_char, len: size_t) -> Option<String> {
+ if ptr.is_null() {
+ return None;
+ }
let slice = slice::from_raw_parts(ptr as *const u8, len as usize);
let ret = slice.to_vec();
raw::libssh2_free(chan.sess.raw(), ptr as *mut c_void);
@@ -243,9 +262,7 @@ impl<'sess> Channel<'sess> {
unsafe {
let mut avail = 0;
let mut init = 0;
- let remaining = raw::libssh2_channel_window_read_ex(self.raw,
- &mut avail,
- &mut init);
+ let remaining = raw::libssh2_channel_window_read_ex(self.raw, &mut avail, &mut init);
ReadWindow {
remaining: remaining as u32,
available: avail as u32,
@@ -258,8 +275,7 @@ impl<'sess> Channel<'sess> {
pub fn write_window(&self) -> WriteWindow {
unsafe {
let mut init = 0;
- let remaining = raw::libssh2_channel_window_write_ex(self.raw,
- &mut init);
+ let remaining = raw::libssh2_channel_window_write_ex(self.raw, &mut init);
WriteWindow {
remaining: remaining as u32,
window_size_initial: init as u32,
@@ -274,14 +290,15 @@ impl<'sess> Channel<'sess> {
///
/// This function returns the new size of the receive window (as understood
/// by remote end) on success.
- pub fn adjust_receive_window(&mut self, adjust: u64, force: bool)
- -> Result<u64, Error> {
+ pub fn adjust_receive_window(&mut self, adjust: u64, force: bool) -> Result<u64, Error> {
let mut ret = 0;
let rc = unsafe {
- raw::libssh2_channel_receive_window_adjust2(self.raw,
- adjust as c_ulong,
- force as c_uchar,
- &mut ret)
+ raw::libssh2_channel_receive_window_adjust2(
+ self.raw,
+ adjust as c_ulong,
+ force as c_uchar,
+ &mut ret,
+ )
};
try!(self.sess.rc(rc));
Ok(ret as u64)
@@ -296,8 +313,7 @@ impl<'sess> Channel<'sess> {
/// Check if the remote host has sent an EOF status for the selected stream.
pub fn eof(&self) -> bool {
- self.read_limit == Some(0) ||
- unsafe { raw::libssh2_channel_eof(self.raw) != 0 }
+ self.read_limit == Some(0) || unsafe { raw::libssh2_channel_eof(self.raw) != 0 }
}
/// Tell the remote host that no further data will be sent on the specified
@@ -305,9 +321,7 @@ impl<'sess> Channel<'sess> {
///
/// Processes typically interpret this as a closed stdin descriptor.
pub fn send_eof(&mut self) -> Result<(), Error> {
- unsafe {
- self.sess.rc(raw::libssh2_channel_send_eof(self.raw))
- }
+ unsafe { self.sess.rc(raw::libssh2_channel_send_eof(self.raw)) }
}
/// Wait for the remote end to send EOF.
@@ -325,9 +339,7 @@ impl<'sess> Channel<'sess> {
/// To wait for the remote end to close its connection as well, follow this
/// command with `wait_closed`
pub fn close(&mut self) -> Result<(), Error> {
- unsafe {
- self.sess.rc(raw::libssh2_channel_close(self.raw))
- }
+ unsafe { self.sess.rc(raw::libssh2_channel_close(self.raw)) }
}
/// Enter a temporary blocking state until the remote host closes the named
@@ -342,15 +354,16 @@ impl<'sess> Channel<'sess> {
impl<'sess> SessionBinding<'sess> for Channel<'sess> {
type Raw = raw::LIBSSH2_CHANNEL;
- unsafe fn from_raw(sess: &'sess Session,
- raw: *mut raw::LIBSSH2_CHANNEL) -> Channel<'sess> {
+ unsafe fn from_raw(sess: &'sess Session, raw: *mut raw::LIBSSH2_CHANNEL) -> Channel<'sess> {
Channel {
raw: raw,
sess: sess,
read_limit: None,
}
}
- fn raw(&self) -> *mut raw::LIBSSH2_CHANNEL { self.raw }
+ fn raw(&self) -> *mut raw::LIBSSH2_CHANNEL {
+ self.raw
+ }
}
impl<'sess> Write for Channel<'sess> {
@@ -379,7 +392,9 @@ impl<'sess> Drop for Channel<'sess> {
impl<'channel, 'sess> Read for Stream<'channel, 'sess> {
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
- if self.channel.eof() { return Ok(0) }
+ if self.channel.eof() {
+ return Ok(0);
+ }
let data = match self.channel.read_limit {
Some(amt) => {
@@ -389,10 +404,12 @@ impl<'channel, 'sess> Read for Stream<'channel, 'sess> {
None => data,
};
let ret = unsafe {
- let rc = raw::libssh2_channel_read_ex(self.channel.raw,
- self.id as c_int,
- data.as_mut_ptr() as *mut _,
- data.len() as size_t);
+ let rc = raw::libssh2_channel_read_ex(
+ self.channel.raw,
+ self.id as c_int,
+ data.as_mut_ptr() as *mut _,
+ data.len() as size_t,
+ );
self.channel.sess.rc(rc as c_int).map(|()| rc as usize)
};
match ret {
@@ -410,23 +427,22 @@ impl<'channel, 'sess> Read for Stream<'channel, 'sess> {
impl<'channel, 'sess> Write for Stream<'channel, 'sess> {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
unsafe {
- let rc = raw::libssh2_channel_write_ex(self.channel.raw,
- self.id as c_int,
- data.as_ptr() as *mut _,
- data.len() as size_t);
+ let rc = raw::libssh2_channel_write_ex(
+ self.channel.raw,
+ self.id as c_int,
+ data.as_ptr() as *mut _,
+ data.len() as size_t,
+ );
self.channel.sess.rc(rc as c_int).map(|()| rc as usize)
- }.map_err(|e| {
- io::Error::new(ErrorKind::Other, e)
- })
+ }
+ .map_err(|e| io::Error::new(ErrorKind::Other, e))
}
fn flush(&mut self) -> io::Result<()> {
unsafe {
- let rc = raw::libssh2_channel_flush_ex(self.channel.raw,
- self.id as c_int);
+ let rc = raw::libssh2_channel_flush_ex(self.channel.raw, self.id as c_int);
self.channel.sess.rc(rc)
- }.map_err(|e| {
- io::Error::new(ErrorKind::Other, e)
- })
+ }
+ .map_err(|e| io::Error::new(ErrorKind::Other, e))
}
}