From 273fb635cc3a97ab09e412e813783469ca436a33 Mon Sep 17 00:00:00 2001 From: Johnnie Birch <45402135+jlb6740@users.noreply.github.com> Date: Wed, 15 May 2019 22:18:21 -0700 Subject: [PATCH] Implement unlinkat This adds the unlinkat function, which is part of POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/functions/unlinkat.html and widely implmented on Unix-family platforms. Implement unlinkat - Update comments to unlink at and breakup tests Changes made based on comments from pull request #1058 Implement unlinkat - Update should_panic for more precise check Updae should_panic attribute for more precise check. Also remove some dead code and note the pull request ID. Implement unlinkat - Update error handling to use unwrap_err The previous patch failed testing on two targets that returned EPERM as the Sys error instead of EISDIR. This patch changes from using the should_panic attribute to using the unwrap_err and matching against the result. Implement Unlinkat - Passing tests should not print anything. Fix. Implement unlinkat - Update location of commit comment in the CHANGELOG Implement unlinkat - Remove newline --- CHANGELOG.md | 2 ++ src/fcntl.rs | 1 + src/unistd.rs | 36 ++++++++++++++++++++++++++++ test/test_unistd.rs | 57 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf30fd983d..dcb2c59927 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#1089](https://github.com/nix-rust/nix/pull/1089)) - Added `AF_VSOCK` to `AddressFamily`. ([#1091](https://github.com/nix-rust/nix/pull/1091)) +- Add `unlinkat` + ([#1058](https://github.com/nix-rust/nix/pull/1058)) ### Changed - Support for `ifaddrs` now present when building for Android. diff --git a/src/fcntl.rs b/src/fcntl.rs index 2201873a63..d99c2c1a71 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -23,6 +23,7 @@ pub use self::posix_fadvise::*; libc_bitflags!{ pub struct AtFlags: c_int { + AT_REMOVEDIR; AT_SYMLINK_NOFOLLOW; #[cfg(any(target_os = "android", target_os = "linux"))] AT_NO_AUTOMOUNT; diff --git a/src/unistd.rs b/src/unistd.rs index 96d8ace78c..f422f09198 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -1144,6 +1144,42 @@ pub fn unlink(path: &P) -> Result<()> { Errno::result(res).map(drop) } +/// Flags for `unlinkat` function. +#[derive(Clone, Copy, Debug)] +pub enum UnlinkatFlags { + RemoveDir, + NoRemoveDir, +} + +/// Remove a directory entry +/// +/// In the case of a relative path, the directory entry to be removed is determined relative to +/// the directory associated with the file descriptor `dirfd` or the current working directory +/// if `dirfd` is `None`. In the case of an absolute `path` `dirfd` is ignored. If `flag` is +/// `UnlinkatFlags::RemoveDir` then removal of the directory entry specified by `dirfd` and `path` +/// is performed. +/// +/// # References +/// See also [unlinkat(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/unlinkat.html) +pub fn unlinkat( + dirfd: Option, + path: &P, + flag: UnlinkatFlags, +) -> Result<()> { + let atflag = + match flag { + UnlinkatFlags::RemoveDir => AtFlags::AT_REMOVEDIR, + UnlinkatFlags::NoRemoveDir => AtFlags::empty(), + }; + let res = path.with_nix_path(|cstr| { + unsafe { + libc::unlinkat(at_rawfd(dirfd), cstr.as_ptr(), atflag.bits() as libc::c_int) + } + })?; + Errno::result(res).map(drop) +} + + #[inline] pub fn chroot(path: &P) -> Result<()> { let res = path.with_nix_path(|cstr| { diff --git a/test/test_unistd.rs b/test/test_unistd.rs index b2061458d4..46196dec7c 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -1,13 +1,14 @@ -use nix::fcntl::{fcntl, FcntlArg, FdFlag, open, OFlag, readlink}; +use nix::fcntl::{self, fcntl, FcntlArg, FdFlag, open, OFlag, readlink}; use nix::unistd::*; use nix::unistd::ForkResult::*; use nix::sys::signal::{SaFlags, SigAction, SigHandler, SigSet, Signal, sigaction}; use nix::sys::wait::*; use nix::sys::stat::{self, Mode, SFlag}; use nix::errno::Errno; +use nix::Error; use std::{env, iter}; use std::ffi::CString; -use std::fs::{self, File}; +use std::fs::{self, DirBuilder, File}; use std::io::Write; use std::os::unix::prelude::*; use tempfile::{self, tempfile}; @@ -599,6 +600,58 @@ fn test_symlinkat() { ); } + +#[test] +fn test_unlinkat_dir_noremovedir() { + let tempdir = tempfile::tempdir().unwrap(); + let dirname = "foo_dir"; + let dirpath = tempdir.path().join(dirname); + + // Create dir + DirBuilder::new().recursive(true).create(&dirpath).unwrap(); + + // Get file descriptor for base directory + let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap(); + + // Attempt unlink dir at relative path without proper flag + let err_result = unlinkat(Some(dirfd), dirname, UnlinkatFlags::NoRemoveDir).unwrap_err(); + assert!(err_result == Error::Sys(Errno::EISDIR) || err_result == Error::Sys(Errno::EPERM)); + } + +#[test] +fn test_unlinkat_dir_removedir() { + let tempdir = tempfile::tempdir().unwrap(); + let dirname = "foo_dir"; + let dirpath = tempdir.path().join(dirname); + + // Create dir + DirBuilder::new().recursive(true).create(&dirpath).unwrap(); + + // Get file descriptor for base directory + let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap(); + + // Attempt unlink dir at relative path with proper flag + unlinkat(Some(dirfd), dirname, UnlinkatFlags::RemoveDir).unwrap(); + assert!(!dirpath.exists()); + } + +#[test] +fn test_unlinkat_file() { + let tempdir = tempfile::tempdir().unwrap(); + let filename = "foo.txt"; + let filepath = tempdir.path().join(filename); + + // Create file + File::create(&filepath).unwrap(); + + // Get file descriptor for base directory + let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).unwrap(); + + // Attempt unlink file at relative path + unlinkat(Some(dirfd), filename, UnlinkatFlags::NoRemoveDir).unwrap(); + assert!(!filepath.exists()); + } + #[test] fn test_access_not_existing() { let tempdir = tempfile::tempdir().unwrap();