Skip to content

Commit 3ecbe33

Browse files
Bind stat and fstat
1 parent 67e6410 commit 3ecbe33

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/sys/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub mod eventfd;
1010

1111
pub mod socket;
1212

13-
#[cfg(target_os = "linux")]
1413
pub mod stat;
1514

1615
#[cfg(target_os = "linux")]

src/sys/stat.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
pub use libc::dev_t;
2+
pub use libc::stat as FileStat;
23

34
use std::fmt;
45
use std::io::FilePermission;
6+
use std::mem;
57
use std::path::Path;
68
use libc::mode_t;
7-
use errno::{SysResult, from_ffi};
9+
use errno::{SysResult, SysError, from_ffi};
10+
use fcntl::Fd;
811

912
mod ffi {
1013
use libc::{c_char, c_int, mode_t, dev_t};
14+
pub use libc::{stat, fstat};
1115

1216
extern {
1317
pub fn mknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> c_int;
@@ -32,18 +36,41 @@ impl fmt::Show for SFlag {
3236
}
3337

3438
pub fn mknod(path: &Path, kind: SFlag, perm: FilePermission, dev: dev_t) -> SysResult<()> {
35-
let res = unsafe { ffi::mknod(path.to_c_str().as_ptr(), kind.bits | perm.bits(), dev) };
39+
let res = unsafe { ffi::mknod(path.to_c_str().as_ptr(), kind.bits | perm.bits() as mode_t, dev) };
3640
from_ffi(res)
3741
}
3842

3943
static MINORBITS: uint = 20;
4044
// static MINORMASK: dev_t = ((1 << MINORBITS) - 1);
4145

46+
#[cfg(target_os = "linux")]
4247
pub fn mkdev(major: u64, minor: u64) -> dev_t {
4348
(major << MINORBITS) | minor
4449
}
4550

4651
pub fn umask(mode: FilePermission) -> FilePermission {
47-
let prev = unsafe { ffi::umask(mode.bits()) };
48-
FilePermission::from_bits(prev).expect("[BUG] umask returned invalid FilePermission")
52+
let prev = unsafe { ffi::umask(mode.bits() as mode_t) };
53+
FilePermission::from_bits(prev as u32).expect("[BUG] umask returned invalid FilePermission")
54+
}
55+
56+
pub fn stat(path: &Path) -> SysResult<FileStat> {
57+
let mut dst = unsafe { mem::uninitialized() };
58+
let res = unsafe { ffi::stat(path.to_c_str().as_ptr(), &mut dst as *mut FileStat) };
59+
60+
if res < 0 {
61+
return Err(SysError::last());
62+
}
63+
64+
Ok(dst)
65+
}
66+
67+
pub fn fstat(fd: Fd) -> SysResult<FileStat> {
68+
let mut dst = unsafe { mem::uninitialized() };
69+
let res = unsafe { ffi::fstat(fd, &mut dst as *mut FileStat) };
70+
71+
if res < 0 {
72+
return Err(SysError::last());
73+
}
74+
75+
Ok(dst)
4976
}

0 commit comments

Comments
 (0)