-
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
5 changed files
with
87 additions
and
1 deletion.
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
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,50 @@ | ||
use {Result, Error, Errno, NixPath}; | ||
use libc::{self, DIR}; | ||
use std::os::unix::io::RawFd; | ||
use errno; | ||
|
||
pub struct Dir { | ||
handle: *mut DIR, | ||
} | ||
|
||
impl Drop for Dir { | ||
fn drop(&mut self) { | ||
unsafe { libc::closedir(self.handle) }; | ||
} | ||
} | ||
|
||
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 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 }) | ||
} | ||
} | ||
|
||
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})) | ||
} | ||
} | ||
|
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,34 @@ | ||
|
||
mod test_dirent { | ||
use nix::dirent::{opendir, fdopendir, readdir, Dir}; | ||
use tempdir::TempDir; | ||
use std::fs::File; | ||
use std::path::Path; | ||
use std::os::unix::io::IntoRawFd; | ||
|
||
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..2], ['.' as i8, '\0' as i8]); | ||
|
||
let entry2 = readdir(&mut dir).unwrap().unwrap().clone(); | ||
assert_eq!(entry2.d_name[0..2], ['.' as i8, '.' as i8]); | ||
|
||
assert!(readdir(&mut dir).unwrap().is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_opendir() { | ||
test_readdir(|path| opendir(path).unwrap() ); | ||
} | ||
|
||
#[test] | ||
fn test_fdopendir() { | ||
test_readdir(|path| | ||
fdopendir(File::open(path).unwrap().into_raw_fd()).unwrap() | ||
); | ||
} | ||
|
||
} |