Skip to content

Commit 50c4aa1

Browse files
committed
add support for unlinkat
1 parent 97c4ca2 commit 50c4aa1

File tree

4 files changed

+35
-4
lines changed

4 files changed

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

2830
### Changed
2931
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))

src/fcntl.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ use sys::uio::IoVec; // For vmsplice
1111
libc_bitflags!{
1212
pub struct AtFlags: c_int {
1313
AT_SYMLINK_NOFOLLOW;
14-
#[cfg(any(target_os = "android", target_os = "linux"))]
14+
#[cfg(any(target_os = "linux", target_os = "android"))]
15+
AT_REMOVEDIR;
16+
#[cfg(any(target_os = "linux", target_os = "android"))]
1517
AT_NO_AUTOMOUNT;
16-
#[cfg(any(target_os = "android", target_os = "linux"))]
17-
AT_EMPTY_PATH;
18+
#[cfg(any(target_os = "linux", target_os = "android"))]
19+
AT_EMPTY_PATH
1820
}
1921
}
2022

src/unistd.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use errno;
44
use {Errno, Error, Result, NixPath};
5-
use fcntl::{fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC};
5+
use fcntl::{AtFlags, fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC};
66
use fcntl::FcntlArg::F_SETFD;
77
use libc::{self, c_char, c_void, c_int, c_long, c_uint, size_t, pid_t, off_t,
88
uid_t, gid_t, mode_t};
@@ -914,6 +914,17 @@ pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> {
914914
Errno::result(res).map(drop)
915915
}
916916

917+
/// Delete a name and possibly the file it refers to
918+
/// ([posix specification](http://pubs.opengroup.org/onlinepubs/9699919799/functions/unlinkat.html)).
919+
pub fn unlinkat<P: ?Sized + NixPath>(fd: RawFd, pathname: &P, flags: AtFlags) -> Result<()> {
920+
let res = try!(pathname.with_nix_path(|cstr| {
921+
unsafe {
922+
libc::unlinkat(fd, cstr.as_ptr(), flags.bits())
923+
}
924+
}));
925+
Errno::result(res).map(drop)
926+
}
927+
917928
#[inline]
918929
pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> {
919930
let res = try!(path.with_nix_path(|cstr| {

test/test_unistd.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern crate tempdir;
22

3+
use nix::fcntl;
34
use nix::unistd::*;
45
use nix::unistd::ForkResult::*;
56
use nix::sys::wait::*;
@@ -225,6 +226,21 @@ fn test_lseek() {
225226
close(tmpfd).unwrap();
226227
}
227228

229+
#[test]
230+
fn test_unlinkat() {
231+
let tempdir = TempDir::new("nix-test_unlinkat").unwrap();
232+
let dirfd = fcntl::open(tempdir.path(),
233+
fcntl::OFlag::empty(),
234+
stat::Mode::empty());
235+
let file = tempdir.path().join("foo");
236+
File::create(&file).unwrap();
237+
238+
unlinkat(dirfd.unwrap(),
239+
&file.file_name(),
240+
fcntl::AtFlags::empty()).unwrap();
241+
assert!(!file.exists());
242+
}
243+
228244
#[cfg(any(target_os = "linux", target_os = "android"))]
229245
#[test]
230246
fn test_lseek64() {

0 commit comments

Comments
 (0)