Skip to content

Commit

Permalink
🔧 change sftp methods to accept &str and &Path
Browse files Browse the repository at this point in the history
  • Loading branch information
k0i committed Mar 30, 2024
1 parent ec94100 commit f8c7352
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
22 changes: 10 additions & 12 deletions src/sftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ impl Sftp {
/// Open a handle to a file.
///
/// The mode will represent the permissions for the file ([Wikipedia](<https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation>)).
pub fn open_mode(
pub fn open_mode<T: AsRef<Path>>(
&self,
filename: &Path,
filename: T,
flags: OpenFlags,
mode: i32,
open_type: OpenType,
) -> Result<File, Error> {
let filename = CString::new(util::path2bytes(filename)?)?;
let filename = CString::new(util::path2bytes(filename.as_ref())?)?;

let locked = self.lock()?;
unsafe {
Expand All @@ -205,7 +205,7 @@ impl Sftp {
}

/// Helper to open a file in the `Read` mode.
pub fn open(&self, filename: &Path) -> Result<File, Error> {
pub fn open<T: AsRef<Path>>(&self, filename: T) -> Result<File, Error> {
self.open_mode(filename, OpenFlags::READ, 0o644, OpenType::File)
}

Expand All @@ -220,16 +220,16 @@ impl Sftp {
}

/// Helper to open a directory for reading its contents.
pub fn opendir(&self, dirname: &Path) -> Result<File, Error> {
pub fn opendir<T: AsRef<Path>>(&self, dirname: T) -> Result<File, Error> {
self.open_mode(dirname, OpenFlags::READ, 0, OpenType::Dir)
}

/// Convenience function to read the files in a directory.
///
/// 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<Vec<(PathBuf, FileStat)>, Error> {
let mut dir = self.opendir(dirname)?;
pub fn readdir<T: AsRef<Path>>(&self, dirname: T) -> Result<Vec<(PathBuf, FileStat)>, Error> {
let mut dir = self.opendir(dirname.as_ref())?;
let mut ret = Vec::new();
loop {
match dir.readdir() {
Expand All @@ -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) => {
Expand All @@ -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](<https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation>)).
pub fn mkdir(&self, filename: &Path, mode: i32) -> Result<(), Error> {
let filename = CString::new(util::path2bytes(filename)?)?;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions tests/all/sftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit f8c7352

Please sign in to comment.