Skip to content

Commit 0d6d280

Browse files
committed
add support for mkdirat
1 parent 97c4ca2 commit 0d6d280

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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::unistd::mkdirat`
28+
([#754](https://github.com/nix-rust/nix/pull/754))
2729

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

src/unistd.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,16 @@ pub fn mkdir<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
441441
Errno::result(res).map(drop)
442442
}
443443

444+
/// Create a directory
445+
/// ([posix specification)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdirat.html)).
446+
pub fn mkdirat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, mode: Mode) -> Result<()> {
447+
let res = try!(pathname.with_nix_path(|cstr| {
448+
unsafe { libc::mkdirat(dirfd, cstr.as_ptr(), mode.bits() as mode_t) }
449+
}));
450+
451+
Errno::result(res).map(drop)
452+
}
453+
444454
/// Returns the current directory as a PathBuf
445455
///
446456
/// Err is returned if the current user doesn't have the permission to read or search a component

test/test_unistd.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern crate tempdir;
22

3+
use nix::fcntl;
34
use nix::unistd::*;
45
use nix::unistd::ForkResult::*;
56
use nix::sys::wait::*;
@@ -104,6 +105,22 @@ mod linux_android {
104105
}
105106
}
106107

108+
#[test]
109+
fn test_mkdirat() {
110+
let tempdir = TempDir::new("nix-test_mkdirat").unwrap();
111+
let path = tempdir.path().join("test_path");
112+
113+
let dirfd = fcntl::open(tempdir.path(),
114+
fcntl::OFlag::empty(),
115+
stat::Mode::empty());
116+
117+
mkdirat(dirfd.unwrap(),
118+
&path.file_name(),
119+
stat::Mode::empty()).unwrap();
120+
121+
assert!(path.exists());
122+
}
123+
107124
macro_rules! execve_test_factory(
108125
($test_name:ident, $syscall:ident, $exe: expr) => (
109126
#[test]

0 commit comments

Comments
 (0)