Skip to content

Commit c0230a9

Browse files
committed
sys/stat: add a safe wrapper for mknodat(2)
This introduces a new `mknodat` helper. Ref: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html
1 parent 7033d47 commit c0230a9

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
88

99
- Added `IPV6_V6ONLY` sockopt.
1010
(#[1470](https://github.com/nix-rust/nix/pull/1470))
11+
- Added `mknodat`.
12+
(#[1473](https://github.com/nix-rust/nix/pull/1473))
1113

1214
### Changed
1315

src/sys/stat.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::os::unix::io::RawFd;
99
use crate::sys::time::{TimeSpec, TimeVal};
1010

1111
libc_bitflags!(
12+
/// "File type" flags for `mknod` and related functions.
1213
pub struct SFlag: mode_t {
1314
S_IFIFO;
1415
S_IFCHR;
@@ -22,6 +23,7 @@ libc_bitflags!(
2223
);
2324

2425
libc_bitflags! {
26+
/// "File mode / permissions" flags.
2527
pub struct Mode: mode_t {
2628
S_IRWXU;
2729
S_IRUSR;
@@ -41,11 +43,26 @@ libc_bitflags! {
4143
}
4244
}
4345

46+
/// Create a special or ordinary file, by pathname.
4447
pub fn mknod<P: ?Sized + NixPath>(path: &P, kind: SFlag, perm: Mode, dev: dev_t) -> Result<()> {
45-
let res = path.with_nix_path(|cstr| {
46-
unsafe {
47-
libc::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev)
48-
}
48+
let res = path.with_nix_path(|cstr| unsafe {
49+
libc::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev)
50+
})?;
51+
52+
Errno::result(res).map(drop)
53+
}
54+
55+
/// Create a special or ordinary file, relative to a given directory.
56+
#[cfg(not(any(target_os = "ios", target_os = "macos", target_os = "redox")))]
57+
pub fn mknodat<P: ?Sized + NixPath>(
58+
dirfd: RawFd,
59+
path: &P,
60+
kind: SFlag,
61+
perm: Mode,
62+
dev: dev_t,
63+
) -> Result<()> {
64+
let res = path.with_nix_path(|cstr| unsafe {
65+
libc::mknodat(dirfd, cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev)
4966
})?;
5067

5168
Errno::result(res).map(drop)

0 commit comments

Comments
 (0)