]> git.lizzy.rs Git - rust.git/commitdiff
Add FileTypeUnix trait to add unix special file types
authorJesús Espino <jespinog@gmail.com>
Sun, 5 Jul 2015 21:16:25 +0000 (23:16 +0200)
committerJesús Espino <jespinog@gmail.com>
Thu, 9 Jul 2015 08:31:28 +0000 (10:31 +0200)
src/liblibc/lib.rs
src/libstd/fs.rs
src/libstd/sys/unix/ext/fs.rs
src/libstd/sys/unix/ext/mod.rs
src/libstd/sys/unix/fs.rs

index 102894bec133ed46ecc163f3e08610cea895c70a..0a32c73a848d0dccd0f121dab06c441040ec61a3 100644 (file)
@@ -2522,6 +2522,7 @@ pub mod posix88 {
             pub const S_IFDIR : c_int = 16384;
             pub const S_IFREG : c_int = 32768;
             pub const S_IFLNK : c_int = 40960;
+            pub const S_IFSOCK : mode_t = 49152;
             pub const S_IFMT : c_int = 61440;
             pub const S_IEXEC : c_int = 64;
             pub const S_IWRITE : c_int = 128;
@@ -2881,6 +2882,7 @@ pub mod posix88 {
             pub const S_IFDIR : mode_t = 16384;
             pub const S_IFREG : mode_t = 32768;
             pub const S_IFLNK : mode_t = 40960;
+            pub const S_IFSOCK : mode_t = 49152;
             pub const S_IFMT : mode_t = 61440;
             pub const S_IEXEC : mode_t = 64;
             pub const S_IWRITE : mode_t = 128;
@@ -3103,6 +3105,7 @@ pub mod posix88 {
             pub const S_IFDIR : mode_t = 16384;
             pub const S_IFREG : mode_t = 32768;
             pub const S_IFLNK : mode_t = 40960;
+            pub const S_IFSOCK : mode_t = 49152;
             pub const S_IFMT : mode_t = 61440;
             pub const S_IEXEC : mode_t = 64;
             pub const S_IWRITE : mode_t = 128;
@@ -3905,6 +3908,7 @@ pub mod posix88 {
             pub const S_IFDIR : mode_t = 16384;
             pub const S_IFREG : mode_t = 32768;
             pub const S_IFLNK : mode_t = 40960;
+            pub const S_IFSOCK : mode_t = 49152;
             pub const S_IFMT : mode_t = 61440;
             pub const S_IEXEC : mode_t = 64;
             pub const S_IWRITE : mode_t = 128;
@@ -4365,6 +4369,7 @@ pub mod posix88 {
             pub const S_IFDIR : mode_t = 16384;
             pub const S_IFREG : mode_t = 32768;
             pub const S_IFLNK : mode_t = 40960;
+            pub const S_IFSOCK : mode_t = 49152;
             pub const S_IFMT : mode_t = 61440;
             pub const S_IEXEC : mode_t = 64;
             pub const S_IWRITE : mode_t = 128;
@@ -4791,6 +4796,7 @@ pub mod posix88 {
             pub const S_IFDIR : mode_t = 16384;
             pub const S_IFREG : mode_t = 32768;
             pub const S_IFLNK : mode_t = 40960;
+            pub const S_IFSOCK : mode_t = 49152;
             pub const S_IFMT : mode_t = 61440;
             pub const S_IEXEC : mode_t = 64;
             pub const S_IWRITE : mode_t = 128;
index c2d3d2fb0c8486caae414a04776a97e4aabf8ea7..e058d9d07078e401b0559eeef3880f6ffc580770 100644 (file)
@@ -637,6 +637,10 @@ pub fn is_file(&self) -> bool { self.0.is_file() }
     pub fn is_symlink(&self) -> bool { self.0.is_symlink() }
 }
 
+impl AsInner<fs_imp::FileType> for FileType {
+    fn as_inner(&self) -> &fs_imp::FileType { &self.0 }
+}
+
 impl FromInner<fs_imp::FilePermissions> for Permissions {
     fn from_inner(f: fs_imp::FilePermissions) -> Permissions {
         Permissions(f)
index 97703b8305683e46031f48d9097b7e6533261e80..4ee790b0161716d215e5af67e5f6fa8889a45747 100644 (file)
@@ -16,6 +16,7 @@
 
 use fs::{self, Permissions, OpenOptions};
 use io;
+use libc;
 use os::raw::c_long;
 use os::unix::raw;
 use path::Path;
@@ -178,6 +179,27 @@ fn blocks(&self) -> raw::blkcnt_t {
     }
 }
 
+/// Add special unix types (block/char device, fifo and socket)
+#[unstable(feature = "file_type_ext", reason = "recently added API")]
+pub trait FileTypeExt {
+    /// Returns whether this file type is a block device.
+    fn is_block_device(&self) -> bool;
+    /// Returns whether this file type is a char device.
+    fn is_char_device(&self) -> bool;
+    /// Returns whether this file type is a fifo.
+    fn is_fifo(&self) -> bool;
+    /// Returns whether this file type is a socket.
+    fn is_socket(&self) -> bool;
+}
+
+#[unstable(feature = "file_type_ext", reason = "recently added API")]
+impl FileTypeExt for fs::FileType {
+    fn is_block_device(&self) -> bool { self.as_inner().is(libc::S_IFBLK) }
+    fn is_char_device(&self) -> bool { self.as_inner().is(libc::S_IFCHR) }
+    fn is_fifo(&self) -> bool { self.as_inner().is(libc::S_IFIFO) }
+    fn is_socket(&self) -> bool { self.as_inner().is(libc::S_IFSOCK) }
+}
+
 /// Unix-specific extension methods for `fs::DirEntry`
 #[stable(feature = "dir_entry_ext", since = "1.1.0")]
 pub trait DirEntryExt {
index 48c77480899f70ce55130b087f726ec8ee1d99af..f7dee1a8f355f2a96453ac0c54c2ce0b80ad6167 100644 (file)
@@ -45,7 +45,7 @@ pub mod prelude {
     #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
     pub use super::ffi::{OsStrExt, OsStringExt};
     #[doc(no_inline)]
-    pub use super::fs::{PermissionsExt, OpenOptionsExt, MetadataExt};
+    pub use super::fs::{PermissionsExt, OpenOptionsExt, MetadataExt, FileTypeExt};
     #[doc(no_inline)]
     pub use super::fs::{DirEntryExt};
     #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
index 128284834ab01362944242581aa25349352eeb96..7396d088ade909ce45b3f55e3224af94b48129b0 100644 (file)
@@ -113,7 +113,7 @@ pub fn is_dir(&self) -> bool { self.is(libc::S_IFDIR) }
     pub fn is_file(&self) -> bool { self.is(libc::S_IFREG) }
     pub fn is_symlink(&self) -> bool { self.is(libc::S_IFLNK) }
 
-    fn is(&self, mode: mode_t) -> bool { self.mode & libc::S_IFMT == mode }
+    pub fn is(&self, mode: mode_t) -> bool { self.mode & libc::S_IFMT == mode }
 }
 
 impl FromInner<raw::mode_t> for FilePermissions {