Skip to content

Commit 75b17b9

Browse files
committed
add support for mkdirat
1 parent 97c4ca2 commit 75b17b9

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ mod linux_android {
102102
let tid: ::libc::pid_t = gettid().into();
103103
assert!(tid > 0);
104104
}
105+
106+
}
107+
#[test]
108+
fn test_mkdirat() {
109+
let tempdir = TempDir::new("nix-test_mkdirat").unwrap();
110+
let path = tempdir.path().join("test_path");
111+
112+
let dirfd = fcntl::open(tempdir.path(),
113+
fcntl::OFlag::empty(),
114+
stat::Mode::empty());
115+
116+
mkdirat(dirfd.unwrap(),
117+
&path.file_name(),
118+
stat::Mode::empty()).unwrap();
119+
assert!(path.exists());
105120
}
106121

107122
macro_rules! execve_test_factory(

0 commit comments

Comments
 (0)