From 1a7f36fe4d22852e3ee2aa6252a0857f5b9181a3 Mon Sep 17 00:00:00 2001 From: "koyama.sojiro" Date: Sat, 30 Mar 2024 14:11:55 +0900 Subject: [PATCH] :wrench: change sftp dir methods to accept AsStr --- src/sftp.rs | 22 ++++++++++------------ tests/all/sftp.rs | 14 +++++++++++--- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/sftp.rs b/src/sftp.rs index 82ff21e1..6299cbdf 100644 --- a/src/sftp.rs +++ b/src/sftp.rs @@ -176,14 +176,14 @@ impl Sftp { /// Open a handle to a file. /// /// The mode will represent the permissions for the file ([Wikipedia]()). - pub fn open_mode( + pub fn open_mode>( &self, - filename: &Path, + filename: T, flags: OpenFlags, mode: i32, open_type: OpenType, ) -> Result { - let filename = CString::new(util::path2bytes(filename)?)?; + let filename = CString::new(util::path2bytes(filename.as_ref())?)?; let locked = self.lock()?; unsafe { @@ -205,7 +205,7 @@ impl Sftp { } /// Helper to open a file in the `Read` mode. - pub fn open(&self, filename: &Path) -> Result { + pub fn open>(&self, filename: T) -> Result { self.open_mode(filename, OpenFlags::READ, 0o644, OpenType::File) } @@ -220,7 +220,7 @@ impl Sftp { } /// Helper to open a directory for reading its contents. - pub fn opendir(&self, dirname: &Path) -> Result { + pub fn opendir>(&self, dirname: T) -> Result { self.open_mode(dirname, OpenFlags::READ, 0, OpenType::Dir) } @@ -228,8 +228,8 @@ impl Sftp { /// /// The returned paths are all joined with `dirname` when returned, and the /// paths `.` and `..` are filtered out of the returned list. - pub fn readdir(&self, dirname: &Path) -> Result, Error> { - let mut dir = self.opendir(dirname)?; + pub fn readdir>(&self, dirname: T) -> Result, Error> { + let mut dir = self.opendir(dirname.as_ref())?; let mut ret = Vec::new(); loop { match dir.readdir() { @@ -238,7 +238,7 @@ impl Sftp { continue; } - ret.push((dirname.join(&filename), stat)) + ret.push((dirname.as_ref().join(&filename), stat)) } Err(ref e) if e.code() == ErrorCode::Session(raw::LIBSSH2_ERROR_FILE) => break, Err(e) => { @@ -252,7 +252,7 @@ impl Sftp { } /// Create a directory on the remote file system. - /// + /// /// The mode will set the permissions of the new directory ([Wikipedia]()). pub fn mkdir(&self, filename: &Path, mode: i32) -> Result<(), Error> { let filename = CString::new(util::path2bytes(filename)?)?; @@ -690,9 +690,7 @@ impl File { // If any other error was returned, or if it completed OK, we must not use the // handle again. match rc { - Err(e) if e.code() == ErrorCode::Session(raw::LIBSSH2_ERROR_EAGAIN) => { - Err(e) - }, + Err(e) if e.code() == ErrorCode::Session(raw::LIBSSH2_ERROR_EAGAIN) => Err(e), rc => { self.inner = None; rc diff --git a/tests/all/sftp.rs b/tests/all/sftp.rs index 8ef32266..ee0fc5d2 100644 --- a/tests/all/sftp.rs +++ b/tests/all/sftp.rs @@ -16,7 +16,10 @@ fn ops() { let sess = ::authed_session(); let sftp = sess.sftp().unwrap(); - sftp.opendir(&td.path().join("bar")).unwrap(); + let path = td.path().join("bar"); + let path_str: &str = path.to_str().unwrap(); + sftp.opendir(&path).unwrap(); + sftp.opendir(path_str).unwrap(); let mut foo = sftp.open(&td.path().join("foo")).unwrap(); sftp.mkdir(&td.path().join("bar2"), 0o755).unwrap(); assert!(fs::metadata(&td.path().join("bar2")) @@ -47,8 +50,13 @@ fn ops() { let realpath = sftp.realpath(&td.path().join("foo2")).unwrap(); assert_eq!(realpath, td.path().join("foo").canonicalize().unwrap()); - let files = sftp.readdir(td.path()).unwrap(); - assert_eq!(files.len(), 4); + let path = td.path(); + let path_str: &str = path.to_str().unwrap(); + let files = sftp.readdir(path).unwrap(); + let files_from_str = sftp.readdir(path_str).unwrap(); + for (f1, f2) in files.iter().zip(files_from_str.iter()) { + assert_eq!(f1, f2); + } } #[test]