Skip to content

[WIP] add support for mkdirat #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#739](https://github.com/nix-rust/nix/pull/739))
- Added nix::sys::ptrace::detach.
([#749](https://github.com/nix-rust/nix/pull/749))
- Added `nix::unistd::mkdirat`
([#754](https://github.com/nix-rust/nix/pull/754))

### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down
10 changes: 10 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,16 @@ pub fn mkdir<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
Errno::result(res).map(drop)
}

/// Create a directory
/// ([posix specification)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdirat.html)).
pub fn mkdirat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, mode: Mode) -> Result<()> {
let res = try!(pathname.with_nix_path(|cstr| {
unsafe { libc::mkdirat(dirfd, cstr.as_ptr(), mode.bits() as mode_t) }
}));

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

/// Returns the current directory as a PathBuf
///
/// Err is returned if the current user doesn't have the permission to read or search a component
Expand Down
17 changes: 17 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate tempdir;

use nix::fcntl;
use nix::unistd::*;
use nix::unistd::ForkResult::*;
use nix::sys::wait::*;
Expand Down Expand Up @@ -104,6 +105,22 @@ mod linux_android {
}
}

#[test]
fn test_mkdirat() {
let tempdir = TempDir::new("nix-test_mkdirat").unwrap();
let path = tempdir.path().join("test_path");

let dirfd = fcntl::open(tempdir.path(),
fcntl::OFlag::empty(),
stat::Mode::empty());

mkdirat(dirfd.unwrap(),
&path.file_name(),
stat::Mode::empty()).unwrap();

assert!(path.exists());
}

macro_rules! execve_test_factory(
($test_name:ident, $syscall:ident, $exe: expr) => (
#[test]
Expand Down