Skip to content

Commit

Permalink
add support for openat
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Mar 20, 2017
1 parent 1fbf1fe commit c351fad
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- Added `openat` in `::nix::fcntl`
- Added support for XNU system control sockets
([#478](https://github.com/nix-rust/nix/pull/478))
- Added support for `ioctl` calls on BSD platforms
Expand Down
9 changes: 9 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<R
Errno::result(fd)
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = try!(path.with_nix_path(|cstr| {
unsafe { libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits()) }
}));

Errno::result(fd)
}

pub enum FcntlArg<'a> {
F_DUPFD(RawFd),
F_DUPFD_CLOEXEC(RawFd),
Expand Down
28 changes: 26 additions & 2 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,35 @@ mod linux_android {

use libc::loff_t;

use nix::fcntl::{SpliceFFlags, splice, tee, vmsplice};
use nix::fcntl::{SpliceFFlags, splice, tee, vmsplice, openat, open, O_PATH, O_RDONLY};
use nix::sys::stat::Mode;
use nix::sys::uio::IoVec;
use nix::unistd::{close, pipe, read, write};

use tempfile::tempfile;
use tempfile::{tempfile, NamedTempFile};

#[test]
fn test_openat() {
const CONTENTS: &'static [u8] = b"abcd";
let mut tmp = NamedTempFile::new().unwrap();
tmp.write(CONTENTS).unwrap();

let dirfd = open(tmp.path().parent().unwrap(),
O_PATH,
Mode::empty()).unwrap();
let fd = openat(dirfd,
tmp.path().file_name().unwrap(),
O_RDONLY,
Mode::empty()).unwrap();

let mut buf = [0u8; 1024];
assert_eq!(4, read(fd, &mut buf).unwrap());
assert_eq!(CONTENTS, &buf[0..4]);

close(fd).unwrap();
close(dirfd).unwrap();
}


#[test]
fn test_splice() {
Expand Down

0 comments on commit c351fad

Please sign in to comment.