From fc02b3db5a858cd7b974743e5b9c39108e658813 Mon Sep 17 00:00:00 2001 From: Veselkov Sergey Date: Wed, 17 Aug 2016 16:00:23 +0300 Subject: Add checks for file types --- src/sftp.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src') 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, } +/// 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(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) -> PathBuf { use std::os::unix::prelude::*; -- cgit v1.2.3