-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use {Result, Error, Errno, NixPath}; | ||
use errno; | ||
use libc::{self, DIR, c_long}; | ||
|
||
#[cfg(not(any(target_os = "ios", target_os = "macos")))] | ||
use std::os::unix::io::RawFd; | ||
|
||
pub struct Dir { | ||
handle: *mut DIR, | ||
} | ||
|
||
impl Drop for Dir { | ||
fn drop(&mut self) { | ||
unsafe { libc::closedir(self.handle) }; | ||
} | ||
} | ||
|
||
pub fn opendir<P: ?Sized + NixPath>(name: &P) -> Result<Dir> { | ||
let dirp = try!(name.with_nix_path(|cstr| unsafe { libc::opendir(cstr.as_ptr()) })); | ||
if dirp.is_null() { | ||
Err(Error::last().into()) | ||
} else { | ||
Ok(Dir { handle: dirp }) | ||
} | ||
} | ||
|
||
#[cfg(not(any(target_os = "ios", target_os = "macos")))] | ||
pub fn fdopendir(fd: RawFd) -> Result<Dir> { | ||
let dirp = unsafe { libc::fdopendir(fd) }; | ||
if dirp.is_null() { | ||
Err(Error::last().into()) | ||
} else { | ||
Ok(Dir { handle: dirp }) | ||
} | ||
} | ||
|
||
pub fn readdir<'a>(dir: &'a mut Dir) -> Result<Option<&'a libc::dirent>> { | ||
let dirent = unsafe { | ||
Errno::clear(); | ||
libc::readdir(dir.handle) | ||
}; | ||
if dirent.is_null() { | ||
match Errno::last() { | ||
errno::UnknownErrno => Ok(None), | ||
_ => Err(Error::last().into()), | ||
} | ||
} else { | ||
Ok(Some(unsafe { &*dirent })) | ||
} | ||
} | ||
|
||
pub fn seekdir<'a>(dir: &'a mut Dir, loc: c_long) { | ||
unsafe { libc::seekdir(dir.handle, loc) }; | ||
} | ||
|
||
pub fn telldir<'a>(dir: &'a mut Dir) -> c_long { | ||
unsafe { libc::telldir(dir.handle) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
mod test_dirent { | ||
use nix::dirent::{self, opendir, readdir, seekdir, telldir, Dir}; | ||
use std::path::Path; | ||
use tempdir::TempDir; | ||
|
||
fn test_readdir<OPEN>(open_fn: OPEN) | ||
where OPEN: Fn(&Path) -> Dir | ||
{ | ||
let tempdir = TempDir::new("nix-test_readdir") | ||
.unwrap_or_else(|e| panic!("tempdir failed: {}", e)); | ||
let mut dir = open_fn(tempdir.path()); | ||
let entry1 = readdir(&mut dir).unwrap().unwrap().clone(); | ||
assert_eq!(entry1.d_name[0], '.' as i8); | ||
|
||
let pos = telldir(&mut dir); | ||
seekdir(&mut dir, pos); // no-op | ||
|
||
let entry2 = readdir(&mut dir).unwrap().unwrap().clone(); | ||
assert_eq!(entry2.d_name[0], '.' as i8); | ||
|
||
assert!(readdir(&mut dir).unwrap().is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_opendir() { | ||
test_readdir(|path| opendir(path).unwrap()); | ||
} | ||
|
||
#[cfg(not(any(target_os = "ios", target_os = "macos")))] | ||
#[test] | ||
fn test_fdopendir() { | ||
use std::os::unix::io::IntoRawFd; | ||
use std::fs::File; | ||
test_readdir(|path| dirent::fdopendir(File::open(path).unwrap().into_raw_fd()).unwrap()); | ||
} | ||
} |