summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVeselkov Sergey <veselkovsd@yandex.ru>2016-08-17 16:00:23 +0300
committerVeselkov Sergey <veselkovsd@yandex.ru>2016-08-17 16:00:23 +0300
commitfc02b3db5a858cd7b974743e5b9c39108e658813 (patch)
treedec3089fde62b3594b70b36cb7cbf0830b7c12b3 /src
parent5446f324aa297474adc412bb3b96fa5b514c9c74 (diff)
downloadssh2-rs-fc02b3db5a858cd7b974743e5b9c39108e658813.zip
Add checks for file types
Diffstat (limited to 'src')
-rw-r--r--src/sftp.rs31
1 files changed, 31 insertions, 0 deletions
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::*;