Skip to content

Commit 3d12b54

Browse files
committed
add support for mknodat
1 parent 97c4ca2 commit 3d12b54

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2424
([#739](https://github.com/nix-rust/nix/pull/739))
2525
- Added nix::sys::ptrace::detach.
2626
([#749](https://github.com/nix-rust/nix/pull/749))
27+
- Added nix::sys::stat::mknod
28+
([#747](https://github.com/nix-rust/nix/pull/747))
2729

2830
### Changed
2931
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))

src/sys/stat.rs

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ pub fn mknod<P: ?Sized + NixPath>(path: &P, kind: SFlag, perm: Mode, dev: dev_t)
5050
Errno::result(res).map(drop)
5151
}
5252

53+
/// Create a special or ordinary file
54+
/// ([posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html)).
55+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
56+
pub fn mknodat<P: ?Sized + NixPath>(dirfd: &RawFd, path: &P, kind: SFlag, perm: Mode, dev: dev_t) -> Result<()> {
57+
let res = try!(path.with_nix_path(|cstr| {
58+
unsafe {
59+
libc::mknodat(*dirfd, cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev)
60+
}
61+
}));
62+
63+
Errno::result(res).map(drop)
64+
}
65+
5366
#[cfg(target_os = "linux")]
5467
pub fn major(dev: dev_t) -> u64 {
5568
((dev >> 32) & 0xfffff000) |

test/test_stat.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::fs::File;
22
use std::os::unix::fs::symlink;
33
use std::os::unix::prelude::AsRawFd;
4+
use std::path::Path;
45

56
use libc::{S_IFMT, S_IFLNK};
67

78
use nix::fcntl;
8-
use nix::sys::stat::{self, stat, fstat, lstat};
9+
use nix::sys::stat::{self, stat, fstat, lstat, mknod, mknodat};
910
use nix::sys::stat::FileStat;
1011
use nix::Result;
1112
use tempdir::TempDir;
@@ -110,3 +111,22 @@ fn test_stat_fstat_lstat() {
110111
let fstat_result = fstat(link.as_raw_fd());
111112
assert_stat_results(fstat_result);
112113
}
114+
115+
fn assert_fifo(path: &Path) {
116+
let stats = stat(path).unwrap();
117+
let typ = stat::SFlag::from_bits_truncate(stats.st_mode);
118+
assert!(typ == stat::S_IFIFO);
119+
}
120+
121+
#[test]
122+
fn test_mknod_mknodat() {
123+
let tempdir = TempDir::new("nix-test_mknodat").unwrap();
124+
let mknod_fifo = tempdir.path().join("mknod");
125+
let mknodat_fifo = tempdir.path().join("mknodat");
126+
127+
mknod(&mknod_fifo, stat::S_IFIFO, stat::S_IRUSR, 0).unwrap();
128+
assert_fifo(&mknod_fifo);
129+
130+
mknodat(&0, &mknodat_fifo, stat::S_IFIFO, stat::S_IRUSR, 0).unwrap();
131+
assert_fifo(&mknodat_fifo);
132+
}

0 commit comments

Comments
 (0)