Skip to content

Commit 79ac223

Browse files
authored
Merge pull request #21 from bk-rs/fix_sftp_readdir
Fix sftp.readdir
2 parents 5447721 + 52a346d commit 79ac223

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

async-ssh2-lite/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ _integration_tests_tokio_ext = []
2222

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

async-ssh2-lite/src/sftp.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,27 @@ where
9090
}
9191

9292
pub async fn readdir(&self, dirname: &Path) -> Result<Vec<(PathBuf, FileStat)>, Error> {
93-
self.stream
94-
.rw_with(|| self.inner.readdir(dirname), &self.sess)
95-
.await
93+
// Copy from ssh2
94+
let mut dir = self.opendir(dirname).await?;
95+
let mut ret = Vec::new();
96+
loop {
97+
match dir.readdir().await {
98+
Ok((filename, stat)) => {
99+
if &*filename == Path::new(".") || &*filename == Path::new("..") {
100+
continue;
101+
}
102+
103+
ret.push((dirname.join(&filename), stat))
104+
}
105+
Err(Error::Ssh2(ref e))
106+
if e.code() == ssh2::ErrorCode::Session(libssh2_sys::LIBSSH2_ERROR_FILE) =>
107+
{
108+
break
109+
}
110+
Err(e) => return Err(e),
111+
}
112+
}
113+
Ok(ret)
96114
}
97115

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

205+
impl<S> AsyncFile<S>
206+
where
207+
S: AsyncSessionStream + Send + Sync + 'static,
208+
{
209+
pub async fn readdir(&mut self) -> Result<(PathBuf, FileStat), Error> {
210+
self.stream
211+
.rw_with(|| self.inner.readdir(), &self.sess)
212+
.await
213+
}
214+
}
215+
187216
mod impl_futures_util {
188217
use core::{
189218
pin::Pin,

async-ssh2-lite/tests/integration_tests/agent__list_identities.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn simple_with_async_io() -> Result<(), Box<dyn error::Error>> {
2424

2525
let dir = tempdir()?;
2626
let path = dir.path().join("ssh_agent");
27-
Async::<UnixListener>::bind(&path)?
27+
Async::<UnixListener>::bind(path)?
2828
} else {
2929
use std::net::TcpListener;
3030

async-ssh2-lite/tests/integration_tests/sftp.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@ async fn __run__session__sftp<S: AsyncSessionStream + Send + Sync + 'static>(
4747
println!("sftp file_stat:{:?}", file_stat);
4848
sftp.unlink(&remote_path).await?;
4949

50+
let list = sftp.readdir(&PathBuf::from("/")).await?;
51+
for (file_path, file_stat) in list.iter().take(10) {
52+
println!("sftp file_path:{:?} file_stat:{:?}", file_path, file_stat);
53+
}
54+
5055
Ok(())
5156
}

0 commit comments

Comments
 (0)