diff options
author | Veselkov Sergey <veselkovsd@yandex.ru> | 2016-08-17 16:00:23 +0300 |
---|---|---|
committer | Veselkov Sergey <veselkovsd@yandex.ru> | 2016-08-17 16:00:23 +0300 |
commit | fc02b3db5a858cd7b974743e5b9c39108e658813 (patch) | |
tree | dec3089fde62b3594b70b36cb7cbf0830b7c12b3 | |
parent | 5446f324aa297474adc412bb3b96fa5b514c9c74 (diff) | |
download | ssh2-rs-fc02b3db5a858cd7b974743e5b9c39108e658813.zip |
Add checks for file types
-rw-r--r-- | libssh2-sys/lib.rs | 5 | ||||
-rw-r--r-- | src/sftp.rs | 31 |
2 files changed, 36 insertions, 0 deletions
diff --git a/libssh2-sys/lib.rs b/libssh2-sys/lib.rs index 1516d18..660587a 100644 --- a/libssh2-sys/lib.rs +++ b/libssh2-sys/lib.rs @@ -145,6 +145,11 @@ pub const LIBSSH2_SFTP_RENAME_NATIVE: c_long = 0x4; pub const LIBSSH2_INIT_NO_CRYPTO: c_int = 0x1; +pub const LIBSSH2_SFTP_S_IFMT: c_ulong = 0o170000; +pub const LIBSSH2_SFTP_S_IFDIR: c_ulong = 0o040000; +pub const LIBSSH2_SFTP_S_IFREG: c_ulong = 0o100000; +pub const LIBSSH2_SFTP_S_IFLNK: c_ulong = 0o120000; + pub enum LIBSSH2_SESSION {} pub enum LIBSSH2_AGENT {} pub enum LIBSSH2_CHANNEL {} diff --git a/src/sftp.rs b/src/sftp.rs index 07251f0..a56d3b1 100644 --- a/src/sftp.rs +++ b/src/sftp.rs @@ -48,6 +48,11 @@ pub struct FileStat { pub mtime: Option<u64>, } +/// An structure representing a type of file. +pub struct FileType { + perm: c_ulong, +} + bitflags! { #[doc = "Options that can be used to configure how a file is opened"] flags OpenFlags: c_ulong { @@ -500,6 +505,17 @@ impl<'sftp> Drop for File<'sftp> { } impl FileStat { + /// Returns the file type for this filestat. + pub fn file_type(&self) -> FileType { + FileType { perm: self.perm.unwrap_or(0) as c_ulong } + } + + /// Returns whether this metadata is for a directory. + pub fn is_dir(&self) -> bool { self.file_type().is_dir() } + + /// Returns whether this metadata is for a regular file. + pub fn is_file(&self) -> bool { self.file_type().is_file() } + /// Creates a new instance of a stat from a raw instance. pub fn from_raw(raw: &raw::LIBSSH2_SFTP_ATTRIBUTES) -> FileStat { fn val<T: Copy>(raw: &raw::LIBSSH2_SFTP_ATTRIBUTES, t: &T, @@ -545,6 +561,21 @@ impl FileStat { } } +impl FileType { + /// Test whether this file type represents a directory. + pub fn is_dir(&self) -> bool { self.is(raw::LIBSSH2_SFTP_S_IFDIR) } + + /// Test whether this file type represents a regular file. + pub fn is_file(&self) -> bool { self.is(raw::LIBSSH2_SFTP_S_IFREG) } + + /// Test whether this file type represents a symbolic link. + pub fn is_symlink(&self) -> bool { self.is(raw::LIBSSH2_SFTP_S_IFLNK) } + + fn is(&self, perm: c_ulong) -> bool { + (self.perm & raw::LIBSSH2_SFTP_S_IFMT) == perm + } +} + #[cfg(unix)] fn mkpath(v: Vec<u8>) -> PathBuf { use std::os::unix::prelude::*; |