diff --git a/CHANGELOG.md b/CHANGELOG.md index 3420681b3a..557077f87b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - Added `MSG_WAITALL` to `MsgFlags` in `sys::socket`. ([#1079](https://github.com/nix-rust/nix/pull/1079)) +- Add `mkdirat`. + ([#1084](https://github.com/nix-rust/nix/pull/1084)) ### Changed - Support for `ifaddrs` now present when building for Android. diff --git a/src/sys/stat.rs b/src/sys/stat.rs index 1e0936ed86..66c8c9dd1b 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -284,3 +284,11 @@ pub fn utimensat( Errno::result(res).map(drop) } + +pub fn mkdirat(fd: RawFd, path: &P, mode: Mode) -> Result<()> { + let res = path.with_nix_path(|cstr| { + unsafe { libc::mkdirat(fd, cstr.as_ptr(), mode.bits() as mode_t) } + })?; + + Errno::result(res).map(drop) +} diff --git a/test/test_stat.rs b/test/test_stat.rs index b9da7fc3ea..b43aa894f6 100644 --- a/test/test_stat.rs +++ b/test/test_stat.rs @@ -1,13 +1,14 @@ use std::fs::{self, File}; -use std::os::unix::fs::symlink; +use std::os::unix::fs::{symlink, PermissionsExt}; use std::os::unix::prelude::AsRawFd; use std::time::{Duration, UNIX_EPOCH}; +use std::path::Path; #[cfg(not(any(target_os = "netbsd")))] -use libc::{S_IFMT, S_IFLNK}; +use libc::{S_IFMT, S_IFLNK, mode_t}; use nix::fcntl; -use nix::sys::stat::{self, fchmod, fchmodat, futimens, stat, utimes, utimensat}; +use nix::sys::stat::{self, fchmod, fchmodat, futimens, stat, utimes, utimensat, mkdirat}; #[cfg(any(target_os = "linux", target_os = "haiku", target_os = "ios", @@ -260,3 +261,36 @@ fn test_utimensat() { UtimensatFlags::FollowSymlink).unwrap(); assert_times_eq(500, 800, &fs::metadata(&fullpath).unwrap()); } + +#[test] +fn test_mkdirat_success_path() { + let tempdir = tempfile::tempdir().unwrap(); + let filename = "example_subdir"; + let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap(); + assert!((mkdirat(dirfd, filename, Mode::S_IRWXU)).is_ok()); + assert!(Path::exists(&tempdir.path().join(filename))); +} + +#[test] +fn test_mkdirat_success_mode() { + let expected_bits = stat::SFlag::S_IFDIR.bits() | stat::Mode::S_IRWXU.bits(); + let tempdir = tempfile::tempdir().unwrap(); + let filename = "example_subdir"; + let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap(); + assert!((mkdirat(dirfd, filename, Mode::S_IRWXU)).is_ok()); + let permissions = fs::metadata(tempdir.path().join(filename)).unwrap().permissions(); + let mode = permissions.mode(); + assert_eq!(mode as mode_t, expected_bits) +} + +#[test] +#[should_panic="EACCES"] +fn test_mkdirat_fail() { + let tempdir = tempfile::tempdir().unwrap(); + let filename = "example_subdir"; + let mut perms = fs::metadata(tempdir.path()).unwrap().permissions(); + perms.set_readonly(true); + fs::set_permissions(tempdir.path(), perms).unwrap(); + let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap(); + let result = mkdirat(dirfd, filename, Mode::S_IRWXU).unwrap(); +}