Skip to content

Add an option to place the temporary path for ControlDirectory in a user-set location. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{Error, Session};
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use tempfile::Builder;
use tokio::io::AsyncReadExt;
Expand All @@ -10,10 +11,11 @@ use tokio::process;
pub struct SessionBuilder {
user: Option<String>,
port: Option<String>,
keyfile: Option<std::path::PathBuf>,
keyfile: Option<PathBuf>,
connect_timeout: Option<String>,
server_alive_interval: Option<u64>,
known_hosts_check: KnownHosts,
control_dir: Option<PathBuf>,
}

impl Default for SessionBuilder {
Expand All @@ -25,6 +27,7 @@ impl Default for SessionBuilder {
connect_timeout: None,
server_alive_interval: None,
known_hosts_check: KnownHosts::Add,
control_dir: None,
}
}
}
Expand All @@ -49,7 +52,7 @@ impl SessionBuilder {
/// Set the keyfile to use (`ssh -i`).
///
/// Defaults to `None`.
pub fn keyfile(&mut self, p: impl AsRef<std::path::Path>) -> &mut Self {
pub fn keyfile(&mut self, p: impl AsRef<Path>) -> &mut Self {
self.keyfile = Some(p.as_ref().to_path_buf());
self
}
Expand Down Expand Up @@ -81,6 +84,15 @@ impl SessionBuilder {
self
}

/// Set the directory in which the temporary directory containing the control socket will
/// be created.
///
/// If not set, `./` will be used (the current directory).
pub fn control_directory(&mut self, p: impl AsRef<Path>) -> &mut Self {
self.control_dir = Some(p.as_ref().to_path_buf());
self
}

/// Connect to the host at the given `host` over SSH.
///
/// The format of `destination` is the same as the `destination` argument to `ssh`. It may be
Expand Down Expand Up @@ -137,9 +149,12 @@ impl SessionBuilder {

pub(crate) async fn just_connect<S: AsRef<str>>(&self, host: S) -> Result<Session, Error> {
let destination = host.as_ref();

let defaultdir = Path::new("./").to_path_buf();
let socketdir = self.control_dir.as_ref().unwrap_or(&defaultdir);
let dir = Builder::new()
.prefix(".ssh-connection")
.tempdir_in("./")
.tempdir_in(socketdir)
.map_err(Error::Master)?;
let mut init = process::Command::new("ssh");

Expand Down
18 changes: 18 additions & 0 deletions tests/openssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ async fn it_connects() {
session.close().await.unwrap();
}

#[tokio::test]
#[cfg_attr(not(ci), ignore)]
async fn control_dir() {
let dirname = std::path::Path::new("control-test");
assert!(!dirname.exists());
std::fs::create_dir(dirname).unwrap();
let session = SessionBuilder::default()
.control_directory(&dirname)
.connect(&addr())
.await
.unwrap();
session.check().await.unwrap();
let mut iter = std::fs::read_dir(&dirname).unwrap();
assert!(iter.next().is_some());
session.close().await.unwrap();
std::fs::remove_dir(&dirname).unwrap();
}

#[tokio::test]
#[cfg_attr(not(ci), ignore)]
async fn terminate_on_drop() {
Expand Down