Skip to content

Commit 8e486f4

Browse files
committed
add support for dirent family
1 parent c086d23 commit 8e486f4

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
88
<!--### Added-->
99
- Added `nix::unistd::{openat, fstatat, readlink, readlinkat}`
1010
([#497](https://github.com/nix-rust/nix/pull/551))
11+
- Added `nix::dirent::{opendir, fdopendir, readdir, telldir, seekdir}`
12+
([#558](https://github.com/nix-rust/nix/pull/558))
1113

1214
### Changed
1315
- Marked `sys::mman::{ mmap, munmap, madvise, munlock, msync }` as unsafe.

src/dirent.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use {Result, Error, Errno, NixPath};
2+
use errno;
3+
use libc::{self, DIR, c_long};
4+
use std::convert::{AsRef, Into};
5+
use std::ffi::CStr;
6+
use std::mem;
7+
8+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
9+
use std::os::unix::io::RawFd;
10+
11+
pub struct Dir(*mut DIR);
12+
13+
impl AsRef<DIR> for Dir {
14+
fn as_ref(&self) -> &DIR {
15+
unsafe { &*self.0 }
16+
}
17+
}
18+
19+
impl Into<*mut DIR> for Dir {
20+
fn into(self) -> *mut DIR {
21+
let dirp = self.0;
22+
mem::forget(self);
23+
dirp
24+
}
25+
}
26+
27+
impl Drop for Dir {
28+
fn drop(&mut self) {
29+
unsafe { libc::closedir(self.0) };
30+
}
31+
}
32+
33+
pub struct Dirent<'a>(&'a libc::dirent);
34+
35+
impl<'a> Dirent<'a> {
36+
pub fn name(&self) -> &CStr {
37+
unsafe{
38+
CStr::from_ptr(self.0.d_name.as_ptr())
39+
}
40+
}
41+
42+
pub fn ino(&self) -> libc::ino_t {
43+
self.0.d_ino
44+
}
45+
}
46+
47+
impl<'a> AsRef<libc::dirent> for Dirent<'a> {
48+
fn as_ref(&self) -> &libc::dirent {
49+
self.0
50+
}
51+
}
52+
53+
pub fn opendir<P: ?Sized + NixPath>(name: &P) -> Result<Dir> {
54+
let dirp = try!(name.with_nix_path(|cstr| unsafe { libc::opendir(cstr.as_ptr()) }));
55+
if dirp.is_null() {
56+
Err(Error::last().into())
57+
} else {
58+
Ok(Dir(dirp))
59+
}
60+
}
61+
62+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
63+
pub fn fdopendir(fd: RawFd) -> Result<Dir> {
64+
let dirp = unsafe { libc::fdopendir(fd) };
65+
if dirp.is_null() {
66+
Err(Error::last().into())
67+
} else {
68+
Ok(Dir(dirp))
69+
}
70+
}
71+
72+
pub fn readdir<'a>(dir: &'a mut Dir) -> Result<Option<Dirent>> {
73+
let dirent = unsafe {
74+
Errno::clear();
75+
libc::readdir(dir.0)
76+
};
77+
if dirent.is_null() {
78+
match Errno::last() {
79+
errno::UnknownErrno => Ok(None),
80+
_ => Err(Error::last().into()),
81+
}
82+
} else {
83+
Ok(Some(Dirent(unsafe { &*dirent })))
84+
}
85+
}
86+
87+
pub fn seekdir<'a>(dir: &'a mut Dir, loc: c_long) {
88+
unsafe { libc::seekdir(dir.0, loc) };
89+
}
90+
91+
pub fn telldir<'a>(dir: &'a mut Dir) -> c_long {
92+
unsafe { libc::telldir(dir.0) }
93+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub mod libc {
3434
pub use libc::{c_int, c_void};
3535
pub use errno::Errno;
3636

37+
pub mod dirent;
3738
pub mod errno;
3839
pub mod features;
3940
pub mod fcntl;

test/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate tempfile;
88
extern crate nix_test as nixtest;
99

1010
mod sys;
11+
mod test_dirent;
1112
mod test_fcntl;
1213
mod test_net;
1314
mod test_nix_path;

test/test_dirent.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use nix::dirent::{self, opendir, readdir, seekdir, telldir, Dir};
2+
use std::path::Path;
3+
use tempdir::TempDir;
4+
5+
fn test_readdir<OPEN>(open_fn: OPEN)
6+
where OPEN: Fn(&Path) -> Dir
7+
{
8+
let tempdir = TempDir::new("nix-test_readdir")
9+
.unwrap_or_else(|e| panic!("tempdir failed: {}", e));
10+
let mut dir = open_fn(tempdir.path());
11+
let letter1 = readdir(&mut dir)
12+
.unwrap()
13+
.unwrap()
14+
.name()
15+
.to_bytes()[0];
16+
assert_eq!(letter1, '.' as u8);
17+
18+
let pos = telldir(&mut dir);
19+
seekdir(&mut dir, pos); // no-op
20+
21+
let letter2 = readdir(&mut dir)
22+
.unwrap()
23+
.unwrap()
24+
.name()
25+
.to_bytes()[0];
26+
assert_eq!(letter2, '.' as u8);
27+
28+
assert!(readdir(&mut dir).unwrap().is_none());
29+
}
30+
31+
#[test]
32+
fn test_opendir() {
33+
test_readdir(|path| opendir(path).unwrap());
34+
}
35+
36+
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
37+
#[test]
38+
fn test_fdopendir() {
39+
use std::os::unix::io::IntoRawFd;
40+
use std::fs::File;
41+
test_readdir(|path| dirent::fdopendir(File::open(path).unwrap().into_raw_fd()).unwrap());
42+
}

0 commit comments

Comments
 (0)