Skip to content

[WIP] add support for unlinkat #753

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::unlinkat`.
([#753](https://github.com/nix-rust/nix/pull/753))

### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down
8 changes: 5 additions & 3 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use sys::uio::IoVec; // For vmsplice
libc_bitflags!{
pub struct AtFlags: c_int {
AT_SYMLINK_NOFOLLOW;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg(any(target_os = "linux", target_os = "android"))]
AT_REMOVEDIR;
#[cfg(any(target_os = "linux", target_os = "android"))]
AT_NO_AUTOMOUNT;
#[cfg(any(target_os = "android", target_os = "linux"))]
AT_EMPTY_PATH;
#[cfg(any(target_os = "linux", target_os = "android"))]
AT_EMPTY_PATH
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use errno;
use {Errno, Error, Result, NixPath};
use fcntl::{fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC};
use fcntl::{AtFlags, fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::F_SETFD;
use libc::{self, c_char, c_void, c_int, c_long, c_uint, size_t, pid_t, off_t,
uid_t, gid_t, mode_t};
Expand Down Expand Up @@ -914,6 +914,17 @@ pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> {
Errno::result(res).map(drop)
}

/// Delete a name and possibly the file it refers to
/// ([posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/unlinkat.html)).
pub fn unlinkat<P: ?Sized + NixPath>(fd: RawFd, pathname: &P, flags: AtFlags) -> Result<()> {
let res = try!(pathname.with_nix_path(|cstr| {
unsafe {
libc::unlinkat(fd, cstr.as_ptr(), flags.bits())
}
}));
Errno::result(res).map(drop)
}

#[inline]
pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
Expand Down
16 changes: 16 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 @@ -225,6 +226,21 @@ fn test_lseek() {
close(tmpfd).unwrap();
}

#[test]
fn test_unlinkat() {
let tempdir = TempDir::new("nix-test_unlinkat").unwrap();
let dirfd = fcntl::open(tempdir.path(),
fcntl::OFlag::empty(),
stat::Mode::empty());
let file = tempdir.path().join("foo");
File::create(&file).unwrap();

unlinkat(dirfd.unwrap(),
&file.file_name(),
fcntl::AtFlags::empty()).unwrap();
assert!(!file.exists());
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_lseek64() {
Expand Down