Skip to content

Commit b6a8a3c

Browse files
committed
add support for fstatat
1 parent 2b0c63f commit b6a8a3c

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
## [Unreleased]
77

88
<!--### Added-->
9-
- Added `openat` in `::nix::unistd`
9+
- Added `openat`, `fstatat` in `::nix::unistd`
1010
([#497](https://github.com/nix-rust/nix/pull/551))
1111

1212
### Changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ preadv_pwritev = []
2222
signalfd = []
2323

2424
[dependencies]
25-
libc = { git = "https://github.com/rust-lang/libc" }
25+
libc = { git = "https://github.com/Mic92/libc" }
2626
bitflags = "0.7"
2727
cfg-if = "0.1.0"
2828
void = "1.0.2"

src/sys/stat.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use libc::dev_t;
22
pub use libc::stat as FileStat;
33

44
use {Errno, Result, NixPath};
5+
use fcntl::AtFlags;
56
use libc::{self, mode_t};
67
use std::mem;
78
use std::os::unix::io::RawFd;
@@ -121,3 +122,15 @@ pub fn fstat(fd: RawFd) -> Result<FileStat> {
121122

122123
Ok(dst)
123124
}
125+
126+
pub fn fstatat<P: ?Sized + NixPath>(dirfd: RawFd, pathname: &P, f: AtFlags) -> Result<FileStat> {
127+
let mut dst = unsafe { mem::uninitialized() };
128+
let res = try!(pathname.with_nix_path(|cstr| {
129+
unsafe { libc::fstatat(dirfd, cstr.as_ptr(), &mut dst as *mut FileStat, f.bits() as libc::c_int) }
130+
}));
131+
132+
try!(Errno::result(res));
133+
134+
Ok(dst)
135+
}
136+

test/test_stat.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::os::unix::prelude::AsRawFd;
44

55
use libc::{S_IFMT, S_IFLNK};
66

7-
use nix::sys::stat::{stat, fstat, lstat};
8-
7+
use nix::fcntl;
8+
use nix::sys::stat::{self, stat, fstat, lstat};
99
use nix::sys::stat::FileStat;
1010
use nix::Result;
1111
use tempdir::TempDir;
@@ -74,6 +74,21 @@ fn test_stat_and_fstat() {
7474
assert_stat_results(fstat_result);
7575
}
7676

77+
#[test]
78+
fn test_fstatat() {
79+
let tempdir = TempDir::new("nix-test_stat_and_fstat").unwrap();
80+
let filename = tempdir.path().join("foo.txt");
81+
File::create(&filename).unwrap();
82+
let dirfd = fcntl::open(tempdir.path(),
83+
fcntl::OFlag::empty(),
84+
stat::Mode::empty());
85+
86+
let result = stat::fstatat(dirfd.unwrap(),
87+
&filename,
88+
fcntl::AtFlags::empty());
89+
assert_stat_results(result);
90+
}
91+
7792
#[test]
7893
fn test_stat_fstat_lstat() {
7994
let tempdir = TempDir::new("nix-test_stat_fstat_lstat").unwrap();

0 commit comments

Comments
 (0)