diff options
author | Carl Lerche <me@carllerche.com> | 2014-08-15 00:16:06 -0700 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2014-08-15 00:16:06 -0700 |
commit | 4e7c65d3a1cf5c8999da9dcc3f55d1ba38b8c816 (patch) | |
tree | f3d2d426a07932a2b1824215c6a008f62130ba8b /src/sys.rs | |
parent | 05134909671ba8dc306f559fd9f9fac29d517b1b (diff) | |
download | nix-4e7c65d3a1cf5c8999da9dcc3f55d1ba38b8c816.zip |
Add mknod, mkdev, and umask APIs
Diffstat (limited to 'src/sys.rs')
-rw-r--r-- | src/sys.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/sys.rs b/src/sys.rs new file mode 100644 index 00000000..1aa19534 --- /dev/null +++ b/src/sys.rs @@ -0,0 +1,51 @@ +pub mod stat { + pub use libc::dev_t; + + use std::fmt; + use std::io::FilePermission; + use std::path::Path; + use libc::mode_t; + use errno::{SysResult, from_ffi}; + + mod ffi { + use libc::{c_char, c_int, mode_t, dev_t}; + + extern { + pub fn mknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> c_int; + pub fn umask(mask: mode_t) -> mode_t; + } + } + + bitflags!( + flags SFlag: mode_t { + static S_IFREG = 0o100000, + static S_IFCHR = 0o020000, + static S_IFBLK = 0o060000, + static S_IFIFO = 0o010000, + static S_IFSOCK = 0o140000 + } + ) + + impl fmt::Show for SFlag { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "SFlag {{ bits: {} }}", self.bits()) + } + } + + 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) }; + from_ffi(res) + } + + static MINORBITS: uint = 20; + static MINORMASK: dev_t = ((1 << MINORBITS) - 1); + + 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") + } +} |