Skip to content
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
1 change: 1 addition & 0 deletions async-ssh2-lite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ _integration_tests_tokio_ext = []

[dependencies]
ssh2 = { version = "0.9", default-features = false }
libssh2-sys = { version = "0.2", default-features = false }
futures-util = { version = "0.3", default-features = false, features = ["io", "std", "async-await-macro"] }
async-trait = { version = "0.1", default-features = false }

Expand Down
35 changes: 32 additions & 3 deletions async-ssh2-lite/src/sftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,27 @@ where
}

pub async fn readdir(&self, dirname: &Path) -> Result<Vec<(PathBuf, FileStat)>, Error> {
self.stream
.rw_with(|| self.inner.readdir(dirname), &self.sess)
.await
// Copy from ssh2
let mut dir = self.opendir(dirname).await?;
let mut ret = Vec::new();
loop {
match dir.readdir().await {
Ok((filename, stat)) => {
if &*filename == Path::new(".") || &*filename == Path::new("..") {
continue;
}

ret.push((dirname.join(&filename), stat))
}
Err(Error::Ssh2(ref e))
if e.code() == ssh2::ErrorCode::Session(libssh2_sys::LIBSSH2_ERROR_FILE) =>
{
break
}
Err(e) => return Err(e),
}
}
Ok(ret)
}

pub async fn mkdir(&self, filename: &Path, mode: i32) -> Result<(), Error> {
Expand Down Expand Up @@ -184,6 +202,17 @@ impl<S> AsyncFile<S> {
}
}

impl<S> AsyncFile<S>
where
S: AsyncSessionStream + Send + Sync + 'static,
{
pub async fn readdir(&mut self) -> Result<(PathBuf, FileStat), Error> {
self.stream
.rw_with(|| self.inner.readdir(), &self.sess)
.await
}
}

mod impl_futures_util {
use core::{
pin::Pin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn simple_with_async_io() -> Result<(), Box<dyn error::Error>> {

let dir = tempdir()?;
let path = dir.path().join("ssh_agent");
Async::<UnixListener>::bind(&path)?
Async::<UnixListener>::bind(path)?
} else {
use std::net::TcpListener;

Expand Down
5 changes: 5 additions & 0 deletions async-ssh2-lite/tests/integration_tests/sftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ async fn __run__session__sftp<S: AsyncSessionStream + Send + Sync + 'static>(
println!("sftp file_stat:{:?}", file_stat);
sftp.unlink(&remote_path).await?;

let list = sftp.readdir(&PathBuf::from("/")).await?;
for (file_path, file_stat) in list.iter().take(10) {
println!("sftp file_path:{:?} file_stat:{:?}", file_path, file_stat);
}

Ok(())
}