summaryrefslogtreecommitdiff
path: root/src/sys
diff options
context:
space:
mode:
authorTilde Engineering <engineering@tilde.io>2014-10-06 15:25:34 -0700
committerTilde Engineering <engineering@tilde.io>2014-10-06 15:25:34 -0700
commit3ecbe3364aa56f3352a07a85b146156bb246f515 (patch)
tree051ce510ce2ddf22599aa25649d89a52a3a65639 /src/sys
parent67e6410805279cb46ddf69c2ddd3454b9f847c92 (diff)
downloadnix-3ecbe3364aa56f3352a07a85b146156bb246f515.zip
Bind stat and fstat
Diffstat (limited to 'src/sys')
-rw-r--r--src/sys/mod.rs1
-rw-r--r--src/sys/stat.rs35
2 files changed, 31 insertions, 5 deletions
diff --git a/src/sys/mod.rs b/src/sys/mod.rs
index 8c588ea1..e6cce4f8 100644
--- a/src/sys/mod.rs
+++ b/src/sys/mod.rs
@@ -10,7 +10,6 @@ pub mod eventfd;
pub mod socket;
-#[cfg(target_os = "linux")]
pub mod stat;
#[cfg(target_os = "linux")]
diff --git a/src/sys/stat.rs b/src/sys/stat.rs
index c26220c0..0a6c2450 100644
--- a/src/sys/stat.rs
+++ b/src/sys/stat.rs
@@ -1,13 +1,17 @@
pub use libc::dev_t;
+pub use libc::stat as FileStat;
use std::fmt;
use std::io::FilePermission;
+use std::mem;
use std::path::Path;
use libc::mode_t;
-use errno::{SysResult, from_ffi};
+use errno::{SysResult, SysError, from_ffi};
+use fcntl::Fd;
mod ffi {
use libc::{c_char, c_int, mode_t, dev_t};
+ pub use libc::{stat, fstat};
extern {
pub fn mknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> c_int;
@@ -32,18 +36,41 @@ impl fmt::Show for SFlag {
}
pub fn mknod(path: &Path, kind: SFlag, perm: FilePermission, dev: dev_t) -> SysResult<()> {
- let res = unsafe { ffi::mknod(path.to_c_str().as_ptr(), kind.bits | perm.bits(), dev) };
+ let res = unsafe { ffi::mknod(path.to_c_str().as_ptr(), kind.bits | perm.bits() as mode_t, dev) };
from_ffi(res)
}
static MINORBITS: uint = 20;
// static MINORMASK: dev_t = ((1 << MINORBITS) - 1);
+#[cfg(target_os = "linux")]
pub fn mkdev(major: u64, minor: u64) -> dev_t {
(major << MINORBITS) | minor
}
pub fn umask(mode: FilePermission) -> FilePermission {
- let prev = unsafe { ffi::umask(mode.bits()) };
- FilePermission::from_bits(prev).expect("[BUG] umask returned invalid FilePermission")
+ let prev = unsafe { ffi::umask(mode.bits() as mode_t) };
+ FilePermission::from_bits(prev as u32).expect("[BUG] umask returned invalid FilePermission")
+}
+
+pub fn stat(path: &Path) -> SysResult<FileStat> {
+ let mut dst = unsafe { mem::uninitialized() };
+ let res = unsafe { ffi::stat(path.to_c_str().as_ptr(), &mut dst as *mut FileStat) };
+
+ if res < 0 {
+ return Err(SysError::last());
+ }
+
+ Ok(dst)
+}
+
+pub fn fstat(fd: Fd) -> SysResult<FileStat> {
+ let mut dst = unsafe { mem::uninitialized() };
+ let res = unsafe { ffi::fstat(fd, &mut dst as *mut FileStat) };
+
+ if res < 0 {
+ return Err(SysError::last());
+ }
+
+ Ok(dst)
}