Skip to content

Commit 4e7c65d

Browse files
committed
Add mknod, mkdev, and umask APIs
1 parent 0513490 commit 4e7c65d

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ pub mod errno;
99
pub mod fcntl;
1010
pub mod mount;
1111
pub mod sched;
12+
pub mod sys;
1213
pub mod syscall;
1314
pub mod unistd;

src/mount.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ mod ffi {
7171
pub fn mount(
7272
source: Option<&Path>,
7373
target: &Path,
74-
fstype: Option<&CString>,
74+
fstype: Option<&str>,
7575
flags: MsFlags,
76-
data: Option<&CString>) -> SysResult<()> {
76+
data: Option<&str>) -> SysResult<()> {
7777

7878
let source = source.map(|s| s.to_c_str());
7979
let target = target.to_c_str();
80+
let fstype = fstype.map(|s| s.to_c_str());
81+
let data = data.map(|s| s.to_c_str());
8082

8183
let res = unsafe {
8284
ffi::mount(

src/sys.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
pub mod stat {
2+
pub use libc::dev_t;
3+
4+
use std::fmt;
5+
use std::io::FilePermission;
6+
use std::path::Path;
7+
use libc::mode_t;
8+
use errno::{SysResult, from_ffi};
9+
10+
mod ffi {
11+
use libc::{c_char, c_int, mode_t, dev_t};
12+
13+
extern {
14+
pub fn mknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> c_int;
15+
pub fn umask(mask: mode_t) -> mode_t;
16+
}
17+
}
18+
19+
bitflags!(
20+
flags SFlag: mode_t {
21+
static S_IFREG = 0o100000,
22+
static S_IFCHR = 0o020000,
23+
static S_IFBLK = 0o060000,
24+
static S_IFIFO = 0o010000,
25+
static S_IFSOCK = 0o140000
26+
}
27+
)
28+
29+
impl fmt::Show for SFlag {
30+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
31+
write!(fmt, "SFlag {{ bits: {} }}", self.bits())
32+
}
33+
}
34+
35+
pub fn mknod(path: &Path, kind: SFlag, perm: FilePermission, dev: dev_t) -> SysResult<()> {
36+
let res = unsafe { ffi::mknod(path.to_c_str().as_ptr(), kind.bits | perm.bits(), dev) };
37+
from_ffi(res)
38+
}
39+
40+
static MINORBITS: uint = 20;
41+
static MINORMASK: dev_t = ((1 << MINORBITS) - 1);
42+
43+
pub fn mkdev(major: u64, minor: u64) -> dev_t {
44+
(major << MINORBITS) | minor
45+
}
46+
47+
pub fn umask(mode: FilePermission) -> FilePermission {
48+
let prev = unsafe { ffi::umask(mode.bits()) };
49+
FilePermission::from_bits(prev).expect("[BUG] umask returned invalid FilePermission")
50+
}
51+
}

0 commit comments

Comments
 (0)